Search in sources :

Example 1 with IChemObjectBuilder

use of org.openscience.cdk.interfaces.IChemObjectBuilder 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 IChemObjectBuilder

use of org.openscience.cdk.interfaces.IChemObjectBuilder 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 3 with IChemObjectBuilder

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

the class SingleRowPredictionTask method run.

/**
 * @see java.lang.Runnable#run()
 */
public void run() {
    setStatus(TaskStatus.PROCESSING);
    resultWindow = new ResultWindow("Searching for " + MZmineCore.getConfiguration().getMZFormat().format(searchedMass), peakListRow, searchedMass, charge, this);
    resultWindow.setVisible(true);
    logger.finest("Starting search for formulas for " + massRange + " Da");
    IsotopePattern detectedPattern = peakListRow.getBestIsotopePattern();
    if ((checkIsotopes) && (detectedPattern == null)) {
        final String msg = "Cannot calculate isotope pattern scores, because selected" + " peak does not have any isotopes. Have you run the isotope peak grouper?";
        MZmineCore.getDesktop().displayMessage(resultWindow, msg);
    }
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
    IMolecularFormula cdkFormula;
    while ((cdkFormula = generator.getNextFormula()) != null) {
        if (isCanceled())
            return;
        // Mass is ok, so test other constraints
        checkConstraints(cdkFormula);
    }
    if (isCanceled())
        return;
    logger.finest("Finished formula search for " + massRange + " m/z, found " + foundFormulas + " formulas");
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            resultWindow.setTitle("Finished searching for " + MZmineCore.getConfiguration().getMZFormat().format(searchedMass) + " amu, " + foundFormulas + " formulas found");
        }
    });
    setStatus(TaskStatus.FINISHED);
}
Also used : MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder)

Example 4 with IChemObjectBuilder

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

the class MSMSScoreCalculator method evaluateMSMS.

/**
 * Returns a calculated similarity score of
 */
public static MSMSScore evaluateMSMS(IMolecularFormula parentFormula, Scan msmsScan, ParameterSet parameters) {
    MZTolerance msmsTolerance = parameters.getParameter(MSMSScoreParameters.msmsTolerance).getValue();
    String massListName = parameters.getParameter(MSMSScoreParameters.massList).getValue();
    MassList massList = msmsScan.getMassList(massListName);
    if (massList == null) {
        throw new IllegalArgumentException("Scan #" + msmsScan.getScanNumber() + " does not have a mass list called '" + massListName + "'");
    }
    DataPoint[] msmsIons = massList.getDataPoints();
    if (msmsIons == null) {
        throw new IllegalArgumentException("Mass list " + massList + " does not contain data for scan #" + msmsScan.getScanNumber());
    }
    MolecularFormulaRange msmsElementRange = new MolecularFormulaRange();
    for (IIsotope isotope : parentFormula.isotopes()) {
        msmsElementRange.addIsotope(isotope, 0, parentFormula.getIsotopeCount(isotope));
    }
    int totalMSMSpeaks = 0, interpretedMSMSpeaks = 0;
    Map<DataPoint, String> msmsAnnotations = new Hashtable<DataPoint, String>();
    msmsCycle: for (DataPoint dp : msmsIons) {
        // Check if this is an isotope
        Range<Double> isotopeCheckRange = Range.closed(dp.getMZ() - 1.4, dp.getMZ() - 0.6);
        for (DataPoint dpCheck : msmsIons) {
            // isotope and we should ignore it
            if (isotopeCheckRange.contains(dpCheck.getMZ()) && (dpCheck.getIntensity() > dp.getIntensity())) {
                continue msmsCycle;
            }
        }
        // If getPrecursorCharge() returns 0, it means charge is unknown. In
        // that case let's assume charge 1
        int precursorCharge = msmsScan.getPrecursorCharge();
        if (precursorCharge == 0)
            precursorCharge = 1;
        // We don't know the charge of the fragment, so we will simply
        // assume 1
        double neutralLoss = msmsScan.getPrecursorMZ() * precursorCharge - dp.getMZ();
        // good threshold
        if (neutralLoss < 5) {
            continue;
        }
        Range<Double> msmsTargetRange = msmsTolerance.getToleranceRange(neutralLoss);
        IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
        MolecularFormulaGenerator msmsEngine = new MolecularFormulaGenerator(builder, msmsTargetRange.lowerEndpoint(), msmsTargetRange.upperEndpoint(), msmsElementRange);
        IMolecularFormula formula = msmsEngine.getNextFormula();
        if (formula != null) {
            String formulaString = MolecularFormulaManipulator.getString(formula);
            msmsAnnotations.put(dp, formulaString);
            interpretedMSMSpeaks++;
        }
        totalMSMSpeaks++;
    }
    // If we did not evaluate any MS/MS peaks, we cannot calculate a score
    if (totalMSMSpeaks == 0) {
        return null;
    }
    double msmsScore = (double) interpretedMSMSpeaks / totalMSMSpeaks;
    MSMSScore result = new MSMSScore(msmsScore, msmsAnnotations);
    return result;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) Hashtable(java.util.Hashtable) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) Range(com.google.common.collect.Range) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) DataPoint(net.sf.mzmine.datamodel.DataPoint) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) DataPoint(net.sf.mzmine.datamodel.DataPoint) MassList(net.sf.mzmine.datamodel.MassList)

