Exemplo n.º 1
0
def MolToImage(mol, size=(300, 300), kekulize=True, wedgeBonds=True, **kwargs):
    """ returns a PIL image containing a drawing of the molecule

    Keyword arguments:
    kekulize -- run kekulization routine on input `mol` (default True)
    size -- final image size, in pixel (default (300,300))
    wedgeBonds -- draw wedge (stereo) bonds (default True)
    highlightAtoms -- list of atoms to highlight (default [])
    highlightMap -- dictionary of (atom, color) pairs (default None)
    highlightBonds -- list of bonds to highlight (default [])
  """
    from MolDrawing import MolDrawing
    if not mol:
        raise ValueError, 'Null molecule provided'
    img, canvas = _createCanvas(size)
    drawer = MolDrawing(canvas)

    if kekulize:
        from rdkit import Chem
        mol = Chem.Mol(mol.ToBinary())
        Chem.Kekulize(mol)

    if not mol.GetNumConformers():
        from rdkit.Chem import AllChem
        AllChem.Compute2DCoords(mol)

    drawer.wedgeDashedBonds = wedgeBonds

    if kwargs.has_key('legend'):
        legend = kwargs['legend']
        del kwargs['legend']
    else:
        legend = ''

    drawer.AddMol(mol, **kwargs)

    if legend:
        from rdkit.Chem.Draw.MolDrawing import Font
        bbox = drawer.boundingBoxes[mol]
        pos = size[0] / 2, int(.94 *
                               size[1])  # the 0.94 is extremely empirical
        # canvas.addCanvasPolygon(((bbox[0],bbox[1]),(bbox[2],bbox[1]),(bbox[2],bbox[3]),(bbox[0],bbox[3])),
        #                         color=(1,0,0),fill=False,stroke=True)
        # canvas.addCanvasPolygon(((0,0),(0,size[1]),(size[0],size[1]),(size[0],0)   ),
        #                         color=(0,0,1),fill=False,stroke=True)
        font = Font(face='sans', size=12)
        canvas.addCanvasText(legend, pos, font)

    if kwargs.get('returnCanvas', False):
        return img, canvas, drawer
    else:
        canvas.flush()
        return img
Exemplo n.º 2
0
def drawmol(s):
    bsize = 200
    tsize = 80
    size = (bsize, bsize + tsize)
    m = Chem.MolFromSmiles(s)
    #print("wt", MolWt(m))
    img, canvas, drawer = Draw.MolToImage(m, size=size, returnCanvas=True)
    #                        centerIt=False, drawingTrans=(bsize/2+1,bsize/2+tsize))
    font = Font(face='sans', size=12)
    pos = bsize / 2, bsize, 0
    canvas.addCanvasText('%s\r\nMolWt: %g\tTPSA: %g' % (s, MolWt(m), TPSA(m)),
                         pos, font)

    with open('xx' + s + '.png', 'w') as f:
        canvas.flush()
        img.save(f)
Exemplo n.º 3
0
 def _CairoDrawer(self, rdmol):
     """Render an svg using cairo or agg renderer."""
     # Some drawing options
     options = Draw.DrawingOptions()
     options.bondLineWidth = 1
     # Prepare drawing
     useAGG, useCairo, Canvas = Draw._getCanvas()
     canvas = Canvas(size=(self.width, self.height),
                     imageType='svg',
                     fileName=self.svgfile)
     drawer = Draw.MolDrawing(canvas=canvas, drawingOptions=options)
     drawer.AddMol(rdmol)
     if self.legend is not None:
         from rdkit.Chem.Draw.MolDrawing import Font
         pos = (self.width / 2, int(.1 * self.height), 0)
         font = Font(face='serif', size=18)
         canvas.addCanvasText(self.legend, pos, font)
     canvas.flush()
Exemplo n.º 4
0
def MolToImage(mol,
               size=(300, 300),
               kekulize=True,
               wedgeBonds=True,
               fitImage=False,
               options=None,
               canvas=None,
               **kwargs):
    """Returns a PIL image containing a drawing of the molecule

      ARGUMENTS:

        - kekulize: run kekulization routine on input `mol` (default True)

        - size: final image size, in pixel (default (300,300))

        - wedgeBonds: draw wedge (stereo) bonds (default True)

        - highlightAtoms: list of atoms to highlight (default [])

        - highlightMap: dictionary of (atom, color) pairs (default None)

        - highlightBonds: list of bonds to highlight (default [])

        - highlightColor: RGB color as tuple (default [1, 0, 0])

      NOTE:

            use 'matplotlib.colors.to_rgb()' to convert string and
            HTML color codes into the RGB tuple representation, eg.

              from matplotlib.colors import ColorConverter
              img = Draw.MolToImage(m, highlightAtoms=[1,2], highlightColor=ColorConverter().to_rgb('aqua'))
              img.save("molecule.png")

      RETURNS:

        a PIL Image object
  """

    if not mol:
        raise ValueError('Null molecule provided')
    if canvas is None:
        img, canvas = _createCanvas(size)
    else:
        img = None

    options = options or DrawingOptions()
    if fitImage:
        options.dotsPerAngstrom = int(min(size) / 10)
    options.wedgeDashedBonds = wedgeBonds
    if 'highlightColor' in kwargs:
        color = kwargs.pop('highlightColor', (1, 0, 0))
        options.selectColor = color

    drawer = MolDrawing(canvas=canvas, drawingOptions=options)

    if kekulize:
        from rdkit import Chem
        mol = Chem.Mol(mol.ToBinary())
        Chem.Kekulize(mol)

    if not mol.GetNumConformers():
        from rdkit.Chem import AllChem
        AllChem.Compute2DCoords(mol)

    if 'legend' in kwargs:
        legend = kwargs['legend']
        del kwargs['legend']
    else:
        legend = ''

    drawer.AddMol(mol, **kwargs)

    if legend:
        from rdkit.Chem.Draw.MolDrawing import Font
        bbox = drawer.boundingBoxes[mol]
        pos = size[0] / 2, int(.94 *
                               size[1]), 0  # the 0.94 is extremely empirical
        # canvas.addCanvasPolygon(((bbox[0],bbox[1]),(bbox[2],bbox[1]),(bbox[2],bbox[3]),(bbox[0],bbox[3])),
        #                         color=(1,0,0),fill=False,stroke=True)
        # canvas.addCanvasPolygon(((0,0),(0,size[1]),(size[0],size[1]),(size[0],0)   ),
        #                         color=(0,0,1),fill=False,stroke=True)
        font = Font(face='sans', size=12)
        canvas.addCanvasText(legend, pos, font)

    if kwargs.get('returnCanvas', False):
        return img, canvas, drawer
    else:
        canvas.flush()
        return img