Skip to content

Commit

Permalink
Utility to methods to clear any mass numbers that are determined to t…
Browse files Browse the repository at this point in the history
…he be the major isotope.
  • Loading branch information
johnmay committed May 29, 2018
1 parent 21a0f5b commit 0b19b79
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
61 changes: 61 additions & 0 deletions base/core/src/main/java/org/openscience/cdk/config/Isotopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
import java.util.HashMap;
import java.util.List;

import com.google.common.collect.FluentIterable;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecularFormula;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.periodictable.PeriodicTable;

/**
Expand All @@ -51,6 +57,9 @@
*/
public class Isotopes extends IsotopeFactory {

private static final ILoggingTool logger =
LoggingToolFactory.createLoggingTool(Isotopes.class);

private static Isotopes myself = null;

/**
Expand Down Expand Up @@ -85,4 +94,56 @@ private Isotopes() throws IOException {
add(isotope);
}
}

private static boolean isMajor(IIsotope atom) {
Integer mass = atom.getMassNumber();
if (mass == null)
return false;
try {
Isotopes instance = Isotopes.getInstance();
IIsotope major = instance.getMajorIsotope(atom.getAtomicNumber());
if (major == null)
return false; // no major isotope
return major.getMassNumber().equals(mass);
} catch (IOException e) {
logger.error("Could not load Isotope data: ", e.getMessage());
return false;
}
}

/**
* Clear the isotope information from atoms that are major isotopes (e.g.
* 12C, 1H, etc).
* @param mol the molecule
*/
public static void clearMajorIsotopes(IAtomContainer mol) {
for (IAtom atom : mol.atoms())
if (isMajor(atom)) {
atom.setMassNumber(null);
atom.setExactMass(null);
atom.setNaturalAbundance(null);
}
}

/**
* Clear the isotope information from istopes that are major (e.g.
* 12C, 1H, etc).
* @param formula the formula
*/
public static void clearMajorIsotopes(IMolecularFormula formula) {
for (IIsotope iso : FluentIterable.from(formula.isotopes()).toList())
if (isMajor(iso)) {
int count = formula.getIsotopeCount(iso);
formula.removeIsotope(iso);
iso.setMassNumber(null);
// may be immutable
if (iso.getMassNumber() != null) {
iso = formula.getBuilder().newInstance(IIsotope.class, iso.getSymbol());
iso.setAtomicNumber(iso.getAtomicNumber());
}
iso.setExactMass(null);
iso.setNaturalAbundance(null);
formula.addIsotope(iso, count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ private static void appendElement(StringBuilder sb, Integer mass, int elem, int
if (mass != null)
sb.append('[')
.append(mass)
.append(Elements.ofNumber(elem).symbol())
.append(']');
.append(']')
.append(Elements.ofNumber(elem).symbol());
else
sb.append(Elements.ofNumber(elem).symbol());
if (count != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,23 @@ public void testImplicitH() throws Exception {
mf.addIsotope(ifac.getIsotope("Br", 81), 1);

assertThat(MolecularFormulaManipulator.getString(mf, false, false), is("C7H3Br2O3"));
assertThat(MolecularFormulaManipulator.getString(mf, false, true), is("C7H3[81Br]BrO3"));
assertThat(MolecularFormulaManipulator.getString(mf, false, true), is("C7H3[81]BrBrO3"));
}

@Test
public void testMassNumberDisplayWithDefinedIsotopes() throws Exception {
IsotopeFactory ifac = Isotopes.getInstance();

IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
IMolecularFormula mf = bldr.newInstance(IMolecularFormula.class);

mf.addIsotope(ifac.getMajorIsotope("C"), 7);
mf.addIsotope(ifac.getMajorIsotope("O"), 3);
mf.addIsotope(ifac.getMajorIsotope("H"), 3);
mf.addIsotope(ifac.getMajorIsotope("Br"), 1);
mf.addIsotope(ifac.getIsotope("Br", 81), 1);
Isotopes.clearMajorIsotopes(mf);
assertThat(MolecularFormulaManipulator.getString(mf, false, false), is("C7H3Br2O3"));
assertThat(MolecularFormulaManipulator.getString(mf, false, true), is("C7H3Br[81]BrO3"));
}
}

0 comments on commit 0b19b79

Please sign in to comment.