📄 cmlroundtriptest.java
字号:
/* $Revision: 8301 $ $Author: egonw $ $Date: 2007-05-05 12:00:36 +0000 (Sa, 05 Mai 2007) $ * * Copyright (C) 2003-2007 The Chemistry Development Kit (CDK) project * * 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 we ask is that proper credit is given for our 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.test.io.cml;import java.io.ByteArrayInputStream;import java.util.Iterator;import javax.vecmath.Point2d;import javax.vecmath.Point3d;import junit.framework.Test;import junit.framework.TestSuite;import nu.xom.Element;import org.openscience.cdk.Atom;import org.openscience.cdk.Bond;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.ChemModel;import org.openscience.cdk.Molecule;import org.openscience.cdk.MoleculeSet;import org.openscience.cdk.PseudoAtom;import org.openscience.cdk.Reaction;import org.openscience.cdk.SingleElectron;import org.openscience.cdk.interfaces.IAtom;import org.openscience.cdk.interfaces.IBond;import org.openscience.cdk.interfaces.IChemFile;import org.openscience.cdk.interfaces.IChemModel;import org.openscience.cdk.interfaces.IChemSequence;import org.openscience.cdk.interfaces.IMolecule;import org.openscience.cdk.interfaces.IMoleculeSet;import org.openscience.cdk.interfaces.IReaction;import org.openscience.cdk.interfaces.IReactionSet;import org.openscience.cdk.io.CMLReader;import org.openscience.cdk.libio.cml.Convertor;import org.openscience.cdk.libio.cml.QSARCustomizer;import org.openscience.cdk.qsar.DescriptorSpecification;import org.openscience.cdk.qsar.DescriptorValue;import org.openscience.cdk.qsar.IMolecularDescriptor;import org.openscience.cdk.qsar.descriptors.molecular.WeightDescriptor;import org.openscience.cdk.templates.MoleculeFactory;import org.openscience.cdk.test.CDKTestCase;import org.openscience.cdk.tools.LoggingTool;/** * TestCase for the reading CML 2 files using a few test files * in data/cmltest. * * @cdk.module test-io * @cdk.require xom-1.0.jar * @cdk.require java1.5+ */public class CMLRoundTripTest extends CDKTestCase { private LoggingTool logger; private Convertor convertor; public CMLRoundTripTest(String name) { super(name); logger = new LoggingTool(this); convertor = new Convertor(false, ""); convertor.registerCustomizer(new QSARCustomizer()); } public static Test suite() { return new TestSuite(CMLRoundTripTest.class); } public void testAtom() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getSymbol(), roundTrippedAtom.getSymbol()); } public void testAtomId() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); atom.setID("N1"); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getID(), roundTrippedAtom.getID()); } public void testAtom2D() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); Point2d p2d = new Point2d(1.3, 1.4); atom.setPoint2d(p2d); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getPoint2d(), roundTrippedAtom.getPoint2d(), 0.00001); } public void testAtom3D() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); Point3d p3d = new Point3d(1.3, 1.4, 0.9); atom.setPoint3d(p3d); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getPoint3d(), roundTrippedAtom.getPoint3d(), 0.00001); } public void testAtom2DAnd3D() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); Point2d p2d = new Point2d(1.3, 1.4); atom.setPoint2d(p2d); Point3d p3d = new Point3d(1.3, 1.4, 0.9); atom.setPoint3d(p3d); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getPoint2d(), roundTrippedAtom.getPoint2d(), 0.00001); assertEquals(atom.getPoint3d(), roundTrippedAtom.getPoint3d(), 0.00001); } public void testAtomFract3D() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); Point3d p3d = new Point3d(0.3, 0.4, 0.9); atom.setFractionalPoint3d(p3d); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getFractionalPoint3d(), roundTrippedAtom.getFractionalPoint3d(), 0.00001); } public void testPseudoAtom() throws Exception { Molecule mol = new Molecule(); PseudoAtom atom = new PseudoAtom("N"); atom.setLabel("Glu55"); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertNotNull(roundTrippedAtom); assertTrue(roundTrippedAtom instanceof PseudoAtom); assertEquals("Glu55", ((PseudoAtom)roundTrippedAtom).getLabel()); } /** * @cdk.bug 1455346 */ public void testChemModel() throws Exception { ChemModel model = new ChemModel(); MoleculeSet moleculeSet = new MoleculeSet(); Molecule mol = new Molecule(); PseudoAtom atom = new PseudoAtom("N"); mol.addAtom(atom); moleculeSet.addAtomContainer(mol); model.setMoleculeSet(moleculeSet); IChemModel roundTrippedModel = roundTripChemModel(model); IMoleculeSet roundTrippedMolSet = roundTrippedModel.getMoleculeSet(); assertNotNull(roundTrippedMolSet); assertEquals(1, roundTrippedMolSet.getAtomContainerCount()); IMolecule roundTrippedMolecule = roundTrippedMolSet.getMolecule(0); assertNotNull(roundTrippedMolecule); assertEquals(1, roundTrippedMolecule.getAtomCount()); } public void testAtomFormalCharge() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("N"); int formalCharge = +1; atom.setFormalCharge(formalCharge); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getFormalCharge(), roundTrippedAtom.getFormalCharge()); } public void testAtomPartialCharge() throws Exception { if (true) return; fail("Have to figure out how to store partial charges in CML2"); Molecule mol = new Molecule(); Atom atom = new Atom("N"); double partialCharge = 0.5; atom.setCharge(partialCharge); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getCharge(), roundTrippedAtom.getCharge(), 0.0001); } public void testAtomStereoParity() throws Exception { if (true) return; fail("Have to figure out how to store atom parity in CML2"); Molecule mol = new Molecule(); Atom atom = new Atom("C"); int stereo = CDKConstants.STEREO_ATOM_PARITY_PLUS; atom.setStereoParity(stereo); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getStereoParity(), roundTrippedAtom.getStereoParity()); } public void testIsotope() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("C"); atom.setMassNumber(13); mol.addAtom(atom); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(atom.getMassNumber(), roundTrippedAtom.getMassNumber()); } public void testBond() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("C"); Atom atom2 = new Atom("O"); mol.addAtom(atom); mol.addAtom(atom2); Bond bond = new Bond(atom, atom2, 1.0); mol.addBond(bond); IMolecule roundTrippedMol = roundTripMolecule(mol);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -