From 1a95a27fac76a006a435c955c081188ddf4c2838 Mon Sep 17 00:00:00 2001 From: Daniel Bojar Date: Mon, 4 Mar 2024 16:20:37 +0100 Subject: [PATCH] support volcano labeling in get_volcano & improve plot_glycans_excel resolution --- build/lib/glycowork/motif/analysis.py | 8 +++++++- build/lib/glycowork/motif/draw.py | 4 ++-- glycowork/motif/analysis.py | 8 +++++++- glycowork/motif/draw.py | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/build/lib/glycowork/motif/analysis.py b/build/lib/glycowork/motif/analysis.py index 82211192..948cab68 100644 --- a/build/lib/glycowork/motif/analysis.py +++ b/build/lib/glycowork/motif/analysis.py @@ -681,7 +681,8 @@ def get_ma(df_res, log2fc_thresh = 1, sig_thresh = 0.05, filepath = ''): def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, - label_changed = True, x_metric = 'Log2FC', filepath = ''): + label_changed = True, x_metric = 'Log2FC', annotate_volcano = False, + filepath = ''): """Plots glycan differential expression results in a volcano plot\n | Arguments: | :- @@ -690,6 +691,7 @@ def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, | x_thresh (float): absolute x metric threshold for labeling datapoints; default:1.0 | label_changed (bool): if True, add text labels to significantly up- and downregulated datapoints; default:True | x_metric (string): x-axis metric; default:'Log2FC'; options are 'Log2FC', 'Effect size' + | annotate_volcano (bool): whether to annotate the dots in the plot with SNFG images; default: False | filepath (string): absolute path including full filename allows for saving the plot\n | Returns: | :- @@ -716,6 +718,10 @@ def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, if filepath: plt.savefig(filepath, format = filepath.split('.')[-1], dpi = 300, bbox_inches = 'tight') + if annotate_volcano: + from glycowork.motif.draw import annotate_figure + annotate_figure(filepath, filepath = filepath.split('.')[0]+'.pdf', scale_by_DE_res = df_res, + y_thresh = y_thresh, x_thresh = x_thresh, x_metric = x_metric) plt.show() diff --git a/build/lib/glycowork/motif/draw.py b/build/lib/glycowork/motif/draw.py index d88a57e6..9df5c43f 100644 --- a/build/lib/glycowork/motif/draw.py +++ b/build/lib/glycowork/motif/draw.py @@ -2291,12 +2291,12 @@ def plot_glycans_excel(df, folder_filepath, glycan_col_num = 0, # Generate glycan image using GlycoDraw svg_data = GlycoDraw(glycan_structure, compact = compact).as_svg() # Convert SVG data to image - png_data = svg2png(bytestring = svg_data) + png_data = svg2png(bytestring = svg_data, output_width = 3000, output_height = 3000) img_stream = BytesIO(png_data) img = Image.open(img_stream) # Set the size of the image img_width, img_height = img.size - img = img.resize((int(img_width * scaling_factor), int(img_height * scaling_factor)), Image.LANCZOS) + img = img.resize((int(img_width * scaling_factor / 5), int(img_height * scaling_factor / 5)), Image.LANCZOS) # Save the image to a BytesIO object img_stream = BytesIO() img.save(img_stream, format = 'PNG') diff --git a/glycowork/motif/analysis.py b/glycowork/motif/analysis.py index 82211192..948cab68 100644 --- a/glycowork/motif/analysis.py +++ b/glycowork/motif/analysis.py @@ -681,7 +681,8 @@ def get_ma(df_res, log2fc_thresh = 1, sig_thresh = 0.05, filepath = ''): def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, - label_changed = True, x_metric = 'Log2FC', filepath = ''): + label_changed = True, x_metric = 'Log2FC', annotate_volcano = False, + filepath = ''): """Plots glycan differential expression results in a volcano plot\n | Arguments: | :- @@ -690,6 +691,7 @@ def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, | x_thresh (float): absolute x metric threshold for labeling datapoints; default:1.0 | label_changed (bool): if True, add text labels to significantly up- and downregulated datapoints; default:True | x_metric (string): x-axis metric; default:'Log2FC'; options are 'Log2FC', 'Effect size' + | annotate_volcano (bool): whether to annotate the dots in the plot with SNFG images; default: False | filepath (string): absolute path including full filename allows for saving the plot\n | Returns: | :- @@ -716,6 +718,10 @@ def get_volcano(df_res, y_thresh = 0.05, x_thresh = 1.0, if filepath: plt.savefig(filepath, format = filepath.split('.')[-1], dpi = 300, bbox_inches = 'tight') + if annotate_volcano: + from glycowork.motif.draw import annotate_figure + annotate_figure(filepath, filepath = filepath.split('.')[0]+'.pdf', scale_by_DE_res = df_res, + y_thresh = y_thresh, x_thresh = x_thresh, x_metric = x_metric) plt.show() diff --git a/glycowork/motif/draw.py b/glycowork/motif/draw.py index d88a57e6..9df5c43f 100644 --- a/glycowork/motif/draw.py +++ b/glycowork/motif/draw.py @@ -2291,12 +2291,12 @@ def plot_glycans_excel(df, folder_filepath, glycan_col_num = 0, # Generate glycan image using GlycoDraw svg_data = GlycoDraw(glycan_structure, compact = compact).as_svg() # Convert SVG data to image - png_data = svg2png(bytestring = svg_data) + png_data = svg2png(bytestring = svg_data, output_width = 3000, output_height = 3000) img_stream = BytesIO(png_data) img = Image.open(img_stream) # Set the size of the image img_width, img_height = img.size - img = img.resize((int(img_width * scaling_factor), int(img_height * scaling_factor)), Image.LANCZOS) + img = img.resize((int(img_width * scaling_factor / 5), int(img_height * scaling_factor / 5)), Image.LANCZOS) # Save the image to a BytesIO object img_stream = BytesIO() img.save(img_stream, format = 'PNG')