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

Source Code for Module Chem.FragmentMatcher

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