ML.Descriptors.MoleculeDescriptors

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

# Source Code for Module ML.Descriptors.MoleculeDescriptors

      1  # $Id: MoleculeDescriptors.py 746 2008-07-07 13:21:24Z glandrum $
      2  #
      3  #  Copyright (C) 2002-2008  greg Landrum and Rational Discovery LLC
      4  #
      5  """ Various bits and pieces for calculating Molecular descriptors
      6
      7  """
      8  import RDConfig
      9  from ML.Descriptors import Descriptors
     10  from Chem import AvailDescriptors
     11  AvailDescriptors.Desensitize()
     12  from RDLogger import logger
     13  logger = logger()
     14  import re
     15
     16 -class MolecularDescriptorCalculator(Descriptors.DescriptorCalculator):
     17    """ used for calculating descriptors for molecules
     18
     19    """
     20 -  def __init__(self,simpleList,*args,**kwargs):
     21      """ Constructor
     22
     23        **Arguments**
     24
     25          - simpleList: list of simple descriptors to be calculated
     26                (see below for format)
     27
     28        **Note**
     29
     30          - format of simpleList:
     31
     32             a list of strings which are keys into _AvailDescriptors.descDict_
     33
     34      """
     35      self.simpleList = simpleList[:]
     36      self.descriptorNames = self.simpleList[:]
     37      self.compoundList = None
     38
     39 -  def SaveState(self,fileName):
     40      """ Writes this calculator off to a file so that it can be easily loaded later
     41
     42       **Arguments**
     43
     44         - fileName: the name of the file to be written
     45
     46      """
     47      import cPickle
     48      try:
     49        f = open(fileName,'wb+')
     50      except:
     51        logger.error('cannot open output file %s for writing'%(fileName))
     52        return
     53      cPickle.dump(self,f)
     54      f.close()
     55
     56 -  def CalcDescriptors(self,mol,*args,**kwargs):
     57      """ calculates all descriptors for a given molecule
     58
     59        **Arguments**
     60
     61          - mol: the molecule to be used
     62
     63        **Returns**
     64          a tuple of all descriptor values
     65
     66      """
     67      res = [-666]*len(self.simpleList)
     68      for i,nm in enumerate(self.simpleList):
     69        fn = AvailDescriptors.descDict.get(nm,lambda x:777)
     70        try:
     71          res[i] = fn(mol)
     72        except:
     73          import traceback
     74          traceback.print_exc()
     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 -  def GetDescriptorVersions(self):
    109      """ returns a tuple of the versions of the descriptor calculators
    110
    111      """
    112      res = []
    113      for nm in self.simpleList:
    114        fn = AvailDescriptors.descDict.get(nm,lambda x:777)
    115        if hasattr(fn,'version'):
    116          vers = fn.version
    117        else:
    118          vers="N/A"
    119        res.append(vers)
    120      return tuple(res)
    121