Networkx is a common graphing tool in python used for graph objects. It is only natural that we also have an extension to convert into networkx objects. This improves interoperability of our software to be included any pipelines. Folk can build chemical graph networks in our program then move into networkx for more processing if they wish.
Imports
from global_chem import GlobalChem
from global_chem_extensions import GlobalChemExtensions
gc = GlobalChem()
cheminformatics = GlobalChemExtensions().cheminformatics()
For the conversion, the algorithm is pretty simple:
1.) Establish all the nodes
2.) Add all child connections
3.) Add all parent connections
We initialize the object:
self.networkx_graph = nx.Graph()
for node_key, node_value in self.network.items():
self.networkx_graph.add_node(node_value['name'])
for node_key, node_value in self.network.items():
parent_name = node_value['name']
children = node_value['children']
for child in children:
self.networkx_graph.add_edge(child, parent_name)
for node_key, node_value in self.network.items():
parents = node_value['parents']
child = node_value['name']
for parent in parents:
self.networkx_graph.add_edge(child, parent)