1 /* Copyright (c) 2014 Collaborative Drug Discovery, Inc. <alex@collaborativedrug.com>
2  *
3  * Implemented by Alex M. Clark, produced by Collaborative Drug Discovery, Inc.
4  * Made available to the CDK community under the terms of the GNU LGPL.
5  *
6  *    http://collaborativedrug.com
7  *
8  * Contact: cdk-devel@lists.sourceforge.net
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  * All we ask is that proper credit is given for our work, which includes
15  * - but is not limited to - adding the above copyright notice to the beginning
16  * of your source code files, and to any copyright notice that you may distribute
17  * with programs based on this work.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27  */
28 
29 package org.openscience.cdk.qsar.descriptors.molecular;
30 
31 import java.io.BufferedReader;
32 import java.io.ByteArrayInputStream;
33 import java.io.ByteArrayOutputStream;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.StringWriter;
37 import java.util.HashMap;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipInputStream;
40 
41 import org.openscience.cdk.AtomContainer;
42 import org.openscience.cdk.exception.CDKException;
43 import org.openscience.cdk.io.MDLV2000Reader;
44 import org.openscience.cdk.io.MDLV2000Writer;
45 import org.openscience.cdk.qsar.DescriptorValue;
46 import org.openscience.cdk.qsar.result.IntegerArrayResult;
47 import org.openscience.cdk.tools.ILoggingTool;
48 import org.openscience.cdk.tools.LoggingToolFactory;
49 
50 import org.junit.Before;
51 import org.junit.Test;
52 
53 /**
54  * Test for small rings descriptor.
55  *
56  * @cdk.module test-qsarmolecular
57  */
58 
59 public class SmallRingDescriptorTest extends MolecularDescriptorTest {
60 
61     private static ILoggingTool logger = LoggingToolFactory.createLoggingTool(SmallRingDescriptorTest.class);
62 
SmallRingDescriptorTest()63     public SmallRingDescriptorTest() {}
64 
65     @Before
setUp()66     public void setUp() throws Exception {
67         setDescriptor(SmallRingDescriptor.class);
68     }
69 
70     @Test
testDescriptors()71     public void testDescriptors() throws Exception {
72         logger.info("CircularFingerprinter test: loading source materials");
73 
74         String fnzip = "data/cdd/aromring_validation.zip";
75         logger.info("Loading source content: " + fnzip);
76         InputStream in = this.getClass().getClassLoader().getResourceAsStream(fnzip);
77         validate(in);
78         in.close();
79 
80         logger.info("CircularFingerprinter test: completed without any problems");
81     }
82 
83     // included to shutdown the warning messages for not having tests for trivial methods
84     @Test
nop()85     public void nop() throws Exception {}
86 
87     // run through the cases
validate(InputStream in)88     private void validate(InputStream in) throws Exception {
89         ZipInputStream zip = new ZipInputStream(in);
90 
91         // stream the contents form the zipfile: these are all short
92         HashMap<String, byte[]> content = new HashMap<String, byte[]>();
93         while (true) {
94             ZipEntry ze = zip.getNextEntry();
95             if (ze == null) break;
96             String fn = ze.getName();
97             ByteArrayOutputStream buff = new ByteArrayOutputStream();
98             while (true) {
99                 int b = zip.read();
100                 if (b < 0) break;
101                 buff.write(b);
102             }
103             content.put(fn, buff.toByteArray());
104         }
105 
106         zip.close();
107 
108         for (int idx = 1;; idx++) {
109             String basefn = String.valueOf(idx);
110             while (basefn.length() < 6)
111                 basefn = "0" + basefn;
112             byte[] molBytes = content.get(basefn + ".mol");
113             if (molBytes == null) break;
114 
115             AtomContainer mol = new AtomContainer();
116             MDLV2000Reader mdl = new MDLV2000Reader(new ByteArrayInputStream(molBytes));
117             mdl.read(mol);
118             mdl.close();
119 
120             ByteArrayInputStream rin = new ByteArrayInputStream(content.get(basefn + ".rings"));
121             BufferedReader rdr = new BufferedReader(new InputStreamReader(rin));
122             String[] bits = rdr.readLine().split(" ");
123             rdr.close();
124             int wantSmallRings = Integer.parseInt(bits[0]);
125             int wantRingBlocks = Integer.parseInt(bits[1]);
126             int wantAromRings = Integer.parseInt(bits[2]);
127             int wantAromBlocks = Integer.parseInt(bits[3]);
128 
129             logger.info("FN=" + basefn + " MOL=" + mol.getAtomCount() + "," + mol.getBondCount() + " nSmallRings="
130                     + wantSmallRings + " nRingBlocks=" + wantRingBlocks + " nAromRings=" + wantAromRings
131                     + " nAromBlocks=" + wantAromBlocks);
132 
133             SmallRingDescriptor descr = new SmallRingDescriptor();
134             DescriptorValue results = descr.calculate(mol);
135             String[] names = results.getNames();
136             IntegerArrayResult values = (IntegerArrayResult) results.getValue();
137 
138             int gotSmallRings = 0, gotRingBlocks = 0, gotAromRings = 0, gotAromBlocks = 0;
139             for (int n = 0; n < names.length; n++) {
140                 if (names[n].equals("nSmallRings"))
141                     gotSmallRings = values.get(n);
142                 else if (names[n].equals("nRingBlocks"))
143                     gotRingBlocks = values.get(n);
144                 else if (names[n].equals("nAromRings"))
145                     gotAromRings = values.get(n);
146                 else if (names[n].equals("nAromBlocks")) gotAromBlocks = values.get(n);
147             }
148 
149             String error = null;
150             if (gotSmallRings != wantSmallRings)
151                 error = "Got " + gotSmallRings + " small rings, expected " + wantSmallRings;
152             else if (gotRingBlocks != wantRingBlocks)
153                 error = "Got " + gotRingBlocks + " ring blocks, expected " + wantRingBlocks;
154             else if (gotAromRings != wantAromRings)
155                 error = "Got " + gotAromRings + " aromatic rings, expected " + wantAromRings;
156             else if (gotAromBlocks != wantAromBlocks)
157                 error = "Got " + gotAromBlocks + " aromatic blocks, expected " + wantAromBlocks;
158 
159             if (error != null) {
160                 StringWriter str = new StringWriter();
161                 MDLV2000Writer wtr = new MDLV2000Writer(str);
162                 wtr.write(mol);
163                 wtr.close();
164                 error += "\nMolecule:\n" + str.toString();
165                 throw new CDKException(error);
166             }
167         }
168     }
169 
170 }
171