Chem.FeatMaps.FeatMapPoint

Package Chem :: Package FeatMaps :: Module FeatMapPoint |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.FeatMaps.FeatMapPoint

      1  # $Id: FeatMapPoint.py 746 2008-07-07 13:21:24Z glandrum $
      2  #
      3  # Copyright (C) 2006 Greg Landrum
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  import Geometry
      8  import Chem
      9  from Chem import ChemicalFeatures
     10
     11 -class FeatMapPoint(ChemicalFeatures.FreeChemicalFeature):
     12    weight=0.0
     13    featDirs = None
     14 -  def __init__(self,*args,**kwargs):
     15      ChemicalFeatures.FreeChemicalFeature.__init__(self,*args,**kwargs)
     16      self.featDirs=[]
     17
     18 -  def initFromFeat(self,feat):
     19      """
     20
     21      >>> sfeat = ChemicalFeatures.FreeChemicalFeature('Aromatic','Foo',Geometry.Point3D(0,0,0))
     22      >>> fmp = FeatMapPoint()
     23      >>> fmp.initFromFeat(sfeat)
     24      >>> fmp.GetFamily()==sfeat.GetFamily()
     25      True
     26      >>> fmp.GetType()==sfeat.GetType()
     27      True
     28      >>> list(fmp.GetPos())
     29      [0.0, 0.0, 0.0]
     30      >>> fmp.featDirs == []
     31      True
     32
     33      >>> sfeat.featDirs = [Geometry.Point3D(1.0,0,0)]
     34      >>> fmp.initFromFeat(sfeat)
     35      >>> len(fmp.featDirs)
     36      1
     37
     38      """
     39      self.SetFamily(feat.GetFamily())
     40      self.SetType(feat.GetType())
     41      self.SetPos(feat.GetPos())
     42      if hasattr(feat,'featDirs'):
     43        self.featDirs = feat.featDirs[:]
     44
     45
     46 -  def GetDist2(self,other):
     47      """
     48
     49      >>> sfeat = ChemicalFeatures.FreeChemicalFeature('Aromatic','Foo',Geometry.Point3D(0,0,0))
     50      >>> fmp = FeatMapPoint()
     51      >>> fmp.initFromFeat(sfeat)
     52      >>> fmp.GetDist2(sfeat)
     53      0.0
     54      >>> sfeat.SetPos(Geometry.Point3D(2,0,0))
     55      >>> fmp.GetDist2(sfeat)
     56      4.0
     57      """
     58      return (self.GetPos()-other.GetPos()).LengthSq()
     59
     60 -  def GetDirMatch(self,other,useBest=True):
     61      """
     62
     63      >>> sfeat = ChemicalFeatures.FreeChemicalFeature('Aromatic','Foo',Geometry.Point3D(0,0,0))
     64      >>> fmp = FeatMapPoint()
     65      >>> fmp.initFromFeat(sfeat)
     66      >>> fmp.GetDirMatch(sfeat)
     67      1.0
     68
     69      >>> sfeat.featDirs=[Geometry.Point3D(0,0,1),Geometry.Point3D(0,0,-1)]
     70      >>> fmp.featDirs=[Geometry.Point3D(0,0,1),Geometry.Point3D(1,0,0)]
     71      >>> fmp.GetDirMatch(sfeat)
     72      1.0
     73      >>> fmp.GetDirMatch(sfeat,useBest=True)
     74      1.0
     75      >>> fmp.GetDirMatch(sfeat,useBest=False)
     76      0.0
     77
     78      >>> sfeat.featDirs=[Geometry.Point3D(0,0,1)]
     79      >>> fmp.GetDirMatch(sfeat,useBest=False)
     80      0.5
     81
     82      >>> sfeat.featDirs=[Geometry.Point3D(0,0,1)]
     83      >>> fmp.featDirs=[Geometry.Point3D(0,0,-1)]
     84      >>> fmp.GetDirMatch(sfeat)
     85      -1.0
     86      >>> fmp.GetDirMatch(sfeat,useBest=False)
     87      -1.0
     88
     89
     90      """
     91      if not self.featDirs or not other.featDirs:
     92        return 1.0
     93
     94      if not useBest:
     95        accum = 0.0
     96      else:
     97        accum = -100000.0
     98      for sDir in self.featDirs:
     99        for oDir in other.featDirs:
    100          d = sDir.DotProduct(oDir)
    101          if useBest:
    102            if d>accum:
    103              accum=d
    104          else:
    105            accum += d
    106
    107      if not useBest:
    108        accum /= len(self.featDirs)*len(other.featDirs)
    109
    110      return accum
    111
    112  #------------------------------------
    113  #
    114  #  doctest boilerplate
    115  #
    116 -def _test():
    117    import doctest,sys
    118    return doctest.testmod(sys.modules["__main__"])
    119
    120  if __name__ == '__main__':
    121    import sys
    122    failed,tried = _test()
    123    sys.exit(failed)
    124