File: UnitTestMCS.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (345 lines) | stat: -rw-r--r-- 11,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import unittest
import time

from rdkit import Chem
from rdkit.Chem import rdFMCS
from rdkit.TestRunner import isDebugBuild

def load_smiles(text):
  mols = []
  for line in text.strip().splitlines():
    smiles = line.split()[0]
    mol = Chem.MolFromSmiles(smiles)
    assert mol is not None, smiles
    mols.append(mol)
  return mols


_ignore = object()


class MCSTestCase(unittest.TestCase):

  def assert_search(self, smiles, numAtoms, numBonds, smarts=_ignore, verbose=False, **kwargs):
    result = rdFMCS.FindMCS(smiles, **kwargs)
    if verbose:
      print("RESULT: ",result.smartsString)
    self.assert_result(result, canceled=False, numAtoms=numAtoms, numBonds=numBonds, smarts=smarts)

  def assert_result(self, result, canceled=_ignore, numAtoms=_ignore, numBonds=_ignore,
                    smarts=_ignore):
    if canceled is not _ignore:
      self.assertEqual(result.canceled, canceled)
    if numAtoms is not _ignore:
      self.assertEqual(result.numAtoms, numAtoms)
    if numBonds is not _ignore:
      self.assertEqual(result.numBonds, numBonds)
    if smarts is not _ignore:
      self.assertEqual(result.smartsString, smarts)


simple_mols = load_smiles("""
c1ccccc1O phenol
CO methanol""")


# class TestMinAtoms(MCSTestCase):
#
#   def test_min_atoms_2(self):
#     self.assert_search(simple_mols, 2, 1, minNumAtoms=2)
#
#   def test_min_atoms_3(self):
#     self.assert_search(simple_mols, -1, -1, smarts=None, minNumAtoms=3)
#
#   def test_min_atoms_1(self):
#     try:
#       result = rdFMCS.FindMCS(simple_mols, minNumAtoms=1)
#     except ValueError:
#       pass
#     else:
#       raise AssertionError("should have raised an exception")


maximize_mols = load_smiles("""
C12CCC1CC2OCCCCCCC 2-rings-and-chain-with-O
C12CCC1CC2SCCCCCCC 2-rings-and-chain-with-S
""")


class TextMaximize(MCSTestCase):
  # C12CCC1CC2OCCCCCCC 2-rings-and-chain-with-O
  # C12CCC1CC2SCCCCCCC 2-rings-and-chain-with-S
  def test_maximize_default(self):
    # default maximizes the number of bonds
    self.assert_search(maximize_mols, 6, 7)

  def _test_maximize_atoms(self):
    # FIX: This fails to maximize the number of atoms
    self.assert_search(maximize_mols, 7, 6, maximizeBonds=False)

  def test_maximize_bonds(self):
    self.assert_search(maximize_mols, 6, 7, maximizeBonds=True)


atomtype_mols = load_smiles("""
c1ccccc1O phenol
CCCCCCOn1cccc1 different-answers-depending-on-type
""")


class TestAtomTypes(MCSTestCase):
  # The tests compare:
  #   c1ccccc1O
  #   CCCCCCOn1cccc1
  def test_atom_compare_default(self):
    self.assert_search(atomtype_mols, 4, 3, smarts='[#6](:[#6]:[#6]):[#6]',
                       bondCompare=rdFMCS.BondCompare.CompareOrderExact)

  def test_atom_compare_elements(self):
    self.assert_search(atomtype_mols, 4, 3, smarts='[#6](:[#6]:[#6]):[#6]',
                       atomCompare=rdFMCS.AtomCompare.CompareElements,
                       bondCompare=rdFMCS.BondCompare.CompareOrderExact)

  def test_atom_compare_any(self):
    # Note: bond aromaticities must still match!
    # 'cccccO' matches 'ccccnO'
    self.assert_search(atomtype_mols, 6, 5,
                       atomCompare=rdFMCS.AtomCompare.CompareAny,
                       bondCompare=rdFMCS.BondCompare.CompareOrderExact)

  def test_atom_compare_any_bond_compare_any(self):
    # Linear chain of 7 atoms
    self.assert_search(atomtype_mols, 7, 6,
                       atomCompare=rdFMCS.AtomCompare.CompareAny,
                        bondCompare=rdFMCS.BondCompare.CompareAny)

  def test_bond_compare_any(self):
    # Linear chain of 7 atoms
    self.assert_search(atomtype_mols, 7, 6,
                       bondCompare=rdFMCS.BondCompare.CompareAny)


isotope_mols = load_smiles("""
C1C[0N]CC[5C]1[1C][2C][2C][3C] C1223
C1CPCC[4C]1[2C][2C][1C][3C] C2213
""")


