/** A unit test for JUnit */ @Test public void testAlanin() throws Exception { HydrogenPlacer hydrogenPlacer = new HydrogenPlacer(); IAtomContainer mol1 = new AtomContainer(); SmilesGenerator sg = new SmilesGenerator(); mol1.addAtom(new Atom("N", new Point2d(1, 0))); // 1 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 2 mol1.addAtom(new Atom("F", new Point2d(1, 2))); // 3 mol1.addAtom(new Atom("C", new Point2d(0, 0))); // 4 mol1.addAtom(new Atom("C", new Point2d(1, 4))); // 5 mol1.addAtom(new Atom("O", new Point2d(1, 5))); // 6 mol1.addAtom(new Atom("O", new Point2d(1, 6))); // 7 mol1.addBond(0, 1, IBond.Order.SINGLE); // 1 mol1.addBond(1, 2, IBond.Order.SINGLE, IBond.Stereo.UP); // 2 mol1.addBond(1, 3, IBond.Order.SINGLE, IBond.Stereo.DOWN); // 3 mol1.addBond(1, 4, IBond.Order.SINGLE); // 4 mol1.addBond(4, 5, IBond.Order.SINGLE); // 5 mol1.addBond(4, 6, IBond.Order.DOUBLE); // 6 addExplicitHydrogens(mol1); hydrogenPlacer.placeHydrogens2D(mol1, 1.0); IsotopeFactory ifac = IsotopeFactory.getInstance(mol1.getBuilder()); ifac.configureAtoms(mol1); String smiles1 = null; if (standAlone) { display(mol1); } smiles1 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]); if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals("[H]OC(=O)[C@](F)(N([H])[H])C([H])([H])[H]", smiles1); mol1.getBond(1).setStereo(IBond.Stereo.DOWN); mol1.getBond(2).setStereo(IBond.Stereo.UP); smiles1 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]); if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals("[H]OC(=O)[C@](F)(C([H])([H])[H])N([H])[H]", smiles1); }
/** * Loads one or more files into IAtomContainer objects. * * <p>This method does not need knowledge of the format since it is autodetected. Note that if * aromaticity detection or atom typing is specified and fails for a specific molecule, that * molecule will be set to <i>null</i> * * @param filenames An array of String's containing the filenames of the structures we want to * load * @param doAromaticity If true, then aromaticity perception is performed * @param doTyping If true, atom typing and configuration is performed. This will use the internal * CDK atom typing scheme * @return An array of AtoContainer's * @throws CDKException if there is an error when reading a file */ public static IAtomContainer[] loadMolecules( String[] filenames, boolean doAromaticity, boolean doTyping, boolean doIsotopes) throws CDKException, IOException { Vector<IAtomContainer> v = new Vector<IAtomContainer>(); IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); try { int i; int j; for (i = 0; i < filenames.length; i++) { File input = new File(filenames[i]); ReaderFactory readerFactory = new ReaderFactory(); ISimpleChemObjectReader reader = readerFactory.createReader(new FileReader(input)); if (reader == null) { // see if it's a SMI file if (filenames[i].endsWith(".smi")) { reader = new SMILESReader(new FileReader(input)); } } IChemFile content = (IChemFile) reader.read(builder.newInstance(IChemFile.class)); if (content == null) continue; List<IAtomContainer> c = ChemFileManipulator.getAllAtomContainers(content); // we should do this loop in case we have files // that contain multiple molecules v.addAll(c); } } catch (Exception e) { e.printStackTrace(); throw new CDKException(e.toString()); } // convert the vector to a simple array IAtomContainer[] retValues = new IAtomContainer[v.size()]; for (int i = 0; i < v.size(); i++) { retValues[i] = v.get(i); } // before returning, lets make see if we // need to perceive aromaticity and atom typing if (doAromaticity) { for (int i = 0; i < retValues.length; i++) { try { CDKHueckelAromaticityDetector.detectAromaticity(retValues[i]); } catch (CDKException e) { retValues[i] = null; } } } if (doTyping) { for (int i = 0; i < retValues.length; i++) { try { AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(retValues[i]); } catch (CDKException e) { retValues[i] = null; } } } if (doIsotopes) { IsotopeFactory ifac = IsotopeFactory.getInstance(DefaultChemObjectBuilder.getInstance()); for (IAtomContainer retValue : retValues) { ifac.configureAtoms(retValue); } } return retValues; }
/** * Calculates the 3 MI's, 3 ration and the R_gyr value. * * <p>The molecule should have hydrogens * * @param container Parameter is the atom container. * @return An ArrayList containing 7 elements in the order described above */ @TestMethod("testCalculate_IAtomContainer") public DescriptorValue calculate(IAtomContainer container) { if (!GeometryTools.has3DCoordinates(container)) return getDummyDescriptorValue(new CDKException("Molecule must have 3D coordinates")); IAtomContainer clone; IsotopeFactory factory; try { clone = (IAtomContainer) container.clone(); factory = IsotopeFactory.getInstance(container.getBuilder()); factory.configureAtoms(clone); } catch (Exception e) { logger.debug(e); return getDummyDescriptorValue(e); } DoubleArrayResult retval = new DoubleArrayResult(7); double ccf = 1.000138; double eps = 1e-5; double[][] imat = new double[3][3]; Point3d centerOfMass = GeometryTools.get3DCentreOfMass(clone); double xdif; double ydif; double zdif; double xsq; double ysq; double zsq; for (int i = 0; i < clone.getAtomCount(); i++) { IAtom currentAtom = clone.getAtom(i); double mass = factory.getMajorIsotope(currentAtom.getSymbol()).getExactMass(); xdif = currentAtom.getPoint3d().x - centerOfMass.x; ydif = currentAtom.getPoint3d().y - centerOfMass.y; zdif = currentAtom.getPoint3d().z - centerOfMass.z; xsq = xdif * xdif; ysq = ydif * ydif; zsq = zdif * zdif; imat[0][0] += mass * (ysq + zsq); imat[1][1] += mass * (xsq + zsq); imat[2][2] += mass * (xsq + ysq); imat[1][0] += -1 * mass * ydif * xdif; imat[0][1] = imat[1][0]; imat[2][0] += -1 * mass * xdif * zdif; imat[0][2] = imat[2][0]; imat[2][1] += -1 * mass * ydif * zdif; imat[1][2] = imat[2][1]; } // diagonalize the MI tensor Matrix tmp = new Matrix(imat); EigenvalueDecomposition eigenDecomp = tmp.eig(); double[] eval = eigenDecomp.getRealEigenvalues(); retval.add(eval[2]); retval.add(eval[1]); retval.add(eval[0]); double etmp = eval[0]; eval[0] = eval[2]; eval[2] = etmp; if (Math.abs(eval[1]) > 1e-3) retval.add(eval[0] / eval[1]); else retval.add(1000); if (Math.abs(eval[2]) > 1e-3) { retval.add(eval[0] / eval[2]); retval.add(eval[1] / eval[2]); } else { retval.add(1000); retval.add(1000); } // finally get the radius of gyration double pri; IMolecularFormula formula = MolecularFormulaManipulator.getMolecularFormula(clone); if (Math.abs(eval[2]) > eps) pri = Math.pow(eval[0] * eval[1] * eval[2], 1.0 / 3.0); else pri = Math.sqrt(eval[0] * ccf / MolecularFormulaManipulator.getTotalExactMass(formula)); retval.add( Math.sqrt( Math.PI * 2 * pri * ccf / MolecularFormulaManipulator.getTotalExactMass(formula))); return new DescriptorValue( getSpecification(), getParameterNames(), getParameters(), retval, getDescriptorNames()); }
/** A unit test for JUnit */ @Test public void testDoubleBondConfiguration() throws Exception { HydrogenPlacer hydrogenPlacer = new HydrogenPlacer(); IAtomContainer mol1 = new AtomContainer(); SmilesGenerator sg = new SmilesGenerator(); mol1.addAtom(new Atom("S", new Point2d(0, 0))); // 1 mol1.addAtom(new Atom("C", new Point2d(1, 1))); // 2 mol1.addAtom(new Atom("F", new Point2d(2, 0))); // 3 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 4 mol1.addAtom(new Atom("F", new Point2d(2, 3))); // 5 mol1.addAtom(new Atom("S", new Point2d(0, 3))); // 1 mol1.addBond(0, 1, IBond.Order.SINGLE); // 1 mol1.addBond(1, 2, IBond.Order.SINGLE); // 2 mol1.addBond(1, 3, IBond.Order.DOUBLE); // 3 mol1.addBond(3, 4, IBond.Order.SINGLE); // 4 mol1.addBond(3, 5, IBond.Order.SINGLE); // 4 try { IsotopeFactory ifac = IsotopeFactory.getInstance(mol1.getBuilder()); ifac.configureAtoms(mol1); } catch (IOException ex) { } String smiles1 = null; if (standAlone) { display(mol1); } boolean[] bool = new boolean[mol1.getBondCount()]; bool[2] = true; try { smiles1 = sg.createSMILES(mol1, true, bool); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals("F/C(=C/(F)S)S", smiles1); mol1.getAtom(4).setPoint2d(new Point2d(0, 3)); mol1.getAtom(5).setPoint2d(new Point2d(2, 3)); try { smiles1 = sg.createSMILES(mol1, true, bool); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals("F/C(=C\\(F)S)S", smiles1); try { addExplicitHydrogens(mol1); hydrogenPlacer.placeHydrogens2D(mol1, 1.0); } catch (IOException ex) { } catch (ClassNotFoundException ex) { } bool = new boolean[mol1.getBondCount()]; bool[2] = true; try { smiles1 = sg.createSMILES(mol1, true, bool); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } Assert.assertEquals("[H]S/C(F)=C/(F)S[H]", smiles1); mol1.getAtom(5).setPoint2d(new Point2d(0, 3)); mol1.getAtom(4).setPoint2d(new Point2d(2, 3)); try { smiles1 = sg.createSMILES(mol1, true, bool); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } Assert.assertEquals("[H]S/C(F)=C\\(F)S[H]", smiles1); }
/** A unit test for JUnit */ @Test public void testCisTransDecalin() throws Exception { HydrogenPlacer hydrogenPlacer = new HydrogenPlacer(); IAtomContainer mol1 = new AtomContainer(); SmilesGenerator sg = new SmilesGenerator(); mol1.addAtom(new Atom("H", new Point2d(1, 0))); // 1 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 2 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 3 mol1.addAtom(new Atom("C", new Point2d(0, 0))); // 4 mol1.addAtom(new Atom("C", new Point2d(1, 4))); // 5 mol1.addAtom(new Atom("C", new Point2d(1, 5))); // 6 mol1.addAtom(new Atom("C", new Point2d(1, 6))); // 7 mol1.addAtom(new Atom("H", new Point2d(1, 0))); // 1 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 2 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 3 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 2 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 3 mol1.addBond(0, 1, IBond.Order.SINGLE, IBond.Stereo.DOWN); // 1 mol1.addBond(1, 2, IBond.Order.SINGLE); // 2 mol1.addBond(2, 3, IBond.Order.SINGLE); // 3 mol1.addBond(3, 4, IBond.Order.SINGLE); // 4 mol1.addBond(4, 5, IBond.Order.SINGLE); // 5 mol1.addBond(5, 6, IBond.Order.SINGLE); // 6 mol1.addBond(6, 7, IBond.Order.SINGLE, IBond.Stereo.DOWN); // 3 mol1.addBond(6, 8, IBond.Order.SINGLE); // 4 mol1.addBond(8, 9, IBond.Order.SINGLE); // 5 mol1.addBond(9, 10, IBond.Order.SINGLE); // 6 mol1.addBond(10, 11, IBond.Order.SINGLE); // 6 mol1.addBond(11, 1, IBond.Order.SINGLE); // 6 mol1.addBond(1, 6, IBond.Order.SINGLE); // 6 try { addExplicitHydrogens(mol1); hydrogenPlacer.placeHydrogens2D(mol1, 1.0); IsotopeFactory ifac = IsotopeFactory.getInstance(mol1.getBuilder()); ifac.configureAtoms(mol1); } catch (IOException ex) { } catch (ClassNotFoundException ex) { } String smiles1 = null; if (standAlone) { display(mol1); } try { smiles1 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals( "[H]C1([H])(C([H])([H])C([H])([H])C\\2([H])(C([H])([H])C([H])([H])C([H])([H])C([H])([H])C\\2([H])(C1([H])([H]))))", smiles1); mol1.getBond(6).setStereo(IBond.Stereo.UP); String smiles3 = null; try { smiles3 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } Assert.assertNotSame(smiles3, smiles1); }
/** A unit test for JUnit */ @Test public void testCisResorcinol() throws Exception { HydrogenPlacer hydrogenPlacer = new HydrogenPlacer(); IAtomContainer mol1 = new AtomContainer(); SmilesGenerator sg = new SmilesGenerator(); mol1.addAtom(new Atom("O", new Point2d(3, 1))); // 1 mol1.addAtom(new Atom("H", new Point2d(2, 0))); // 2 mol1.addAtom(new Atom("C", new Point2d(2, 1))); // 3 mol1.addAtom(new Atom("C", new Point2d(1, 1))); // 4 mol1.addAtom(new Atom("C", new Point2d(1, 4))); // 5 mol1.addAtom(new Atom("C", new Point2d(1, 5))); // 6 mol1.addAtom(new Atom("C", new Point2d(1, 2))); // 7 mol1.addAtom(new Atom("C", new Point2d(2, 2))); // 1 mol1.addAtom(new Atom("O", new Point2d(3, 2))); // 2 mol1.addAtom(new Atom("H", new Point2d(2, 3))); // 3 mol1.addBond(0, 2, IBond.Order.SINGLE, IBond.Stereo.DOWN); // 1 mol1.addBond(1, 2, IBond.Order.SINGLE, IBond.Stereo.UP); // 2 mol1.addBond(2, 3, IBond.Order.SINGLE); // 3 mol1.addBond(3, 4, IBond.Order.SINGLE); // 4 mol1.addBond(4, 5, IBond.Order.SINGLE); // 5 mol1.addBond(5, 6, IBond.Order.SINGLE); // 6 mol1.addBond(6, 7, IBond.Order.SINGLE); // 3 mol1.addBond(7, 8, IBond.Order.SINGLE, IBond.Stereo.UP); // 4 mol1.addBond(7, 9, IBond.Order.SINGLE, IBond.Stereo.DOWN); // 5 mol1.addBond(7, 2, IBond.Order.SINGLE); // 6 try { addExplicitHydrogens(mol1); hydrogenPlacer.placeHydrogens2D(mol1, 1.0); IsotopeFactory ifac = IsotopeFactory.getInstance(mol1.getBuilder()); ifac.configureAtoms(mol1); } catch (IOException ex) { } catch (ClassNotFoundException ex) { } String smiles1 = null; if (standAlone) { display(mol1); } try { smiles1 = sg.createSMILES(mol1, true, new boolean[mol1.getBondCount()]); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals( "[H]O[C@]1(C([H])([H])C([H])([H])C([H])([H])C([H])([H])[C@]1(O[H])([H]))([H])", smiles1); mol1 = AtomContainerManipulator.removeHydrogens(mol1); try { smiles1 = sg.createSMILES(mol1); } catch (Exception exc) { System.out.println(exc); if (!standAlone) { Assert.fail(); } } if (standAlone) { System.err.println("SMILES 1: " + smiles1); } Assert.assertNotNull(smiles1); Assert.assertEquals("OC1CCCCC1(O)", smiles1); }