/** * Returns a set of nodes excluding all the hydrogens. * * @param formula The IMolecularFormula * @return The heavyElements value into a List * @cdk.keyword hydrogen, removal */ public static List<IElement> getHeavyElements(IMolecularFormula formula) { List<IElement> newEle = new ArrayList<IElement>(); for (IElement element : elements(formula)) { if (!element.getSymbol().equals("H")) { newEle.add(element); } } return newEle; }
/** * True, if the MolecularFormula contains the given element as IIsotope object. * * @param formula IMolecularFormula molecularFormula * @param element The element this MolecularFormula is searched for * @return True, if the MolecularFormula contains the given element object */ public static boolean containsElement(IMolecularFormula formula, IElement element) { for (IIsotope isotope : formula.isotopes()) { if (element.getSymbol().equals(isotope.getSymbol())) return true; } return false; }
/** * Get a list of IIsotope from a given IElement which is contained molecular. The search is based * only on the IElement. * * @param formula The MolecularFormula to check * @param element The IElement object * @return The list with the IIsotopes in this molecular formula */ public static List<IIsotope> getIsotopes(IMolecularFormula formula, IElement element) { List<IIsotope> isotopeList = new ArrayList<IIsotope>(); for (IIsotope isotope : formula.isotopes()) { if (isotope.getSymbol().equals(element.getSymbol())) isotopeList.add(isotope); } return isotopeList; }
/** * Checks a set of Nodes for the occurrence of the isotopes in the molecular formula from a * particular IElement. It returns 0 if the element does not exist. The search is based only on * the IElement. * * @param formula The MolecularFormula to check * @param element The IElement object * @return The occurrence of this element in this molecular formula */ public static int getElementCount(IMolecularFormula formula, IElement element) { int count = 0; for (IIsotope isotope : formula.isotopes()) { if (isotope.getSymbol().equals(element.getSymbol())) count += formula.getIsotopeCount(isotope); } return count; }
/** * Returns the string representation of the molecule formula with numbers wrapped in * <sub></sub> tags and the isotope of each Element in <sup></sup> tags * and the total showCharge of IMolecularFormula in <sup></sup> tags. Useful for * displaying formulae in Swing components or on the web. * * @param formula The IMolecularFormula object * @param orderElements The order of Elements * @param showCharge True, If it has to show the showCharge * @param showIsotopes 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, String[] orderElements, boolean showCharge, boolean showIsotopes) { StringBuilder sb = new StringBuilder(); for (String orderElement : orderElements) { IElement element = formula.getBuilder().newInstance(IElement.class, orderElement); if (containsElement(formula, element)) { if (!showIsotopes) { sb.append(element.getSymbol()); int n = getElementCount(formula, element); if (n > 1) { sb.append("<sub>").append(n).append("</sub>"); } } else { for (IIsotope isotope : getIsotopes(formula, element)) { Integer massNumber = isotope.getMassNumber(); if (massNumber != null) sb.append("<sup>").append(massNumber).append("</sup>"); sb.append(isotope.getSymbol()); int n = formula.getIsotopeCount(isotope); if (n > 1) { sb.append("<sub>").append(n).append("</sub>"); } } } } } if (showCharge) { Integer charge = formula.getCharge(); if (charge == CDKConstants.UNSET || charge == 0) { return sb.toString(); } else { sb.append("<sup>"); if (charge > 1 || charge < -1) sb.append(Math.abs(charge)); if (charge > 0) sb.append('+'); else sb.append(MINUS); // note, not a hyphen! sb.append("</sup>"); } } return sb.toString(); }
@Test public void testMassNumber() { IElement chemObject = new NaturalElement("C", 12); Assert.assertEquals(12, chemObject.getAtomicNumber().intValue()); }
@Test public void testSymbol() { IElement chemObject = new NaturalElement("C", 12); Assert.assertEquals("C", chemObject.getSymbol()); }