MassToFormulaTool { public IMolecularFormulaSet generate(double mass) { if (mass <= 0.0) { logger.error("Proposed mass is not valid: ", mass); return null; } IMolecularFormula minimalMF = MolecularFormulaRangeManipulator.getMinimalFormula(mfRange, builder); IMolecularFormula maximalMF = MolecularFormulaRangeManipulator.getMaximalFormula(mfRange, builder); double massMim = MolecularFormulaManipulator.getTotalExactMass(minimalMF) - tolerance; double massMap = MolecularFormulaManipulator.getTotalExactMass(maximalMF) + tolerance; if (massMim > mass || massMap < mass) { logger.error("Proposed mass is out of the range: ", mass); return null; } IMolecularFormulaSet molecularFormulaSet = builder.newInstance(IMolecularFormulaSet.class); int[][] matrix = this.matrix_Base; int numberElements = mfRange.getIsotopeCount(); List<IIsotope> isotopes_TO = new ArrayList<IIsotope>(); Iterator<IIsotope> isIt = mfRange.isotopes().iterator(); while (isIt.hasNext()) isotopes_TO.add(isIt.next()); isotopes_TO = orderList(isotopes_TO); for (int i = 0; i < matrix.length; i++) { int[] value_In = new int[numberElements]; for (int j = 0; j < numberElements; j++) { if (matrix[i][j] == 0) value_In[j] = 0; else value_In[j] = 1; } int count_E = 0; ArrayList<Integer> elem_Pos = new ArrayList<Integer>(); for (int j = 0; j < matrix[1].length; j++) if (value_In[j] != 0) { count_E++; elem_Pos.add(j); } boolean flag = true; int possChan = 0; String lastMFString = ""; while (flag) { boolean flagBreak = false; for (int j = 0; j < matrix[1].length; j++) { int min = mfRange.getIsotopeCountMin(isotopes_TO.get(j)); if (value_In[j] == 0) if (min != 0) flagBreak = true; } if (flagBreak) break; int occurence = getMaxOccurence(mass, elem_Pos.get(possChan).intValue(), value_In, isotopes_TO); if (occurence == 0) break; int maxx = mfRange.getIsotopeCountMax(isotopes_TO.get(elem_Pos.get(possChan).intValue())); int minn = mfRange.getIsotopeCountMin(isotopes_TO.get(elem_Pos.get(possChan).intValue())); if (occurence < minn | maxx < occurence) { if (possChan < elem_Pos.size() - 1) { if (maxx < occurence) value_In[elem_Pos.get(possChan).intValue()] = maxx; possChan++; } else { boolean foundZ = false; for (int z = possChan - 1; z >= 0; z--) { if (value_In[elem_Pos.get(z).intValue()] != 1) { possChan = z; foundZ = true; int newValue = value_In[elem_Pos.get(possChan).intValue()] - 1; value_In[elem_Pos.get(possChan).intValue()] = newValue; for (int j = possChan + 1; j < elem_Pos.size(); j++) { int p = elem_Pos.get(j).intValue(); value_In[p] = 1; } possChan++; break; } } if (!foundZ) break; } continue; } value_In[elem_Pos.get(possChan).intValue()] = occurence; double massT = calculateMassT(isotopes_TO, value_In); double diff_new = Math.abs(mass - (massT)); if (diff_new < tolerance) { IMolecularFormula myMF = getFormula(isotopes_TO, value_In); String newMFString = MolecularFormulaManipulator.getString(myMF); if (!newMFString.equals(lastMFString)) { molecularFormulaSet.addMolecularFormula(myMF); lastMFString = newMFString; } } if (count_E == 1) break; if (possChan < elem_Pos.size() - 1) { possChan++; } else { boolean foundZ = false; for (int z = possChan - 1; z >= 0; z--) { if (value_In[elem_Pos.get(z).intValue()] != 1) { possChan = z; foundZ = true; int newValue = value_In[elem_Pos.get(possChan).intValue()] - 1; value_In[elem_Pos.get(possChan).intValue()] = newValue; for (int j = possChan + 1; j < elem_Pos.size(); j++) { int p = elem_Pos.get(j).intValue(); value_In[p] = 1; } possChan++; break; } } if (!foundZ) break; } } } return returnOrdered(mass, molecularFormulaSet); } MassToFormulaTool(IChemObjectBuilder builder); void setRestrictions(List<IRule> rulesNew); List<IRule> getRestrictions(); void setDefaultRestrictions(); IMolecularFormulaSet generate(double mass);  }
@Test public void testMass_0Null() { Assert.assertNull(new MassToFormulaTool(builder).generate(0.0)); }
@Test public void testMass_NegativeNULL() { Assert.assertNull(new MassToFormulaTool(builder).generate(-10.0)); }
@Test public void testGenerate_double() { Assert.assertNotNull(new MassToFormulaTool(builder).generate(44.0032)); }
@Test public void testValidation_Orthinine() { IIsotope carb = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope oxy = ifac.getMajorIsotope("O"); IIsotope nit = ifac.getMajorIsotope("N"); IMolecularFormula mf1 = new MolecularFormula(); mf1.addIsotope(carb, 3); mf1.addIsotope(h, 11); mf1.addIsotope(oxy, 1); mf1.addIsotope(nit, 5); IMolecularFormula mf2 = new MolecularFormula(); mf2.addIsotope(carb, 5); mf2.addIsotope(h, 13); mf2.addIsotope(oxy, 2); mf2.addIsotope(nit, 2); IMolecularFormulaSet mfSet = new MassToFormulaTool(builder).generate(133.0968); Assert.assertEquals(37, mfSet.size()); Assert.assertEquals(MolecularFormulaManipulator.getAtomCount(mf1), MolecularFormulaManipulator.getAtomCount(mfSet.getMolecularFormula(0))); Assert.assertEquals(mf1.getIsotopeCount(), mfSet.getMolecularFormula(0).getIsotopeCount()); Assert.assertEquals(mf1.getIsotopeCount(carb), mfSet.getMolecularFormula(0).getIsotopeCount(carb)); Assert.assertEquals(mf1.getIsotopeCount(ifac.getMajorIsotope("N")), mfSet.getMolecularFormula(0) .getIsotopeCount(ifac.getMajorIsotope("N"))); Assert.assertEquals(MolecularFormulaManipulator.getAtomCount(mf2), MolecularFormulaManipulator.getAtomCount(mfSet.getMolecularFormula(1))); Assert.assertEquals(mf2.getIsotopeCount(), mfSet.getMolecularFormula(1).getIsotopeCount()); Assert.assertEquals(mf2.getIsotopeCount(ifac.getMajorIsotope("C")), mfSet.getMolecularFormula(1) .getIsotopeCount(ifac.getMajorIsotope("C"))); Assert.assertEquals(mf2.getIsotopeCount(ifac.getMajorIsotope("N")), mfSet.getMolecularFormula(1) .getIsotopeCount(ifac.getMajorIsotope("N"))); }