Beispiel #1
0
def MolToMPL(mol,size=(300,300),kekulize=True, wedgeBonds=True,
             imageType=None,**kwargs):
  """ Generates a drawing of a molecule on a matplotlib canvas
  """
  if not mol:
    raise ValueError,'Null molecule provided'
  from MolDrawing import MolDrawing
  from mplCanvas import Canvas
  canvas = Canvas(size)
  drawer = MolDrawing(canvas)
  omol=mol
  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
  drawer.AddMol(mol,**kwargs)
  omol._atomPs=drawer.atomPs[mol]
  for k,v in omol._atomPs.iteritems():
    omol._atomPs[k]=canvas.rescalePt(v)
  canvas._figure.set_size_inches(float(size[0])/100,float(size[1])/100)
  return canvas._figure
Beispiel #2
0
def MolToMPL(mol,
             size=(300, 300),
             kekulize=True,
             wedgeBonds=True,
             imageType=None,
             **kwargs):
    """ Generates a drawing of a molecule on a matplotlib canvas
  """
    if not mol:
        raise ValueError, 'Null molecule provided'
    from mplCanvas import Canvas
    canvas = Canvas(size)
    drawer = MolDrawing(canvas)
    omol = mol
    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
    drawer.AddMol(mol, **kwargs)
    omol._atomPs = drawer.atomPs[mol]
    for k, v in omol._atomPs.iteritems():
        omol._atomPs[k] = canvas.rescalePt(v)
    canvas._figure.set_size_inches(float(size[0]) / 100, float(size[1]) / 100)
    return canvas._figure
Beispiel #3
0
def _createCanvas(size):
    useAGG, useCairo, Canvas = _getCanvas()
    if useAGG or useCairo:
        import Image
        img = Image.new("RGBA", size, "white")
        canvas = Canvas(img)
    else:
        MolDrawing.radicalSymbol = '.'  #<- the sping canvas doesn't support unicode well
        from spingCanvas import Canvas
        canvas = Canvas(size=size, name='MolToImageFile')
        img = canvas._image
    return img, canvas
Beispiel #4
0
def _createCanvas(size):
    useAGG, useCairo, Canvas = _getCanvas()
    if useAGG or useCairo:
        try:
            import Image
        except ImportError:
            from PIL import Image
        img = Image.new("RGBA", size, (0, 0, 0, 0))
        canvas = Canvas(img)
    else:
        from spingCanvas import Canvas
        canvas = Canvas(size=size, name='MolToImageFile')
        img = canvas._image
    return img, canvas
Beispiel #5
0
def MolToFile(mol,
              fileName,
              size=(300, 300),
              kekulize=True,
              wedgeBonds=True,
              imageType=None,
              fitImage=False,
              options=None,
              **kwargs):
    """ Generates a drawing of a molecule and writes it to a file
  """
    # original contribution from Uwe Hoffmann
    if not fileName:
        raise ValueError, 'no fileName provided'
    if not mol:
        raise ValueError, 'Null molecule provided'

    if imageType is None:
        imageType = os.path.splitext(fileName)[1][1:]

    if options is None:
        options = DrawingOptions()
    useAGG, useCairo, Canvas = _getCanvas()
    if fitImage:
        options.dotsPerAngstrom = int(min(size) / 10)
    options.wedgeDashedBonds = wedgeBonds
    if useCairo or useAGG:
        canvas = Canvas(size=size, imageType=imageType, fileName=fileName)
    else:
        options.radicalSymbol = '.'  #<- the sping canvas doesn't support unicode well
        canvas = Canvas(size=size, name=fileName, imageType=imageType)
    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)

    drawer.AddMol(mol, **kwargs)
    if useCairo or useAGG:
        canvas.flush()
    else:
        canvas.save()
Beispiel #6
0
def MolToFile(mol,fileName,size=(300,300),kekulize=True, wedgeBonds=True,
              imageType=None, fitImage=False, options=None, **kwargs):
  """ Generates a drawing of a molecule and writes it to a file
  """
  # original contribution from Uwe Hoffmann
  if not fileName:
    raise ValueError,'no fileName provided'
  if not mol:
    raise ValueError,'Null molecule provided'

  if imageType is None:
    imageType=os.path.splitext(fileName)[1][1:]

  if options is None:
    options = DrawingOptions()
  useAGG,useCairo,Canvas = _getCanvas()
  if fitImage:
      options.dotsPerAngstrom = int(min(size) / 10)
  options.wedgeDashedBonds = wedgeBonds
  if useCairo or useAGG:
    canvas = Canvas(size=size,imageType=imageType,
                              fileName=fileName)
  else:
    options.radicalSymbol = '.' #<- the sping canvas doesn't support unicode well
    canvas = Canvas(size=size,name=fileName,imageType=imageType)
  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)

  drawer.AddMol(mol,**kwargs)
  if useCairo or useAGG:
    canvas.flush()
  else:
    canvas.save()