Chem.Pharm2D.LazyGenerator

Package Chem :: Package Pharm2D :: Module LazyGenerator |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.Pharm2D.LazyGenerator

      1  # $Id: LazyGenerator.py 2 2006-05-06 22:54:39Z glandrum $
      2  #
      3  # Copyright (C) 2003-2006 greg Landrum and Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  """ lazy generator of 2D pharmacophore signature data
      8
      9  """
     10  import Chem
     11  from Chem.Pharm2D import SigFactory,Matcher
     12
     13 -class Generator(object):
     14    """
     15
     16    Important attributes:
     17
     18     - mol: the molecules whose signature is being worked with
     19
     20     - sig: the signature to be used to decide what bits are set in the
     21       molecule.  This signature can be considered to be constant in
     22       this class, none of the class methods modify sig
     23
     24    """
     25 -  def __init__(self,sig,mol,dMat=None,bitCache=1):
     26      """ constructor
     27
     28        **Arguments**
     29
     30         - sig: a signature, see class docs
     31
     32         - mol: a molecule, see class docs
     33
     34         - dMat: (optional) a distance matrix for the molecule.  If this
     35           is not provided, one will be calculated
     36
     37         - bitCache: (optional) if nonzero, a local cache of which bits
     38           have been queried will be maintained.  Otherwise things must
     39           be recalculate each time a bit is queried.
     40
     41      """
     42      if isinstance(sig,SigFactory.SigFactory):
     43        sig = sig.GetSignature()
     44      self.sig = sig
     45      self.mol = mol
     46
     47      if dMat is None:
     48        useBO = self.sig.GetIncludeBondOrder()
     49        dMat = Chem.GetDistanceMatrix(mol,useBO)
     50
     51      self.dMat = dMat
     52
     53      if bitCache:
     54        self.bits = {}
     55      else:
     56        self.bits = None
     57
     58      nPatts = self.sig.GetNumPatterns()
     59      pattMatches = [None]*nPatts
     60      for i in range(nPatts):
     61        patt = self.sig.GetPattern(i)
     62        pattMatches[i]=self.mol.GetSubstructMatches(patt)
     63      self.pattMatches = pattMatches
     64
     65 -  def GetBit(self,idx):
     66      """ returns a bool indicating whether or not the bit is set
     67
     68      """
     69      if idx < 0 or idx >= self.sig.GetSize():
     70        raise IndexError,'Index %d invalid'%(idx)
     71      if self.bits is not None and self.bits.has_key(idx):
     72        return self.bits[idx]
     73
     74      tmp = Matcher.GetAtomsMatchingBit(self.sig,idx,self.mol,
     75                                        dMat=self.dMat,justOne=1,
     76                                        matchingAtoms=self.pattMatches)
     77      if not tmp or len(tmp)==0: res = 0
     78      else: res = 1
     79
     80      if self.bits is not None:
     81        self.bits[idx] = res
     82      return res
     83
     84 -  def __len__(self):
     85      """ allows class to support len()
     86
     87      """
     88      return self.sig.GetSize()
     89 -  def __getitem__(self,itm):
     90      """ allows class to support random access.
     91        Calls self.GetBit()
     92
     93      """
     94      return self.GetBit(itm)
     95
     96
     97
     98
     99  if __name__ == '__main__':
    100    import time
    101    import RDConfig,Chem
    102    from Chem.Pharm2D import Gobbi_Pharm2D,Generate
    103    import RandomArray
    104
    105    factory = Gobbi_Pharm2D.factory
    106    nToDo=100
    107    inD = open(RDConfig.RDDataDir+"/NCI/first_5K.smi",'r').readlines()[:nToDo]
    108    mols = [None]*len(inD)
    109    for i in range(len(inD)):
    110      smi = inD[i].split('\t')[0]
    111      smi.strip()
    112      mols[i] = Chem.MolFromSmiles(smi)
    113
    114    sig = factory.GetSignature()
    115
    116    nBits = 300
    117    RandomArray.seed(23,47)
    118    bits = RandomArray.random_integers(sig.GetSize()-1,0,shape=[nBits])
    119
    120    print 'Using the Lazy Generator'
    121    t1 = time.time()
    122    for i in range(len(mols)):
    123      if not i % 10: print 'done mol %d of %d'%(i,len(mols))
    124      gen = Generator(factory,mols[i])
    125      for bit in bits:
    126        v = gen[bit]
    127    t2 = time.time()
    128    print '\tthat took %4.2f seconds'%(t2-t1)
    129
    130
    131    print 'Generating and checking signatures'
    132    t1 = time.time()
    133    for i in range(len(mols)):
    134      if not i % 10: print 'done mol %d of %d'%(i,len(mols))
    135      sig = Generate.Gen2DFingerprint(mols[i],factory)
    136      for bit in bits:
    137        v = sig[bit]
    138    t2 = time.time()
    139    print '\tthat took %4.2f seconds'%(t2-t1)
    140