class TestIsotopes(MCSTestCase):
  # C1C[0N]CC[5C]1[1C][2C][2C][3C] C1223
  # C1CPCC[4C]1[2C][2C][1C][3C] C2213
  def test_without_isotope(self):
    # The entire system, except the N/P in the ring
    self.assert_search(isotope_mols, numAtoms=9, numBonds=8)

  def test_isotopes(self):
    # 5 atoms of class '0' in the ring
    self.assert_search(isotope_mols, 5, 4,
                       atomCompare=rdFMCS.AtomCompare.CompareIsotopes)

  def test_isotope_complete_ring_only(self):
    # the 122 in the chain
    self.assert_search(isotope_mols, 3, 2,
                       atomCompare=rdFMCS.AtomCompare.CompareIsotopes,
                       completeRingsOnly=True)


bondtype_mols = load_smiles("""
C1CCCCC1OC#CC#CC#CC#CC#CC first
c1ccccc1ONCCCCCCCCCC second
""")


class TestBondTypes(MCSTestCase):
  # C1CCCCC1OC#CC#CC#CC#CC#CC
  # c1ccccc1ONCCCCCCCCCC second
  def test_bond_compare_default(self):
    self.assert_search(bondtype_mols, 7, 7)

  def test_bond_compare_bondtypes(self):
    # Match the 'CCCCCC' part of the first ring, with the second's tail
    self.assert_search(bondtype_mols, 6, 5,
                       bondCompare=rdFMCS.BondCompare.CompareOrderExact)

  def test_bond_compare_any(self):
    # the CC#CC chain matches the CCCC tail
    self.assert_search(bondtype_mols, 10, 9,
                       bondCompare=rdFMCS.BondCompare.CompareAny)

  def test_atom_compare_elements_bond_compare_any(self):
    self.assert_search(bondtype_mols, 10, 9,
                       atomCompare=rdFMCS.AtomCompare.CompareElements,
                       bondCompare=rdFMCS.BondCompare.CompareAny)

  def test_atom_compare_any_bond_compare_any(self):
    # complete match!
    self.assert_search(bondtype_mols, 18, 18,
                       atomCompare=rdFMCS.AtomCompare.CompareAny,
                       bondCompare=rdFMCS.BondCompare.CompareAny)


valence_mols = load_smiles("""
CCCCCCCCN
CCCC(C)CCCC
""")


class TestValences(MCSTestCase):

  def test_valence_compare_default(self):
    # match 'CCCCCCCC'
    self.assert_search(valence_mols, 8, 7)

  def _test_valence_compare_valence(self):
    # FIX: matchValence isn't used?
    # match 'CCCC'
    self.assert_search(valence_mols, 4, 3, matchValences=True)

  def _test_valence_compare_valence(self):
    # FIX: matchValence isn't used?
    # match 'CCCCN' to '[CH-]CCCC' (but in reverse)
    self.assert_search(valence_mols, 5, 4, matchValences=True,
                       atomCompare=rdFMCS.AtomCompare.CompareAny)


ring_mols = load_smiles("""
C12CCCC(N2)CCCC1 6-and-7-bridge-rings-with-N
C1CCCCN1 6-ring
C1CCCCCN1 7-ring
C1CCCCCCCC1 9-ring
NC1CCCCCC1 N+7-ring
C1CC1CCCCCC 3-ring-with-tail
C12CCCC(O2)CCCC1 6-and-7-bridge-rings-with-O
""")


def SELECT(mols, *offsets):
  return [mols[offset - 1] for offset in offsets]


