1 /* Copyright (C) 2003-2018 The Chemistry Development Kit (CDK) project 2 * 3 * Contact: cdk-devel@lists.sourceforge.net 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public License 7 * as published by the Free Software Foundation; either version 2.1 8 * of the License, or (at your option) any later version. 9 * All we ask is that proper credit is given for our work, which includes 10 * - but is not limited to - adding the above copyright notice to the beginning 11 * of your source code files, and to any copyright notice that you may distribute 12 * with programs based on this work. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * */ 23 package org.openscience.cdk.io; 24 25 import java.io.BufferedInputStream; 26 import java.io.InputStream; 27 import java.util.zip.GZIPInputStream; 28 29 import org.junit.Assert; 30 import org.junit.Ignore; 31 import org.junit.Test; 32 import org.openscience.cdk.AtomContainer; 33 import org.openscience.cdk.ChemFile; 34 import org.openscience.cdk.interfaces.IAtomContainer; 35 import org.openscience.cdk.interfaces.IChemFile; 36 import org.openscience.cdk.io.formats.CMLFormat; 37 import org.openscience.cdk.io.formats.CTXFormat; 38 import org.openscience.cdk.io.formats.Gaussian98Format; 39 import org.openscience.cdk.io.formats.GhemicalSPMFormat; 40 import org.openscience.cdk.io.formats.IChemFormat; 41 import org.openscience.cdk.io.formats.IChemFormatMatcher; 42 import org.openscience.cdk.io.formats.MDLFormat; 43 import org.openscience.cdk.io.formats.MDLRXNV2000Format; 44 import org.openscience.cdk.io.formats.MDLRXNV3000Format; 45 import org.openscience.cdk.io.formats.MDLV2000Format; 46 import org.openscience.cdk.io.formats.MDLV3000Format; 47 import org.openscience.cdk.io.formats.Mol2Format; 48 import org.openscience.cdk.io.formats.PDBFormat; 49 import org.openscience.cdk.io.formats.PubChemASNFormat; 50 import org.openscience.cdk.io.formats.PubChemCompoundXMLFormat; 51 import org.openscience.cdk.io.formats.PubChemSubstanceXMLFormat; 52 import org.openscience.cdk.io.formats.ShelXFormat; 53 import org.openscience.cdk.io.formats.XYZFormat; 54 import org.openscience.cdk.tools.manipulator.ChemFileManipulator; 55 56 /** 57 * TestCase for the instantiation and functionality of the {@link ReaderFactory}. 58 * 59 * @cdk.module test-io 60 */ 61 public class ReaderFactoryTest extends AbstractReaderFactoryTest { 62 63 private ReaderFactory factory = new ReaderFactory(); 64 65 @Test testCreateReader_IChemFormat()66 public void testCreateReader_IChemFormat() { 67 IChemFormat format = (IChemFormat) XYZFormat.getInstance(); 68 ISimpleChemObjectReader reader = factory.createReader(format); 69 Assert.assertNotNull(reader); 70 Assert.assertEquals(format.getFormatName(), reader.getFormat().getFormatName()); 71 } 72 73 @Test testGaussian98()74 public void testGaussian98() throws Exception { 75 expectReader("data/gaussian/g98.out", Gaussian98Format.getInstance(), -1, -1); 76 } 77 78 @Test testGhemical()79 public void testGhemical() throws Exception { 80 expectReader("data/ghemical/ethene.mm1gp", GhemicalSPMFormat.getInstance(), 6, 5); 81 } 82 83 @Test testCML()84 public void testCML() throws Exception { 85 expectReader("data/cml/estron.cml", CMLFormat.getInstance(), -1, -1); 86 } 87 88 @Test testXYZ()89 public void testXYZ() throws Exception { 90 expectReader("data/xyz/bf3.xyz", XYZFormat.getInstance(), -1, -1); 91 } 92 93 @Test testShelX()94 public void testShelX() throws Exception { 95 expectReader("data/shelx/frame_1.res", ShelXFormat.getInstance(), -1, -1); 96 } 97 98 @Test testMDLMol()99 public void testMDLMol() throws Exception { 100 expectReader("data/mdl/bug1014344-1.mol", MDLFormat.getInstance(), 21, 21); 101 } 102 103 @Test testMDLMolV2000()104 public void testMDLMolV2000() throws Exception { 105 expectReader("data/mdl/methylbenzol.mol", MDLV2000Format.getInstance(), 15, 15); 106 } 107 108 @Test testDetection()109 public void testDetection() throws Exception { 110 expectReader("data/mdl/withcharges.mol", MDLV2000Format.getInstance(), 9, 9); 111 } 112 113 @Test testMDLMolV3000()114 public void testMDLMolV3000() throws Exception { 115 expectReader("data/mdl/molV3000.mol", MDLV3000Format.getInstance(), -1, -1); 116 } 117 118 @Test testMDLRxnV2000()119 public void testMDLRxnV2000() throws Exception { 120 expectReader("data/mdl/reaction-1.rxn", MDLRXNV2000Format.getInstance(), -1, -1); 121 } 122 123 @Test testMDLRxnV3000()124 public void testMDLRxnV3000() throws Exception { 125 expectReader("data/mdl/reaction_v3.rxn", MDLRXNV3000Format.getInstance(), -1, -1); 126 } 127 128 @Ignore("test moved to cdk-test-pdb/PDBReaderFactoryTest") testPDB()129 public void testPDB() throws Exception { 130 expectReader("data/pdb/coffeine.pdb", PDBFormat.getInstance(), -1, -1); 131 } 132 133 @Test testMol2()134 public void testMol2() throws Exception { 135 expectReader("data/mol2/fromWebsite.mol2", Mol2Format.getInstance(), -1, -1); 136 } 137 138 @Test testCTX()139 public void testCTX() throws Exception { 140 expectReader("data/ctx/methanol_with_descriptors.ctx", CTXFormat.getInstance(), -1, -1); 141 } 142 143 @Test testPubChemCompoundASN()144 public void testPubChemCompoundASN() throws Exception { 145 expectReader("data/asn/pubchem/cid1.asn", PubChemASNFormat.getInstance(), -1, -1); 146 } 147 148 @Test testPubChemSubstanceXML()149 public void testPubChemSubstanceXML() throws Exception { 150 expectReader("data/asn/pubchem/sid577309.xml", PubChemSubstanceXMLFormat.getInstance(), -1, -1); 151 } 152 153 @Test testPubChemCompoundXML()154 public void testPubChemCompoundXML() throws Exception { 155 expectReader("data/asn/pubchem/cid1145.xml", PubChemCompoundXMLFormat.getInstance(), -1, -1); 156 } 157 158 @Test testSmiles()159 public void testSmiles() throws Exception { 160 InputStream is = this.getClass().getClassLoader().getResourceAsStream("data/smiles/drugs.smi"); 161 Object reader = factory.createReader(is); 162 Assert.assertNull(reader); 163 } 164 165 /** 166 * @cdk.bug 2153298 167 */ 168 @Test testBug2153298()169 public void testBug2153298() throws Exception { 170 String filename = "data/asn/pubchem/cid1145.xml"; 171 InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); 172 Assert.assertNotNull("Cannot find file: " + filename, ins); 173 IChemFormatMatcher realFormat = (IChemFormatMatcher) PubChemCompoundXMLFormat.getInstance(); 174 factory.registerFormat(realFormat); 175 // ok, if format ok, try instantiating a reader 176 ins = this.getClass().getClassLoader().getResourceAsStream(filename); 177 ISimpleChemObjectReader reader = factory.createReader(ins); 178 Assert.assertNotNull(reader); 179 Assert.assertEquals(((IChemFormat) PubChemCompoundXMLFormat.getInstance()).getReaderClassName(), reader 180 .getClass().getName()); 181 // now try reading something from it 182 IAtomContainer molecule = (IAtomContainer) reader.read(new AtomContainer()); 183 Assert.assertNotNull(molecule); 184 Assert.assertNotSame(0, molecule.getAtomCount()); 185 Assert.assertNotSame(0, molecule.getBondCount()); 186 } 187 188 @Test testReadGz()189 public void testReadGz() throws Exception { 190 String filename = "data/xyz/bf3.xyz.gz"; 191 InputStream input = new BufferedInputStream(new GZIPInputStream(this.getClass().getClassLoader() 192 .getResourceAsStream(filename))); 193 // ok, if format ok, try instantiating a reader 194 ISimpleChemObjectReader reader = factory.createReader(input); 195 Assert.assertNotNull(reader); 196 Assert.assertEquals(((IChemFormat) XYZFormat.getInstance()).getReaderClassName(), reader.getClass().getName()); 197 // now try reading something from it 198 IChemFile chemFile = (IChemFile) reader.read(new ChemFile()); 199 IAtomContainer molecule = new AtomContainer(); 200 for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(chemFile)) { 201 molecule.add(container); 202 } 203 Assert.assertNotNull(molecule); 204 Assert.assertEquals(4, molecule.getAtomCount()); 205 } 206 207 @Test testReadGzWithGzipDetection()208 public void testReadGzWithGzipDetection() throws Exception { 209 String filename = "data/xyz/bf3.xyz.gz"; 210 InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename); 211 // ok, if format ok, try instantiating a reader 212 ISimpleChemObjectReader reader = factory.createReader(input); 213 Assert.assertNotNull(reader); 214 Assert.assertEquals(((IChemFormat) XYZFormat.getInstance()).getReaderClassName(), reader.getClass().getName()); 215 // now try reading something from it 216 IChemFile chemFile = (IChemFile) reader.read(new ChemFile()); 217 IAtomContainer molecule = new AtomContainer(); 218 for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(chemFile)) { 219 molecule.add(container); 220 } 221 Assert.assertNotNull(molecule); 222 Assert.assertEquals(4, molecule.getAtomCount()); 223 } 224 225 } 226