@Test public void testReading() throws Exception { String filename = "data/asn/pubchem/cid1145.xml"; logger.info("Testing: " + filename); InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); PCCompoundXMLReader reader = new PCCompoundXMLReader(ins); IMolecule molecule = (IMolecule) reader.read(new Molecule()); Assert.assertNotNull(molecule); // check atom stuff Assert.assertEquals(14, molecule.getAtomCount()); Assert.assertEquals("O", molecule.getAtom(0).getSymbol()); Assert.assertEquals(Integer.valueOf(-1), molecule.getAtom(0).getFormalCharge()); Assert.assertEquals("N", molecule.getAtom(1).getSymbol()); Assert.assertEquals(Integer.valueOf(1), molecule.getAtom(1).getFormalCharge()); // check bond stuff Assert.assertEquals(13, molecule.getBondCount()); Assert.assertNotNull(molecule.getBond(3)); // coordinates Assert.assertNull(molecule.getAtom(0).getPoint3d()); Point2d point = molecule.getAtom(0).getPoint2d(); Assert.assertNotNull(point); Assert.assertEquals(3.7320508956909, point.x, 0.00000001); Assert.assertEquals(0.5, point.y, 0.00000001); }
/** * Test for SF bug #1309731. * * @cdk.bug 1309731 */ @Test public void testModelBuilder3D_keepChemObjectIDs() throws Exception { ModelBuilder3D mb3d = ModelBuilder3D.getInstance(); IMolecule methanol = new org.openscience.cdk.Molecule(); IChemObjectBuilder builder = methanol.getBuilder(); IAtom carbon1 = builder.newInstance(IAtom.class, "C"); carbon1.setID("carbon1"); methanol.addAtom(carbon1); for (int i = 0; i < 3; i++) { IAtom hydrogen = builder.newInstance(IAtom.class, "H"); methanol.addAtom(hydrogen); methanol.addBond(builder.newInstance(IBond.class, carbon1, hydrogen, IBond.Order.SINGLE)); } IAtom oxygen1 = builder.newInstance(IAtom.class, "O"); oxygen1.setID("oxygen1"); methanol.addAtom(oxygen1); methanol.addBond(builder.newInstance(IBond.class, carbon1, oxygen1, IBond.Order.SINGLE)); IAtom hydrogen = builder.newInstance(IAtom.class, "H"); methanol.addAtom(hydrogen); methanol.addBond(builder.newInstance(IBond.class, hydrogen, oxygen1, IBond.Order.SINGLE)); Assert.assertEquals(6, methanol.getAtomCount()); Assert.assertEquals(5, methanol.getBondCount()); mb3d.generate3DCoordinates(methanol, false); checkAverageBondLength(methanol); Assert.assertEquals("carbon1", carbon1.getID()); Assert.assertEquals("oxygen1", oxygen1.getID()); }
@Test public void testNNMolecule_int_int_int_int() { IMolecule m = new NNMolecule(5, 5, 1, 1); Assert.assertNotNull(m); Assert.assertEquals(0, m.getAtomCount()); Assert.assertEquals(0, m.getBondCount()); Assert.assertEquals(0, m.getLonePairCount()); Assert.assertEquals(0, m.getSingleElectronCount()); }
@Test public void testBondStereo() throws Exception { String cmlString = "<molecule id='m1'><atomArray><atom id='a1'/><atom id='a2'/></atomArray><bondArray><bond id='b1' atomRefs2='a1 a2'><bondStereo dictRef='cml:H'/></bond></bondArray></molecule>"; IChemFile chemFile = parseCMLString(cmlString); IMolecule mol = checkForSingleMoleculeFile(chemFile); Assert.assertEquals(2, mol.getAtomCount()); Assert.assertEquals(1, mol.getBondCount()); IBond bond = mol.getBond(0); Assert.assertEquals(IBond.Stereo.DOWN, bond.getStereo()); }
@Test public void testBondAromatic2() throws Exception { String cmlString = "<molecule id='m1'><atomArray atomID='a1 a2'/><bondArray><bond atomRefs='a1 a2' order='2'><bondType dictRef='cdk:aromaticBond'/></bond></bondArray></molecule>"; IChemFile chemFile = parseCMLString(cmlString); IMolecule mol = checkForSingleMoleculeFile(chemFile); Assert.assertEquals(2, mol.getAtomCount()); Assert.assertEquals(1, mol.getBondCount()); org.openscience.cdk.interfaces.IBond bond = mol.getBond(0); Assert.assertEquals(CDKConstants.BONDORDER_DOUBLE, bond.getOrder()); Assert.assertTrue(bond.getFlag(CDKConstants.ISAROMATIC)); }
@Test public void testBondId() throws Exception { String cmlString = "<molecule id='m1'><atomArray><atom id='a1'/><atom id='a2'/></atomArray><bondArray><bond id='b1' atomRefs2='a1 a2'/></bondArray></molecule>"; IChemFile chemFile = parseCMLString(cmlString); IMolecule mol = checkForSingleMoleculeFile(chemFile); Assert.assertEquals(2, mol.getAtomCount()); Assert.assertEquals(1, mol.getBondCount()); org.openscience.cdk.interfaces.IBond bond = mol.getBond(0); Assert.assertEquals("b1", bond.getID()); }
@Test public void testBond5() throws Exception { String cmlString = "<molecule id='m1'><atomArray atomID='a1 a2 a3'/><bondArray atomRef1='a1 a1' atomRef2='a2 a3' order='1 1'/></molecule>"; IChemFile chemFile = parseCMLString(cmlString); IMolecule mol = checkForSingleMoleculeFile(chemFile); Assert.assertEquals(3, mol.getAtomCount()); Assert.assertEquals(2, mol.getBondCount()); org.openscience.cdk.interfaces.IBond bond = mol.getBond(0); Assert.assertEquals(2, bond.getAtomCount()); Assert.assertEquals(IBond.Order.SINGLE, bond.getOrder()); bond = mol.getBond(1); Assert.assertEquals(2, bond.getAtomCount()); Assert.assertEquals(IBond.Order.SINGLE, bond.getOrder()); }
@Test public void testBond4() throws Exception { String cmlString = "<molecule id='m1'><atomArray atomID='a1 a2 a3'/><bondArray atomRef1='a1 a1' atomRef2='a2 a3' bondID='b1 b2'/></molecule>"; IChemFile chemFile = parseCMLString(cmlString); IMolecule mol = checkForSingleMoleculeFile(chemFile); Assert.assertEquals(3, mol.getAtomCount()); Assert.assertEquals(2, mol.getBondCount()); org.openscience.cdk.interfaces.IBond bond = mol.getBond(0); Assert.assertEquals(2, bond.getAtomCount()); IAtom atom1 = bond.getAtom(0); IAtom atom2 = bond.getAtom(1); Assert.assertEquals("a1", atom1.getID()); Assert.assertEquals("a2", atom2.getID()); Assert.assertEquals("b2", mol.getBond(1).getID()); }
@Test public void testNNMolecule_IAtomContainer() { IAtomContainer acetone = new org.openscience.cdk.AtomContainer(); IAtom c1 = acetone.getBuilder().newAtom("C"); IAtom c2 = acetone.getBuilder().newAtom("C"); IAtom o = acetone.getBuilder().newAtom("O"); IAtom c3 = acetone.getBuilder().newAtom("C"); acetone.addAtom(c1); acetone.addAtom(c2); acetone.addAtom(c3); acetone.addAtom(o); IBond b1 = acetone.getBuilder().newBond(c1, c2, IBond.Order.SINGLE); IBond b2 = acetone.getBuilder().newBond(c1, o, IBond.Order.DOUBLE); IBond b3 = acetone.getBuilder().newBond(c1, c3, IBond.Order.SINGLE); acetone.addBond(b1); acetone.addBond(b2); acetone.addBond(b3); IMolecule m = new NNMolecule(acetone); Assert.assertNotNull(m); Assert.assertEquals(4, m.getAtomCount()); Assert.assertEquals(3, m.getBondCount()); }
/** * Get the container which is found resonance from a IMolecule. It is based on looking if the * order of the bond changes. * * @param molecule The IMolecule to analyze * @return The different containers */ @TestMethod("testGetContainers_IMolecule") public IAtomContainerSet getContainers(IMolecule molecule) { IAtomContainerSet setOfCont = molecule.getBuilder().newAtomContainerSet(); IMoleculeSet setOfMol = getStructures(molecule); if (setOfMol.getMoleculeCount() == 0) return setOfCont; /*extraction of all bonds which has been produced a changes of order*/ List<IBond> bondList = new ArrayList<IBond>(); for (int i = 1; i < setOfMol.getMoleculeCount(); i++) { IMolecule mol = setOfMol.getMolecule(i); for (int j = 0; j < mol.getBondCount(); j++) { IBond bond = molecule.getBond(j); if (!mol.getBond(j).getOrder().equals(bond.getOrder())) { if (!bondList.contains(bond)) bondList.add(bond); } } } if (bondList.size() == 0) return null; int[] flagBelonging = new int[bondList.size()]; for (int i = 0; i < flagBelonging.length; i++) flagBelonging[i] = 0; int[] position = new int[bondList.size()]; int maxGroup = 1; /*Analysis if the bond are linked together*/ List<IBond> newBondList = new ArrayList<IBond>(); newBondList.add(bondList.get(0)); int pos = 0; for (int i = 0; i < newBondList.size(); i++) { if (i == 0) flagBelonging[i] = maxGroup; else { if (flagBelonging[position[i]] == 0) { maxGroup++; flagBelonging[position[i]] = maxGroup; } } IBond bondA = newBondList.get(i); for (int ato = 0; ato < 2; ato++) { IAtom atomA1 = bondA.getAtom(ato); List<IBond> bondA1s = molecule.getConnectedBondsList(atomA1); for (int j = 0; j < bondA1s.size(); j++) { IBond bondB = bondA1s.get(j); if (!newBondList.contains(bondB)) for (int k = 0; k < bondList.size(); k++) if (bondList.get(k).equals(bondB)) if (flagBelonging[k] == 0) { flagBelonging[k] = maxGroup; pos++; newBondList.add(bondB); position[pos] = k; } } } // if it is final size and not all are added if (newBondList.size() - 1 == i) for (int k = 0; k < bondList.size(); k++) if (!newBondList.contains(bondList.get(k))) { newBondList.add(bondList.get(k)); position[i + 1] = k; break; } } /*creating containers according groups*/ for (int i = 0; i < maxGroup; i++) { IAtomContainer container = molecule.getBuilder().newAtomContainer(); for (int j = 0; j < bondList.size(); j++) { if (flagBelonging[j] != i + 1) continue; IBond bond = bondList.get(j); IAtom atomA1 = bond.getAtom(0); IAtom atomA2 = bond.getAtom(1); if (!container.contains(atomA1)) container.addAtom(atomA1); if (!container.contains(atomA2)) container.addAtom(atomA2); container.addBond(bond); } setOfCont.addAtomContainer(container); } return setOfCont; }