Skip to content

Commit a594e5c

Browse files
author
Oliver Scott
committed
function to create bipartite networks from scaffold graphs
1 parent bfe7287 commit a594e5c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

scaffoldgraph/utils/bipartite.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
scaffoldgraph.utils.bipartite
3+
4+
Defines functions for creating bipartite graphs from scaffold graphs.
5+
"""
6+
7+
from scaffoldgraph.core import ScaffoldGraph
8+
9+
10+
def make_bipartite_graph(graph):
11+
"""Collapse a scaffold hierarchy into a bipartite representation.
12+
13+
Scaffold --> Molecule
14+
15+
The returned output will inherit the class of the input graph.
16+
17+
Parameters
18+
----------
19+
graph : sg.core.ScaffoldGraph
20+
A scaffold graph template for producing a bipaertite
21+
graph.
22+
23+
Returns
24+
-------
25+
sg.core.ScaffoldGraph
26+
Bipartite scaffoldgraph where the scaffold hierarchy
27+
has been collapsed.
28+
29+
"""
30+
if not issubclass(type(graph), ScaffoldGraph):
31+
raise ValueError(f'{graph} must be a ScaffoldGraph')
32+
graph_type = type(graph)
33+
B = graph_type(None)
34+
for scf, sdata in graph.get_scaffold_nodes(True):
35+
B.add_node(scf, **sdata)
36+
for mol, mdata in graph.get_molecules_for_scaffold(scf, True):
37+
if not B.molecule_in_graph(mol):
38+
B.add_node(mol, **mdata)
39+
B.add_edge(scf, mol)
40+
return B

0 commit comments

Comments
 (0)