MolecularFormulaGenerator implements IFormulaGenerator { @Override public IMolecularFormulaSet getAllFormulas() { return formulaGenerator.getAllFormulas(); } MolecularFormulaGenerator(final IChemObjectBuilder builder,
                                     final double minMass, final double maxMass,
                                     final MolecularFormulaRange mfRange); @Override IMolecularFormula getNextFormula(); @Override IMolecularFormulaSet getAllFormulas(); @Override double getFinishedPercentage(); @Override void cancel();  }
@Test public void testGetAllFormulas() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 10); mfRange.addIsotope(h, 0, 10); mfRange.addIsotope(o, 0, 10); mfRange.addIsotope(n, 0, 10); double minMass = 100.0; double maxMass = 100.05; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertNotEquals(0, mfSet.size()); }
@Test public void testMinCounts() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 5, 20); mfRange.addIsotope(h, 5, 20); mfRange.addIsotope(o, 5, 20); mfRange.addIsotope(n, 5, 20); double minMass = 100; double maxMass = 250; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); for (IMolecularFormula f : mfSet.molecularFormulas()) { for (IIsotope i : f.isotopes()) { int count = f.getIsotopeCount(i); Assert.assertTrue(count >= 5); } } }
@Test public void testMaxCounts() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 3, 7); mfRange.addIsotope(h, 3, 7); mfRange.addIsotope(o, 3, 7); mfRange.addIsotope(n, 3, 7); double minMass = 250; double maxMass = 400; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); for (IMolecularFormula f : mfSet.molecularFormulas()) { for (IIsotope i : f.isotopes()) { int count = f.getIsotopeCount(i); Assert.assertTrue(count <= 7); } } }
@Test public void testSingleCarbon() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 100); double minMass = 5; double maxMass = 15; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C", MolecularFormulaManipulator.getString(mfSet .getMolecularFormula(0))); }
@Test public void testCarbons() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 100000); double minMass = 120000.0 - 1; double maxMass = 120000.0 + 1; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C10000", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testWater() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); IIsotope p = ifac.getMajorIsotope("P"); IIsotope s = ifac.getMajorIsotope("S"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 10); mfRange.addIsotope(h, 0, 10); mfRange.addIsotope(o, 0, 10); mfRange.addIsotope(n, 0, 10); mfRange.addIsotope(p, 0, 10); mfRange.addIsotope(s, 0, 10); double minMass = 1; double maxMass = 20; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); boolean found = false; for (IMolecularFormula formula : mfSet.molecularFormulas()) { String mf = MolecularFormulaManipulator.getString(formula); if (mf.equals("H2O")) { found = true; break; } } Assert.assertTrue("The molecular formula H2O should be found", found); }
@Test public void testSmallMass() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 20); mfRange.addIsotope(h, 0, 30); mfRange.addIsotope(o, 0, 15); mfRange.addIsotope(n, 0, 10); double minMass = 115.08714 - 0.0001; double maxMass = 115.08714 + 0.0001; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C5H11N2O", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testMiddleMass() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 50); mfRange.addIsotope(h, 0, 100); mfRange.addIsotope(o, 0, 30); mfRange.addIsotope(n, 0, 10); double minMass = 698.25879 - 0.0001; double maxMass = 698.25879 + 0.0001; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C37H38N4O10", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testHighMass() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); IIsotope s = ifac.getMajorIsotope("S"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 350, 400); mfRange.addIsotope(h, 620, 650); mfRange.addIsotope(o, 100, 150); mfRange.addIsotope(n, 100, 150); mfRange.addIsotope(s, 0, 10); double minMass = 8445.573784 - 0.00001; double maxMass = 8445.573784 + 0.00001; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C374H623N103O116S", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testFormulaFoundInRange() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 1, 50); mfRange.addIsotope(h, 1, 100); mfRange.addIsotope(o, 1, 50); mfRange.addIsotope(n, 1, 50); double minMass = 137.0; double maxMass = 137.2; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertEquals(48, mfSet.size()); boolean found = false; for (IMolecularFormula formula : mfSet.molecularFormulas()) { String mf = MolecularFormulaManipulator.getString(formula); if (mf.equals("C4H11NO4")) { found = true; break; } } Assert.assertTrue("The molecular formula C4H11NO4 should be found", found); }
@Test public void testFormulaFoundInRange2() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 1, 50); mfRange.addIsotope(h, 1, 100); mfRange.addIsotope(o, 1, 50); mfRange.addIsotope(n, 1, 50); double minMass = 187; double maxMass = 189; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertEquals(528, mfSet.size()); boolean found = false; for (IMolecularFormula formula : mfSet.molecularFormulas()) { String mf = MolecularFormulaManipulator.getString(formula); if (mf.equals("C11H10NO2")) { found = true; break; } } Assert.assertTrue("The molecular formula C11H10NO2 should be found", found); }
@Test public void testCompoundWith7Elements() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); IIsotope s = ifac.getMajorIsotope("S"); IIsotope p = ifac.getMajorIsotope("P"); IIsotope cl = ifac.getMajorIsotope("Cl"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 7, 9); mfRange.addIsotope(h, 8, 10); mfRange.addIsotope(o, 1, 3); mfRange.addIsotope(n, 0, 2); mfRange.addIsotope(s, 0, 2); mfRange.addIsotope(p, 0, 2); mfRange.addIsotope(cl, 2, 4); double minMass = 318.915719 - 0.0001; double maxMass = 318.915719 + 0.0001; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C8H9Cl3NO2PS", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testDifferentIsotopes() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope[] carbons = ifac.getIsotopes("C"); IIsotope c13 = carbons[5]; IIsotope h = ifac.getMajorIsotope("H"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 11); mfRange.addIsotope(c13, 0, 10); mfRange.addIsotope(h, 0, 10); double minMass = 54.04193 - 0.001; double maxMass = 54.04193 + 0.001; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, minMass, maxMass, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); IMolecularFormula trueFormula = new MolecularFormula(); trueFormula.addIsotope(c, 3); trueFormula.addIsotope(c13, 1); trueFormula.addIsotope(h, 5); Assert.assertEquals(trueFormula.getIsotopeCount(), mfSet .getMolecularFormula(0).getIsotopeCount()); Assert.assertEquals(trueFormula.getIsotopeCount(c), mfSet .getMolecularFormula(0).getIsotopeCount(c)); Assert.assertEquals(trueFormula.getIsotopeCount(c13), mfSet .getMolecularFormula(0).getIsotopeCount(c13)); }
@Test public void testFixedElementCounts() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 7, 7); mfRange.addIsotope(h, 15, 15); mfRange.addIsotope(o, 4, 4); mfRange.addIsotope(n, 2, 2); double massMin = 10d; double massMax = 1000d; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, massMin, massMax, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(1, mfSet.size()); Assert.assertEquals("C7H15N2O4", MolecularFormulaManipulator .getString(mfSet.getMolecularFormula(0))); }
@Test public void testMassRangeTooHigh() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 10); mfRange.addIsotope(h, 0, 10); mfRange.addIsotope(o, 0, 10); mfRange.addIsotope(n, 0, 10); double massMin = 1000d; double massMax = 2000d; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, massMin, massMax, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(0, mfSet.size()); }
@Test public void testMassRangeTooLow() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 100, 200); mfRange.addIsotope(h, 100, 200); mfRange.addIsotope(o, 100, 200); mfRange.addIsotope(n, 100, 200); double massMin = 50d; double massMax = 100d; MolecularFormulaGenerator gen = new MolecularFormulaGenerator(builder, massMin, massMax, mfRange); IMolecularFormulaSet mfSet = gen.getAllFormulas(); Assert.assertNotNull(mfSet); Assert.assertEquals(0, mfSet.size()); }