Skip to content

Commit

Permalink
.network fixes and opening up GlycoDraw
Browse files Browse the repository at this point in the history
  • Loading branch information
Bribak committed Dec 5, 2023
1 parent 436d1ed commit bbbdf3b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build/lib/glycowork/motif/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ def get_jtk(df, timepoints, replicates, periods, interval, motifs = False, featu
df.replace(0, np.nan, inplace = True)
df = mf.fit_transform(df)
if motifs:
df = quantify_motifs(pd.DataFrame(df.iloc[:, 1:], df.iloc[:, 0].values.tolist()), feature_set).T
df = quantify_motifs(df.iloc[:, 1:], df.iloc[:, 0].values.tolist(), feature_set).T
res = df.apply(jtkx, param_dic = param_dic, axis = 1)
JTK_BHQ = pd.DataFrame(sm.stats.multipletests(res[0], method = 'fdr_bh')[1])
Results = pd.concat([res.iloc[:, 0], JTK_BHQ, res.iloc[:, 1:]], axis = 1)
Expand Down
20 changes: 13 additions & 7 deletions build/lib/glycowork/motif/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import copy

try:
import cairosvg
import drawsvg as draw
except ImportError:
raise ImportError("<draw dependencies missing; did you do 'pip install glycowork[draw]'?>")
import networkx as nx
import numpy as np
import sys
import re
Expand Down Expand Up @@ -2147,7 +2145,11 @@ def calculate_degree(y1, y2, x1, x2):
with open(filepath, 'w') as f:
f.write(data)
elif 'pdf' in filepath:
cairosvg.svg2pdf(bytestring = data, write_to = filepath)
try:
from cairosvg import svg2pdf
svg2pdf(bytestring = data, write_to = filepath)
except ImportError:
raise ImportError("You're missing some draw dependencies. Either use .svg or head to https://bojarlab.github.io/glycowork/examples.html#glycodraw-code-snippets to learn more.")
return d2


Expand Down Expand Up @@ -2244,11 +2246,15 @@ def annotate_figure(svg_input, scale_range = (25, 80), compact = False, glycan_s
svg_tmp += '</svg>'

if filepath:
try:
from cairosvg import svg2pdf, svg2svg, svg2png
if filepath.split('.')[-1] == 'pdf':
cairosvg.svg2pdf(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2pdf(bytestring = svg_tmp, write_to = filepath, dpi = 300)
elif filepath.split('.')[-1] == 'svg':
cairosvg.svg2svg(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2svg(bytestring = svg_tmp, write_to = filepath, dpi = 300)
elif filepath.split('.')[-1] == 'png':
cairosvg.svg2png(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2png(bytestring = svg_tmp, write_to = filepath, dpi = 300)
except ImportError:
raise ImportError("You're missing some draw dependencies. Either don't use filepath or head to https://bojarlab.github.io/glycowork/examples.html#glycodraw-code-snippets to learn more.")
else:
return svg_tmp
return svg_tmp
3 changes: 3 additions & 0 deletions build/lib/glycowork/motif/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ def choose_correct_isoform(glycans, reverse = False):
| :-
| Returns the correct isomer as a string (if reverse=False; otherwise it returns a list of strings)
"""
glycans = list(set(glycans))
if len(glycans) == 1:
return glycans[0]
if not any(['[' in g for g in glycans]):
return [] if reverse else glycans
floaty = False
if '{' in glycans[0]:
floaty = glycans[0][:glycans[0].rindex('}')+1]
Expand Down
2 changes: 1 addition & 1 deletion glycowork/motif/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ def get_jtk(df, timepoints, replicates, periods, interval, motifs = False, featu
df.replace(0, np.nan, inplace = True)
df = mf.fit_transform(df)
if motifs:
df = quantify_motifs(pd.DataFrame(df.iloc[:, 1:], df.iloc[:, 0].values.tolist()), feature_set).T
df = quantify_motifs(df.iloc[:, 1:], df.iloc[:, 0].values.tolist(), feature_set).T
res = df.apply(jtkx, param_dic = param_dic, axis = 1)
JTK_BHQ = pd.DataFrame(sm.stats.multipletests(res[0], method = 'fdr_bh')[1])
Results = pd.concat([res.iloc[:, 0], JTK_BHQ, res.iloc[:, 1:]], axis = 1)
Expand Down
20 changes: 13 additions & 7 deletions glycowork/motif/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import copy

try:
import cairosvg
import drawsvg as draw
except ImportError:
raise ImportError("<draw dependencies missing; did you do 'pip install glycowork[draw]'?>")
import networkx as nx
import numpy as np
import sys
import re
Expand Down Expand Up @@ -2147,7 +2145,11 @@ def calculate_degree(y1, y2, x1, x2):
with open(filepath, 'w') as f:
f.write(data)
elif 'pdf' in filepath:
cairosvg.svg2pdf(bytestring = data, write_to = filepath)
try:
from cairosvg import svg2pdf
svg2pdf(bytestring = data, write_to = filepath)
except ImportError:
raise ImportError("You're missing some draw dependencies. Either use .svg or head to https://bojarlab.github.io/glycowork/examples.html#glycodraw-code-snippets to learn more.")
return d2


Expand Down Expand Up @@ -2244,11 +2246,15 @@ def annotate_figure(svg_input, scale_range = (25, 80), compact = False, glycan_s
svg_tmp += '</svg>'

if filepath:
try:
from cairosvg import svg2pdf, svg2svg, svg2png
if filepath.split('.')[-1] == 'pdf':
cairosvg.svg2pdf(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2pdf(bytestring = svg_tmp, write_to = filepath, dpi = 300)
elif filepath.split('.')[-1] == 'svg':
cairosvg.svg2svg(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2svg(bytestring = svg_tmp, write_to = filepath, dpi = 300)
elif filepath.split('.')[-1] == 'png':
cairosvg.svg2png(bytestring = svg_tmp, write_to = filepath, dpi = 300)
svg2png(bytestring = svg_tmp, write_to = filepath, dpi = 300)
except ImportError:
raise ImportError("You're missing some draw dependencies. Either don't use filepath or head to https://bojarlab.github.io/glycowork/examples.html#glycodraw-code-snippets to learn more.")
else:
return svg_tmp
return svg_tmp
3 changes: 3 additions & 0 deletions glycowork/motif/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ def choose_correct_isoform(glycans, reverse = False):
| :-
| Returns the correct isomer as a string (if reverse=False; otherwise it returns a list of strings)
"""
glycans = list(set(glycans))
if len(glycans) == 1:
return glycans[0]
if not any(['[' in g for g in glycans]):
return [] if reverse else glycans
floaty = False
if '{' in glycans[0]:
floaty = glycans[0][:glycans[0].rindex('}')+1]
Expand Down
3 changes: 2 additions & 1 deletion glycowork/network/biosynthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,8 @@ def construct_network(glycans, libr = None, allowed_ptms = allowed_ptms,
if len(isomeric_graphs) > 0:
isomeric_nodes = [[virtual_nodes[virtual_graphs.index(k[0])],
virtual_nodes[virtual_graphs.index(k[1])]] for k in isomeric_graphs]
network.remove_nodes_from([choose_correct_isoform(k, reverse = True)[0] for k in isomeric_nodes])
isomeric_nodes = [choose_correct_isoform(k, reverse = True) for k in isomeric_nodes if len(set(k)) > 1]
network.remove_nodes_from([n[0] for n in isomeric_nodes if n])
network.remove_edges_from(nx.selfloop_edges(network))
# Make network directed
network = prune_directed_edges(network.to_directed())
Expand Down
2 changes: 1 addition & 1 deletion glycowork/network/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def get_communities(graph_list, label_list = None):
comm_list = [community_louvain.best_partition(g) for g in graph_list]
if label_list is None:
label_list = list(range(len(comm_list)))
final_comm_dict = []
final_comm_dict = {}
# Label the communities by species name and running number to distinguish them afterwards
for comm_dict, label in zip(comm_list, label_list):
updated_dict = {
Expand Down

0 comments on commit bbbdf3b

Please sign in to comment.