Skip to content

Commit

Permalink
improve robustness of graph_to_string_int
Browse files Browse the repository at this point in the history
  • Loading branch information
Bribak committed Dec 3, 2023
1 parent aa81da3 commit 401cc91
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 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 @@ -428,15 +428,16 @@ def graph_to_string_int(graph):
if min(graph.nodes()) > 0:
graph = nx.relabel_nodes(graph, {n: i for i, n in enumerate(graph.nodes())})
nodes = list(nx.get_node_attributes(graph, "string_labels").values())
nodes = [k+')' if graph.degree[min(i+1, len(nodes)-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)]
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:
nodes = ''.join(nodes)[1:][::-1].replace('(', '', 1)[::-1]
else:
nodes[-1] = ')'+nodes[-1]
nodes = ''.join(nodes)[1:]
if ')(' in nodes and ((nodes.index(')(') < nodes.index('(')) or (nodes[:nodes.index(')(')].count(')') == nodes[:nodes.index(')(')].count('('))):
nodes = nodes.replace(')(', '(', 1)
return canonicalize_iupac(nodes)
return canonicalize_iupac(nodes.strip('()'))


def graph_to_string(graph):
Expand Down
5 changes: 3 additions & 2 deletions glycowork/motif/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,16 @@ def graph_to_string_int(graph):
if min(graph.nodes()) > 0:
graph = nx.relabel_nodes(graph, {n: i for i, n in enumerate(graph.nodes())})
nodes = list(nx.get_node_attributes(graph, "string_labels").values())
nodes = [k+')' if graph.degree[min(i+1, len(nodes)-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)]
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:
nodes = ''.join(nodes)[1:][::-1].replace('(', '', 1)[::-1]
else:
nodes[-1] = ')'+nodes[-1]
nodes = ''.join(nodes)[1:]
if ')(' in nodes and ((nodes.index(')(') < nodes.index('(')) or (nodes[:nodes.index(')(')].count(')') == nodes[:nodes.index(')(')].count('('))):
nodes = nodes.replace(')(', '(', 1)
return canonicalize_iupac(nodes)
return canonicalize_iupac(nodes.strip('()'))


def graph_to_string(graph):
Expand Down

0 comments on commit 401cc91

Please sign in to comment.