1
2
3
4
5
6
7
8
9
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
48 self._onPatt = None
49 self._offPatts = []
50
52 self._offPatts.append(Chem.MolFromSmarts(sma))
54 self._onPatt = Chem.MolFromSmarts(sma)
55
60
61
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
75 if self._onPatt is None:
76 return None
77 return mol.GetSubstructMatch(self._onPatt)
78
80 if self._onPatt is None:
81 return None
82 return mol.GetSubstructMatches(self._onPatt,uniquify)
83
85 if self._onPatt is None:
86 return None
87 return self._onPatt.GetBondWithIdx(idx)
88
89
90
91
92
93
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