rdkit.ML.Descriptors.MoleculeDescriptors

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

# Source Code for Module rdkit.ML.Descriptors.MoleculeDescriptors

      1  # $Id$
      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
      9  import re
     10
     11  from rdkit.Chem import Descriptors as DescriptorsMod
     12  from rdkit.ML.Descriptors import Descriptors
     13  from rdkit.RDLogger import logger
     14  from rdkit.six.moves import cPickle
     15
     16  logger = logger()
     17
     18
     19 -class MolecularDescriptorCalculator(Descriptors.DescriptorCalculator):
     20    """ used for calculating descriptors for molecules
     21
     22    """
     23
     24 -  def __init__(self, simpleList, *args, **kwargs):
     25      """ Constructor
     26
     27        **Arguments**
     28
     29          - simpleList: list of simple descriptors to be calculated
     30                (see below for format)
     31
     32        **Note**
     33
     34          - format of simpleList:
     35
     36             a list of strings which are functions in the rdkit.Chem.Descriptors module
     37
     38      """
     39      self.simpleList = tuple(simpleList)
     40      self.descriptorNames = tuple(self.simpleList)
     41      self.compoundList = None
     42      self._findVersions()
     43
     44 -  def _findVersions(self):
     45      """ returns a tuple of the versions of the descriptor calculators
     46
     47      """
     48      self.descriptorVersions = []
     49      for nm in self.simpleList:
     50        vers = 'N/A'
     51        if hasattr(DescriptorsMod, nm):
     52          fn = getattr(DescriptorsMod, nm)
     53          if hasattr(fn, 'version'):
     54            vers = fn.version
     55        self.descriptorVersions.append(vers)
     56
     57 -  def SaveState(self, fileName):
     58      """ Writes this calculator off to a file so that it can be easily loaded later
     59
     60       **Arguments**
     61
     62         - fileName: the name of the file to be written
     63
     64      """
     65      try:
     66        f = open(fileName, 'wb+')
     67      except Exception:
     68        logger.error('cannot open output file %s for writing' % (fileName))
     69        return
     70      cPickle.dump(self, f)
     71      f.close()
     72
     73 -  def CalcDescriptors(self, mol, *args, **kwargs):
     74      """ calculates all descriptors for a given molecule
     75
     76        **Arguments**
     77
     78          - mol: the molecule to be used
     79
     80        **Returns**
     81          a tuple of all descriptor values
     82
     83      """
     84      res = [-666] * len(self.simpleList)
     85      for i, nm in enumerate(self.simpleList):
     86        fn = getattr(DescriptorsMod, nm, lambda x: 777)
     87        try:
     88          res[i] = fn(mol)
     89        except Exception:
     90          import traceback
     91          traceback.print_exc()
     92      return tuple(res)
     93
     94 -  def GetDescriptorNames(self):
     95      """ returns a tuple of the names of the descriptors this calculator generates
     96
     97      """
     98      return self.descriptorNames
     99
    100 -  def GetDescriptorSummaries(self):
    101      """ returns a tuple of summaries for the descriptors this calculator generates
    102
    103      """
    104      res = []
    105      for nm in self.simpleList:
    106        fn = getattr(DescriptorsMod, nm, lambda x: 777)
    107        if hasattr(fn, '__doc__') and fn.__doc__:
    108          doc = fn.__doc__.split('\n\n')[0].strip()
    109          doc = re.sub('\ *\n\ *', ' ', doc)
    110        else:
    111          doc = 'N/A'
    112        res.append(doc)
    113      return res
    114
    115 -  def GetDescriptorFuncs(self):
    116      """ returns a tuple of the functions used to generate this calculator's descriptors
    117
    118      """
    119      res = []
    120      for nm in self.simpleList:
    121        fn = getattr(DescriptorsMod, nm, lambda x: 777)
    122        res.append(fn)
    123      return tuple(res)
    124
    125 -  def GetDescriptorVersions(self):
    126      """ returns a tuple of the versions of the descriptor calculators
    127
    128      """
    129      return tuple(self.descriptorVersions)
    130