/** * In the minimal IMolecularFormula must contain all those IElement found in the * minimal IMolecularFormula. * * @param formulaMax A IMolecularFormula which contains the maximal representation of the Elements * @param formulaMin A IMolecularFormula which contains the minimal representation of the Elements * @return True, if the correlation is valid */ private static boolean validCorrelation(IMolecularFormula formulaMin, IMolecularFormula formulamax) { for (IElement element : MolecularFormulaManipulator.elements(formulaMin)) { if (!MolecularFormulaManipulator.containsElement(formulamax, element)) return false; } return true; }
public static List<IIsotope> putInOrder(String[] orderElements, IMolecularFormula formula) { List<IIsotope> isotopesList = new ArrayList<IIsotope>(); for (String orderElement : orderElements) { IElement element = formula.getBuilder().newInstance(IElement.class, orderElement); if (containsElement(formula, element)) { List<IIsotope> isotopes = getIsotopes(formula, element); Collections.sort(isotopes, new Comparator<IIsotope>() { @Override public int compare(IIsotope a, IIsotope b) { Integer aMass = a.getMassNumber(); Integer bMass = b.getMassNumber(); if (aMass == null) return -1; if (bMass == null) return +1; return aMass.compareTo(bMass); } }); isotopesList.addAll(isotopes); } } return isotopesList; }
for (String orderElement : orderElements) { IElement element = formula.getBuilder().newInstance(IElement.class, orderElement); if (containsElement(formula, element)) { if (!showIsotopes) { sb.append(element.getSymbol());
/** * Put in order the elements of the molecular formula. * * @param formula The IMolecularFormula to put in order * @return IMolecularFormula object */ private IMolecularFormula putInOrder(IMolecularFormula formula) { IMolecularFormula new_formula = formula.getBuilder().newInstance(IMolecularFormula.class); for (int i = 0; i < orderElements.length; i++) { IElement element = builder.newInstance(IElement.class, orderElements[i]); if (MolecularFormulaManipulator.containsElement(formula, element)) { Iterator<IIsotope> isotopes = MolecularFormulaManipulator.getIsotopes(formula, element).iterator(); while (isotopes.hasNext()) { IIsotope isotope = isotopes.next(); new_formula.addIsotope(isotope, formula.getIsotopeCount(isotope)); } } } // new_mf.setCharge(charge); return new_formula; }
/** * Returns the string representation of the molecule formula based on Hill * System with numbers wrapped in <sub></sub> tags and the * isotope of each Element in <sup></sup> tags and the total * charge of IMolecularFormula in <sup></sup> tags. Useful for * displaying formulae in Swing components or on the web. * * * @param formula The IMolecularFormula object * @param chargeB True, If it has to show the charge * @param isotopeB True, If it has to show the Isotope mass * @return A HTML representation of the molecular formula * @see #getHTML(IMolecularFormula) * */ public static String getHTML(IMolecularFormula formula, boolean chargeB, boolean isotopeB) { String[] orderElements; if (containsElement(formula, formula.getBuilder().newInstance(IElement.class, "C"))) orderElements = generateOrderEle_Hill_WithCarbons(); else orderElements = generateOrderEle_Hill_NoCarbons(); return getHTML(formula, orderElements, chargeB, isotopeB); }
/** * Returns the string representation of the molecule formula. * Based on Hill System. The Hill system is a system of writing * chemical formulas such that the number of carbon atoms in a * molecule is indicated first, the number of hydrogen atoms next, * and then the number of all other chemical elements subsequently, * in alphabetical order. When the formula contains no carbon, all * the elements, including hydrogen, are listed alphabetically. * * @param formula The IMolecularFormula Object * @param setOne True, when must be set the value 1 for elements with * one atom * @return A String containing the molecular formula * * @see #getHTML(IMolecularFormula) */ public static String getString(IMolecularFormula formula, boolean setOne) { if (containsElement(formula, formula.getBuilder().newInstance(IElement.class, "C"))) return getString(formula, generateOrderEle_Hill_WithCarbons(), setOne, false); else return getString(formula, generateOrderEle_Hill_NoCarbons(), setOne, false); }
/** * Returns the string representation of the molecule formula. * Based on Hill System. The Hill system is a system of writing * chemical formulas such that the number of carbon atoms in a * molecule is indicated first, the number of hydrogen atoms next, * and then the number of all other chemical elements subsequently, * in alphabetical order. When the formula contains no carbon, all * the elements, including hydrogen, are listed alphabetically. * * @param formula The IMolecularFormula Object * @param setOne True, when must be set the value 1 for elements with * one atom * @param setMassNumber If the formula contains an isotope of an element that is the * non-major isotope, the element is represented as <code>[XE]</code> where * <code>X</code> is the mass number and <code>E</code> is the element symbol * @return A String containing the molecular formula * * @see #getHTML(IMolecularFormula) */ public static String getString(IMolecularFormula formula, boolean setOne, boolean setMassNumber) { if (containsElement(formula, formula.getBuilder().newInstance(IElement.class, "C"))) return getString(formula, generateOrderEle_Hill_WithCarbons(), setOne, setMassNumber); else return getString(formula, generateOrderEle_Hill_NoCarbons(), setOne, setMassNumber); }
@Test public void testContainsElement_IMolecularFormula_IElement() { IMolecularFormula formula = new MolecularFormula(); IIsotope carb = builder.newInstance(IIsotope.class, "C"); IIsotope flu = builder.newInstance(IIsotope.class, "F"); IIsotope h1 = builder.newInstance(IIsotope.class, "H"); IIsotope h2 = builder.newInstance(IIsotope.class, "H"); h2.setExactMass(2.014101778); formula.addIsotope(carb, 1); formula.addIsotope(flu); formula.addIsotope(h1, 1); formula.addIsotope(h2, 2); Assert.assertTrue(MolecularFormulaManipulator.containsElement(formula, builder.newInstance(IElement.class, "C"))); Assert.assertTrue(MolecularFormulaManipulator.containsElement(formula, builder.newInstance(IElement.class, "H"))); Assert.assertTrue(MolecularFormulaManipulator.containsElement(formula, builder.newInstance(IElement.class, "F"))); }
/** * Extract from a set of MolecularFormula the maximum occurrence of each element found and * put the element and occurrence in a new IMolecularFormula. * * @param mfSet The set of molecularFormules to inspect * @return A IMolecularFormula containing the maximum occurrence of the elements * @see #getMinOccurrenceElements(IMolecularFormulaSet) */ public static IMolecularFormula getMaxOccurrenceElements(IMolecularFormulaSet mfSet) { IMolecularFormula molecularFormula = mfSet.getBuilder().newInstance(IMolecularFormula.class); for (IMolecularFormula mf : mfSet.molecularFormulas()) { for (IIsotope isotope : mf.isotopes()) { IElement element = mfSet.getBuilder().newInstance(IElement.class, isotope); int occur_new = MolecularFormulaManipulator.getElementCount(mf, element); if (!MolecularFormulaManipulator.containsElement(molecularFormula, element)) { molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } else { int occur_old = MolecularFormulaManipulator.getElementCount(molecularFormula, element); if (occur_new > occur_old) { MolecularFormulaManipulator.removeElement(molecularFormula, element); molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } } } } return molecularFormula; }
/** * Extract from a set of MolecularFormula the minimal occurrence of each element found and * put the element and occurrence in a new IMolecularFormula. * * @param mfSet The set of molecularFormules to inspect * @return A IMolecularFormula containing the minimal occurrence of the elements * @see #getMaxOccurrenceElements(IMolecularFormulaSet) */ public static IMolecularFormula getMinOccurrenceElements(IMolecularFormulaSet mfSet) { IMolecularFormula molecularFormula = mfSet.getBuilder().newInstance(IMolecularFormula.class); for (IMolecularFormula mf : mfSet.molecularFormulas()) { for (IIsotope isotope : mf.isotopes()) { IElement element = mfSet.getBuilder().newInstance(IElement.class, isotope); int occur_new = MolecularFormulaManipulator.getElementCount(mf, element); if (!MolecularFormulaManipulator.containsElement(molecularFormula, element)) { molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } else { int occur_old = MolecularFormulaManipulator.getElementCount(molecularFormula, element); if (occur_new < occur_old) { MolecularFormulaManipulator.removeElement(molecularFormula, element); molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } } } } return molecularFormula; }