Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 06, 2014
@johnmay johnmay Consider implicit hydrogen counts in getNaturalExactMass.
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
7433ac1
@johnmay johnmay Consider implicit hydrogens in exact mass utility.
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
0e85729
@johnmay johnmay Consider implicit hydrogens in natural abundance utility.
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
9ee71fb
View
81 ...tandard/src/main/java/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java
@@ -196,35 +196,54 @@ public static double getTotalCharge(IAtomContainer atomContainer) {
*/
@TestMethod("testGetTotalExactMass_IAtomContainer")
public static double getTotalExactMass(IAtomContainer atomContainer) {
- double mass = 0.0;
- for (IAtom atom : atomContainer.atoms()) {
- mass += atom.getExactMass();
+ try {
+
+ Isotopes isotopes = Isotopes.getInstance();
+ double mass = 0.0;
+ double hExactMass = isotopes.getMajorIsotope(1).getExactMass();
+ for (IAtom atom : atomContainer.atoms()) {
+ if (atom.getImplicitHydrogenCount() == null)
+ throw new IllegalArgumentException("an atom had with unknown (null) implicit hydrogens");
+ mass += atom.getExactMass();
+ mass += atom.getImplicitHydrogenCount() * hExactMass;
+ }
+ return mass;
+ } catch (IOException e) {
+ throw new RuntimeException("Isotopes definitions could not be loaded", e);
}
- return mass;
}
/**
- * 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
*
@@ -233,11 +252,25 @@ public static double getNaturalExactMass(IAtomContainer atomContainer) {
*/
@TestMethod("testGetTotalNaturalAbundance_IAtomContainer")
public static double getTotalNaturalAbundance(IAtomContainer atomContainer) {
- double abundance = 1.0;
- for (IAtom iAtom : atomContainer.atoms()) abundance = abundance * iAtom.getNaturalAbundance();
-
-
- return abundance/Math.pow(100,atomContainer.getAtomCount());
+ try {
+ Isotopes isotopes = Isotopes.getInstance();
+ double abundance = 1.0;
+ double hAbundance = isotopes.getMajorIsotope(1).getNaturalAbundance();
+
+ int nImplH = 0;
+
+ for (IAtom atom : atomContainer.atoms()) {
+ if (atom.getImplicitHydrogenCount() == null)
+ throw new IllegalArgumentException("an atom had with unknown (null) implicit hydrogens");
+ abundance *= atom.getNaturalAbundance();
+ for (int h = 0; h < atom.getImplicitHydrogenCount(); h++)
+ abundance *= hAbundance;
+ nImplH += atom.getImplicitHydrogenCount();
+ }
+ return abundance / Math.pow(100, nImplH + atomContainer.getAtomCount());
+ } catch (IOException e) {
+ throw new RuntimeException("Isotopes definitions could not be loaded", e);
+ }
}
/**
View
23 ...ard/src/test/java/org/openscience/cdk/tools/manipulator/AtomContainerManipulatorTest.java
@@ -528,18 +528,37 @@ private IAtomContainer getChiralMolTemplate() {
mol.getAtom(1).setExactMass(34.96885268);
double totalExactMass = AtomContainerManipulator.getTotalExactMass(mol);
- Assert.assertEquals(46.96885268,totalExactMass,0.000001);
+ Assert.assertEquals(49.992327775,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);
@@ -558,7 +577,7 @@ private IAtomContainer getChiralMolTemplate() {
mol.getAtom(1).setNaturalAbundance(75.78);
double totalAbudance = AtomContainerManipulator.getTotalNaturalAbundance(mol);
- Assert.assertEquals(0.74969154,totalAbudance,0.000001);
+ Assert.assertEquals(0.749432,totalAbudance,0.000001);
}
/**

No commit comments for this range

Something went wrong with that request. Please try again.