Search in sources :

Example 6 with IChemObjectBuilder

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);
}
Also used : MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) DPPSumFormulaResult(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.results.DPPSumFormulaResult) ArrayList(java.util.ArrayList) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) DataPoint(net.sf.mzmine.datamodel.DataPoint) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint)

Example 7 with IChemObjectBuilder

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;
    }
}
Also used : IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) IOException(java.io.IOException)

Example 8 with IChemObjectBuilder

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;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Example 9 with IChemObjectBuilder

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;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IOException(java.io.IOException) Isotopes(org.openscience.cdk.config.Isotopes) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Aggregations

IChemObjectBuilder (org.openscience.cdk.interfaces.IChemObjectBuilder)9 IMolecularFormula (org.openscience.cdk.interfaces.IMolecularFormula)8 MolecularFormulaGenerator (org.openscience.cdk.formula.MolecularFormulaGenerator)5 MolecularFormulaRange (org.openscience.cdk.formula.MolecularFormulaRange)4 Range (com.google.common.collect.Range)3 DataPoint (net.sf.mzmine.datamodel.DataPoint)3 MZTolerance (net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance)3 IIsotope (org.openscience.cdk.interfaces.IIsotope)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Logger (java.util.logging.Logger)2 IonizationType (net.sf.mzmine.datamodel.IonizationType)2 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)2 MassList (net.sf.mzmine.datamodel.MassList)2 Scan (net.sf.mzmine.datamodel.Scan)2 MZmineCore (net.sf.mzmine.main.MZmineCore)2 ElementalHeuristicChecker (net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.elements.ElementalHeuristicChecker)2