File: SubstructLibraryTests.java

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (390 lines) | stat: -rw-r--r-- 14,417 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* 
 *
 *  Copyright (c) 2018, Novartis Institutes for BioMedical Research Inc.
 *  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: 
 *
 *     * Redistributions of source code must retain the above copyright 
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following 
 *       disclaimer in the documentation and/or other materials provided 
 *       with the distribution.
 *     * Neither the name of Novartis Institutes for BioMedical Research Inc. 
 *       nor the names of its contributors may be used to endorse or promote 
 *       products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.RDKit;

import static org.junit.Assert.*;
import org.junit.*;
    
public class SubstructLibraryTests extends GraphMolTest {
	private ROMol mol;
        private ROMol m;

	@Before public void setUp() {
	}

	@Test 
	public void test1Basics() {
            SubstructLibrary lib = new SubstructLibrary();
            assertEquals(0, lib.size());
            // mol holders
            MolHolder mh = new MolHolder();
            assertEquals(0, mh.size());
            CachedMolHolder cmh = new CachedMolHolder();
            assertEquals(0, cmh.size());
            CachedSmilesMolHolder csmh = new CachedSmilesMolHolder();
            assertEquals(0, csmh.size());
            CachedTrustedSmilesMolHolder ctsmh = new CachedTrustedSmilesMolHolder();
            assertEquals(0, ctsmh.size());
	    KeyFromPropHolder keys = new KeyFromPropHolder();
	    
            // fpholder - can we create it...
            PatternHolder ph = new PatternHolder();
	    TautomerPatternHolder tph = new TautomerPatternHolder();

            // Now lets make some molecules
            mol = RWMol.MolFromSmiles("c1ccccc1");
            String smiles = mol.MolToSmiles();
            mol.setProp("_Name", "foo");
	    
            mh.addMol(mol);
            assertEquals(1, mh.size());
            cmh.addMol(mol);
            assertEquals(1, cmh.size());
            csmh.addMol(mol);
            assertEquals(1, csmh.size());
            ctsmh.addMol(mol);
            assertEquals(1, ctsmh.size());
            keys.addMol(mol);
	    assertEquals(1, keys.size());
	    
            m = mh.getMol(0);
            assertEquals(smiles, m.MolToSmiles());
            m = cmh.getMol(0);
            assertEquals(smiles, m.MolToSmiles());
            m = csmh.getMol(0);
            assertEquals(smiles, m.MolToSmiles());
            m = ctsmh.getMol(0);
            assertEquals(smiles, m.MolToSmiles());
	    assertEquals("foo", keys.getKey(0));
	    
            mol = RWMol.MolFromSmiles("CCN");
	    mol.setProp("_Name", "bar");
            smiles = mol.MolToSmiles();

            mh.addMol(mol);
            assertEquals(2, mh.size());
            cmh.addMol(mol);
            assertEquals(2, mh.size());
	    keys.addMol(mol);
	    assertEquals(2, keys.size());
	    
            csmh.addSmiles("CCN");
            assertEquals(2, csmh.size());
            ctsmh.addSmiles("CCN");
            assertEquals(2, ctsmh.size());

            m = mh.getMol(1);
            assertEquals(smiles, m.MolToSmiles());
            m = cmh.getMol(1);
            assertEquals(smiles, m.MolToSmiles());
            m = csmh.getMol(1);
            assertEquals(smiles, m.MolToSmiles());
            m = ctsmh.getMol(1);
            assertEquals(smiles, m.MolToSmiles());
	    assertEquals("bar", keys.getKey(1));
	}


