ML.Descriptors.MoleculeDescriptors

Package ML :: Package Descriptors :: Module MoleculeDescriptors |
[hide private]
[frames] | no frames]

# Source Code for Module ML.Descriptors.MoleculeDescriptors

      1  #
      2  #  Copyright (C) 2002  greg Landrum and Rational Discovery LLC
      3  #
      4  """ Various bits and pieces for calculating Molecular descriptors
      5
      6  """
      7  import RDConfig
      8  from ML.Descriptors import Descriptors
      9  from Chem import AvailDescriptors
     10  AvailDescriptors.Desensitize()
     11  import re
     12
     13 -class MolecularDescriptorCalculator(Descriptors.DescriptorCalculator):
     14    """ used for calculating descriptors for molecules
     15
     16    """
     17 -  def __init__(self,simpleList,*args,**kwargs):
     18      """ Constructor
     19
     20        **Arguments**
     21
     22          - simpleList: list of simple descriptors to be calculated
     23                (see below for format)
     24
     25        **Note**
     26
     27          - format of simpleList:
     28
     29             a list of strings which are keys into _AvailDescriptors.descDict_
     30
     31      """
     32      self.simpleList = simpleList[:]
     33      self.descriptorNames = self.simpleList[:]
     34      self.compoundList = None
     35
     36 -  def SaveState(self,fileName):
     37      """ Writes this calculator off to a file so that it can be easily loaded later
     38
     39       **Arguments**
     40
     41         - fileName: the name of the file to be written
     42
     43      """
     44      import cPickle
     45      try:
     46        f = open(fileName,'wb+')
     47      except:
     48        print 'cannot open output file %s for writing'%(fileName)
     49        return
     50      cPickle.dump(self,f)
     51      f.close()
     52
     53 -  def CalcDescriptors(self,mol,*args,**kwargs):
     54      """ calculates all descriptors for a given molecule
     55
     56        **Arguments**
     57
     58          - mol: the molecule to be used
     59
     60        **Returns**
     61          a tuple of all descriptor values
     62
     63      """
     64      res = [None]*len(self.simpleList)
     65      for i in range(len(self.simpleList)):
     66        nm = self.simpleList[i]
     67        fn = AvailDescriptors.descDict.get(nm,lambda x:777)
     68        #print '>',nm
     69        try:
     70          res[i] = fn(mol)
     71        except:
     72          import traceback
     73          traceback.print_exc()
     74          res[i]=-666
     75      return tuple(res)
     76
     77 -  def GetDescriptorNames(self):
     78      """ returns a tuple of the names of the descriptors this calculator generates
     79
     80      """
     81      return tuple(self.descriptorNames)
     82
     83 -  def GetDescriptorSummaries(self):
     84      """ returns a tuple of summaries for the descriptors this calculator generates
     85
     86      """
     87      res = []
     88      for nm in self.simpleList:
     89        fn = AvailDescriptors.descDict.get(nm,lambda x:777)
     90        if hasattr(fn,'__doc__') and fn.__doc__:
     91          doc = fn.__doc__.split('\n\n')[0].strip()
     92          doc = re.sub('\ *\n\ *',' ',doc)
     93        else:
     94          doc = 'N/A'
     95        res.append(doc)
     96      return res
     97
     98 -  def GetDescriptorFuncs(self):
     99      """ returns a tuple of the functions used to generate this calculator's descriptors
    100
    101      """
    102      res = []
    103      for nm in self.simpleList:
    104        fn = AvailDescriptors.descDict.get(nm,lambda x:777)
    105        res.append(fn)
    106      return tuple(res)
    107
    108  if __name__ == '__main__':
    109    from Chem import *
    110    from ML.Descriptors import MoleculeDescriptors
    111    descs = ['MolLogP','IWd','Chi1v']
    112    smis = ['CCOC','CC=O','CCC(=O)O']
    113    calc = MoleculeDescriptors.MolecularDescriptorCalculator(descs)
    114    calc.SaveState('test_data/molcalc.dsc')
    115    print calc.GetDescriptorNames()
    116
    117    for smi in smis:
    118      mol = MolFromSmiles(smi)
    119      print smi,calc.CalcDescriptors(mol)
    120