/
FingerprinterTest.java
351 lines (303 loc) · 12 KB
/
FingerprinterTest.java
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
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 1997-2007 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All I ask is that proper credit is given for my work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.fingerprint;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.BitSet;
import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.Reaction;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.graph.AtomContainerAtomPermutor;
import org.openscience.cdk.graph.AtomContainerBondPermutor;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.io.IChemObjectReader.Mode;
import org.openscience.cdk.io.MDLRXNV2000Reader;
import org.openscience.cdk.templates.MoleculeFactory;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
/**
* @cdk.module test-standard
*/
public class FingerprinterTest extends AbstractFingerprinterTest {
boolean standAlone = false;
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(FingerprinterTest.class);
public IFingerprinter getFingerprinter() {
return new Fingerprinter();
}
@Test public void testRegression() throws Exception {
IMolecule mol1 = MoleculeFactory.makeIndole();
IMolecule mol2 = MoleculeFactory.makePyrrole();
Fingerprinter fingerprinter = new Fingerprinter();
BitSet bs1 = fingerprinter.getFingerprint(mol1);
Assert.assertEquals("Seems the fingerprint code has changed. This will cause a number of other tests to fail too!", 33, bs1.cardinality());
BitSet bs2 = fingerprinter.getFingerprint(mol2);
Assert.assertEquals("Seems the fingerprint code has changed. This will cause a number of other tests to fail too!", 13, bs2.cardinality());
}
@Test public void testGetSize() throws java.lang.Exception {
IFingerprinter fingerprinter = new Fingerprinter(512);
Assert.assertNotNull(fingerprinter);
Assert.assertEquals(512, fingerprinter.getSize());
}
@Test public void testGetSearchDepth() throws java.lang.Exception {
Fingerprinter fingerprinter = new Fingerprinter(512,3);
Assert.assertNotNull(fingerprinter);
Assert.assertEquals(3, fingerprinter.getSearchDepth());
}
@Test public void testGetFingerprint_IAtomContainer() throws java.lang.Exception
{
Fingerprinter fingerprinter = new Fingerprinter();
Molecule mol = MoleculeFactory.makeIndole();
BitSet bs = fingerprinter.getFingerprint(mol);
Assert.assertNotNull(bs);
Assert.assertEquals(fingerprinter.getSize(), bs.size());
}
@Test public void testFingerprinter() throws java.lang.Exception
{
Fingerprinter fingerprinter = new Fingerprinter();
Assert.assertNotNull(fingerprinter);
Molecule mol = MoleculeFactory.makeIndole();
BitSet bs = fingerprinter.getFingerprint(mol);
Molecule frag1 = MoleculeFactory.makePyrrole();
BitSet bs1 = fingerprinter.getFingerprint(frag1);
Assert.assertTrue(FingerprinterTool.isSubset(bs, bs1));
}
@Test public void testFingerprinter_int() throws java.lang.Exception
{
Fingerprinter fingerprinter = new Fingerprinter(512);
Assert.assertNotNull(fingerprinter);
Molecule mol = MoleculeFactory.makeIndole();
BitSet bs = fingerprinter.getFingerprint(mol);
Molecule frag1 = MoleculeFactory.makePyrrole();
BitSet bs1 = fingerprinter.getFingerprint(frag1);
Assert.assertTrue(FingerprinterTool.isSubset(bs, bs1));
}
@Test public void testFingerprinter_int_int() throws java.lang.Exception
{
Fingerprinter fingerprinter = new Fingerprinter(1024,7);
Assert.assertNotNull(fingerprinter);
Molecule mol = MoleculeFactory.makeIndole();
BitSet bs = fingerprinter.getFingerprint(mol);
Molecule frag1 = MoleculeFactory.makePyrrole();
BitSet bs1 = fingerprinter.getFingerprint(frag1);
Assert.assertTrue(FingerprinterTool.isSubset(bs, bs1));
}
@Test public void testFingerprinterBitSetSize() throws Exception {
Fingerprinter fingerprinter = new Fingerprinter(1024,7);
Assert.assertNotNull(fingerprinter);
Molecule mol = MoleculeFactory.makeIndole();
BitSet bs = fingerprinter.getFingerprint(mol);
Assert.assertEquals(994, bs.length()); // highest set bit
Assert.assertEquals(1024, bs.size()); // actual bit set size
}
/**
* @cdk.bug 1851202
*/
@Test
public void testBug1851202() throws Exception {
String filename1 = "data/mdl/0002.stg01.rxn";
logger.info("Testing: " + filename1);
InputStream ins1 = this.getClass().getClassLoader().getResourceAsStream(filename1);
MDLRXNV2000Reader reader = new MDLRXNV2000Reader(ins1, Mode.STRICT);
IReaction reaction = (IReaction)reader.read(new Reaction());
Assert.assertNotNull(reaction);
IAtomContainer reactant = reaction.getReactants().getAtomContainer(0);
IAtomContainer product = reaction.getProducts().getAtomContainer(0);
Fingerprinter fingerprinter = new Fingerprinter(64*26,8);
BitSet bs1 = fingerprinter.getFingerprint(reactant);
Assert.assertNotNull(bs1);
BitSet bs2 = fingerprinter.getFingerprint(product);
Assert.assertNotNull(bs2);
}
/**
* @cdk.bug 2819557
* @throws org.openscience.cdk.exception.CDKException
*/
@Test
public void testBug2819557() throws CDKException {
Molecule butane = makeButane();
Molecule propylAmine = makePropylAmine();
Fingerprinter fp = new Fingerprinter();
BitSet b1 = fp.getFingerprint(butane);
BitSet b2 = fp.getFingerprint(propylAmine);
Assert.assertFalse("butane should not be a substructure of propylamine", FingerprinterTool.isSubset(b2, b1));
}
@Test
public void testBondPermutation() throws CDKException {
IMolecule pamine = makePropylAmine();
Fingerprinter fp = new Fingerprinter();
BitSet bs1 = fp.getFingerprint(pamine);
AtomContainerBondPermutor acp = new AtomContainerBondPermutor(pamine);
while (acp.hasNext()) {
IAtomContainer container = acp.next();
BitSet bs2 = fp.getFingerprint(container);
Assert.assertTrue(bs1.equals(bs2));
}
}
@Test
public void testAtomPermutation() throws CDKException {
IMolecule pamine = makePropylAmine();
Fingerprinter fp = new Fingerprinter();
BitSet bs1 = fp.getFingerprint(pamine);
AtomContainerAtomPermutor acp = new AtomContainerAtomPermutor(pamine);
while (acp.hasNext()) {
IAtomContainer container = acp.next();
BitSet bs2 = fp.getFingerprint(container);
Assert.assertTrue(bs1.equals(bs2));
}
}
@Test
public void testBondPermutation2() throws CDKException {
IMolecule pamine = MoleculeFactory.makeCyclopentane();
Fingerprinter fp = new Fingerprinter();
BitSet bs1 = fp.getFingerprint(pamine);
AtomContainerBondPermutor acp = new AtomContainerBondPermutor(pamine);
while (acp.hasNext()) {
IAtomContainer container = acp.next();
BitSet bs2 = fp.getFingerprint(container);
Assert.assertTrue(bs1.equals(bs2));
}
}
@Test
public void testAtomPermutation2() throws CDKException {
IMolecule pamine = MoleculeFactory.makeCyclopentane();
Fingerprinter fp = new Fingerprinter();
BitSet bs1 = fp.getFingerprint(pamine);
AtomContainerAtomPermutor acp = new AtomContainerAtomPermutor(pamine);
while (acp.hasNext()) {
IAtomContainer container = acp.next();
BitSet bs2 = fp.getFingerprint(container);
Assert.assertTrue(bs1.equals(bs2));
}
}
public static Molecule makeFragment1()
{
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addAtom(new Atom("C")); // 2
mol.addAtom(new Atom("C")); // 3
mol.addAtom(new Atom("C")); // 4
mol.addAtom(new Atom("C")); // 5
mol.addAtom(new Atom("C")); // 6
mol.addBond(0, 1, IBond.Order.SINGLE); // 1
mol.addBond(0, 2, IBond.Order.SINGLE); // 2
mol.addBond(0, 3, IBond.Order.SINGLE); // 3
mol.addBond(0, 4, IBond.Order.SINGLE); // 4
mol.addBond(3, 5, IBond.Order.SINGLE); // 5
mol.addBond(5, 6, IBond.Order.DOUBLE); // 6
return mol;
}
public static Molecule makeFragment4()
{
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addBond(0, 1, IBond.Order.SINGLE); // 1
return mol;
}
public static Molecule makeFragment2()
{
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addAtom(new Atom("C")); // 2
mol.addAtom(new Atom("S")); // 3
mol.addAtom(new Atom("O")); // 4
mol.addAtom(new Atom("C")); // 5
mol.addAtom(new Atom("C")); // 6
mol.addBond(0, 1, IBond.Order.DOUBLE); // 1
mol.addBond(0, 2, IBond.Order.SINGLE); // 2
mol.addBond(0, 3, IBond.Order.SINGLE); // 3
mol.addBond(0, 4, IBond.Order.SINGLE); // 4
mol.addBond(3, 5, IBond.Order.SINGLE); // 5
mol.addBond(5, 6, IBond.Order.DOUBLE); // 6
mol.addBond(5, 6, IBond.Order.DOUBLE); // 7
return mol;
}
public static Molecule makeFragment3()
{
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addAtom(new Atom("C")); // 2
mol.addAtom(new Atom("C")); // 3
mol.addAtom(new Atom("C")); // 4
mol.addAtom(new Atom("C")); // 5
mol.addAtom(new Atom("C")); // 6
mol.addBond(0, 1, IBond.Order.SINGLE); // 1
mol.addBond(0, 2, IBond.Order.SINGLE); // 2
mol.addBond(0, 3, IBond.Order.SINGLE); // 3
mol.addBond(0, 4, IBond.Order.SINGLE); // 4
mol.addBond(3, 5, IBond.Order.DOUBLE); // 5
mol.addBond(5, 6, IBond.Order.SINGLE); // 6
return mol;
}
public static Molecule makeButane() {
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addAtom(new Atom("C")); // 2
mol.addAtom(new Atom("C")); // 3
mol.addBond(0, 1, IBond.Order.SINGLE); // 1
mol.addBond(1, 2, IBond.Order.SINGLE); // 2
mol.addBond(2, 3, IBond.Order.SINGLE); // 3
return mol;
}
public static Molecule makePropylAmine() {
Molecule mol = new Molecule();
mol.addAtom(new Atom("C")); // 0
mol.addAtom(new Atom("C")); // 1
mol.addAtom(new Atom("C")); // 2
mol.addAtom(new Atom("N")); // 3
mol.addBond(0, 1, IBond.Order.SINGLE); // 1
mol.addBond(1, 2, IBond.Order.SINGLE); // 2
mol.addBond(2, 3, IBond.Order.SINGLE); // 3
return mol;
}
public static void main(String[] args) throws Exception
{
BigInteger bi=new BigInteger("0");
bi=bi.add(BigInteger.valueOf((long) Math.pow(2, 63)));
System.err.println(bi.toString());
bi=bi.add(BigInteger.valueOf((long) Math.pow(2, 0)));
System.err.println(bi.toString());
FingerprinterTest fpt = new FingerprinterTest();
fpt.standAlone = true;
//fpt.testFingerprinter();
//fpt.testFingerprinterArguments();
//fpt.testBug706786();
//fpt.testBug771485();
//fpt.testBug853254();
//fpt.testBug931608();
fpt.testBug934819();
}
}