Skip to content

Commit bfe7287

Browse files
author
Oliver Scott
committed
graph + node summary
1 parent 6cd23c4 commit bfe7287

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

scaffoldgraph/utils/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
scaffoldgraph.utils
33
"""
44

5-
from .misc import canonize_smiles
5+
from .misc import canonize_smiles, summary
66
from .aggregate import aggregate
77

88
__all__ = [
99
'canonize_smiles',
10-
'aggregate'
10+
'aggregate',
11+
'summary'
1112
]

scaffoldgraph/utils/misc.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Defines miscellaneous functions used within scaffoldgraph.
55
"""
66

7+
import networkx as nx
8+
79
from rdkit import Chem
810

911

@@ -29,3 +31,66 @@ def canonize_smiles(smiles, failsafe=True):
2931
if mol is None and failsafe:
3032
return smiles
3133
return Chem.MolToSmiles(mol)
34+
35+
36+
def summary(graph, n=None):
37+
"""Return a summary of information for the graph or a single node n.
38+
39+
Parameters
40+
----------
41+
graph : sg.core.ScaffoldGraph or NetworkX graph
42+
A graph object which can either be a ScaffoldGraph graph or a NetworkX
43+
graph object.
44+
n : any hashable, optional
45+
A node in the graph. The default is None.
46+
47+
Returns
48+
-------
49+
info : str
50+
A string containing the summary.
51+
52+
Raises
53+
------
54+
ValueError
55+
If n is not in the graph.
56+
57+
"""
58+
from scaffoldgraph.core import ScaffoldGraph
59+
if not issubclass(type(graph), ScaffoldGraph):
60+
return nx.info(graph, n)
61+
info = ""
62+
if n is None:
63+
type_name = [type(graph).__name__]
64+
info += f"Type: {','.join(type_name)}\n"
65+
info += f"Number of molecule nodes: {graph.num_molecule_nodes}\n"
66+
info += f"Number of scaffold nodes: {graph.num_scaffold_nodes}\n"
67+
info += f"Number of edges: {graph.number_of_edges()}\n"
68+
info += f"Max hierarchy: {graph.max_hierarchy()}\n"
69+
info += f"Min hierarchy: {graph.min_hierarchy()}\n"
70+
else:
71+
if graph.molecule_in_graph(n):
72+
info += f"Node {n} has the following properties:\n"
73+
info += "Type: molecule\n"
74+
info += f"SMILES: {graph.nodes[n].get('smiles')}\n"
75+
info += f"Degree: {graph.degree(n)}\n"
76+
info += "Parent scaffolds: "
77+
info += " ".join(str(s) for s in graph.predecessors(n))
78+
elif graph.scaffold_in_graph(n):
79+
key = canonize_smiles(n)
80+
info += f"Node {key} has the following properties:\n"
81+
info += "Type: scaffold\n"
82+
info += f"Hierarchy: {graph.nodes[key].get('hierarchy')}\n"
83+
info += f"Degree: {graph.degree(key)}\n"
84+
info += "Parent scaffolds: "
85+
info += " ".join(str(s) for s in graph.get_parent_scaffolds(key, max_levels=1))
86+
info += "\n"
87+
info += "Child scaffolds: "
88+
info += " ".join(str(s) for s in graph.get_child_scaffolds(key, max_levels=1))
89+
info += "\n"
90+
info += "Child molecules: "
91+
info += " ".join(
92+
str(s) for s in graph.successors(key) if graph.nodes[s].get('type') == 'molecule'
93+
)
94+
else:
95+
raise ValueError(f"node {n} not in graph")
96+
return info

0 commit comments

Comments
 (0)