Skip to content

Commit

Permalink
Round trip MF charge.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Oct 11, 2018
1 parent 156b407 commit 6819a15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public static String getString(IMolecularFormula formula, String[] orderElements
boolean setOne, boolean setMassNumber) {
StringBuilder stringMF = new StringBuilder();
List<IIsotope> isotopesList = putInOrder(orderElements, formula);
Integer q = formula.getCharge();

if (q != null && q != 0)
stringMF.append('[');

if (!setMassNumber) {
int count = 0;
Expand Down Expand Up @@ -258,6 +262,20 @@ public static String getString(IMolecularFormula formula, String[] orderElements
}
}


if (q != null && q != 0) {
stringMF.append(']');
if (q > 0) {
if (q > 1)
stringMF.append(q);
stringMF.append('+');
} else {
if (q < 1)
stringMF.append(-q);
stringMF.append('-');
}
}

return stringMF.toString();
}

Expand Down Expand Up @@ -539,7 +557,7 @@ private static IMolecularFormula getMolecularFormula(String stringMF, boolean as
private static final char MINUS = '–';
private static final String HYPHEN_STR = "-";
private static final String MINUS_STR = "–";

/**
* add in a instance of IMolecularFormula the elements extracts form
* molecular formula string. The string is immediately analyzed and a set of Nodes
Expand Down Expand Up @@ -707,7 +725,7 @@ private static String cleanMFfromCharge(String formula) {
}
return finalFormula;
}

/**
* Extract the charge given a molecular formula format [O3S]2-.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,4 +1363,13 @@ public void testMassNumberDisplayWithDefinedIsotopes() throws Exception {
assertThat(MolecularFormulaManipulator.getString(mf, false, true),
is("C7H3Br[81]BrO3"));
}

@Test
public void testRoundTripCharge() {
String f = "[C3H7]+";
IMolecularFormula m =
MolecularFormulaManipulator.getMolecularFormula(f,
SilentChemObjectBuilder.getInstance());
assertThat(MolecularFormulaManipulator.getString(m), is("[C3H7]+"));
}
}

0 comments on commit 6819a15

Please sign in to comment.