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