Search in sources :

Example 6 with IMolecularFormula

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

the class NeutralLossFilterParameters method checkParameterValues.

@Override
public boolean checkParameterValues(Collection<String> errorMessages) {
    if (super.checkParameterValues(errorMessages) == false)
        return false;
    String molecule = this.getParameter(NeutralLossFilterParameters.molecule).getValue();
    // no formula -> use deltaMass
    if (molecule.isEmpty())
        return true;
    // formula can be parsed
    IMolecularFormula formula = FormulaUtils.createMajorIsotopeMolFormula(molecule);
    if (formula != null) {
        return true;
    }
    errorMessages.add("Formula cannot be parsed. Enter valid formula.");
    return false;
}
Also used : IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula)

Example 7 with IMolecularFormula

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

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

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

Example 10 with IMolecularFormula

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

the class DPPAnyElementIsotopeGrouperTask method getIsotopePatterns.

/**
 * Returns an array of isotope patterns for the given string. Every element gets its own isotope
 * pattern.
 *
 * @param elements String of element symbols
 * @param mergeWidth
 * @param minAbundance
 * @return
 */
public static ExtendedIsotopePattern[] getIsotopePatterns(String elements, double mergeWidth, double minAbundance) {
    SilentChemObjectBuilder builder = (SilentChemObjectBuilder) SilentChemObjectBuilder.getInstance();
    IMolecularFormula form = MolecularFormulaManipulator.getMajorIsotopeMolecularFormula(elements, builder);
    ExtendedIsotopePattern[] isotopePatterns = new ExtendedIsotopePattern[form.getIsotopeCount()];
    int i = 0;
    // create a isotope pattern for every element
    for (IIsotope element : form.isotopes()) {
        isotopePatterns[i] = (ExtendedIsotopePattern) IsotopePatternCalculator.calculateIsotopePattern(element.getSymbol(), minAbundance, mergeWidth, 1, PolarityType.NEUTRAL, true);
        i++;
    }
    // also, we want to keep track of the isotope composition, to do that cleanly, we remove the
    // lightest isotope description
    ExtendedIsotopePattern[] cleanedPatterns = new ExtendedIsotopePattern[form.getIsotopeCount()];
    i = 0;
    for (ExtendedIsotopePattern p : isotopePatterns) {
        String[] composition = p.getIsotopeCompositions();
        composition[0] = "";
        cleanedPatterns[i] = new ExtendedIsotopePattern(p.getDataPoints(), p.getStatus(), p.getDescription(), composition);
        i++;
    }
    return cleanedPatterns;
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) SilentChemObjectBuilder(org.openscience.cdk.silent.SilentChemObjectBuilder) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ExtendedIsotopePattern(net.sf.mzmine.datamodel.impl.ExtendedIsotopePattern) DataPoint(net.sf.mzmine.datamodel.DataPoint) 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