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

Source Code for Module rdkit.Chem.Descriptors

  1  # $Id: Descriptors.py 1911 2012-01-07 06:06:58Z glandrum $ 
  2  # 
  3  # Copyright (C) 2001-2010 greg Landrum and Rational Discovery LLC 
  4  # 
  5  #   @@ All Rights Reserved @@ 
  6  #  This file is part of the RDKit. 
  7  #  The contents are covered by the terms of the BSD license 
  8  #  which is included in the file license.txt, found at the root 
  9  #  of the RDKit source tree. 
 10  # 
 11  from rdkit import Chem 
 12  import collections 
 13   
14 -def _isCallable(thing):
15 return (hasattr(collections,'Callable') and isinstance(thing,collections.Callable)) or \ 16 hasattr(thing,'__call__')
17 18 _descList=[]
19 -def _setupDescriptors(namespace):
20 global _descList,descList 21 from rdkit.Chem import GraphDescriptors,MolSurf,Lipinski,Fragments,Crippen 22 from rdkit.Chem.EState import EState_VSA 23 mods = [GraphDescriptors,MolSurf,EState_VSA,Lipinski,Crippen,Fragments] 24 25 otherMods = [Chem] 26 27 for nm,thing in namespace.iteritems(): 28 if nm[0]!='_' and _isCallable(thing): 29 _descList.append((nm,thing)) 30 31 others = [] 32 for mod in otherMods: 33 tmp = dir(mod) 34 for name in tmp: 35 if name[0] != '_': 36 thing = getattr(mod,name) 37 if _isCallable(thing): 38 others.append(name) 39 40 for mod in mods: 41 tmp = dir(mod) 42 43 for name in tmp: 44 if name[0] != '_' and name[-1] != '_' and name not in others: 45 # filter out python reference implementations: 46 if name[:2]=='py' and name[2:] in tmp: 47 continue 48 thing = getattr(mod,name) 49 if _isCallable(thing): 50 namespace[name]=thing 51 _descList.append((name,thing)) 52 descList=_descList
53 54 from rdkit.Chem import rdMolDescriptors as _rdMolDescriptors 55 MolWt = lambda *x,**y:_rdMolDescriptors._CalcMolWt(*x,**y) 56 MolWt.version=_rdMolDescriptors._CalcMolWt_version 57 MolWt.__doc__="""The average molecular weight of the molecule ignoring hydrogens 58 59 >>> MolWt(Chem.MolFromSmiles('CC')) 60 30.07... 61 >>> MolWt(Chem.MolFromSmiles('[NH4+].[Cl-]')) 62 53.49... 63 64 """ 65 66 HeavyAtomMolWt=lambda x:MolWt(x,True) 67 HeavyAtomMolWt.__doc__="""The average molecular weight of the molecule ignoring hydrogens 68 69 >>> HeavyAtomMolWt(Chem.MolFromSmiles('CC')) 70 24.02... 71 >>> HeavyAtomMolWt(Chem.MolFromSmiles('[NH4+].[Cl-]')) 72 49.46... 73 74 """ 75 HeavyAtomMolWt.version="1.0.0" 76
77 -def NumValenceElectrons(mol):
78 """ The number of valence electrons the molecule has 79 80 >>> NumValenceElectrons(Chem.MolFromSmiles('CC')) 81 14.0 82 >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)O')) 83 18.0 84 >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)[O-]')) 85 18.0 86 >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)')) 87 12.0 88 89 90 """ 91 tbl = Chem.GetPeriodicTable() 92 accum = 0.0 93 for atom in mol.GetAtoms(): 94 accum += tbl.GetNOuterElecs(atom.GetAtomicNum()) 95 accum -= atom.GetFormalCharge() 96 accum += atom.GetTotalNumHs() 97 98 return accum
99 NumValenceElectrons.version="1.0.0" 100
101 -def _pyMolecularFormula(mol):
102 """ DEPRECATED: this is now implemented in C++ 103 Return the molecular formula 104 105 contribution from Andrew Dalke 106 107 """ 108 import collections 109 110 # Count the different atom types 111 counts = collections.defaultdict(int) 112 charge = 0 113 for atom in mol.GetAtoms(): 114 symb = atom.GetSymbol() 115 counts[symb]+=1 116 # Also track the implicit hydrogen counts and total charge 117 num_hydrogens = atom.GetTotalNumHs() 118 # A counts key should only exist if an element is present. 119 # Do this test to prevent the creation of an "H": 0 for 120 # things like [C], which have no implicit hydrogens. 121 if num_hydrogens: 122 counts['H'] += num_hydrogens 123 charge += atom.GetFormalCharge() 124 125 # Alphabetize elements by name 126 elements = sorted(counts) 127 128 # Put into Hill system order: 129 # If there are carbons then they go first, followed by hydrogens, 130 # then followed alphabetically by the other elements. 131 # If there are no carbons then the elements are in alphabetical 132 # order (including hydrogens) 133 if "C" in elements: 134 elements.remove("C") 135 elements.insert(0, "C") 136 if "H" in elements: 137 elements.remove("H") 138 elements.insert(1, "H") 139 140 # Include the count, so {"C": 1, "H": 4} becomes ["C", "H4"] 141 formula_terms = [] 142 for element in elements: 143 formula_terms.append(element) 144 if counts[element] > 1: 145 formula_terms.append(str(counts[element])) 146 147 # Handle the total charge. The result will be NH4+, Ca+2, etc. 148 if charge == 0: 149 pass 150 elif charge == 1: 151 formula_terms.append("+") 152 elif charge == -1: 153 formula_terms.append("-") 154 else: 155 formula_terms.append("%+d" % charge) 156 157 return "".join(formula_terms)
158 MolecularFormula=lambda x:_rdMolDescriptors.CalcMolFormula(x) 159 MolecularFormula.version=_rdMolDescriptors._CalcMolFormula_version 160 161 _setupDescriptors(locals()) 162 163 164 165 #------------------------------------ 166 # 167 # doctest boilerplate 168 #
169 -def _test():
170 import doctest,sys 171 return doctest.testmod(sys.modules["__main__"],optionflags=doctest.ELLIPSIS)
172 173 if __name__ == '__main__': 174 import sys 175 failed,tried = _test() 176 sys.exit(failed) 177