Home > Community > How to highlight the substructure of a molecule with thick red lines in RDKit as SVG (high res)
Upvote

VOTE

Downvote
+ Cheminformatics
+ Chemistry
Posted by
Larry King

How to highlight the substructure of a molecule with thick red lines in RDKit as SVG (high res)

Charles Brown  Follow

It may be useful to work RDKit's cookbook from top to bottom. I.e., to start with drawing a molecule

# source: https://www.rdkit.org/docs/Cookbook.htmlfrom rdkit import Chemfrom rdkit.Chem.Draw import IPythonConsolefrom rdkit.Chem import DrawIPythonConsole.ipython_useSVG=Truedef mol_with_atom_index(mol):    for atom in mol.GetAtoms():        atom.SetAtomMapNum(atom.GetIdx())    return mol# Test in a kinase inhibitormol = Chem.MolFromSmiles("C1CC2=C3C(=CC=C2)C(=CN3C1)[C@H]4[C@@H](C(=O)NC4=O)C5=CNC6=CC=CC=C65")# Defaultmol

For me, it works good enough (though in a Jupyter notebook I prefer the output as .png):

enter image description here

Then, running your example as a subsequent cell

# your examplem = Chem.MolFromSmiles('c1cc(C(=O)O)c(OC(=O)C)cc1')substructure = Chem.MolFromSmarts('C(=O)O')print(m.GetSubstructMatches(substructure))m

both yields

((3, 4, 5), (8, 9, 7))

as well as the captured illustration below:

enter image description here

Note, it might be that RDKit packaged for your OS lags a little behind the one packaged by Miniconda (cf. tangents in this answer). On occasion, this may affect some of the functionality/syntax at your disposition.

More

Upvote

VOTE

Downvote
Astro-Nuts  Follow

In your code for SVG you use GetSubstructMatch instead of GetSubstructMatches so only one match is found.To get all matches you have to use GetSubstructMatches and then transform the matches in one single tuple for the highlights.

from rdkit import Chemfrom rdkit.Chem.Draw import IPythonConsolefrom rdkit.Chem.Draw import rdMolDraw2Dfrom IPython.display import SVGfrom itertools import chainm = Chem.MolFromSmiles('c1cc(C(=O)O)c(OC(=O)C)cc1')sub = m.GetSubstructMatches(Chem.MolFromSmarts('C(=O)O'))print(sub)((3, 4, 5), (8, 9, 7))allsubs = tuple(chain.from_iterable(sub))print(allsubs)(3, 4, 5, 8, 9, 7)

Now use allsubs and you get the your image.

drawer = rdMolDraw2D.MolDraw2DSVG(400,200)drawer.DrawMolecule(m,highlightAtoms = allsubs)drawer.FinishDrawing()svg = drawer.GetDrawingText().replace('svg:','')SVG(svg)

More

Upvote

VOTE

Downvote