rdkit.Chem.Subshape.SubshapeObjects

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

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

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