Skip to content

Commit

Permalink
Consider implicit hydrogen counts in getNaturalExactMass.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Apr 6, 2014
1 parent 8f48540 commit 7433ac1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Expand Up @@ -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
*
Expand Down
Expand Up @@ -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);

Expand Down

0 comments on commit 7433ac1

Please sign in to comment.