Hi, greg, DrawMolecules did not work in my compute, I guess it is a future
feature from my version (v2016.3.1), isn't it? And I tried your second
suggestion but it still cracked. Maybe the old version of MolDraw2D is buggy.
The following are fake compouns for test, and each one is drawable.
# mycompounds.smiSMILESc1cc1[N+]([O-])=O1CCC[N+]([O-])=O
2CC[N+]([O-])=O 3C[N+]([O-])=O 4c1c(CC)1[N+]([O-])=O 5
# codefrom rdkit import Chemfrom rdkit.Chem.Draw import rdMolDraw2Dfrom
rdkit.Chem import Drawfrom rdkit.Chem import rdDepictorfrom rdkit.Chem import
rdMolTransformsmols =
Chem.SmilesMolSupplier('mycompounds.smi',delimiter='\t')len(mols)mols =
list(mols)def centerMol(mol): conf = mol.GetConformer() pt =
rdMolTransforms.ComputeCentroid(conf) for i in range(conf.GetNumAtoms()):
conf.SetAtomPosition(i,conf.GetAtomPosition(i) - pt) drawer =
rdMolDraw2D.MolDraw2DSVG(400,400) smarts = Chem.MolFromSmarts('N(-O)=O')i=0 for
mol in mols: if not mol.HasSubstructMatch(smarts): continue
rdDepictor.Compute2DCoords(mol) tm = Chem.Mol(mol) centerMol(tm)
Draw.PrepareMolForDrawing(tm)
#drawer.DrawMolecule(mol,highlightAtoms=mol.GetSubstructMatch(p))
drawer.DrawMolecule(tm,highlightAtoms=tm.GetSubstructMatch(smarts))
drawer.FinishDrawing() svg = drawer.GetDrawingText().replace('svg:','')
Hongbin Yang
From: Greg LandrumDate: 2017-02-18 12:59To: 杨弘宾CC: rdkit-discussSubject: Re:
[Rdkit-discuss] jupyter cracked when drawing with "abnormal" operation of
rdMolDraw2DHi,
On Fri, Feb 17, 2017 at 6:31 AM, 杨弘宾 wrote:
Hi, everyone, I want to draw two molecules in a svg file with rdMolDraw2D.
When I executed the following code, the jupyter cracked without any error or
warning.```drawer = rdMolDraw2D.MolDraw2DSVG(400,400)
i=0
for mol in mols:
if mol.HasSubstructMatch(smarts):
rdDepictor.Compute2DCoords(mol) #if i == 1: # continue
drawer.DrawMolecule(mol,highlightAtoms=mol.GetSubstructMatch(smarts))
i+=1
if i > 1:
break
drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:','')
SVG(svg)```
It seems that we cannot directly draw two molecules with the same drawer? So
how can I draw as I wanted?
Do you want to draw the molecules on top of each other (somewhat problematic at
the moment, but doable) or in a grid?If you want to have them in a grid, the
solution is:
drawer = rdMolDraw2D.MolDraw2DSVG(400,400,200,200) p =
Chem.MolFromSmarts('c1n1')drawer.DrawMolecules(mols[:4])drawer.FinishDrawing()
svg = drawer.GetDrawingText().replace('svg:','')
That's an overall image size of 400x400 with 200x200 panes for the individual
molecules. At the moment molecular highlighting does not work when you do this
(there's a github item for that here:
https://github.com/rdkit/rdkit/issues/1323)
If you want to put them on top of each other, what you show above should kind
of work. You probably should center each of the molecules first though:
from rdkit.Chem import rdMolTransformsdef centerMol(mol): conf =
mol.GetConformer() pt = rdMolTransforms.ComputeCentroid(conf) for i in
range(conf.GetNumAtoms()):
conf.SetAtomPosition(i,conf.GetAtomPosition(i) - pt) drawer =
rdMolDraw2D.MolDraw2DSVG(400,400) p = Chem.MolFromSmarts('c1n1')i=0 for mol
in mols: tm = Chem.Mol(mol) centerMol(tm)
Draw.PrepareMolForDrawing(tm)
#drawer.DrawMolecule(mol,highlightAtoms=mol.GetSubstructMatch(p))
drawer.DrawMolecule(tm,highlightAtoms=tm.GetSubstructMatch(p)) i+=1 if
i > 1: break drawer.FinishDrawing() svg =
drawer.GetDrawingText().replace('svg:','')
Note that this can end up being somewhat ugly since the drawing code will
determine the scaling factors to make the molecules fit in the canvas from the
first molecule.
I think it is fixable by adding some additional logic to DrawMolecules() but
I'm going to have to look into it. that's this item in github:
https://github.com/rdkit/rdkit/issues/1325
By the way, I've no idea why it cracked. From the experiment of the commented
code, I can conclude it was caused by the drawer. So is it possible to fix the
bug, adding error or warning instead of "KernelRestarter: restarting kernel" in
console.
I can't reproduce a crash there. Which version of the RDKit are you using?
-greg
--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot___
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss