rdkit.Chem.Draw.DrawUtils

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

# Source Code for Module rdkit.Chem.Draw.DrawUtils

      1  # $Id: DrawUtils.py 997 2009-02-25 06:12:43Z glandrum $
      2  #
      3  #  Copyright (C) 2004-2006 Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  import os
      8  from MolDrawing import MolDrawing
      9  from rdkit.sping.SVG.pidSVG import SVGCanvas as Canvas
     10  from rdkit.sping import pid
     11  import tempfile
     12
     13  _batikLoc=os.environ.get('BATIK_HOME','/usr/local/src/batik-1.5.1/')
     14  _batikJar='batik-rasterizer.jar'
     15  _javaExe='/usr/java/j2sdk1.4.2_05/bin/java'
     16
     17 -def Init(batikLoc=None,batikJar=None,javaExe=None):
     18    global _batikLoc,_batikJar,_javaExe
     19    if batikLoc: _batikLoc = batikLoc
     20    if batikJar: _batikJar = batikJar
     21    if javaExe:  _javaExe  = javaExe
     22
     23    if os.path.exists(os.path.join(_batikLoc,_batikJar)):
     24      return 1
     25    else:
     26      return 0
     27
     28 -def MolsToJpeg(mols,filenames,size=(200,200),dblSize=0,frame=0,
     29                highlightAtoms=None,
     30                verbose=0):
     31    if isinstance(filenames,basestring):
     32      mols = [mols]
     33      filenames = [filenames]
     34      if highlightAtoms: highlightAtoms = [highlightAtoms]
     35
     36    cmd = _javaExe
     37    batik = os.path.join(_batikLoc,_batikJar)
     38    javaArgs='-Djava.awt.headless=true -jar %s'%batik
     39    canvas = Canvas(size=size)
     40    drawing = MolDrawing(canvas)
     41
     42    if dblSize:
     43      drawing.atomLabelFontSize=18
     44      drawing.bondLineWidth *= 2
     45      drawing.dblBondOffset *= 2
     46
     47    if verbose: sys.stderr.write('generating SVG\n')
     48    svgNames = []
     49    for i,mol in enumerate(mols):
     50      filename = filenames[i]
     51      if highlightAtoms:
     52        highlight=highlightAtoms[i]
     53      else:
     54        highlight=None
     55      canvas.clear()
     56      drawing.AddMol(mol,canvas=canvas,highlightAtoms=highlight)
     57      svgName = '%s.svg'%(filename.split('.jpg')[0])
     58      if frame:
     59        canvas.drawRect(0,0,size[0]-1,size[1]-1,edgeColor=pid.black)
     60      canvas.save(svgName)
     61      svgNames.append(svgName)
     62
     63    if verbose: sys.stderr.write('converting to jpg:\n')
     64    fNames=' %s'%(' '.join(svgNames))
     65    fmtArgs='-m image/jpeg -dpi 100 -w %d -h %d -q .80'%(size[0],size[1])
     66
     67    try:
     68      #res = os.spawnlp(os.P_WAIT,cmd,javaArgs,'-m image/jpeg','-dpi 100',
     69      #          '-q .80','-w',size[0],'-h',size[1],fNames)
     70      arg = '%s %s %s %s &> /dev/null'%(cmd,javaArgs,fmtArgs,fNames)
     71      os.system(arg)
     72    except:
     73      import traceback
     74      if verbose: traceback.print_exc()
     75      ok = 0
     76    else:
     77      ok = 1
     78
     79      for name in svgNames:
     80        try:
     81          #os.unlink(name)
     82          pass
     83        except:
     84          import traceback
     85          if verbose: traceback.print_exc()
     86    return ok
     87 -def SmilesToJpeg(smiles,filenames,**kwargs):
     88    from rdkit import Chem
     89    if isinstance(filenames,basestring):
     90      smiles = [smiles]
     91      filenames = [filenames]
     92    mols = []
     93    for smi in smiles:
     94      mol = Chem.MolFromSmiles(smi)
     95      Chem.Compute2DCoords(mol)
     96      mols.append(mol)
     97    return MolsToJpeg(mols,filenames,**kwargs)
     98
     99  if __name__=='__main__':
    100    from rdkit import Chem
    101    import tempfile
    102    fN1 = tempfile.mktemp('.jpg')
    103    print fN1
    104    SmilesToJpeg(['c1ccccc1C(=O)O','c1ccccc1C(=O)N'],
    105                 [fN1,'foo2.jpg'],size=(300,300),verbose=1,
    106                 highlightAtoms=[range(3),range(4)],dblSize=1,
    107                 frame=1)
    108