rdkit.Chem.Pharm2D.Generate

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

# Source Code for Module rdkit.Chem.Pharm2D.Generate

      1  # $Id: Generate.py 1023 2009-03-20 05:15:16Z glandrum $
      2  #
      3  # Copyright (C) 2002-2008 greg Landrum and Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  """ generation of 2D pharmacophores
      8
      9  **Notes**
     10
     11    - The terminology for this gets a bit rocky, so here's a glossary of what
     12      terms used here mean:
     13
     14        1) *N-point pharmacophore* a combination of N features along with
     15           distances betwen them.
     16
     17        2) *N-point proto-pharmacophore*: a combination of N feature
     18           definitions without distances.  Each N-point
     19           proto-pharmacophore defines a manifold of potential N-point
     20           pharmacophores.
     21
     22        3) *N-point scaffold*: a collection of the distances defining
     23           an N-point pharmacophore without feature identities.
     24
     25    See Docs/Chem/Pharm2D.triangles.jpg for an illustration of the way
     26    pharmacophores are broken into triangles and labelled.
     27
     28    See Docs/Chem/Pharm2D.signatures.jpg for an illustration of bit
     29    numbering
     30
     31  """
     32  from rdkit.Chem.Pharm2D import Utils,SigFactory
     33  from rdkit.RDLogger import logger
     34  logger = logger()
     35
     36  _verbose = 0
     37
     38 -def _ShortestPathsMatch(match,featureSet,sig,dMat,sigFactory):
     39    """  Internal use only
     40
     41    """
     42    if _verbose:
     43      print 'match:',match
     44    nPts = len(match)
     45    distsToCheck =  Utils.nPointDistDict[nPts]
     46    nDists = len(distsToCheck)
     47    dist = [0]*nDists
     48    bins = sigFactory.GetBins()
     49    minD,maxD = bins[0][0],bins[-1][1]
     50
     51    for i in range(nDists):
     52      pt0,pt1 = distsToCheck[i]
     53      minSeen=maxD
     54      for idx1 in match[pt0]:
     55        for idx2 in match[pt1]:
     56          d = dMat[idx1,idx2]
     57          if d<minSeen:
     58            minSeen=d
     59      # FIX: this won't be an int if we're using the bond order.
     60      d = int(minSeen)
     61      # do a quick distance filter
     62      if d == 0 or d < minD or d >= maxD:
     63        return
     64      dist[i] = d
     65
     66    idx = sigFactory.GetBitIdx(featureSet,dist,sortIndices=False)
     67    if _verbose:
     68      print '\t',dist,minD,maxD,idx
     69
     70    if sigFactory.useCounts:
     71      sig[idx] = sig[idx]+1
     72    else:
     73      sig.SetBit(idx)
     74
     75
     76 -def Gen2DFingerprint(mol,sigFactory,perms=None,dMat=None):
     77    """ generates a 2D fingerprint for a molecule using the
     78     parameters in _sig_
     79
     80     **Arguments**
     81
     82       - mol: the molecule for which the signature should be generated
     83
     84       - sigFactory : the SigFactory object with signature parameters
     85         NOTE: no preprocessing is carried out for _sigFactory_.
     86               It *must* be pre-initialized.
     87
     88       - perms: (optional) a sequence of permutation indices limiting which
     89         pharmacophore combinations are allowed
     90
     91       - dMat: (optional) the distance matrix to be used
     92
     93    """
     94    if not isinstance(sigFactory,SigFactory.SigFactory):
     95      raise ValueError,'bad factory'
     96    featFamilies=sigFactory.GetFeatFamilies()
     97    if _verbose:
     98      print '* feat famillies:',featFamilies
     99    nFeats = len(featFamilies)
    100    minCount = sigFactory.minPointCount
    101    maxCount = sigFactory.maxPointCount
    102    if maxCount>3:
    103      logger.warning(' Pharmacophores with more than 3 points are not currently supported.\nSetting maxCount to 3.')
    104      maxCount=3
    105
    106    # generate the molecule's distance matrix, if required
    107    if dMat is None:
    108      from rdkit import Chem
    109      useBO = sigFactory.includeBondOrder
    110      dMat = Chem.GetDistanceMatrix(mol,useBO)
    111
    112    # generate the permutations, if required
    113    if perms is None:
    114      perms = []
    115      for count in range(minCount,maxCount+1):
    116        perms += Utils.GetIndexCombinations(nFeats,count)
    117
    118    # generate the matches:
    119    featMatches = sigFactory.GetMolFeats(mol)
    120    if _verbose:
    121      print '  featMatches:',featMatches
    122
    123    sig = sigFactory.GetSignature()
    124    for perm in perms:
    125      # the permutation is a combination of feature indices
    126      #   defining the feature set for a proto-pharmacophore
    127      featClasses=[0]
    128      for i in range(1,len(perm)):
    129        if perm[i]==perm[i-1]:
    130          featClasses.append(featClasses[-1])
    131        else:
    132          featClasses.append(featClasses[-1]+1)
    133
    134      # Get a set of matches at each index of
    135      #  the proto-pharmacophore.
    136      matchPerms = [featMatches[x] for x in perm]
    137      if _verbose:
    138        print '\n->Perm: %s'%(str(perm))
    139        print '    matchPerms: %s'%(str(matchPerms))
    140
    141      # Get all unique combinations of those possible matches:
    142      matchesToMap=Utils.GetUniqueCombinations(matchPerms,featClasses)
    143      for i,entry in enumerate(matchesToMap):
    144        entry = [x[1] for x in entry]
    145        matchesToMap[i]=entry
    146      if _verbose:
    147        print '    mtM:',matchesToMap
    148
    149      for match in matchesToMap:
    150        if sigFactory.shortestPathsOnly:
    151          _ShortestPathsMatch(match,perm,sig,dMat,sigFactory)
    152    return sig
    153