rdkit.Chem.Draw

Package rdkit :: Package Chem :: Package Draw |
[hide private]
[frames] | no frames]

# Source Code for Package rdkit.Chem.Draw

     1  # $Id: __init__.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  # Copyright (C) 2006-2008 Greg Landrum
     4  #  All Rights Reserved
     5  #
     6
     7
     8 -def MolToImage(mol,size=(300,300),kekulize=True, wedgeBonds=True):
     9    if not mol:
    10      raise ValueError,'Null molecule provided'
    11    import MolDrawing
    12    try:
    13      from aggdraw import Draw
    14      from PIL import Image
    15      MolDrawing.registerCanvas('agg')
    16      useAGG=True
    17    except:
    18      import traceback
    19      traceback.print_exc()
    20      useAGG=False
    21      from rdkit.sping.PIL.pidPIL import PILCanvas as Canvas
    22      canvas = Canvas(size=size,name='MolToImageFile')
    23      img = canvas._image
    24      MolDrawing.registerCanvas('sping')
    25      drawer = MolDrawing.MolDrawing(canvas)
    26    if useAGG:
    27      img = Image.new("RGBA",size,"white")
    28      canvas = Draw(img)
    29      canvas.setantialias(True)
    30      drawer = MolDrawing.MolDrawing(canvas)
    31
    32    if kekulize:
    33      from rdkit import Chem
    34      mol = Chem.Mol(mol.ToBinary())
    35      Chem.Kekulize(mol)
    36
    37    if not mol.GetNumConformers():
    38      from rdkit.Chem import AllChem
    39      AllChem.Compute2DCoords(mol)
    40
    41    drawer.wedgeDashedBonds=wedgeBonds
    42    drawer.AddMol(mol)
    43    canvas.flush()
    44
    45    return img
    46
    47 -def MolToImageFile(mol,filename,size=(300,300),kekulize=True, wedgeBonds=True):
    48    img = MolToImage(mol,size=size,kekulize=kekulize,wedgeBonds=wedgeBonds)
    49    img.save(filename)
    50
    51  tkRoot=None
    52  tkLabel=None
    53  tkPI=None
    54 -def ShowMol(mol,size=(300,300),kekulize=True,wedgeBonds=True,
    55              title='RDKit Molecule'):
    56    global tkRoot,tkLabel,tkPI
    57    import Tkinter
    58    from PIL import ImageTk
    59
    60    img = MolToImage(mol,size,kekulize,wedgeBonds)
    61
    62    if not tkRoot:
    63      tkRoot = Tkinter.Tk()
    64      tkRoot.title(title)
    65      tkPI = ImageTk.PhotoImage(img)
    66      tkLabel = Tkinter.Label(tkRoot,image=tkPI)
    67      tkLabel.place(x=0,y=0,width=img.size[0],height=img.size[1])
    68    else:
    69      tkPI.paste(img)
    70    tkRoot.geometry('%dx%d'%(img.size))
    71