Search in sources :

Example 1 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class FormulaPredictionPeakListTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    totalRows = peakList.getNumberOfRows();
    for (PeakListRow row : peakList.getRows()) {
        if (row.getPeakIdentities().length > 0) {
            continue;
        }
        this.searchedMass = (row.getAverageMZ() - ionType.getAddedMass()) * charge;
        message = "Formula prediction for " + MZmineCore.getConfiguration().getMZFormat().format(searchedMass);
        massRange = mzTolerance.getToleranceRange(searchedMass);
        IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
        generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
        IMolecularFormula cdkFormula;
        // create a map to store ResultFormula and relative mass deviation for sorting
        Map<Double, String> possibleFormulas = new TreeMap<>();
        while ((cdkFormula = generator.getNextFormula()) != null) {
            if (isCanceled())
                return;
            // Mass is ok, so test other constraints
            if (checkConstraints(cdkFormula, row) == true) {
                String formula = MolecularFormulaManipulator.getString(cdkFormula);
                // calc rel mass deviation
                Double relMassDev = ((searchedMass - (FormulaUtils.calculateExactMass(formula))) / searchedMass) * 1000000;
                // write to map
                possibleFormulas.put(relMassDev, formula);
            }
        }
        if (isCanceled())
            return;
        // create a map to store ResultFormula and relative mass deviation for sorting
        Map<Double, String> possibleFormulasSorted = new TreeMap<>((Comparator<Double>) (o1, o2) -> Double.compare(Math.abs(o1), Math.abs(o2)));
        possibleFormulasSorted.putAll(possibleFormulas);
        // Add the new formula entry top results
        int ctr = 0;
        for (Map.Entry<Double, String> entry : possibleFormulasSorted.entrySet()) {
            if (ctr < maxBestFormulasPerPeak) {
                SimplePeakIdentity newIdentity = new SimplePeakIdentity(entry.getValue(), entry.getValue(), this.getClass().getName(), null, null);
                row.addPeakIdentity(newIdentity, false);
                ctr++;
            }
        }
        if (isCanceled())
            return;
        finishedRows++;
    }
    if (isCanceled())
        return;
    logger.finest("Finished formula search for all the peaks");
    setStatus(TaskStatus.FINISHED);
}
Also used : Scan(net.sf.mzmine.datamodel.Scan) MZmineCore(net.sf.mzmine.main.MZmineCore) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) RDBERestrictionChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.rdbe.RDBERestrictionChecker) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) MolecularFormulaManipulator(org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) IsotopePatternScoreParameters(net.sf.mzmine.modules.peaklistmethods.isotopes.isotopepatternscore.IsotopePatternScoreParameters) PeakList(net.sf.mzmine.datamodel.PeakList) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) Feature(net.sf.mzmine.datamodel.Feature) ParameterSet(net.sf.mzmine.parameters.ParameterSet) Map(java.util.Map) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) FormulaUtils(net.sf.mzmine.util.FormulaUtils) MSMSScoreParameters(net.sf.mzmine.modules.peaklistmethods.msms.msmsscore.MSMSScoreParameters) MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) SilentChemObjectBuilder(org.openscience.cdk.silent.SilentChemObjectBuilder) Range(com.google.common.collect.Range) IsotopePatternScoreCalculator(net.sf.mzmine.modules.peaklistmethods.isotopes.isotopepatternscore.IsotopePatternScoreCalculator) IsotopePatternCalculator(net.sf.mzmine.modules.peaklistmethods.isotopes.isotopeprediction.IsotopePatternCalculator) Logger(java.util.logging.Logger) IonizationType(net.sf.mzmine.datamodel.IonizationType) ElementalHeuristicChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.elements.ElementalHeuristicChecker) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) MassList(net.sf.mzmine.datamodel.MassList) MSMSScore(net.sf.mzmine.modules.peaklistmethods.msms.msmsscore.MSMSScore) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) TreeMap(java.util.TreeMap) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) Comparator(java.util.Comparator) MSMSScoreCalculator(net.sf.mzmine.modules.peaklistmethods.msms.msmsscore.MSMSScoreCalculator) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) TreeMap(java.util.TreeMap) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class IonType method addToFormula.

