Skip to content

Instantly share code, notes, and snippets.

@jhjensen2
Last active April 25, 2024 20:41
Show Gist options
  • Save jhjensen2/6450138cda3ab796a30850610843cfff to your computer and use it in GitHub Desktop.
Save jhjensen2/6450138cda3ab796a30850610843cfff to your computer and use it in GitHub Desktop.
Compute graph edit distance between two molecules using RDKit and Networkx
'''
Written by Jan H. Jensen, 2020
'''
from rdkit import Chem
import networkx as nx
def get_graph(mol):
Chem.Kekulize(mol)
atoms = [atom.GetAtomicNum() for atom in mol.GetAtoms()]
am = Chem.GetAdjacencyMatrix(mol,useBO=True)
for i,atom in enumerate(atoms):
am[i,i] = atom
G = nx.from_numpy_matrix(am)
return G
mol1 = Chem.MolFromSmiles('c1ccccc1')
#mol2 = Chem.MolFromSmiles('c1cnccc1')
mol2 = Chem.MolFromSmiles('C=CC=CC=C')
G1 = get_graph(mol1)
G2 = get_graph(mol2)
GDE = nx.graph_edit_distance(G1, G2, edge_match=lambda a,b: a['weight'] == b['weight'])
print(GDE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment