2
$\begingroup$

I have the following code:

from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
from IPython.display import SVG


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

Which produces the following plot.

image of a molecular structure of a molecule generated by the above code

However the code above doesn't produce the high resolution image. I'd like to have the SVG. I tried this:

drawer = rdMolDraw2D.MolDraw2DSVG(400,200)
drawer.DrawMolecule(m,highlightAtoms=m.GetSubstructMatch(Chem.MolFromSmarts('C(=O)O')))
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:','')
SVG(svg)

But I get:

another image of a molecular structure of a molecule generated by the above code

What's the right way to do it?

The code can be tested in my Google Colab.

$\endgroup$

2 Answers 2

4
$\begingroup$

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 Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem.Draw import rdMolDraw2D
from IPython.display import SVG
from itertools import chain

m = 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)
$\endgroup$
3
$\begingroup$

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.html
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw
IPythonConsole.ipython_useSVG=True

def mol_with_atom_index(mol):
    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(atom.GetIdx())
    return mol

# Test in a kinase inhibitor
mol = Chem.MolFromSmiles("C1CC2=C3C(=CC=C2)C(=CN3C1)[C@H]4[C@@H](C(=O)NC4=O)C5=CNC6=CC=CC=C65")
# Default
mol

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 example
m = 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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.