Skip to content

Commit

Permalink
support volcano labeling in get_volcano & improve plot_glycans_excel …
Browse files Browse the repository at this point in the history
…resolution
  • Loading branch information
Bribak committed Mar 4, 2024
1 parent 0fe246e commit 1a95a27
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion build/lib/glycowork/motif/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
| :-
Expand All @@ -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:
| :-
Expand All @@ -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()


Expand Down
4 changes: 2 additions & 2 deletions build/lib/glycowork/motif/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 7 additions & 1 deletion glycowork/motif/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
| :-
Expand All @@ -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:
| :-
Expand All @@ -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()


Expand Down
4 changes: 2 additions & 2 deletions glycowork/motif/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 1a95a27

Please sign in to comment.