rdkit.Chem.AtomPairs.Torsions

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

# Source Code for Module rdkit.Chem.AtomPairs.Torsions

      1  # $Id: Torsions.py 997 2009-02-25 06:12:43Z glandrum $
      2  #
      3  #  Copyright (C) 2004-2006 Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  """ Contains an implementation of Topological-torsion fingerprints, as
      8  described in:
      9
     10  R. Nilakantan, N. Bauman, J. S. Dixon, R. Venkataraghavan;
     11  "Topological Torsion: A New Molecular Descriptor for SAR Applications.
     12  Comparison with Other Descriptors" JCICS 27, 82-85 (1987).
     13
     14  """
     15  from rdkit import Chem
     16  from rdkit.Chem import rdMolDescriptors
     17  import Utils
     18
     19 -def pyScorePath(mol,path,size,atomCodes=None):
     20    """ Returns a score for an individual path.
     21
     22    >>> m = Chem.MolFromSmiles('CCCCC')
     23    >>> c1 = long(Utils.GetAtomCode(m.GetAtomWithIdx(0),1))
     24    >>> c2 = long(Utils.GetAtomCode(m.GetAtomWithIdx(1),2))
     25    >>> c3 = long(Utils.GetAtomCode(m.GetAtomWithIdx(2),2))
     26    >>> c4 = long(Utils.GetAtomCode(m.GetAtomWithIdx(3),1))
     27    >>> t = c1 | (c2 << rdMolDescriptors.AtomPairsParameters.codeSize) | (c3 << (rdMolDescriptors.AtomPairsParameters.codeSize*2)) | (c4 << (rdMolDescriptors.AtomPairsParameters.codeSize*3))
     28    >>> pyScorePath(m,(0,1,2,3),4)==t
     29    1
     30
     31    The scores are path direction independent:
     32    >>> pyScorePath(m,(3,2,1,0),4)==t
     33    1
     34
     35
     36    >>> m = Chem.MolFromSmiles('C=CC(=O)O')
     37    >>> c1 = long(Utils.GetAtomCode(m.GetAtomWithIdx(0),1))
     38    >>> c2 = long(Utils.GetAtomCode(m.GetAtomWithIdx(1),2))
     39    >>> c3 = long(Utils.GetAtomCode(m.GetAtomWithIdx(2),2))
     40    >>> c4 = long(Utils.GetAtomCode(m.GetAtomWithIdx(4),1))
     41    >>> t = c1 | (c2 << rdMolDescriptors.AtomPairsParameters.codeSize) | (c3 << (rdMolDescriptors.AtomPairsParameters.codeSize*2)) | (c4 << (rdMolDescriptors.AtomPairsParameters.codeSize*3))
     42    >>> pyScorePath(m,(0,1,2,4),4)==t
     43    1
     44
     45    """
     46    codes = [None]*size
     47    for i in range(size):
     48      if i==0 or i==(size-1):
     49        sub = 1
     50      else:
     51        sub = 2
     52      if not atomCodes:
     53        codes[i] = Utils.GetAtomCode(mol.GetAtomWithIdx(path[i]),sub)
     54      else:
     55        base = atomCodes[path[i]]
     56        codes[i]=base-sub
     57
     58    # "canonicalize" the code vector:
     59    beg=0
     60    end = len(codes)-1
     61    while(beg < end):
     62      if codes[beg] > codes[end]:
     63        codes.reverse()
     64        break
     65      elif codes[beg]==codes[end]:
     66        beg += 1
     67        end -= 1
     68      else:
     69        break
     70    accum = 0L
     71    for i in range(size):
     72      accum |= long(codes[i]) << (rdMolDescriptors.AtomPairsParameters.codeSize*i)
     73    return accum
     74
     75 -def ExplainPathScore(score,size=4):
     76    """
     77
     78    >>> m = Chem.MolFromSmiles('C=CC')
     79    >>> score=pyScorePath(m,(0,1,2),3)
     80    >>> ExplainPathScore(score,3)
     81    (('C', 1, 0), ('C', 2, 1), ('C', 1, 1))
     82
     83    Again, it's order independent:
     84    >>> score=pyScorePath(m,(2,1,0),3)
     85    >>> ExplainPathScore(score,3)
     86    (('C', 1, 0), ('C', 2, 1), ('C', 1, 1))
     87
     88
     89    >>> m = Chem.MolFromSmiles('C=CO')
     90    >>> score=pyScorePath(m,(0,1,2),3)
     91    >>> ExplainPathScore(score,3)
     92    (('C', 1, 1), ('C', 2, 1), ('O', 1, 0))
     93
     94    >>> m = Chem.MolFromSmiles('OC=CO')
     95    >>> score=pyScorePath(m,(0,1,2,3),4)
     96    >>> ExplainPathScore(score,4)
     97    (('O', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0))
     98
     99    >>> m = Chem.MolFromSmiles('CC=CO')
    100    >>> score=pyScorePath(m,(0,1,2,3),4)
    101    >>> ExplainPathScore(score,4)
    102    (('C', 1, 0), ('C', 2, 1), ('C', 2, 1), ('O', 1, 0))
    103
    104
    105    >>> m = Chem.MolFromSmiles('C=CC(=O)O')
    106    >>> score=pyScorePath(m,(0,1,2,3),4)
    107    >>> ExplainPathScore(score,4)
    108    (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 1))
    109    >>> score=pyScorePath(m,(0,1,2,4),4)
    110    >>> ExplainPathScore(score,4)
    111    (('C', 1, 1), ('C', 2, 1), ('C', 3, 1), ('O', 1, 0))
    112
    113
    114    >>> m = Chem.MolFromSmiles('OOOO')
    115    >>> score=pyScorePath(m,(0,1,2),3)
    116    >>> ExplainPathScore(score,3)
    117    (('O', 1, 0), ('O', 2, 0), ('O', 2, 0))
    118    >>> score=pyScorePath(m,(0,1,2,3),4)
    119    >>> ExplainPathScore(score,4)
    120    (('O', 1, 0), ('O', 2, 0), ('O', 2, 0), ('O', 1, 0))
    121
    122    """
    123    codeMask=(1<<rdMolDescriptors.AtomPairsParameters.codeSize)-1
    124    res=[None]*size
    125    #print '>>>>>>>>>>>',score,size,codeMask
    126    for i in range(size):
    127      if i==0 or i==(size-1):
    128        sub = 1
    129      else:
    130        sub = 2
    131      code = score&codeMask
    132      #print i,code,score
    133      score = score>>rdMolDescriptors.AtomPairsParameters.codeSize
    134      symb,nBranch,nPi = Utils.ExplainAtomCode(code)
    135      expl = symb,nBranch+sub,nPi
    136      res[i] = expl
    137    return tuple(res)
    138
    139  GetTopologicalTorsionFingerprintAsIntVect=rdMolDescriptors.GetTopologicalTorsionFingerprint
    140  GetTopologicalTorsionFingerprint=rdMolDescriptors.GetTopologicalTorsionFingerprint
    141 -def GetTopologicalTorsionFingerprintAsIds(mol,targetSize=4):
    142    iv = rdMolDescriptors.GetTopologicalTorsionFingerprint(mol,targetSize)
    143    res=[]
    144    for k,v in iv.GetNonzeroElements().iteritems():
    145      res.extend([k]*v)
    146    res.sort()
    147    return res
    148
    149
    150
    151
    152  #------------------------------------
    153  #
    154  #  doctest boilerplate
    155  #
    156 -def _test():
    157    import doctest,sys
    158    return doctest.testmod(sys.modules["__main__"])
    159
    160
    161  if __name__ == '__main__':
    162    import sys
    163    failed,tried = _test()
    164    sys.exit(failed)
    165