  	@Test 
	public void test2Basics() {
            MolHolder mh = new MolHolder();
            assertEquals(0, mh.size());
            CachedMolHolder cmh = new CachedMolHolder();
            assertEquals(0, cmh.size());
            CachedSmilesMolHolder csmh = new CachedSmilesMolHolder();
            assertEquals(0, csmh.size());
            CachedTrustedSmilesMolHolder ctsmh = new CachedTrustedSmilesMolHolder();
            assertEquals(0, ctsmh.size());
            mol = RWMol.MolFromSmiles("c1ccccc1");
            // mol holder
            SubstructLibrary lib = new SubstructLibrary(mh);
            SubstructLibrary lib2;
            lib.addMol(mol);
            
            UInt_Vect matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

            if(lib.canSerialize()) {
                byte pickle[] = lib.Serialize();
                assertTrue(pickle != null);
                lib2 = SubstructLibrary.Deserialize(pickle);
                assertFalse(lib2 == null);
                assertEquals(lib.size(), 1);
                matches = lib.getMatches(mol);
                assertEquals(1, matches.size());
            }
            
            // cached mol holder
            lib = new SubstructLibrary(cmh);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
	    assertEquals(1, lib.countMatches(mol));
	    assertTrue(lib.hasMatch(mol));

	    
            if(lib.canSerialize()) {
                byte pickle[] = lib.Serialize();
                assertTrue(pickle != null);
                lib2 = SubstructLibrary.Deserialize(pickle);
                assertFalse(lib2 == null);
                assertEquals(lib.size(), 1);
                matches = lib.getMatches(mol);
                assertEquals(1, matches.size());
		assertEquals(1, lib.countMatches(mol));
		assertTrue(lib.hasMatch(mol));
            }

            // cached smiles mol holder
            lib = new SubstructLibrary(csmh);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

            if(lib.canSerialize()) {
                byte pickle[] = lib.Serialize();
                assertTrue(pickle != null);
                lib2 = SubstructLibrary.Deserialize(pickle);
                assertFalse(lib2 == null);
                assertEquals(lib.size(), 1);
                matches = lib.getMatches(mol);
                assertEquals(1, matches.size());
            }
            
            // cached trusted smiles mol holder
            lib = new SubstructLibrary(ctsmh);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

            if(lib.canSerialize()) {
                byte pickle[] = lib.Serialize();
                assertTrue(pickle != null);
                lib2 = SubstructLibrary.Deserialize(pickle);
                assertFalse(lib2 == null);
                assertEquals(lib.size(), 1);
                matches = lib.getMatches(mol);
                assertEquals(1, matches.size());
            }
            
        }

  	@Test 
	public void test3Basics() {
            MolHolder mh = new MolHolder();
            PatternHolder pat = new PatternHolder();
            assertEquals(0, mh.size());
            CachedMolHolder cmh = new CachedMolHolder();
            assertEquals(0, cmh.size());
            CachedSmilesMolHolder csmh = new CachedSmilesMolHolder();
            assertEquals(0, csmh.size());
            CachedTrustedSmilesMolHolder ctsmh = new CachedTrustedSmilesMolHolder();
            assertEquals(0, ctsmh.size());
            mol = RWMol.MolFromSmiles("c1ccccc1");
            // mol holder
            SubstructLibrary lib = new SubstructLibrary(mh, pat);
            lib.addMol(mol);
            
            UInt_Vect matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

            // cached mol holder
            try {
              lib = new SubstructLibrary(cmh, pat);
              lib.addMol(mol);
              assertTrue(false); // shouldn't get here can't share patternholder and addmol
            } catch(Exception e) {
              // ok to get here
            }
            cmh = new CachedMolHolder();
            pat = new PatternHolder();
            lib = new SubstructLibrary(cmh, pat);
            lib.addMol(mol);
            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

            // cached smiles mol holder
            pat = new PatternHolder();
            lib = new SubstructLibrary(csmh, pat);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
            
            // cached trusted smiles mol holder
            pat = new PatternHolder();
            lib = new SubstructLibrary(ctsmh, pat);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
        }

