1
2
3
4
5
6
7
8
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
46
48 self._onPatt = None
49 self._offPatts = []
50
52 self._offPatts.append(Chem.MolFromSmarts(sma))
53
54 - def Init(self, sma):
55 self._onPatt = Chem.MolFromSmarts(sma)
56
59
62
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
76 if self._onPatt is None:
77 return None
78 return mol.GetSubstructMatch(self._onPatt)
79
81 if self._onPatt is None:
82 return None
83 return mol.GetSubstructMatches(self._onPatt, uniquify=uniquify)
84
86 if self._onPatt is None:
87 return None
88 return self._onPatt.GetBondWithIdx(idx)
89
90
91
92
93
94
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