Chem.PyMol

Package Chem :: Module PyMol |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.PyMol

      1  # $Id: PyMol.py 2 2006-05-06 22:54:39Z glandrum $

      2  #

      3  # Copyright (C) 2004-2006 Rational Discovery LLC

      4  #

      5  #   @@ All Rights Reserved  @@

      6  #

      7  """ uses pymol to interact with molecules

      8

      9  """
     10  import Chem
     11  import xmlrpclib
     12

     13

     14  _server=None
     15 -class MolViewer(object):
     16 -  def __init__(self,host='localhost',port=9123,force=0,**kwargs):
     17      global _server
     18      if not force and _server is not None:
     19        self.server=_server
     20      else:
     21        _server=None
     22        serv = xmlrpclib.Server('http://%s:%d'%(host,port))
     23        serv.ping()
     24        _server = serv
     25        self.server=serv
     26      self.InitializePyMol()
     27

     28 -  def InitializePyMol(self):
     29      """ does some initializations to set up PyMol according to our

     30      tastes

     31

     32      """
     33      self.server.do('set valence,1')
     34      self.server.do('set stick_rad,0.15')
     35      self.server.do('set mouse_selection_mode,0')
     36      self.server.do('set line_width,2')
     37      self.server.do('set selection_width,10')
     38      self.server.do('set auto_zoom,0')
     39

     40

     41 -  def DeleteAll(self):
     42      self.server.deleteAll()
     43

     44 -  def DeleteAllExcept(self,excludes):
     45      allNames = self.server.getNames('*',False)
     46      for nm in allNames:
     47        if nm not in excludes:
     48          self.server.deleteObject(nm)
     49

     50 -  def LoadFile(self,filename,name,showOnly=False):
     51      if showOnly:
     52        self.DeleteAll()
     53      id = self.server.loadFile(filename,name)
     54      return id
     55

     56 -  def ShowMol(self,mol,name='molecule',showOnly=True,highlightFeatures=[],

     57                molB="",confId=-1,zoom=True):
     58      """ special case for displaying a molecule or mol block """
     59

     60      if not molB:
     61        molB = Chem.MolToMolBlock(mol,confId=confId)
     62      server = self.server
     63      if not zoom:
     64        self.server.do('view rdinterface,store')
     65      if showOnly:
     66        self.DeleteAll()
     67      id = server.loadMolBlock(molB,name)
     68      if highlightFeatures:
     69        nm = name+'-features'
     70        conf = mol.GetConformer(confId)
     71        for feat in highlightFeatures:
     72          pt = [0.0,0.0,0.0]
     73          for idx in feat:
     74            loc = conf.GetAtomPosition(idx)
     75            pt[0] += loc[0]/len(feat)
     76            pt[1] += loc[1]/len(feat)
     77            pt[2] += loc[2]/len(feat)
     78          server.sphere(pt,0.2,(1,1,1),nm)
     79      if zoom:
     80        server.zoom('visible')
     81      else:
     82        self.server.do('view rdinterface,recall')
     83      return id
     84 -  def GetSelectedAtoms(self,whichSelection=None):
     85      if not whichSelection:
     86        sels = self.server.getNames('selections')
     87        if sels:
     88          whichSelection = sels[-1]
     89        else:
     90          whichSelection=None
     91      if whichSelection:
     92        items = self.server.index(whichSelection)
     93      else:
     94        items = []
     95      return items
     96

     97

     98 -  def SelectAtoms(self,itemId,atomIndices,selName='selection'):
     99      ids = '(id '
    100      ids += ','.join(['%d'%(x+1) for x in atomIndices])
    101      ids += ')'
    102      cmd = 'select %s,%s and %s'%(selName,ids,itemId)
    103      self.server.do(cmd)
    104

    105 -  def HighlightAtoms(self,indices,where,extraHighlight=False):
    106      if extraHighlight:
    107        idxText = ','.join(['%s and (id %d)'%(where,x) for x in indices])
    108        self.server.do('edit %s'%idxText)
    109      else:
    110        idxText = ' or '.join(['id %d'%x for x in indices])
    111        self.server.do('select selection, %s and (%s)'%(where,idxText))
    112

    113 -  def SetDisplayStyle(self,obj,style=''):
    114      self.server.do('hide everything,%s'%(obj,))
    115      if style:
    116        self.server.do('show %s,%s'%(style,obj))
    117

    118 -  def SelectProteinNeighborhood(self,aroundObj,inObj,distance=5.0,

    119                                  name='neighborhood',showSurface=False):
    120      self.server.do('select %(name)s,byres (%(aroundObj)s around %(distance)f) and %(inObj)s'%locals())
    121

    122      if showSurface:
    123        self.server.do('show surface,%s'%name)
    124        self.server.do('disable %s'%name)
    125

    126

    127 -  def AddPharmacophore(self,locs,colors,label,sphereRad=0.5):
    128      self.server.do('view rdinterface,store')
    129      self.server.resetCGO(label)
    130      for i,loc in enumerate(locs):
    131        self.server.sphere(loc,sphereRad,colors[i],label,1)
    132      self.server.do('enable %s'%label)
    133      self.server.do('view rdinterface,recall')
    134

    135

    136 -  def SetDisplayUpdate(self,val):
    137      if not val:
    138        self.server.do('set defer_update,1')
    139      else:
    140        self.server.do('set defer_update,0')
    141

    142

    143 -  def GetAtomCoords(self,sels):
    144      res = {}
    145      for label,idx in sels:
    146        coords = self.server.getAtomCoords('(%s and id %d)'%(label,idx))
    147        res[(label,idx)] = coords
    148        print 'grab:',label,idx,coords
    149      return res
    150

    151 -  def HideAll(self):
    152      self.server.do('disable all')
    153 -  def HideObject(self,objName):
    154      self.server.do('disable %s'%objName)
    155 -  def DisplayObject(self,objName):
    156      self.server.do('enable %s'%objName)
    157

    158 -  def Redraw(self):
    159      self.server.do('refresh')
    160 -  def Zoom(self,objName):
    161      self.server.zoom(objName)
    162

    163 -  def DisplayHBonds(self,objName,molName,proteinName,

    164                      molSelText='(%(molName)s)',

    165                      proteinSelText='(%(proteinName)s and not het)'):
    166      cmd = "delete %(objName)s;\n"
    167      cmd += "dist %(objName)s," + molSelText+","+proteinSelText+",mode=2;\n"
    168      cmd += "enable %(objName)s;"
    169      cmd = cmd%locals()
    170

    171      self.server.do(cmd)
    172

    173 -  def DisplayCollisions(self,objName,molName,proteinName,distCutoff=3.0,

    174                          color='red',

    175                          molSelText='(%(molName)s)',

    176                          proteinSelText='(%(proteinName)s and not het)'):
    177      cmd = "delete %(objName)s;\n"
    178      cmd += "dist %(objName)s," + molSelText+","+proteinSelText+",%(distCutoff)f,mode=0;\n"
    179      cmd += """enable %(objName)s

    180      color %(color)s, %(objName)s"""
    181      cmd = cmd%locals()
    182      self.server.do(cmd)
    183