Skip to content

Commit edb39b0

Browse files
committed
Improved behaviour of badly specified isotopes, we can do a bit better than returning 0.
1 parent 8032dff commit edb39b0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

base/core/src/main/java/org/openscience/cdk/config/IsotopeFactory.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public IIsotope getMajorIsotope(int elem) {
230230

231231
/**
232232
* Get the mass of the most abundant (major) isotope, if there is no
233-
* major isotopes 0 is returned.
233+
* major isotopes 2*elem is returned.
234234
*
235235
* @param elem the atomic number
236236
* @return the major isotope mass
@@ -239,11 +239,17 @@ public double getMajorIsotopeMass(int elem) {
239239
if (this.majorIsotope[elem] != null)
240240
return this.majorIsotope[elem].getExactMass();
241241
IIsotope major = getMajorIsotope(elem);
242-
return major != null ? major.getExactMass() : 0;
242+
if (major == null) {
243+
logger.warn("No major isotope for elem" + elem);
244+
return 2*elem;
245+
}
246+
return major.getExactMass();
243247
}
244248

245249
/**
246-
* Get the exact mass of a specified isotope for an atom.
250+
* Get the exact mass of a specified isotope for an atom. If there is no isotope with that
251+
* mass number then it is returned.
252+
*
247253
* @param atomicNumber atomic number
248254
* @param massNumber the mass number
249255
* @return the exact mass
@@ -255,7 +261,7 @@ public double getExactMass(Integer atomicNumber, Integer massNumber) {
255261
if (isotope.getMassNumber().equals(massNumber))
256262
return isotope.getExactMass();
257263
}
258-
return 0;
264+
return massNumber;
259265
}
260266

261267
private IIsotope clone(IIsotope isotope) {

base/test-standard/src/test/java/org/openscience/cdk/tools/manipulator/AtomContainerManipulatorTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,19 @@ static void assertAddH(String smiIn, String smiExp) throws Exception {
13871387
closeTo(4729.147, 0.001));
13881388
}
13891389

1390+
@Test public void getMassBadIsotopes() throws InvalidSmilesException {
1391+
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
1392+
IAtomContainer mol = new SmilesParser(bldr).parseSmiles("[24cH]1[24cH][24cH][24cH][24cH][24cH]1");
1393+
assertThat(AtomContainerManipulator.getMass(mol, MolWeight),
1394+
closeTo(150.0476, 0.001));
1395+
assertThat(AtomContainerManipulator.getMass(mol, MolWeightIgnoreSpecified),
1396+
closeTo(78.1120, 0.001));
1397+
assertThat(AtomContainerManipulator.getMass(mol, MonoIsotopic),
1398+
closeTo(150.0469, 0.001));
1399+
assertThat(AtomContainerManipulator.getMass(mol, MostAbundant),
1400+
closeTo(150.0469, 0.001));
1401+
}
1402+
13901403
@Test public void getMassCranbinSpecIsotopes() {
13911404
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
13921405
IAtomContainer mol =

0 commit comments

Comments
 (0)