Skip to content

Commit

Permalink
speed up construct_network
Browse files Browse the repository at this point in the history
  • Loading branch information
Bribak committed Mar 9, 2024
1 parent 7e448a4 commit 3d0290d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions build/lib/glycowork/motif/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ def graph_to_string_int(graph):
if len(nodes) == 1:
return nodes[0]
edges = {k: v for k, v in graph.edges()}
nodes = [k+')' if graph.degree[edges.get(i, len(graph)-1)] > 2 or neighbor_is_branchpoint(graph, i) else k if graph.degree[i] == 2 else '('+k if graph.degree[i] == 1 else k for i, k in enumerate(nodes)]
if graph.degree[len(graph)-1] < 2:
cache_last_index = len(graph)-1
nodes = [k+')' if graph.degree[edges.get(i, cache_last_index)] > 2 or neighbor_is_branchpoint(graph, i) else k if graph.degree[i] == 2 else '('+k if graph.degree[i] == 1 else k for i, k in enumerate(nodes)]
if graph.degree[cache_last_index] < 2:
nodes = ''.join(nodes)[1:][::-1].replace('(', '', 1)[::-1]
else:
nodes[-1] = ')'+nodes[-1]
Expand Down
5 changes: 3 additions & 2 deletions glycowork/motif/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ def graph_to_string_int(graph):
if len(nodes) == 1:
return nodes[0]
edges = {k: v for k, v in graph.edges()}
nodes = [k+')' if graph.degree[edges.get(i, len(graph)-1)] > 2 or neighbor_is_branchpoint(graph, i) else k if graph.degree[i] == 2 else '('+k if graph.degree[i] == 1 else k for i, k in enumerate(nodes)]
if graph.degree[len(graph)-1] < 2:
cache_last_index = len(graph)-1
nodes = [k+')' if graph.degree[edges.get(i, cache_last_index)] > 2 or neighbor_is_branchpoint(graph, i) else k if graph.degree[i] == 2 else '('+k if graph.degree[i] == 1 else k for i, k in enumerate(nodes)]
if graph.degree[cache_last_index] < 2:
nodes = ''.join(nodes)[1:][::-1].replace('(', '', 1)[::-1]
else:
nodes[-1] = ')'+nodes[-1]
Expand Down
8 changes: 5 additions & 3 deletions glycowork/network/biosynthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ def find_shared_virtuals(glycan_a, glycan_b, graph_dic, min_size = 1):
| :-
| Returns list of edges between glycan and virtual node (if virtual node connects the two glycans)
"""
if abs(glycan_a.count('(') - glycan_b.count('(')) != 2:
return []
# Get virtual nodes of both glycans
ggraph_nb_a, glycans_a = get_virtual_nodes(glycan_a, graph_dic, min_size = min_size)
ggraph_nb_b, glycans_b = get_virtual_nodes(glycan_b, graph_dic, min_size = min_size)
ggraph_nb_a, glycans_a = get_virtual_nodes(glycan_a, graph_dic, min_size = min_size)
if not ggraph_nb_a:
return []
ggraph_nb_b, glycans_b = get_virtual_nodes(glycan_b, graph_dic, min_size = min_size)
out = set()
# Check whether any of the nodes of glycan_a and glycan_b are the same
for k, graph_a in enumerate(ggraph_nb_a):
Expand Down Expand Up @@ -238,7 +240,7 @@ def create_adjacency_matrix(glycans, graph_dic, min_size = 1):
# Connect glycans with biosynthetic precursors
df_out = pd.DataFrame(0, index = glycans, columns = glycans)
graphs = [safe_index(k, graph_dic) for k in glycans]
neighbors, idx = zip(*[get_neighbors(safe_index(k, graph_dic), glycans, graphs,
_, idx = zip(*[get_neighbors(safe_index(k, graph_dic), glycans, graphs,
min_size = min_size) for k in glycans])
# Fill adjacency matrix
for j in range(len(glycans)):
Expand Down

0 comments on commit 3d0290d

Please sign in to comment.