use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class FormulaUtils method createMajorIsotopeMolFormula.
/**
* Creates a formula with the major isotopes (important to use this method for exact mass
* calculation over the CDK version, which generates formulas without an exact mass)
*
* @param formula
* @return the formula or null
*/
public static IMolecularFormula createMajorIsotopeMolFormula(String formula) {
try {
// new formula consists of isotopes without exact mass
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
IMolecularFormula f = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula.replace(" ", ""), builder);
if (f == null)
return null;
// needed, as MolecularFormulaManipulator method returns isotopes without exact mass info
try {
return replaceAllIsotopesWithoutExactMass(f);
} catch (Exception e) {
logger.log(Level.SEVERE, "Cannot create formula for: " + formula, e);
return null;
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Cannot create formula for: " + formula, e);
return null;
}
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class FormulaUtils method checkMolecularFormula.
/**
* Checks if a formula string only contains valid isotopes/elements.
*
* @param formula String of the molecular formula.
* @return true / false
*/
public static boolean checkMolecularFormula(String formula) {
if (formula.matches(".*[äöüÄÖÜß°§$%&/()=?ß²³´`+*~'#;:<>|]")) {
// check for this first
logger.info("Formula contains illegal characters.");
return false;
}
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
IMolecularFormula molFormula;
molFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula, builder);
boolean valid = true;
for (IIsotope iso : molFormula.isotopes()) {
if ((iso.getAtomicNumber() == null) || (iso.getAtomicNumber() == 0)) {
// iso.getAtomicNumber() != null has to be checked, e.g. for some reason an element with
// Symbol "R" and number 0 exists in the CDK
valid = false;
}
}
if (!valid) {
logger.warning("Formula invalid! Formula contains element symbols that do not exist.");
return false;
}
return true;
}
use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.
the class FormulaUtils method getFormulaSize.
public static long getFormulaSize(String formula) {
long size = 1;
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
IMolecularFormula molFormula;
molFormula = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(formula, builder);
Isotopes isotopeFactory;
try {
isotopeFactory = Isotopes.getInstance();
for (IIsotope iso : molFormula.isotopes()) {
int naturalIsotopes = 0;
for (IIsotope i : isotopeFactory.getIsotopes(iso.getSymbol())) {
if (i.getNaturalAbundance() > 0.0) {
naturalIsotopes++;
}
}
try {
size = Math.multiplyExact(size, (molFormula.getIsotopeCount(iso) * naturalIsotopes));
} catch (ArithmeticException e) {
e.printStackTrace();
logger.info("Formula size of " + formula + " is too big.");
return -1;
}
}
} catch (IOException e) {
logger.warning("Unable to initialise Isotopes.");
e.printStackTrace();
}
return size;
}
Aggregations