/**
 * Is adding or removing all sub adducts / modifications from the molecular formula
 *
 * @param formula
 * @return
 * @throws CloneNotSupportedException
 */
public IMolecularFormula addToFormula(IMolecularFormula formula) throws CloneNotSupportedException {
    IMolecularFormula result = (IMolecularFormula) formula.clone();
    // add for n molecules the M formula
    for (int i = 2; i <= molecules; i++) FormulaUtils.addFormula(result, formula);
    // add
    Arrays.stream(adduct.getAdducts()).filter(m -> m.getMass() >= 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.addFormula(result, m.getCDKFormula()));
    if (mod != null)
        Arrays.stream(mod.getAdducts()).filter(m -> m.getMass() >= 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.addFormula(result, m.getCDKFormula()));
    // subtract
    Arrays.stream(adduct.getAdducts()).filter(m -> m.getMass() < 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.subtractFormula(result, m.getCDKFormula()));
    if (mod != null)
        Arrays.stream(mod.getAdducts()).filter(m -> m.getMass() < 0 && m.getCDKFormula() != null).forEach(m -> FormulaUtils.subtractFormula(result, m.getCDKFormula()));
    return result;
}
Also used : Objects(java.util.Objects) NeutralMolecule(net.sf.mzmine.datamodel.identities.NeutralMolecule) Arrays(java.util.Arrays) List(java.util.List) MZmineCore(net.sf.mzmine.main.MZmineCore) PolarityType(net.sf.mzmine.datamodel.PolarityType) FormulaUtils(net.sf.mzmine.util.FormulaUtils) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula)

Example 3 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class IsotopePatternCalculator method calculateIsotopePattern.

public static IsotopePattern calculateIsotopePattern(String molecularFormula, double minAbundance, double mergeWidth, int charge, PolarityType polarity, boolean storeFormula) {
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    molecularFormula = molecularFormula.replace(" ", "");
    IMolecularFormula cdkFormula = MolecularFormulaManipulator.getMolecularFormula(molecularFormula, builder);
    return calculateIsotopePattern(cdkFormula, minAbundance, mergeWidth, charge, polarity, storeFormula);
}
Also used : IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Example 4 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class DPPSumFormulaPredictionTask method generateFormulas.

/**
 * Predicts sum formulas for a given m/z and parameters.
 *
 * @param mz m/z to generate sum formulas from
 * @param massRange Mass range for sum formulas
 * @param charge Charge of the molecule
 * @param generator instance of MolecularFormulaGenerator
 * @return List<PredResult> sorted by relative ppm difference and String of the formula.
 */
private List<PredResult> generateFormulas(ProcessedDataPoint dp, Range<Double> massRange, int charge, MolecularFormulaGenerator generator) {
    List<PredResult> possibleFormulas = new ArrayList<>();
    IMolecularFormula cdkFormula;
    while ((cdkFormula = generator.getNextFormula()) != null) {
        // Mass is ok, so test other constraints
        if (!checkConstraints(cdkFormula))
            continue;
        String formula = MolecularFormulaManipulator.getString(cdkFormula);
        // calc rel mass deviation
        Double relMassDev = ((((// 
        dp.getMZ() - ionType.getAddedMass()) / // 
        charge) - (// 
        FormulaUtils.calculateExactMass(MolecularFormulaManipulator.getString(cdkFormula))) / charge) / ((// 
        dp.getMZ() - ionType.getAddedMass()) / charge)) * 1000000;
        // write to map
        if (checkIsotopes && dp.resultTypeExists(ResultType.ISOTOPEPATTERN)) {
            double score = getIsotopeSimilarityScore(cdkFormula, (IsotopePattern) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN).getValue());
            possibleFormulas.add(new PredResult(relMassDev, formula, score));
        } else {
            possibleFormulas.add(new PredResult(relMassDev, formula));
        }
    }
    evaluateAndSortFormulas(dp, possibleFormulas);
    return possibleFormulas;
}
Also used : ArrayList(java.util.ArrayList) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula)