  	@Test 
	public void test4Basics() {
            MolHolder mh = new MolHolder();
            TautomerPatternHolder pat = new TautomerPatternHolder();
            assertEquals(0, mh.size());
            CachedMolHolder cmh = new CachedMolHolder();
            assertEquals(0, cmh.size());
            CachedSmilesMolHolder csmh = new CachedSmilesMolHolder();
            assertEquals(0, csmh.size());
            CachedTrustedSmilesMolHolder ctsmh = new CachedTrustedSmilesMolHolder();
            assertEquals(0, ctsmh.size());
            mol = RWMol.MolFromSmiles("c1ccccc1");
	    TautomerQuery tautomerQuery = TautomerQuery.fromMol(mol);
	    
            // mol holder
            SubstructLibrary lib = new SubstructLibrary(mh, pat);
            lib.addMol(mol);
            
            UInt_Vect matches = lib.getMatches(mol);
            assertEquals(1, matches.size());

	    matches = lib.getMatches(tautomerQuery);
            assertEquals(1, matches.size());
	    assertEquals(1, lib.countMatches(mol));
	    assertEquals(1, lib.countMatches(tautomerQuery));
	    
            // cached mol holder
            try {
              lib = new SubstructLibrary(cmh, pat);
              lib.addMol(mol);
              assertTrue(false); // shouldn't get here can't share patternholder and addmol
            } catch(Exception e) {
              // ok to get here
            }
            cmh = new CachedMolHolder();
            pat = new TautomerPatternHolder();
            lib = new SubstructLibrary(cmh, pat);
            lib.addMol(mol);
            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
	    matches = lib.getMatches(tautomerQuery);
            assertEquals(1, matches.size());
	    assertEquals(1, lib.countMatches(mol));
	    assertEquals(1, lib.countMatches(tautomerQuery));
	    
            // cached smiles mol holder
            pat = new TautomerPatternHolder();
            lib = new SubstructLibrary(csmh, pat);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
	    matches = lib.getMatches(tautomerQuery);
            assertEquals(1, matches.size());
	    assertEquals(1, lib.countMatches(mol));
	    assertEquals(1, lib.countMatches(tautomerQuery));
	    
            // cached trusted smiles mol holder
            pat = new TautomerPatternHolder();
            lib = new SubstructLibrary(ctsmh, pat);
            lib.addMol(mol);

            matches = lib.getMatches(mol);
            assertEquals(1, matches.size());
	    matches = lib.getMatches(tautomerQuery);
            assertEquals(1, matches.size());
	    assertEquals(1, lib.countMatches(mol));
	    assertEquals(1, lib.countMatches(tautomerQuery));
        }
    

  	@Test 
	public void test5Basics() {
            MolHolder mh = new MolHolder();
            MolHolder mh2 = new MolHolder();
            PatternHolder pat = new PatternHolder();
            assertEquals(0, mh.size());
            CachedMolHolder cmh = new CachedMolHolder();
            assertEquals(0, cmh.size());
            mol = RWMol.MolFromSmiles("c1ccccc1");
	    mol.setProp("_Name", "foo");
	    KeyFromPropHolder keys = new KeyFromPropHolder();
	    KeyFromPropHolder keys2 = new KeyFromPropHolder();

            // mol holder
            SubstructLibrary lib = new SubstructLibrary(mh, pat, keys);
            lib.addMol(mol);
            
            SubstructLibrary lib2 = new SubstructLibrary(mh2, keys2);
	    lib2.addMol(mol);

            UInt_Vect matches = lib.getMatches(mol);
            UInt_Vect matches2 = lib2.getMatches(mol);
            assertEquals(1, matches.size());
	    assertEquals(matches.get(0), matches.get(0));

	    Str_Vect ids = lib.getKeys().getKeys(matches);
	    Str_Vect ids2 = lib2.getKeys().getKeys(matches2);
	    assertEquals(ids.get(0), "foo");
	    assertEquals(ids2.get(0), "foo");
        }



  	@Test 
	public void test6XQM() {
            MolHolder mh = new MolHolder();
            PatternHolder pat = new TautomerPatternHolder();
            assertEquals(0, mh.size());
            // mol holder
            SubstructLibrary lib = new SubstructLibrary(mh, pat);

            mol = RWMol.MolFromSmiles("COCC=O");
            lib.addMol(mol);
            mol = RWMol.MolFromSmiles("COOCC=O");
            lib.addMol(mol);
            mol = RWMol.MolFromSmiles("COOOCC=O");
            lib.addMol(mol);
            mol = RWMol.MolFromSmiles("COOOOCC=O");
            lib.addMol(mol);


            RWMol rwm = RWMol.MolFromSmiles("COC=CO |LN:1:1.3|");
            ExtendedQueryMol qry = RDKFuncs.createExtendedQueryMol(rwm);

            UInt_Vect matches = lib.getMatches(qry);
            assertEquals(3, matches.size());
        }

  
	public static void main(String args[]) {
		org.junit.runner.JUnitCore.main("org.RDKit.SubstructLibraryTests");
	}

}