Package rdkit :: Package Chem :: Package Subshape :: Module SubshapeObjects

[hide private]
[frames] | no frames]

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

     1  # $Id$
     2  #
     3  # Copyright (C) 2007 by Greg Landrum
     4  #  All rights reserved
     5  #
     6  from rdkit import Chem
     7  from rdkit import Geometry
     8
     9 -class SkeletonPoint(object):
    10    location=None
    11    shapeMoments=None
    12    shapeDirs=None
    13    molFeatures=None
    14    featmapFeatures=None
    15    fracVol=0.0
    16
    17 -  def __init__(self,*args,**kwargs):
    18      self._initMemberData()
    19      self.location = kwargs.get('location',None)
    20
    21 -  def _initMemberData(self):
    22      self.shapeMoments=(0.0,)*3
    23      self.shapeDirs=[None]*3
    24      self.molFeatures=[]
    25      self.featmapFeatures=[]
    26
    27 -class ShapeWithSkeleton(object):
    28    grid=None
    29    skelPts=None
    30
    31 -  def __init__(self,*args,**kwargs):
    32      self._initMemberData()
    33
    34 -  def _initMemberData(self):
    35      self.skelPts=[]
    36
    37
    38 -class SubshapeShape(object):
    39    shapes=None # a list of ShapeWithSkeleton objects at multiple resolutions (low to high)
    40    featMap=None
    41    keyFeat=None
    42
    43 -  def __init__(self,*args,**kwargs):
    44      self._initMemberData()
    45
    46 -  def _initMemberData(self):
    47      self.shapes=[]
    48
    49 -def _displaySubshapeSkelPt(viewer,skelPt,cgoNm,color):
    50    viewer.server.sphere(tuple(skelPt.location),.5,color,cgoNm)
    51    if hasattr(skelPt,'shapeDirs'):
    52      momBeg = skelPt.location-skelPt.shapeDirs[0]
    53      momEnd = skelPt.location+skelPt.shapeDirs[0]
    54      viewer.server.cylinder(tuple(momBeg),tuple(momEnd),.1,color,cgoNm)
    55
    56 -def DisplaySubshapeSkeleton(viewer,shape,name,color=(1,0,1),colorByOrder=False):
    57    orderColors=((1,0,0),(0,1,0),(0,0,1),(1,1,0),(1,0,1),(0,1,1))
    58    cgoNm='%s-skeleton'%name
    59    viewer.server.resetCGO(cgoNm)
    60    for i,pt in enumerate(shape.skelPts):
    61      if colorByOrder:
    62        color = orderColors[i%len(orderColors)]
    63      _displaySubshapeSkelPt(viewer,pt,cgoNm,color)
    64
    65 -def DisplaySubshape(viewer,shape,name,showSkelPts=True,color=(1,0,1)):
    66    from rdkit import Geometry
    67    import os,tempfile
    68    fName = tempfile.mktemp('.grd')
    69    Geometry.WriteGridToFile(shape.grid,fName)
    70    viewer.server.loadSurface(fName,name,'',2.5)
    71    if showSkelPts:
    72      DisplaySubshapeSkeleton(viewer,shape,name,color)
    73    # On Windows, the file cannot be deleted if the viewer still has the file open.
    74    # Pause for a moment, to give the viewer a chance, then try again.
    75    try:
    76      os.unlink(fName)
    77    except Exception:
    78      import time
    79      time.sleep(.5)
    80      try:
    81        os.unlink(fName)
    82      except Exception:
    83        # Fall back to the default of letting the system clean up the temporary directory.
    84        pass
    85