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