com.hfg.chem.Isotope Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.chem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringUtil;
import com.hfg.util.io.CSV;
import com.hfg.util.io.StreamUtil;
//------------------------------------------------------------------------------
/**
Isotope. Data from NIST.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class Isotope extends Element
{
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private Element mElement;
private int mMassNum; // The number of protons plus the number of neutrons
private Double mIsotopicComposition;
// This declaration has to come before the public constants below.
private static Map> sValueMap = new HashMap<>();
static
{
init();
}
//##########################################################################
// PUBLIC FIELDS
//##########################################################################
/* Now loading data from a resource file instead of defining constants. JAT 2017-11-03
public static final Isotope HYDROGEN_1 = new Isotope("Hydrogen 1", Element.HYDROGEN, 1, 1.00782503223, 0.999885);
public static final Isotope DEUTERIUM = new Isotope("Deuterium", Element.HYDROGEN, 2, 2.01410177812, 0.000115);
public static final Isotope TRITIUM = new Isotope("Tritium", Element.HYDROGEN, 3, 3.0160492779);
public static final Isotope HELIUM_3 = new Isotope("Helium 3", Element.HELIUM, 3, 3.0160293201, 0.00000134);
public static final Isotope HELIUM_4 = new Isotope("Helium 4", Element.HELIUM, 4, 4.00260325413, 0.99999866);
public static final Isotope CARBON_13 = new Isotope("Carbon 13", Element.CARBON, 13, 13.0033548378);
public static final Isotope NITROGEN_15 = new Isotope("Nitrogen 15", Element.NITROGEN, 15, 15.0001088984);
public static final Isotope OXYGEN_18 = new Isotope("Oxygen 18", Element.OXYGEN, 18, 17.9991604);
*/
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
private Isotope(String inName, Element inElement, int inMassNum,
double inMonoMass, double inIsotopicComposition)
{
this(inName, inElement, inMassNum, inMonoMass);
setIsotopicComposition(inIsotopicComposition);
}
//--------------------------------------------------------------------------
private Isotope(String inName, Element inElement, int inMassNum,
double inMonoMass)
{
super(inName, inMassNum + inElement.getSymbol(), inElement.getAtomicNum(), inMonoMass, inMonoMass);
mElement = inElement;
mMassNum = inMassNum;
Map isotopeMap = sValueMap.get(inElement);
if (null == isotopeMap)
{
isotopeMap = new HashMap<>(5);
sValueMap.put(inElement, isotopeMap);
}
isotopeMap.put(inMassNum, this);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public static Map valuesForElement(Element inElement)
{
return sValueMap.get(inElement);
}
//--------------------------------------------------------------------------
public static Isotope valueOf(Element inElement, int inMassNum)
{
return sValueMap.get(inElement).get(inMassNum);
}
//--------------------------------------------------------------------------
public Element getElement()
{
return mElement;
}
//--------------------------------------------------------------------------
/**
Returns the mass number (the number of protons plus neutrons) of the isotope.
@return the mass number (the number of protons plus neutrons) of the isotope
*/
public int getMassNumber()
{
return mMassNum;
}
//--------------------------------------------------------------------------
@Override
public String getSymbol()
{
return getMassNumber() + getElement().getSymbol();
}
//--------------------------------------------------------------------------
/**
Specifies the isotopic composition (mole fraction) for the isotope.
* @return this Isotope object to enable method chaining
*/
private Isotope setIsotopicComposition(double inValue)
{
mIsotopicComposition = inValue;
return this;
}
//--------------------------------------------------------------------------
/**
Specifies the isotopic composition (mole fraction) for the isotope.
* @return the isotopic composition (mole fraction) for the isotope
*/
public Double getIsotopicComposition()
{
return mIsotopicComposition;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private static void init()
{
Pattern noDataLinePattern = Pattern.compile("[\\s\u00A0,]+");
String rsrc = "rsrc/isotope_data.csv";
InputStream stream = Isotope.class.getResourceAsStream(rsrc);
if (null == stream)
{
throw new ProgrammingException("The rsrc " + rsrc + " couldn't be found!?");
}
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(stream));
String line;
int lineNum = 0;
Element currentElement = null;
while ((line = reader.readLine()) != null)
{
lineNum++;
if (!StringUtil.isSet(line) // Skip blank lines
|| 1 == lineNum // Skip the header line
|| line.startsWith("#") // Skip comment lines
|| noDataLinePattern.matcher(line).matches()) // Skip lines with all empty fields
{
continue;
}
String[] fields = CSV.parseLine(line);
if (StringUtil.isSet(fields[0]))
{
currentElement = Element.valueOf(Integer.parseInt(fields[0].trim()));
}
String monoMassString = StringUtil.removeWhitespace(fields[3]);
int uncertaintyIndex = monoMassString.indexOf("(");
if (uncertaintyIndex > 0)
{
monoMassString = monoMassString.substring(0, uncertaintyIndex);
}
String isotopicCompostionString = StringUtil.removeWhitespace(fields[3]);
if (StringUtil.isSet(isotopicCompostionString))
{
uncertaintyIndex = isotopicCompostionString.indexOf("(");
if (uncertaintyIndex > 0)
{
isotopicCompostionString = isotopicCompostionString.substring(0, uncertaintyIndex);
}
new Isotope(currentElement.getName() + " " + fields[2], currentElement, Integer.parseInt(fields[2]), Double.parseDouble(monoMassString), Double.parseDouble(isotopicCompostionString));
}
else
{
new Isotope(currentElement.getName() + " " + fields[2], currentElement, Integer.parseInt(fields[2]), Double.parseDouble(monoMassString));
}
}
}
catch (IOException e)
{
throw new RuntimeException("Problem initializing isotope data!", e);
}
finally
{
StreamUtil.close(reader);
}
}
}