On Fri, Feb 17, 2017 at 6:31 AM, 杨弘宾 <yanyangh...@163.com> 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('c1ccccn1')
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/

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 rdMolTransforms
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)
p = Chem.MolFromSmarts('c1ccccn1')
for mol in mols:
    tm = Chem.Mol(mol)
    if i > 1:
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:

> 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?

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

Reply via email to