Skip to content

Commit

Permalink
return the matching isotope closest to the provided exact mass
Browse files Browse the repository at this point in the history
Change-Id: I842af6599bc25ebfab9b6f1f7a150973c8e253c3
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Mar 24, 2013
1 parent 8e9243e commit e69c387
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/org/openscience/cdk/config/IsotopeFactory.java
Expand Up @@ -196,26 +196,29 @@ public IIsotope getIsotope(String symbol, int massNumber) {
/**
* Get an isotope based on the element symbol and exact mass.
*
* @param symbol the element symbol
* @param massNumber the mass number
* @param symbol the element symbol
* @param exactMass the mass number
* @param tolerance allowed difference from provided exact mass
* @return the corresponding isotope
*/
@TestMethod("testGetIsotopeFromExactMass")
public IIsotope getIsotope(String symbol, double exactMass, double tolerance) {
IIsotope ret = null;
IIsotope ret = null;
double minDiff = Double.MAX_VALUE;
for (IIsotope isotope : isotopes) {
double diff = Math.abs(isotope.getExactMass() - exactMass);
if (isotope.getSymbol().equals(symbol) &&
Math.abs(isotope.getExactMass() - exactMass) <= tolerance) {
diff <= tolerance && diff < minDiff) {
try {
ret = (IIsotope) isotope.clone();
minDiff = diff;
} catch (CloneNotSupportedException e) {
logger.error("Could not clone IIsotope: ", e.getMessage());
logger.debug(e);
}
return ret;
}
}
return null;
return ret;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/org/openscience/cdk/config/IsotopeFactoryTest.java
Expand Up @@ -318,6 +318,14 @@ public void warning(SAXParseException arg0) throws SAXException {
Assert.assertNull(match);
}

@Test public void testGetIsotopeFromExactMass_LargeTolerance() throws Exception {
IsotopeFactory isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder());
IIsotope carbon13 = isofac.getIsotope("C", 13);
IIsotope match = isofac.getIsotope(carbon13.getSymbol(), carbon13.getExactMass(), 2.0);
Assert.assertNotNull(match);
Assert.assertEquals(13, match.getMassNumber().intValue());
}

/**
* @cdk.bug 3534288
*/
Expand Down

0 comments on commit e69c387

Please sign in to comment.