Example 5 with IChemObjectBuilder

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

the class SpectraIdentificationSumFormulaTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.finest("Starting search for formulas for " + massRange + " Da");
    // create mass list for scan
    DataPoint[] massList = null;
    ArrayList<DataPoint> massListAnnotated = new ArrayList<>();
    MassDetector massDetector = null;
    ArrayList<String> allCompoundIDs = new ArrayList<>();
    // Create a new mass list for MS/MS scan. Check if sprectrum is profile or centroid mode
    if (currentScan.getSpectrumType() == MassSpectrumType.CENTROIDED) {
        massDetector = new CentroidMassDetector();
        CentroidMassDetectorParameters parameters = new CentroidMassDetectorParameters();
        CentroidMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    } else {
        massDetector = new ExactMassDetector();
        ExactMassDetectorParameters parameters = new ExactMassDetectorParameters();
        ExactMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    }
    numItems = massList.length;
    // loop through every peak in mass list
    if (getStatus() != TaskStatus.PROCESSING) {
        return;
    }
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    for (int i = 0; i < massList.length; i++) {
        massRange = mzTolerance.getToleranceRange((massList[i].getMZ() - ionType.getAddedMass()) / charge);
        generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
        IMolecularFormula cdkFormula;
        String annotation = "";
        // 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) == true) {
                String formula = MolecularFormulaManipulator.getString(cdkFormula);
                // calc rel mass deviation
                Double relMassDev = ((((// 
                massList[i].getMZ() - ionType.getAddedMass()) / // 
                charge) - (// 
                FormulaUtils.calculateExactMass(MolecularFormulaManipulator.getString(cdkFormula))) / charge) / ((// 
                massList[i].getMZ() - ionType.getAddedMass()) / charge)) * 1000000;
                // write to map
                possibleFormulas.put(relMassDev, formula);
            }
        }
        Map<Double, String> treeMap = new TreeMap<>((Comparator<Double>) (o1, o2) -> Double.compare(Math.abs(o1), Math.abs(o2)));
        treeMap.putAll(possibleFormulas);
        // get top 3
        int ctr = 0;
        for (Map.Entry<Double, String> entry : treeMap.entrySet()) {
            int number = ctr + 1;
            if (ctr > 2)
                break;
            annotation = annotation + number + ". " + entry.getValue() + " Δ " + NumberFormat.getInstance().format(entry.getKey()) + " ppm; ";
            ctr++;
            if (isCanceled())
                return;
        }
        if (annotation != "") {
            allCompoundIDs.add(annotation);
            massListAnnotated.add(massList[i]);
        }
        logger.finest("Finished formula search for " + massRange + " m/z, found " + foundFormulas + " formulas");
    }
    // new mass list
    DataPoint[] annotatedMassList = new DataPoint[massListAnnotated.size()];
    massListAnnotated.toArray(annotatedMassList);
    String[] annotations = new String[annotatedMassList.length];
    allCompoundIDs.toArray(annotations);
    DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet("Detected compounds", annotatedMassList);
    // Add label generator for the dataset
    SpectraDatabaseSearchLabelGenerator labelGenerator = new SpectraDatabaseSearchLabelGenerator(annotations, spectraPlot);
    spectraPlot.addDataSet(detectedCompoundsDataset, Color.orange, true, labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setSeriesItemLabelGenerator(spectraPlot.getXYPlot().getSeriesCount(), labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT, TextAnchor.BOTTOM_CENTER, 0.0), true);
    setStatus(TaskStatus.FINISHED);
}
Also used : Color(java.awt.Color) Scan(net.sf.mzmine.datamodel.Scan) MZmineCore(net.sf.mzmine.main.MZmineCore) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) CentroidMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) RDBERestrictionChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.rdbe.RDBERestrictionChecker) TextAnchor(org.jfree.chart.ui.TextAnchor) MolecularFormulaManipulator(org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) SpectraDatabaseSearchLabelGenerator(net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator) NumberFormat(java.text.NumberFormat) DataPoint(net.sf.mzmine.datamodel.DataPoint) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) ArrayList(java.util.ArrayList) MassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector) ParameterSet(net.sf.mzmine.parameters.ParameterSet) Map(java.util.Map) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) FormulaUtils(net.sf.mzmine.util.FormulaUtils) MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) SilentChemObjectBuilder(org.openscience.cdk.silent.SilentChemObjectBuilder) Range(com.google.common.collect.Range) Logger(java.util.logging.Logger) IonizationType(net.sf.mzmine.datamodel.IonizationType) ElementalHeuristicChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.elements.ElementalHeuristicChecker) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) TreeMap(java.util.TreeMap) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) Comparator(java.util.Comparator) ItemLabelAnchor(org.jfree.chart.labels.ItemLabelAnchor) ExactMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters) SpectraPlot(net.sf.mzmine.modules.visualization.spectra.simplespectra.SpectraPlot) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) ArrayList(java.util.ArrayList) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) DataPoint(net.sf.mzmine.datamodel.DataPoint) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) MassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) CentroidMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters) TreeMap(java.util.TreeMap) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) DataPoint(net.sf.mzmine.datamodel.DataPoint) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) ExactMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters) Map(java.util.Map) TreeMap(java.util.TreeMap) SpectraDatabaseSearchLabelGenerator(net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator)

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