📄 cmlroundtriptest.java
字号:
assertEquals(2, roundTrippedMol.getAtomCount()); assertEquals(1, roundTrippedMol.getBondCount()); IBond roundTrippedBond = roundTrippedMol.getBond(0); assertEquals(2, roundTrippedBond.getAtomCount()); assertEquals("C", roundTrippedBond.getAtom(0).getSymbol()); // preserved direction? assertEquals("O", roundTrippedBond.getAtom(1).getSymbol()); assertEquals(bond.getOrder(), roundTrippedBond.getOrder(), 0.0001); } public void testBondID() 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); bond.setID("b1"); mol.addBond(bond); IMolecule roundTrippedMol = roundTripMolecule(mol); IBond roundTrippedBond = roundTrippedMol.getBond(0); assertEquals(bond.getID(), roundTrippedBond.getID()); } public void testBondStereo() 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); int stereo = CDKConstants.STEREO_BOND_DOWN; bond.setStereo(stereo); mol.addBond(bond); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(2, roundTrippedMol.getAtomCount()); assertEquals(1, roundTrippedMol.getBondCount()); IBond roundTrippedBond = roundTrippedMol.getBond(0); assertEquals(bond.getStereo(), roundTrippedBond.getStereo()); } /** * Convert a Molecule to CML and back to a Molecule again. * Given that CML reading is working, the problem is with the * CMLWriter. * * @see org.openscience.cdk.CMLFragmentsTest */ private IMolecule roundTripMolecule(IMolecule mol) throws Exception { String cmlString = "<!-- failed -->"; Element cmlDOM = convertor.cdkMoleculeToCMLMolecule(mol); cmlString = cmlDOM.toXML(); IMolecule roundTrippedMol = null; logger.debug("CML string: ", cmlString); CMLReader reader = new CMLReader(new ByteArrayInputStream(cmlString.getBytes())); IChemFile file = (IChemFile)reader.read(new org.openscience.cdk.ChemFile()); assertNotNull(file); assertEquals(1, file.getChemSequenceCount()); IChemSequence sequence = file.getChemSequence(0); assertNotNull(sequence); assertEquals(1, sequence.getChemModelCount()); IChemModel chemModel = sequence.getChemModel(0); assertNotNull(chemModel); IMoleculeSet moleculeSet = chemModel.getMoleculeSet(); assertNotNull(moleculeSet); assertEquals(1, moleculeSet.getMoleculeCount()); roundTrippedMol = moleculeSet.getMolecule(0); assertNotNull(roundTrippedMol); return roundTrippedMol; } private IChemModel roundTripChemModel(IChemModel model) throws Exception { String cmlString = "<!-- failed -->"; Element cmlDOM = convertor.cdkChemModelToCMLList(model); cmlString = cmlDOM.toXML(); logger.debug("CML string: ", cmlString); CMLReader reader = new CMLReader(new ByteArrayInputStream(cmlString.getBytes())); IChemFile file = (IChemFile)reader.read(model.getBuilder().newChemFile()); assertNotNull(file); assertEquals(1, file.getChemSequenceCount()); IChemSequence sequence = file.getChemSequence(0); assertNotNull(sequence); assertEquals(1, sequence.getChemModelCount()); IChemModel chemModel = sequence.getChemModel(0); assertNotNull(chemModel); return chemModel; } private IReaction roundTripReaction(IReaction reaction) throws Exception { String cmlString = "<!-- failed -->"; Element cmlDOM = convertor.cdkReactionToCMLReaction(reaction); cmlString = cmlDOM.toXML(); IReaction roundTrippedReaction = null; logger.debug("CML string: ", cmlString); CMLReader reader = new CMLReader(new ByteArrayInputStream(cmlString.getBytes())); IChemFile file = (IChemFile)reader.read(new org.openscience.cdk.ChemFile()); assertNotNull(file); assertEquals(1, file.getChemSequenceCount()); IChemSequence sequence = file.getChemSequence(0); assertNotNull(sequence); assertEquals(1, sequence.getChemModelCount()); IChemModel chemModel = sequence.getChemModel(0); assertNotNull(chemModel); IReactionSet reactionSet = chemModel.getReactionSet(); assertNotNull(reactionSet); assertEquals(1, reactionSet.getReactionCount()); roundTrippedReaction = reactionSet.getReaction(0); assertNotNull(roundTrippedReaction); return roundTrippedReaction; } public void testPartialCharge() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("C"); mol.addAtom(atom); double charge = -0.267; atom.setCharge(charge); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(charge, roundTrippedAtom.getCharge(), 0.0001); } public void testInChI() throws Exception { Molecule mol = new Molecule(); String inchi = "InChI=1/CH2O2/c2-1-3/h1H,(H,2,3)"; mol.setProperty(CDKConstants.INCHI, inchi); IMolecule roundTrippedMol = roundTripMolecule(mol); assertNotNull(roundTrippedMol); assertEquals(inchi, roundTrippedMol.getProperty(CDKConstants.INCHI)); } public void testSpinMultiplicity() throws Exception { Molecule mol = new Molecule(); Atom atom = new Atom("C"); mol.addAtom(atom); mol.addSingleElectron(new SingleElectron(atom)); IMolecule roundTrippedMol = roundTripMolecule(mol); assertEquals(1, roundTrippedMol.getAtomCount()); assertEquals(1, roundTrippedMol.getElectronContainerCount()); IAtom roundTrippedAtom = roundTrippedMol.getAtom(0); assertEquals(1, roundTrippedMol.getConnectedSingleElectronsCount(roundTrippedAtom)); } public void testReaction() throws Exception { logger.debug("********** TEST REACTION **********"); IReaction reaction = new Reaction(); reaction.setID("reaction.1"); IMolecule reactant = reaction.getBuilder().newMolecule(); reactant.setID("react"); IAtom atom = reaction.getBuilder().newAtom("C"); reactant.addAtom(atom); reaction.addReactant(reactant); IMolecule product = reaction.getBuilder().newMolecule(); product.setID("product"); atom = reaction.getBuilder().newAtom("X"); product.addAtom(atom); reaction.addProduct(product); IMolecule agent = reaction.getBuilder().newMolecule(); agent.setID("water"); atom = reaction.getBuilder().newAtom("H"); agent.addAtom(atom); reaction.addAgent(agent); IReaction roundTrippedReaction = roundTripReaction(reaction); assertNotNull(roundTrippedReaction); assertEquals("reaction.1", roundTrippedReaction.getID()); assertNotNull(roundTrippedReaction); IMoleculeSet reactants = roundTrippedReaction.getReactants(); assertNotNull(reactants); assertEquals(1, reactants.getMoleculeCount()); IMolecule roundTrippedReactant = reactants.getMolecule(0); assertEquals("react", roundTrippedReactant.getID()); assertEquals(1, roundTrippedReactant.getAtomCount()); IMoleculeSet products = roundTrippedReaction.getProducts(); assertNotNull(products); assertEquals(1, products.getMoleculeCount()); IMolecule roundTrippedProduct = products.getMolecule(0); assertEquals("product", roundTrippedProduct.getID()); assertEquals(1, roundTrippedProduct.getAtomCount()); IMoleculeSet agents = roundTrippedReaction.getAgents(); assertNotNull(agents); assertEquals(1, agents.getMoleculeCount()); IMolecule roundTrippedAgent = agents.getMolecule(0); assertEquals("water", roundTrippedAgent.getID()); assertEquals(1, roundTrippedAgent.getAtomCount()); } public void testDescriptorValue_QSAR() throws Exception { Molecule molecule = MoleculeFactory.makeBenzene(); IMolecularDescriptor descriptor = new WeightDescriptor(); DescriptorValue originalValue = null; originalValue = descriptor.calculate(molecule); molecule.setProperty(originalValue.getSpecification(), originalValue); IMolecule roundTrippedMol = roundTripMolecule(molecule); assertEquals(1, roundTrippedMol.getProperties().size()); Object object = roundTrippedMol.getProperties().keySet().toArray()[0]; assertTrue(object instanceof DescriptorSpecification); DescriptorSpecification spec = (DescriptorSpecification)object; assertEquals(descriptor.getSpecification().getSpecificationReference(), spec.getSpecificationReference()); assertEquals(descriptor.getSpecification().getImplementationIdentifier(), spec.getImplementationIdentifier()); assertEquals(descriptor.getSpecification().getImplementationTitle(), spec.getImplementationTitle()); assertEquals(descriptor.getSpecification().getImplementationVendor(), spec.getImplementationVendor()); Object value = roundTrippedMol.getProperty(spec); assertNotNull(value); assertTrue(value instanceof DescriptorValue); DescriptorValue descriptorResult = (DescriptorValue)value; assertEquals(originalValue.getClass().getName(), descriptorResult.getClass().getName()); assertEquals(originalValue.getValue().toString(), descriptorResult.getValue().toString()); } public void testDescriptorValue() throws Exception { Molecule molecule = MoleculeFactory.makeBenzene(); String propertyName = "testKey"; String propertyValue = "testValue"; molecule.setProperty(propertyName, propertyValue); IMolecule roundTrippedMol = roundTripMolecule(molecule); assertNotNull(roundTrippedMol.getProperty(propertyName)); assertEquals(propertyValue, roundTrippedMol.getProperty(propertyName)); } /** * Tests of bond order information is stored even when aromiticity is given. * * @throws Exception */ public void testAromaticity() throws Exception { IMolecule molecule = MoleculeFactory.makeBenzene(); for (Iterator bonds=molecule.bonds(); bonds.hasNext();) { ((IBond)bonds.next()).setFlag(CDKConstants.ISAROMATIC, true); } IMolecule roundTrippedMol = roundTripMolecule(molecule); double orderSum = 0.0; for (Iterator bonds=roundTrippedMol.bonds(); bonds.hasNext();) { IBond bond = (IBond)bonds.next(); orderSum += bond.getOrder(); assertTrue(bond.getFlag(CDKConstants.ISAROMATIC)); } assertEquals(9.0, orderSum, 0.001); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -