Search in sources :

Example 11 with MZTolerance

use of net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance 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 12 with MZTolerance

use of net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance in project mzmine2 by mzmine.

the class MassListDeisotoper method filterIsotopes.

public static DataPoint[] filterIsotopes(DataPoint[] dataPoints, ParameterSet parameterSet) {
    if (dataPoints == null || dataPoints.length == 0) {
        return dataPoints;
    }
    MZTolerance mzTolerance = parameterSet.getParameter(MassListDeisotoperParameters.mzTolerance).getValue();
    boolean monotonicShape = parameterSet.getParameter(MassListDeisotoperParameters.monotonicShape).getValue();
    int maximumCharge = parameterSet.getParameter(MassListDeisotoperParameters.maximumCharge).getValue();
    int[] charges = new int[maximumCharge];
    for (int i = 0; i < maximumCharge; i++) charges[i] = i + 1;
    // sort by intensity
    dataPoints = dataPoints.clone();
    Arrays.sort(dataPoints, new DataPointSorter(SortingProperty.Intensity, SortingDirection.Descending));
    List<DataPoint> deisotopedDataPoints = new ArrayList<>();
    for (int i = 0; i < dataPoints.length; i++) {
        DataPoint aPeak = dataPoints[i];
        if (aPeak == null) {
            continue;
        }
        // Check which charge state fits best around this peak
        int bestFitCharge = 0;
        int bestFitScore = -1;
        List<DataPoint> bestFitPeaks = null;
        for (int charge : charges) {
            List<DataPoint> fittedPeaks = new ArrayList<>();
            fittedPeaks.add(aPeak);
            fitPattern(fittedPeaks, aPeak, charge, dataPoints, DELTA, monotonicShape, mzTolerance);
            int score = fittedPeaks.size();
            if ((score > bestFitScore) || ((score == bestFitScore) && (bestFitCharge > charge))) {
                bestFitScore = score;
                bestFitCharge = charge;
                bestFitPeaks = fittedPeaks;
            }
        }
        assert bestFitPeaks != null;
        // add to deisotoped
        deisotopedDataPoints.add(dataPoints[i]);
        // remove all
        for (int j = 0; j < dataPoints.length; j++) {
            if (bestFitPeaks.contains(dataPoints[j]))
                dataPoints[j] = null;
        }
    }
    return deisotopedDataPoints.toArray(new DataPoint[deisotopedDataPoints.size()]);
}
Also used : MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) DataPoint(net.sf.mzmine.datamodel.DataPoint) DataPointSorter(net.sf.mzmine.util.DataPointSorter) ArrayList(java.util.ArrayList) DataPoint(net.sf.mzmine.datamodel.DataPoint)

Aggregations

MZTolerance (net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance)12 ArrayList (java.util.ArrayList)6 DataPoint (net.sf.mzmine.datamodel.DataPoint)6 Nullable (javax.annotation.Nullable)3 MassList (net.sf.mzmine.datamodel.MassList)3 ParameterSet (net.sf.mzmine.parameters.ParameterSet)3 Range (com.google.common.collect.Range)2 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)2 Scan (net.sf.mzmine.datamodel.Scan)2 DataPointSorter (net.sf.mzmine.util.DataPointSorter)2 ExitCode (net.sf.mzmine.util.ExitCode)2 Color (java.awt.Color)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Optional (java.util.Optional)1 Vector (java.util.Vector)1