Skip to content

Commit

Permalink
Only show non-null isotope labels and don't append digit of monoions.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Nov 11, 2015
1 parent 948da17 commit 38dadf5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Expand Up @@ -359,33 +359,35 @@ public static String getHTML(IMolecularFormula formula, boolean chargeB, boolean
/**
* 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 charge of IMolecularFormula
* 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 chargeB True, If it has to show the charge
* @param isotopeB True, If it has to show the Isotope mass
* @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 chargeB, boolean isotopeB) {
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 (!isotopeB) {
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)) {
sb.append("<sup>").append(isotope.getMassNumber()).append("</sup>");
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) {
Expand All @@ -396,14 +398,19 @@ public static String getHTML(IMolecularFormula formula, String[] orderElements,
}
}

if (chargeB) {
if (showCharge) {
Integer charge = formula.getCharge();
if (charge == CDKConstants.UNSET || charge == 0) {
return sb.toString();
} else if (charge < 0) {
sb.append("<sup>").append(charge * -1).append('-').append("</sup>");
} else {
sb.append("<sup>").append(charge).append('+').append("</sup>");
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();
Expand Down
Expand Up @@ -571,9 +571,21 @@ public void testGetHTML_IMolecularFormula_boolean_boolean() {

Assert.assertEquals("C<sub>10</sub>", MolecularFormulaManipulator.getHTML(formula, true, false));
formula.setCharge(1);
Assert.assertEquals("C<sub>10</sub><sup>1+</sup>", MolecularFormulaManipulator.getHTML(formula, true, false));
Assert.assertEquals("C<sub>10</sub><sup>+</sup>", MolecularFormulaManipulator.getHTML(formula, true, false));
formula.setCharge(formula.getCharge() - 2);
Assert.assertEquals("C<sub>10</sub><sup>1-</sup>", MolecularFormulaManipulator.getHTML(formula, true, false));
Assert.assertEquals("C<sub>10</sub><sup>–</sup>", MolecularFormulaManipulator.getHTML(formula, true, false));
}

@Test
public void nullIsotopeLabels() {
MolecularFormula formula = new MolecularFormula();
formula.addIsotope(builder.newInstance(IIsotope.class, "C"), 10);

Assert.assertEquals("C<sub>10</sub>", MolecularFormulaManipulator.getHTML(formula, true, false));
formula.setCharge(1);
Assert.assertEquals("C<sub>10</sub><sup>+</sup>", MolecularFormulaManipulator.getHTML(formula, true, true));
formula.setCharge(formula.getCharge() - 2);
Assert.assertEquals("C<sub>10</sub><sup>–</sup>", MolecularFormulaManipulator.getHTML(formula, true, true));
}

@Test
Expand Down Expand Up @@ -605,7 +617,7 @@ public void testGetHTML_IMolecularFormulaWithIsotopeAndCharge() {
formula.addIsotope(ifac.getMajorIsotope("C"), 2);
formula.addIsotope(ifac.getMajorIsotope("H"), 6);
formula.setCharge(1);
Assert.assertEquals("<sup>12</sup>C<sub>2</sub><sup>1</sup>H<sub>6</sub><sup>1+</sup>",
Assert.assertEquals("<sup>12</sup>C<sub>2</sub><sup>1</sup>H<sub>6</sub><sup>+</sup>",
MolecularFormulaManipulator.getHTML(formula, true, true));
}

Expand Down

0 comments on commit 38dadf5

Please sign in to comment.