Skip to content

Commit

Permalink
add utility function to split its graph in G and H graph
Browse files Browse the repository at this point in the history
  • Loading branch information
klausweinbauer committed Aug 7, 2024
1 parent 6269f00 commit 414417f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions fgutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ def add_implicit_hydrogens(graph: nx.Graph) -> nx.Graph:
graph.add_node(h_id, symbol="H")
graph.add_edge(n_id, h_id, bond=1)
return graph


def split_its(graph: nx.Graph) -> tuple[nx.Graph, nx.Graph]:
def _set_rc_edge(g, u, v, b):
if b == 0:
g.remove_edge(u, v)
else:
g[u][v]["bond"] = b

g = graph.copy()
h = graph.copy()
for u, v, d in graph.edges(data=True):
bond = d["bond"]
if isinstance(bond, tuple):
_set_rc_edge(g, u, v, bond[0])
_set_rc_edge(h, u, v, bond[1])
return g, h
35 changes: 33 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from fgutils.parse import parse
from fgutils.utils import add_implicit_hydrogens
from fgutils.utils import add_implicit_hydrogens, split_its

from .test_parse import _assert_graph


def _assert_Hs(graph, idx, h_cnt):
Expand Down Expand Up @@ -103,4 +105,33 @@ def test_tin_tetrachloride():
_assert_Hs(graph, 4, 0)


# SeO2 SnCL4
def test_split_its():
its = parse("C1<2,>C<,2>C<2,>C(C)<0,1>C<2,>C(C(=O)O)<0,1>1")
exp_nodes = {i: "C" for i in range(10)}
exp_nodes[8] = "O"
exp_nodes[9] = "O"
exp_edges_g = [
(0, 1, 2),
(1, 2, 1),
(2, 3, 2),
(3, 4, 1),
(5, 6, 2),
(6, 7, 1),
(7, 8, 2),
(7, 9, 1),
]
exp_edges_h = [
(0, 1, 1),
(0, 6, 1),
(1, 2, 2),
(2, 3, 1),
(3, 4, 1),
(3, 5, 1),
(5, 6, 1),
(6, 7, 1),
(7, 8, 2),
(7, 9, 1),
]
g, h = split_its(its)
_assert_graph(g, exp_nodes, exp_edges_g)
_assert_graph(h, exp_nodes, exp_edges_h)

0 comments on commit 414417f

Please sign in to comment.