Skip to content

Commit 6154028

Browse files
committed
Utility methods in the isotope factory.
1 parent d58c0cd commit 6154028

File tree

1 file changed

+67
-15
lines changed

1 file changed

+67
-15
lines changed

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,28 @@ protected void add(IIsotope isotope) {
8181
* Gets an array of all isotopes known to the IsotopeFactory for the given
8282
* element symbol.
8383
*
84-
*@param symbol An element symbol to search for
85-
*@return An array of isotopes that matches the given element symbol
84+
* @param elem atomic number
85+
* @return An array of isotopes that matches the given element symbol
8686
*/
87-
public IIsotope[] getIsotopes(String symbol) {
88-
final int elem = Elements.ofString(symbol).number();
87+
public IIsotope[] getIsotopes(int elem) {
8988
if (isotopes[elem] == null)
9089
return EMPTY_ISOTOPE_ARRAY;
91-
List<IIsotope> list = new ArrayList<IIsotope>();
90+
List<IIsotope> list = new ArrayList<>();
9291
for (IIsotope isotope : isotopes[elem]) {
9392
list.add(clone(isotope));
9493
}
95-
return list.toArray(new IIsotope[list.size()]);
94+
return list.toArray(new IIsotope[0]);
95+
}
96+
97+
/**
98+
* Gets an array of all isotopes known to the IsotopeFactory for the given
99+
* element symbol.
100+
*
101+
* @param symbol An element symbol to search for
102+
* @return An array of isotopes that matches the given element symbol
103+
*/
104+
public IIsotope[] getIsotopes(String symbol) {
105+
return getIsotopes(Elements.ofString(symbol).number());
96106
}
97107

98108
/**
@@ -210,6 +220,36 @@ public IIsotope getMajorIsotope(int elem) {
210220
return clone(major);
211221
}
212222

223+
/**
224+
* Get the mass of the most abundant (major) isotope, if there is no
225+
* major isotopes 0 is returned.
226+
*
227+
* @param elem the atomic number
228+
* @return the major isotope mass
229+
*/
230+
public double getMajorIsotopeMass(int elem) {
231+
if (this.majorIsotope[elem] != null)
232+
return this.majorIsotope[elem].getExactMass();
233+
IIsotope major = getMajorIsotope(elem);
234+
return major != null ? major.getExactMass() : 0;
235+
}
236+
237+
/**
238+
* Get the exact mass of a specified isotope for an atom.
239+
* @param atomicNumber atomic number
240+
* @param massNumber the mass number
241+
* @return the exact mass
242+
*/
243+
public double getExactMass(Integer atomicNumber, Integer massNumber) {
244+
if (atomicNumber == null || massNumber == null)
245+
return 0;
246+
for (IIsotope isotope : this.isotopes[atomicNumber]) {
247+
if (isotope.getMassNumber().equals(massNumber))
248+
return isotope.getExactMass();
249+
}
250+
return 0;
251+
}
252+
213253
private IIsotope clone(IIsotope isotope) {
214254
if (isotope == null)
215255
return null;
@@ -320,23 +360,35 @@ public void configureAtoms(IAtomContainer container) {
320360
}
321361

322362
/**
323-
* Gets the natural mass of this element, defined as average of masses of isotopes,
324-
* weighted by abundance.
363+
* Gets the natural mass of this element, defined as average of masses of
364+
* isotopes, weighted by abundance.
325365
*
326-
* @param element the element in question
327-
* @return The natural mass value
366+
* @param atomicNum the element in question
367+
* @return The natural mass value
328368
*/
329-
public double getNaturalMass(IElement element) {
330-
IIsotope[] isotopes = getIsotopes(element.getSymbol());
369+
public double getNaturalMass(int atomicNum) {
370+
List<IIsotope> isotopes = this.isotopes[atomicNum];
371+
if (isotopes == null)
372+
return 0;
331373
double summedAbundances = 0;
332374
double summedWeightedAbundances = 0;
333375
double getNaturalMass = 0;
334-
for (int i = 0; i < isotopes.length; i++) {
335-
summedAbundances += isotopes[i].getNaturalAbundance();
336-
summedWeightedAbundances += isotopes[i].getNaturalAbundance() * isotopes[i].getExactMass();
376+
for (IIsotope isotope : isotopes) {
377+
summedAbundances += isotope.getNaturalAbundance();
378+
summedWeightedAbundances += isotope.getNaturalAbundance() * isotope.getExactMass();
337379
getNaturalMass = summedWeightedAbundances / summedAbundances;
338380
}
339381
return getNaturalMass;
340382
}
341383

384+
/**
385+
* Gets the natural mass of this element, defined as average of masses of
386+
* isotopes, weighted by abundance.
387+
*
388+
* @param element the element in question
389+
* @return The natural mass value
390+
*/
391+
public double getNaturalMass(IElement element) {
392+
return getNaturalMass(element.getAtomicNumber());
393+
}
342394
}

0 commit comments

Comments
 (0)