class TestRingMatchesRingOnly(MCSTestCase):
  # C12CCCC(N2)CCCC1 6-and-7-bridge-rings-with-N
  # C1CCCCN1 6-ring
  # C1CCCCCN1 7-ring
  # C1CCCCCCCC1 9-ring
  # NC1CCCCCC1 N+7-ring
  # C1CC1CCCCCC 3-ring-with-tail
  # C12CCCC(O2)CCCC1 6-and-7-bridge-rings-with-O
  def test_default(self):
    # Should match 'CCCCC'
    self.assert_search(ring_mols, 5, 4)

  def test_ring_only(self):
    # Should match "CCC"
    self.assert_search(ring_mols, 3, 2, ringMatchesRingOnly=True)

  def test_ring_only_select_1_2(self):
    # Should match "C1CCCCCN1"
    self.assert_search(SELECT(ring_mols, 1, 2), 6, 6, ringMatchesRingOnly=True)

  def test_ring_only_select_1_3(self):
    # Should match "C1CCCCCCN1"
    self.assert_search(SELECT(ring_mols, 1, 3), 7, 7, ringMatchesRingOnly=True)

  def test_ring_only_select_1_4(self):
    # Should match "C1CCCCCCCC1"
    self.assert_search(SELECT(ring_mols, 1, 4), 9, 9, ringMatchesRingOnly=True)

  def test_select_1_5(self):
    # Should match "NCCCCCC"
    self.assert_search(SELECT(ring_mols, 1, 5), 8, 7, ringMatchesRingOnly=False)

  def test_ring_only_select_1_5(self):
    # Should match "CCCCCC"
    self.assert_search(SELECT(ring_mols, 1, 5), 7, 6, ringMatchesRingOnly=True)

  def test_select_1_6(self):
    # Should match "CCCCCCCCC" by breaking one of the 3-carbon ring bonds
    self.assert_search(SELECT(ring_mols, 1, 6), 9, 8)

  def test_ring_only_select_1_6(self):
    # Should match "CCC" from the three atom ring
    self.assert_search(SELECT(ring_mols, 1, 6), 3, 2, ringMatchesRingOnly=True)

  def test_ring_only_select_1_7(self):
    # Should match the outer ring "C1CCCCCCCC1"
    self.assert_search(SELECT(ring_mols, 1, 7), 9, 9)

  def test_ring_only_select_1_7_any_atoms(self):
    # Should match everything
    self.assert_search(SELECT(ring_mols, 1, 7), 10, 11, ringMatchesRingOnly=True,
                       atomCompare=rdFMCS.AtomCompare.CompareAny)


class TestCompleteRingsOnly(MCSTestCase):
  # C12CCCC(N2)CCCC1 6-and-7-bridge-rings-with-N
  # C1CCCCN1 6-ring
  # C1CCCCCN1 7-ring
  # C1CCCCCCCC1 9-ring
  # NC1CCCCCC1 N+7-ring
  # C1CC1CCCCCC 3-ring-with-tail
  # C12CCCC(O2)CCCC1 6-and-7-bridge-rings-with-O
  def test_ring_only(self):
    # No match: "CCC" is not in a ring
    self.assert_search(ring_mols, 0, 0, completeRingsOnly=True)

  def test_ring_only_select_1_2(self):
    # Should match "C1CCCCCN1"
    self.assert_search(SELECT(ring_mols, 1, 2), 6, 6, completeRingsOnly=True)

  def test_ring_only_select_1_3(self):
    # Should match "C1CCCCCCN1"
    self.assert_search(SELECT(ring_mols, 1, 3), 7, 7, completeRingsOnly=True)

  def _test_ring_only_select_1_4(self):
    # FIX: This does not match and should
    # Should match "C1CCCCCCCC1"
    self.assert_search(SELECT(ring_mols, 1, 4), 9, 9, completeRingsOnly=True)

  def test_ring_only_select_1_5(self):
    # No match: "CCCCCC" is not in a ring
    self.assert_search(SELECT(ring_mols, 1, 5), 0, 0, completeRingsOnly=True)

  def _test_ring_only_select_1_7(self):
    # FIX: This does not match and should
    # Should match the outer ring "C1CCCCCCCC1"
    self.assert_search(SELECT(ring_mols, 1, 7), 9, 9, completeRingsOnly=True)

  def test_ring_only_select_1_7_any_atoms(self):
    # Should match everything
    self.assert_search(SELECT(ring_mols, 1, 7), 10, 11, completeRingsOnly=True,
                       atomCompare=rdFMCS.AtomCompare.CompareAny)


  def test_ring_to_nonring_bond(self):
    # Should allow the cO in phenol to match the CO in the other structure
    self.assert_search(atomtype_mols, 2, 1, completeRingsOnly=True)


lengthy_mols = [Chem.MolFromSmiles("Nc1ccccc1" * 20), Chem.MolFromSmiles("Nc1ccccccccc1" * 20)]


class TestTimeout(MCSTestCase):
  # This should take over two minutes to process. Give it 1 seconds.
  @unittest.skipIf(isDebugBuild(), "Timeout test will fail on debug builds.")
  def test_timeout(self):
    t1 = time.time()
    result = rdFMCS.FindMCS(lengthy_mols, timeout=1)
    self.assert_result(result, canceled=True)
    self.assertTrue(result.numAtoms > 1)
    self.assertTrue(result.numBonds >= result.numAtoms - 1, (result.numAtoms, result.numBonds))
    t2 = time.time()
    self.assertTrue(t2 - t1 < 2, t2 - t1)

  # Check for non-negative values
  def test_timeout_negative(self):
    try:
      rdFMCS.FindMCS(lengthy_mols, timeout=-1)
    except OverflowError:
      pass
    else:
      raise AssertionError("bad range check for timeout")


if __name__ == "__main__":
  unittest.main()