rdkit.Chem.FragmentMatcher

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

# Source Code for Module rdkit.Chem.FragmentMatcher

      1  #
      2  # Copyright (C) 2002-2006 greg Landrum and Rational Discovery LLC
      3  #
      4  #   @@ All Rights Reserved @@
      5  #  This file is part of the RDKit.
      6  #  The contents are covered by the terms of the BSD license
      7  #  which is included in the file license.txt, found at the root
      8  #  of the RDKit source tree.
      9  #
     10  """ exposes a class for matching fragments of molecules.
     11
     12  The class exposes a simple API:
     13
     14  If you want a matcher that hits C=O, you'd do:
     15  >>> p = FragmentMatcher()
     16  >>> p.Init('C=O')
     17
     18  you can then match with:
     19  >>> mol = Chem.MolFromSmiles('CC(=O)O')
     20  >>> p.HasMatch(mol)
     21  1
     22  >>> p.HasMatch(Chem.MolFromSmiles('CC(C)C'))
     23  0
     24
     25  information about the matches:
     26  >>> len(p.GetMatches(Chem.MolFromSmiles('CC=O')))
     27  1
     28  >>> len(p.GetMatches(Chem.MolFromSmiles('O=CC=O')))
     29  2
     30
     31  or, you can add exclusion fragments (defined as smarts) with:
     32  >>> p.AddExclusion('c1ccccc1')
     33
     34  now the matcher will not hit anything that has a benzene ring.
     35  >>> p.HasMatch(Chem.MolFromSmiles('CC=O'))
     36  1
     37  >>> p.HasMatch(Chem.MolFromSmiles('c1ccccc1CC=O'))
     38  0
     39
     40
     41  """
     42  from rdkit import Chem
     43
     44
     45 -class FragmentMatcher(object):
     46
     47 -  def __init__(self):
     48      self._onPatt = None
     49      self._offPatts = []
     50
     51 -  def AddExclusion(self, sma):
     52      self._offPatts.append(Chem.MolFromSmarts(sma))
     53
     54 -  def Init(self, sma):
     55      self._onPatt = Chem.MolFromSmarts(sma)
     56
     57 -  def GetSMARTS(self):
     58      pass
     59
     60 -  def GetExclusionSMARTS(self):
     61      pass
     62
     63 -  def HasMatch(self, mol):
     64      if self._onPatt is None:
     65        return 0
     66      t = mol.HasSubstructMatch(self._onPatt)
     67      if not t:
     68        return 0
     69      else:
     70        for patt in self._offPatts:
     71          if mol.HasSubstructMatch(patt):
     72            return 0
     73      return 1
     74
     75 -  def GetMatch(self, mol):
     76      if self._onPatt is None:
     77        return None
     78      return mol.GetSubstructMatch(self._onPatt)
     79
     80 -  def GetMatches(self, mol, uniquify=1):
     81      if self._onPatt is None:
     82        return None
     83      return mol.GetSubstructMatches(self._onPatt, uniquify=uniquify)
     84
     85 -  def GetBond(self, idx):
     86      if self._onPatt is None:
     87        return None
     88      return self._onPatt.GetBondWithIdx(idx)
     89
     90
     91    #------------------------------------
     92    #
     93    #  doctest boilerplate
     94    #
     95 -def _test():
     96    import doctest, sys
     97    return doctest.testmod(sys.modules["__main__"])
     98
     99
    100  if __name__ == '__main__':
    101    import sys
    102    failed, tried = _test()
    103    sys.exit(failed)
    104