Skip to content
Browse files

Consider implicit hydrogen counts in getNaturalExactMass.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information...
1 parent 8f48540 commit 7433ac10e36c4d7d538dc559d0f83ca37c13628d @johnmay johnmay committed with egonw Apr 4, 2014
View
39 ...tandard/src/main/java/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java
@@ -204,27 +204,36 @@ public static double getTotalExactMass(IAtomContainer atomContainer) {
}
/**
- * Returns the molecular mass of the IAtomContainer. For the calculation it uses the
- * masses of the isotope mixture using natural abundances.
+ * Returns the molecular mass of the IAtomContainer. For the calculation it
+ * uses the masses of the isotope mixture using natural abundances.
*
- * @param atomContainer
+ * @param atomContainer
* @cdk.keyword mass, molecular
*/
@TestMethod("testGetNaturalExactMass_IAtomContainer")
- public static double getNaturalExactMass(IAtomContainer atomContainer) {
- double mass = 0.0;
- IsotopeFactory factory;
- try {
- factory = Isotopes.getInstance();
- } catch (IOException e) {
- throw new RuntimeException("Could not instantiate the IsotopeFactory.");
- }
- for (IAtom atom : atomContainer.atoms()) {
- IElement isotopesElement = atom.getBuilder().newInstance(IElement.class,atom.getSymbol());
- mass += factory.getNaturalMass(isotopesElement);
+ public static double getNaturalExactMass(IAtomContainer atomContainer) {
+ try {
+ Isotopes isotopes = Isotopes.getInstance();
+ double hydgrogenMass = isotopes.getNaturalMass(Elements.HYDROGEN);
+
+ double mass = 0.0;
+ for (final IAtom atom : atomContainer.atoms()) {
+
+ if (atom.getAtomicNumber() == null)
+ throw new IllegalArgumentException("an atom had with unknown (null) atomic number");
+ if (atom.getImplicitHydrogenCount() == null)
+ throw new IllegalArgumentException("an atom had with unknown (null) implicit hydrogens");
+
+ mass += isotopes.getNaturalMass(Elements.ofNumber(atom.getAtomicNumber()).toIElement());
+ mass += hydgrogenMass * atom.getImplicitHydrogenCount();
+ }
+ return mass;
+
+ } catch (IOException e) {
+ throw new RuntimeException("Isotopes definitions could not be loaded", e);
}
- return mass;
}
+
/**
* Get the summed natural abundance of all atoms in an AtomContainer
*
View
19 ...ard/src/test/java/org/openscience/cdk/tools/manipulator/AtomContainerManipulatorTest.java
@@ -530,16 +530,35 @@ private IAtomContainer getChiralMolTemplate() {
Assert.assertEquals(46.96885268,totalExactMass,0.000001);
}
+
+ @Test(expected = IllegalArgumentException.class)
+ public void getNaturalExactMassNeedsHydrogens() {
+ IAtomContainer mol = new AtomContainer();
+ mol.addAtom(new Atom("C"));
+ AtomContainerManipulator.getNaturalExactMass(mol);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void getNaturalExactMassNeedsAtomicNumber() {
+ IAtomContainer mol = new AtomContainer();
+ mol.addAtom(new Atom("C"));
+ mol.getAtom(0).setAtomicNumber(null);
+ AtomContainerManipulator.getNaturalExactMass(mol);
+ }
@Test public void testGetNaturalExactMass_IAtomContainer() throws Exception {
IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
IAtomContainer mol = builder.newInstance(IAtomContainer.class);
mol.addAtom(new Atom("C"));
mol.addAtom(new Atom("Cl"));
+
+ mol.getAtom(0).setImplicitHydrogenCount(4);
+ mol.getAtom(1).setImplicitHydrogenCount(1);
double expectedMass = 0.0;
expectedMass += Isotopes.getInstance().getNaturalMass(builder.newInstance(IElement.class,"C"));
expectedMass += Isotopes.getInstance().getNaturalMass(builder.newInstance(IElement.class,"Cl"));
+ expectedMass += 5 * Isotopes.getInstance().getNaturalMass(builder.newInstance(IElement.class,"H"));
double totalExactMass = AtomContainerManipulator.getNaturalExactMass(mol);

0 comments on commit 7433ac1

Please sign in to comment.
Something went wrong with that request. Please try again.