GlobalChem Graph to Networkx Graph

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()

Convert GlobalChem Graph to NetworkX Graph

gc = GlobalChem()
gc.build_global_chem_network(print_output=False, debugger=False)
network = gc.network
networkx_graph = cheminformatics.convert_to_networkx(network)
print (networkx_graph.nodes.data())

Algorithm

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'])

And then return the graph! As simple as that.

Last updated