Example 5 with IMolecularFormula

use of org.openscience.cdk.interfaces.IMolecularFormula in project mzmine2 by mzmine.

the class DynamicParameterUtils method buildFormulaRangeOnIsotopePatternResults.

/**
 * Creates an ElementParameter based on the previous processing results. If no results were
 * detected, the default value is returned. Upper and lower boundaries are chosen according to
 * lowerElementBoundaryPercentage and upperElementBoundaryPercentage values of this utility class.
 * These values can be set via {@link #setLowerElementBoundaryPercentage} and
 * {@link #setUpperElementBoundaryPercentage}. The elements contained in
 *
 * @param dp The data point to build a parameter for.
 * @param def The default set of parameters.
 * @return The built ElementsParameter
 */
public static MolecularFormulaRange buildFormulaRangeOnIsotopePatternResults(ProcessedDataPoint dp, MolecularFormulaRange def) {
    DPPIsotopePatternResult result = (DPPIsotopePatternResult) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN);
    if (result == null)
        return def;
    if (!(result.getValue() instanceof ExtendedIsotopePattern))
        return def;
    ExtendedIsotopePattern pattern = (ExtendedIsotopePattern) result.getValue();
    String form = IsotopePatternUtils.makePatternSuggestion(pattern.getIsotopeCompositions());
    MolecularFormulaRange range = new MolecularFormulaRange();
    IMolecularFormula formula = FormulaUtils.createMajorIsotopeMolFormula(form);
    if (formula == null) {
        logger.finest("could not generate formula for m/z " + dp.getMZ() + " " + form);
        return def;
    }
    for (IIsotope isotope : def.isotopes()) range.addIsotope(isotope, def.getIsotopeCountMin(isotope), def.getIsotopeCountMax(isotope));
    for (IIsotope isotope : formula.isotopes()) {
        if (range.contains(isotope))
            continue;
        int count = formula.getIsotopeCount(isotope);
        range.addIsotope(isotope, (int) (count * lowerElementBoundaryPercentage), (int) (count * upperElementBoundaryPercentage));
    }
    for (IIsotope isotope : range.isotopes()) {
        int min = range.getIsotopeCountMin(isotope);
        int max = range.getIsotopeCountMax(isotope);
    // logger.info("m/z = " + dp.getMZ() + " " + isotope.getSymbol() + " " + min + " - " + max);
    }
    return range;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) DPPIsotopePatternResult(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.results.DPPIsotopePatternResult) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ExtendedIsotopePattern(net.sf.mzmine.datamodel.impl.ExtendedIsotopePattern) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint)

Aggregations

IMolecularFormula (org.openscience.cdk.interfaces.IMolecularFormula)13 IChemObjectBuilder (org.openscience.cdk.interfaces.IChemObjectBuilder)8 MolecularFormulaRange (org.openscience.cdk.formula.MolecularFormulaRange)4 IIsotope (org.openscience.cdk.interfaces.IIsotope)4 Range (com.google.common.collect.Range)3 ArrayList (java.util.ArrayList)3 DataPoint (net.sf.mzmine.datamodel.DataPoint)3 MZmineCore (net.sf.mzmine.main.MZmineCore)3 FormulaUtils (net.sf.mzmine.util.FormulaUtils)3 MolecularFormulaGenerator (org.openscience.cdk.formula.MolecularFormulaGenerator)3 IOException (java.io.IOException)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 ExtendedIsotopePattern (net.sf.mzmine.datamodel.impl.ExtendedIsotopePattern)2