use of org.openscience.cdk.interfaces.IChemObjectBuilder in project mzmine2 by mzmine.
the class DPPSumFormulaPredictionTask method run.
@Override
public void run() {
if (!checkParameterSet() || !checkValues()) {
setStatus(TaskStatus.ERROR);
return;
}
if (getDataPoints().length == 0) {
logger.info("Data point/Spectra processing: 0 data points were passed to " + getTaskDescription() + " Please check the parameters.");
setStatus(TaskStatus.CANCELED);
return;
}
if (!(getDataPoints() instanceof ProcessedDataPoint[])) {
logger.info("Data point/Spectra processing: The array of data points passed to " + getTaskDescription() + " is not an instance of ProcessedDataPoint. Make sure to run mass detection first.");
setStatus(TaskStatus.CANCELED);
return;
}
setStatus(TaskStatus.PROCESSING);
List<ProcessedDataPoint> resultList = new ArrayList<>();
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
for (int i = 0; i < dataPoints.length; i++) {
if (isCanceled())
return;
if (dataPoints[i].getIntensity() < noiseLevel)
continue;
massRange = mzTolerance.getToleranceRange((dataPoints[i].getMZ() - ionType.getAddedMass()) / charge);
MolecularFormulaRange elCounts = DynamicParameterUtils.buildFormulaRangeOnIsotopePatternResults((ProcessedDataPoint) dataPoints[i], elementCounts);
generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elCounts);
List<PredResult> formulas = generateFormulas((ProcessedDataPoint) dataPoints[i], massRange, charge, generator);
DPPSumFormulaResult[] results = genereateResults(formulas, numResults);
((ProcessedDataPoint) dataPoints[i]).addAllResults(results);
resultList.add((ProcessedDataPoint) dataPoints[i]);
currentIndex++;
}
// setResults((ProcessedDataPoint[]) dataPoints);
setResults(resultList.toArray(new ProcessedDataPoint[0]));
setStatus(TaskStatus.FINISHED);
}
use of org.openscience.cdk.interfaces.IChemObjectBuilder 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.IChemObjectBuilder 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.IChemObjectBuilder 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