Package rdkit :: Package Chem :: Module Descriptors

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Descriptors

      1  # $Id$
      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  from rdkit.Chem import rdPartialCharges
     13  import collections
     14
     15 -def _isCallable(thing):
     16    return (hasattr(collections,'Callable') and isinstance(thing,collections.Callable)) or \
     17                hasattr(thing,'__call__')
     18
     19  _descList=[]
     20 -def _setupDescriptors(namespace):
     21    global _descList,descList
     22    from rdkit.Chem import GraphDescriptors,MolSurf,Lipinski,Fragments,Crippen
     23    from rdkit.Chem.EState import EState_VSA
     24    mods = [GraphDescriptors,MolSurf,EState_VSA,Lipinski,Crippen,Fragments]
     25
     26    otherMods = [Chem]
     27
     28    for nm,thing in namespace.items():
     29      if nm[0]!='_' and _isCallable(thing):
     30        _descList.append((nm,thing))
     31
     32    others = []
     33    for mod in otherMods:
     34      tmp = dir(mod)
     35      for name in tmp:
     36        if name[0] != '_':
     37          thing = getattr(mod,name)
     38          if _isCallable(thing):
     39            others.append(name)
     40
     41    for mod in mods:
     42      tmp = dir(mod)
     43
     44      for name in tmp:
     45        if name[0] != '_' and name[-1] != '_' and name not in others:
     46          # filter out python reference implementations:
     47          if name[:2]=='py' and name[2:] in tmp:
     48            continue
     49          thing = getattr(mod,name)
     50          if _isCallable(thing):
     51            namespace[name]=thing
     52            _descList.append((name,thing))
     53    descList=_descList
     54
     55  from rdkit.Chem import rdMolDescriptors as _rdMolDescriptors
     56  MolWt = lambda *x,**y:_rdMolDescriptors._CalcMolWt(*x,**y)
     57  MolWt.version=_rdMolDescriptors._CalcMolWt_version
     58  MolWt.__doc__="""The average molecular weight of the molecule
     59
     60    >>> MolWt(Chem.MolFromSmiles('CC'))
     61    30.07
     62    >>> MolWt(Chem.MolFromSmiles('[NH4+].[Cl-]'))
     63    53.49...
     64
     65  """
     66
     67  HeavyAtomMolWt=lambda x:MolWt(x,True)
     68  HeavyAtomMolWt.__doc__="""The average molecular weight of the molecule ignoring hydrogens
     69
     70    >>> HeavyAtomMolWt(Chem.MolFromSmiles('CC'))
     71    24.02...
     72    >>> HeavyAtomMolWt(Chem.MolFromSmiles('[NH4+].[Cl-]'))
     73    49.46
     74
     75  """
     76  HeavyAtomMolWt.version="1.0.0"
     77
     78  ExactMolWt = lambda *x,**y:_rdMolDescriptors.CalcExactMolWt(*x,**y)
     79  ExactMolWt.version=_rdMolDescriptors._CalcExactMolWt_version
     80  ExactMolWt.__doc__="""The exact molecular weight of the molecule
     81
     82    >>> ExactMolWt(Chem.MolFromSmiles('CC'))
     83    30.04...
     84    >>> ExactMolWt(Chem.MolFromSmiles('[13CH3]C'))
     85    31.05...
     86
     87  """
     88
     89 -def NumValenceElectrons(mol):
     90    """ The number of valence electrons the molecule has
     91
     92    >>> NumValenceElectrons(Chem.MolFromSmiles('CC'))
     93    14
     94    >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)O'))
     95    18
     96    >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)[O-]'))
     97    18
     98    >>> NumValenceElectrons(Chem.MolFromSmiles('C(=O)'))
     99    12
    100
    101    """
    102    tbl = Chem.GetPeriodicTable()
    103    return sum(tbl.GetNOuterElecs(atom.GetAtomicNum()) - atom.GetFormalCharge() +
    104               atom.GetTotalNumHs() for atom in mol.GetAtoms())
    105  NumValenceElectrons.version="1.1.0"
    106
    107 -def NumRadicalElectrons(mol):
    108    """ The number of radical electrons the molecule has
    109      (says nothing about spin state)
    110
    111    >>> NumRadicalElectrons(Chem.MolFromSmiles('CC'))
    112    0
    113    >>> NumRadicalElectrons(Chem.MolFromSmiles('C[CH3]'))
    114    0
    115    >>> NumRadicalElectrons(Chem.MolFromSmiles('C[CH2]'))
    116    1
    117    >>> NumRadicalElectrons(Chem.MolFromSmiles('C[CH]'))
    118    2
    119    >>> NumRadicalElectrons(Chem.MolFromSmiles('C[C]'))
    120    3
    121
    122    """
    123    return sum(atom.GetNumRadicalElectrons() for atom in mol.GetAtoms())
    124  NumRadicalElectrons.version="1.1.0"
    125
    126 -def _ChargeDescriptors(mol,force=False):
    127    if not force and hasattr(mol,'_chargeDescriptors'):
    128      return mol._chargeDescriptors
    129    chgs = rdPartialCharges.ComputeGasteigerCharges(mol)
    130    minChg=500.
    131    maxChg=-500.
    132    for at in mol.GetAtoms():
    133      chg = float(at.GetProp('_GasteigerCharge'))
    134      minChg = min(chg,minChg)
    135      maxChg = max(chg,maxChg)
    136    res = (minChg,maxChg)
    137    mol._chargeDescriptors=res
    138    return res
    139
    140
    141 -def MaxPartialCharge(mol,force=False):
    142    _,res = _ChargeDescriptors(mol,force)
    143    return res
    144  MaxPartialCharge.version="1.0.0"
    145
    146 -def MinPartialCharge(mol,force=False):
    147    res,_ = _ChargeDescriptors(mol,force)
    148    return res
    149  MinPartialCharge.version="1.0.0"
    150
    151 -def MaxAbsPartialCharge(mol,force=False):
    152    v1,v2 = _ChargeDescriptors(mol,force)
    153    return max(abs(v1),abs(v2))
    154  MaxAbsPartialCharge.version="1.0.0"
    155
    156 -def MinAbsPartialCharge(mol,force=False):
    157    v1,v2 = _ChargeDescriptors(mol,force)
    158    return min(abs(v1),abs(v2))
    159  MinAbsPartialCharge.version="1.0.0"
    160
    161  from rdkit.Chem.EState.EState import MaxEStateIndex,MinEStateIndex,MaxAbsEStateIndex,MinAbsEStateIndex
    162
    163  _setupDescriptors(locals())
    164
    165
    166
    167  #------------------------------------
    168  #
    169  #  doctest boilerplate
    170  #
    171 -def _test():
    172    import doctest,sys
    173    return doctest.testmod(sys.modules["__main__"],optionflags=doctest.ELLIPSIS)
    174
    175  if __name__ == '__main__':
    176    import sys
    177    failed,tried = _test()
    178    sys.exit(failed)
    179