Skip to content

Commit

Permalink
Two convenience methods to make dealing with BODR data a lot easier
Browse files Browse the repository at this point in the history
Change-Id: I1c29e2d1b412a898bf01ecc2a4d0bb3a145249cd
Signed-off-by: John May <john.wilkinsonmay@gmail.com>
  • Loading branch information
egonw authored and johnmay committed Aug 2, 2013
1 parent d5b5947 commit c4c63dd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/org/openscience/cdk/config/IsotopeFactory.java
Expand Up @@ -169,6 +169,51 @@ public IIsotope[] getIsotopes(String symbol) {
return list.toArray(new IIsotope[list.size()]);
}

/**
* Gets a array of all isotopes known to the IsotopeFactory.
*
* @return An array of all isotopes
*/
@TestMethod("testGetIsotopes")
public IIsotope[] getIsotopes() {
ArrayList<IIsotope> list = new ArrayList<IIsotope>();
for (IIsotope isotope : isotopes) {
try {
IIsotope clone = (IIsotope) isotope.clone();
list.add(clone);
} catch (CloneNotSupportedException e) {
logger.error("Could not clone IIsotope: ", e.getMessage());
logger.debug(e);
}
}
return list.toArray(new IIsotope[list.size()]);
}

/**
* Gets an array of all isotopes matching the searched exact mass within
* a certain difference.
*
* @param exactMass search mass
* @param difference mass the isotope is allowed to differ from the search mass
* @return An array of all isotopes
*/
@TestMethod("testGetIsotopes_double_double")
public IIsotope[] getIsotopes(double exactMass, double difference) {
ArrayList<IIsotope> list = new ArrayList<IIsotope>();
for (IIsotope isotope : isotopes) {
if (Math.abs(isotope.getExactMass() - exactMass) <= difference) {
try {
IIsotope clone = (IIsotope) isotope.clone();
list.add(clone);
} catch (CloneNotSupportedException e) {
logger.error("Could not clone IIsotope: ", e.getMessage());
logger.debug(e);
}
}
}
return list.toArray(new IIsotope[list.size()]);
}

/**
* Get isotope based on element symbol and mass number.
*
Expand Down
19 changes: 19 additions & 0 deletions src/test/org/openscience/cdk/config/IsotopeFactoryTest.java
Expand Up @@ -157,6 +157,25 @@ public void testGetIsotopes_String() throws Exception {
Assert.assertEquals(8, list.length);
}

@Test
public void testGetIsotopes() throws Exception {
IsotopeFactory isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder());
IIsotope[] list = isofac.getIsotopes();
Assert.assertTrue(list.length > 200);
}

@Test
public void testGetIsotopes_double_double() throws Exception {
IsotopeFactory isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder());
IIsotope[] list = isofac.getIsotopes(87.90, 0.01);
// should return:
// Isotope match: 88Sr has mass 87.9056121
// Isotope match: 88Y has mass 87.9095011
Assert.assertEquals(2, list.length);
Assert.assertEquals(88, list[0].getMassNumber().intValue());
Assert.assertEquals(88, list[1].getMassNumber().intValue());
}

@Test
public void testIsElement_String() throws Exception {
IsotopeFactory isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder());
Expand Down

0 comments on commit c4c63dd

Please sign in to comment.