rdkit.Chem.MolDb.FingerprintUtils

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

# Source Code for Module rdkit.Chem.MolDb.FingerprintUtils

      1  # $Id$
      2  #
      3  # Copyright (C) 2009 Greg Landrum
      4  #  All Rights Reserved
      5  #
      6  from __future__ import print_function
      7  from rdkit.six.moves import cPickle
      8  from rdkit.six import iterkeys
      9  from rdkit import DataStructs,Chem
     10  from rdkit import Chem
     11
     12  similarityMethods={'RDK':DataStructs.ExplicitBitVect,
     13                     'AtomPairs':DataStructs.IntSparseIntVect,
     14                     'TopologicalTorsions':DataStructs.LongSparseIntVect,
     15                     'Pharm2D':DataStructs.SparseBitVect,
     16                     'Gobbi2D':DataStructs.SparseBitVect,
     17                     'Morgan':DataStructs.UIntSparseIntVect
     18                     }
     19  supportedSimilarityMethods=list(iterkeys(similarityMethods))
     20
     21
     22 -class LayeredOptions:
     23    loadLayerFlags=0xFFFFFFFF
     24    searchLayerFlags=0x7
     25    minPath=1
     26    maxPath=6
     27    fpSize=1024
     28    wordSize=32
     29    nWords=fpSize//wordSize
     30    @staticmethod
     31 -  def GetFingerprint(mol,query=True):
     32      if query:
     33        flags=LayeredOptions.searchLayerFlags
     34      else:
     35        flags=LayeredOptions.loadLayerFlags
     36      return Chem.LayeredFingerprint(mol,layerFlags=flags,
     37                                     minPath=LayeredOptions.minPath,maxPath=LayeredOptions.maxPath,
     38                                     fpSize=LayeredOptions.fpSize)
     39    @staticmethod
     40 -  def GetWords(mol,query=True):
     41      txt = LayeredOptions.GetFingerprint(mol,query=query).ToBitString()
     42      words = [int(txt[x:x+32],2) for x in range(0,len(txt),32)]
     43      return  words
     44
     45    @staticmethod
     46 -  def GetQueryText(mol,query=True):
     47      words = LayeredOptions.GetWords(mol,query=query)
     48      colqs = []
     49      for idx,word in enumerate(words):
     50        if not word:
     51          continue
     52        idx = idx+1
     53        colqs.append('%(word)d&Col_%(idx)d=%(word)d'%locals())
     54      return  ' and '.join(colqs)
     55
     56
     57
     58 -def BuildSigFactory(options=None,fdefFile=None,
     59                      bins=[(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,100)],
     60                      skipFeats=('LumpedHydrophobe','ZnBinder')):
     61    if options:
     62      fdefFile = options.fdefFile
     63    if not fdefFile:
     64      raise ValueError('bad fdef file')
     65    from rdkit.Chem import ChemicalFeatures
     66    from rdkit.Chem.Pharm2D import SigFactory
     67    featFactory = ChemicalFeatures.BuildFeatureFactory(fdefFile)
     68    sigFactory = SigFactory.SigFactory(featFactory,
     69                                       skipFeats=skipFeats,
     70                                       trianglePruneBins=False)
     71    sigFactory.SetBins(bins)
     72    return sigFactory
     73
     74 -def BuildAtomPairFP(mol):
     75    from rdkit.Chem.AtomPairs import Pairs
     76    fp=Pairs.GetAtomPairFingerprintAsIntVect(mol)
     77    fp._sumCache = fp.GetTotalVal()
     78    return fp
     79 -def BuildTorsionsFP(mol):
     80    from rdkit.Chem.AtomPairs import Torsions
     81    fp=Torsions.GetTopologicalTorsionFingerprintAsIntVect(mol)
     82    fp._sumCache = fp.GetTotalVal()
     83    return fp
     84 -def BuildRDKitFP(mol):
     85    fp=Chem.RDKFingerprint(mol,nBitsPerHash=1)
     86    return fp
     87 -def BuildPharm2DFP(mol):
     88    global sigFactory
     89    from rdkit.Chem.Pharm2D import Generate
     90    try:
     91      fp=Generate.Gen2DFingerprint(mol,sigFactory)
     92    except IndexError:
     93      print('FAIL:',Chem.MolToSmiles(mol,True))
     94      raise
     95    return fp
     96 -def BuildMorganFP(mol):
     97    from rdkit.Chem import rdMolDescriptors
     98    fp = rdMolDescriptors.GetMorganFingerprint(mol,2)
     99    fp._sumCache = fp.GetTotalVal()
    100    return fp
    101
    102 -def DepickleFP(pkl,similarityMethod):
    103      if not isinstance(pkl,(bytes,str)):
    104        pkl = str(pkl)
    105      try:
    106          klass=similarityMethods[similarityMethod]
    107          fp = klass(pkl)
    108      except Exception:
    109          import traceback
    110          traceback.print_exc()
    111          fp = cPickle.loads(pkl)
    112      return fp
    113