All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.chem.Isotope Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
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(); } //########################################################################## // 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[4]); 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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy