rdkit.Chem.Subshape.SubshapeBuilder

Package rdkit :: Package Chem :: Package Subshape :: Module SubshapeBuilder |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Subshape.SubshapeBuilder

      1  #
      2  # Copyright (C) 2007 by Greg Landrum
      3  #  All rights reserved
      4  #
      5  from __future__ import print_function
      6
      7  import copy
      8  import time
      9
     10  from rdkit import Chem, Geometry
     11  from rdkit.Chem import AllChem
     12  from rdkit.Chem.Subshape import BuilderUtils
     13  from rdkit.Chem.Subshape import SubshapeObjects
     14  from rdkit.six.moves import cPickle
     15
     16
     17 -class SubshapeCombineOperations(object):
     18    UNION = 0
     19    SUM = 1
     20    INTERSECT = 2
     21
     22
     23 -class SubshapeBuilder(object):
     24    gridDims = (20, 15, 10)
     25    gridSpacing = 0.5
     26    winRad = 3.0
     27    nbrCount = 7
     28    terminalPtRadScale = 0.75
     29    fraction = 0.25
     30    stepSize = 1.0
     31    featFactory = None
     32
     33 -  def SampleSubshape(self, subshape1, newSpacing):
     34      ogrid = subshape1.grid
     35      rgrid = Geometry.UniformGrid3D(self.gridDims[0], self.gridDims[1], self.gridDims[2], newSpacing)
     36      for idx in range(rgrid.GetSize()):
     37        l = rgrid.GetGridPointLoc(idx)
     38        v = ogrid.GetValPoint(l)
     39        rgrid.SetVal(idx, v)
     40
     41      res = SubshapeObjects.ShapeWithSkeleton()
     42      res.grid = rgrid
     43      return res
     44
     45 -  def GenerateSubshapeShape(self, cmpd, confId=-1, addSkeleton=True, **kwargs):
     46      shape = SubshapeObjects.ShapeWithSkeleton()
     47      shape.grid = Geometry.UniformGrid3D(self.gridDims[0], self.gridDims[1], self.gridDims[2],
     48                                          self.gridSpacing)
     49      AllChem.EncodeShape(cmpd, shape.grid, ignoreHs=False, confId=confId)
     50      if addSkeleton:
     51        conf = cmpd.GetConformer(confId)
     52        self.GenerateSubshapeSkeleton(shape, conf, **kwargs)
     53      return shape
     54
     55 -  def __call__(self, cmpd, **kwargs):
     56      return self.GenerateSubshapeShape(cmpd, **kwargs)
     57
     58 -  def GenerateSubshapeSkeleton(self, shape, conf=None, terminalPtsOnly=False, skelFromConf=True):
     59      if conf and skelFromConf:
     60        pts = BuilderUtils.FindTerminalPtsFromConformer(conf, self.winRad, self.nbrCount)
     61      else:
     62        pts = BuilderUtils.FindTerminalPtsFromShape(shape, self.winRad, self.fraction)
     63      pts = BuilderUtils.ClusterTerminalPts(pts, self.winRad, self.terminalPtRadScale)
     64      BuilderUtils.ExpandTerminalPts(shape, pts, self.winRad)
     65      if len(pts) < 3:
     66        raise ValueError('only found %d terminals, need at least 3' % len(pts))
     67
     68      if not terminalPtsOnly:
     69        pts = BuilderUtils.AppendSkeletonPoints(shape.grid, pts, self.winRad, self.stepSize)
     70      for pt in pts:
     71        BuilderUtils.CalculateDirectionsAtPoint(pt, shape.grid, self.winRad)
     72      if conf and self.featFactory:
     73        BuilderUtils.AssignMolFeatsToPoints(pts, conf.GetOwningMol(), self.featFactory, self.winRad)
     74      shape.skelPts = pts
     75
     76 -  def CombineSubshapes(self, subshape1, subshape2, operation=SubshapeCombineOperations.UNION):
     77      cs = copy.deepcopy(subshape1)
     78      if operation == SubshapeCombineOperations.UNION:
     79        cs.grid |= subshape2.grid
     80      elif operation == SubshapeCombineOperations.SUM:
     81        cs.grid += subshape2.grid
     82      elif operation == SubshapeCombineOperations.INTERSECT:
     83        cs.grid &= subshape2.grid
     84      else:
     85        raise ValueError('bad combination operation')
     86      return cs
     87
     88
     89  if __name__ == '__main__':  # pragma: nocover
     90    from rdkit.Chem.PyMol import MolViewer
     91    from rdkit.six.moves import cPickle
     92    import tempfile
     93
     94    # cmpd = Chem.MolFromSmiles('CCCc1cc(C(=O)O)ccc1')
     95    # cmpd = Chem.AddHs(cmpd)
     96    if 1:
     97      cmpd = Chem.MolFromSmiles('C1=CC=C1C#CC1=CC=C1')
     98      cmpd = Chem.AddHs(cmpd)
     99      AllChem.EmbedMolecule(cmpd)
    100      AllChem.UFFOptimizeMolecule(cmpd)
    101      AllChem.CanonicalizeMol(cmpd)
    102      # print(Chem.MolToMolBlock(cmpd), file=file('testmol.mol', 'w+'))
    103    else:
    104      cmpd = Chem.MolFromMolFile('testmol.mol')
    105    builder = SubshapeBuilder()
    106    if 1:
    107      shape = builder.GenerateSubshapeShape(cmpd)
    108    v = MolViewer()
    109    if 1:
    110      tmpFile = tempfile.mktemp('.grd')
    111      v.server.deleteAll()
    112      Geometry.WriteGridToFile(shape.grid, tmpFile)
    113      time.sleep(1)
    114      v.ShowMol(cmpd, name='testMol', showOnly=True)
    115      v.server.loadSurface(tmpFile, 'testGrid', '', 2.5)
    116    v.server.resetCGO('*')
    117
    118    with open('subshape.pkl', 'w+') as f:
    119      cPickle.dump(shape, f)
    120    for i, pt in enumerate(shape.skelPts):
    121      v.server.sphere(tuple(pt.location), .5, (1, 0, 1), 'Pt-%d' % i)
    122      if not hasattr(pt, 'shapeDirs'):
    123        continue
    124      momBeg = pt.location - pt.shapeDirs[0]
    125      momEnd = pt.location + pt.shapeDirs[0]
    126      v.server.cylinder(tuple(momBeg), tuple(momEnd), .1, (1, 0, 1), 'v-%d' % i)
    127