MolecularFormulaManipulator { public static String simplifyMolecularFormula(String formula) { String newFormula = formula; char thisChar; if (formula.contains(" ")) { newFormula = newFormula.replace(" ", ""); } if (!formula.contains(".")) return breakExtractor(formula); List<String> listMF = new ArrayList<String>(); while (newFormula.contains(".")) { int pos = newFormula.indexOf('.'); String thisFormula = newFormula.substring(0, pos); if (thisFormula.charAt(0) >= '0' && thisFormula.charAt(0) <= '9') thisFormula = multipleExtractor(thisFormula); if (thisFormula.contains("(")) thisFormula = breakExtractor(thisFormula); listMF.add(thisFormula); thisFormula = newFormula.substring(pos + 1, newFormula.length()); if (!thisFormula.contains(".")) { if (thisFormula.charAt(0) >= '0' && thisFormula.charAt(0) <= '9') thisFormula = multipleExtractor(thisFormula); if (thisFormula.contains("(")) thisFormula = breakExtractor(thisFormula); listMF.add(thisFormula); } newFormula = thisFormula; } if (newFormula.contains("(")) newFormula = breakExtractor(newFormula); String recentElementSymbol = ""; String recentElementCountString = "0"; List<String> eleSymb = new ArrayList<String>(); List<Integer> eleCount = new ArrayList<Integer>(); for (int i = 0; i < listMF.size(); i++) { String thisFormula = listMF.get(i); for (int f = 0; f < thisFormula.length(); f++) { thisChar = thisFormula.charAt(f); if (f < thisFormula.length()) { if (thisChar >= 'A' && thisChar <= 'Z') { recentElementSymbol = String.valueOf(thisChar); recentElementCountString = "0"; } if (thisChar >= 'a' && thisChar <= 'z') { recentElementSymbol += thisChar; } if (thisChar >= '0' && thisChar <= '9') { recentElementCountString += thisChar; } } if (f == thisFormula.length() - 1 || (thisFormula.charAt(f + 1) >= 'A' && thisFormula.charAt(f + 1) <= 'Z')) { int posit = eleSymb.indexOf(recentElementSymbol); int count = Integer.valueOf(recentElementCountString); if (posit == -1) { eleSymb.add(recentElementSymbol); eleCount.add(count); } else { int countP = Integer.valueOf(recentElementCountString); if (countP == 0) countP = 1; int countA = eleCount.get(posit); if (countA == 0) countA = 1; int value = countP + countA; eleCount.remove(posit); eleCount.add(posit, value); } } } } String newF = ""; for (int i = 0; i < eleCount.size(); i++) { String element = eleSymb.get(i); int num = eleCount.get(i); if (num == 0) newF += element; else newF += element + num; } return newF; }  static int getAtomCount(IMolecularFormula formula); static int getElementCount(IMolecularFormula formula, IElement element); static int getElementCount(IMolecularFormula formula, IIsotope isotope); static int getElementCount(IMolecularFormula formula, String symbol); static List<IIsotope> getIsotopes(IMolecularFormula formula, IElement element); static List<IElement> elements(IMolecularFormula formula); static boolean containsElement(IMolecularFormula formula, IElement element); static IMolecularFormula removeElement(IMolecularFormula formula, IElement element); static String getString(IMolecularFormula formula, String[] orderElements,
                                   boolean setOne); static String getString(IMolecularFormula formula, String[] orderElements,
                                   boolean setOne, boolean setMassNumber); static String getString(IMolecularFormula formula); static String getString(IMolecularFormula formula, boolean setOne); static String getString(IMolecularFormula formula, boolean setOne, boolean setMassNumber); static List<IIsotope> putInOrder(String[] orderElements, IMolecularFormula formula); @Deprecated static String getHillString(IMolecularFormula formula); static String getHTML(IMolecularFormula formula); static String getHTML(IMolecularFormula formula, boolean chargeB, boolean isotopeB); static String getHTML(IMolecularFormula formula, String[] orderElements, boolean showCharge, boolean showIsotopes); static IMolecularFormula getMolecularFormula(String stringMF, IChemObjectBuilder builder); static IMolecularFormula getMajorIsotopeMolecularFormula(String stringMF, IChemObjectBuilder builder); static IMolecularFormula getMolecularFormula(String stringMF, IMolecularFormula formula); @Deprecated static double getTotalExactMass(IMolecularFormula formula); static double getTotalMassNumber(IMolecularFormula formula); static double getMass(IMolecularFormula mf, int flav); static double getMass(IMolecularFormula mf); @Deprecated static double getNaturalExactMass(IMolecularFormula formula); @Deprecated static double getMajorIsotopeMass(IMolecularFormula formula); static double getTotalNaturalAbundance(IMolecularFormula formula); static double getDBE(IMolecularFormula formula); static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer); static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula); static IAtomContainer getAtomContainer(IMolecularFormula formula); static IAtomContainer getAtomContainer(IMolecularFormula formula, IAtomContainer atomContainer); static IAtomContainer getAtomContainer(String formulaString, IChemObjectBuilder builder); static String[] generateOrderEle(); static boolean compare(IMolecularFormula formula1, IMolecularFormula formula2); static List<IElement> getHeavyElements(IMolecularFormula formula); static String simplifyMolecularFormula(String formula); static boolean adjustProtonation(IMolecularFormula mf, int hcnt); static IMolecularFormula getMostAbundant(IMolecularFormula mf); static IMolecularFormula getMostAbundant(IAtomContainer mol); static final int MolWeight; static final int MolWeightIgnoreSpecified; static final int MonoIsotopic; static final int MostAbundant; static final Comparator<IIsotope> NAT_ABUN_COMP; }
@Test public void testSimplifyMolecularFormula_String() { String formula = "C1H41.H2O"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("C1H43O", simplifyMF); }
@Test public void testSimplifyMolecularFormula_String2() { String formula = "CH41.H2O"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("CH43O", simplifyMF); }
@Test public void testSpace() { String formula = "C17H21NO. C7H6O3"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("C24H27NO4", simplifyMF); }
@Test public void test0() { String formula = "Fe.(C6H11O7)3"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("FeC18H33O21", simplifyMF); }
@Test public void test1() { String formula = "(C6H11O7)3.Fe"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("C18H33O21Fe", simplifyMF); }
@Test public void test2() { String formula = "C14H14N2.2HCl"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("C14H16N2Cl2", simplifyMF); }
@Test public void test3() { String formula = "(C27H33N3O8)2.2HNO3.3H2O"; String simplifyMF = MolecularFormulaManipulator.simplifyMolecularFormula(formula); Assert.assertEquals("C54H74N8O25", simplifyMF); }