Package rdkit :: Package Chem :: Module FragmentMatcher

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.FragmentMatcher

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