Search in sources :

Example 1 with MZTolerance

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

the class MsMsSpectraMergeModule method mergeConsecutiveScans.

/**
 * Internal method for merging a list of consecutive MS/MS scans.
 *
 * @param scans MS/MS scans with their precursor information
 * @param massList name of the mass list to use when extracting peaks
 * @param scoreModel scoring model to use when removing low quality MS/MS and selecting the best
 *        quality MS/MS
 * @return merged spectrum
 */
protected MergedSpectrum mergeConsecutiveScans(ParameterSet parameters, FragmentScan scans, String massList, Ms2QualityScoreModel scoreModel) {
    int totalNumberOfScans = scans.ms2ScanNumbers.length;
    /*
     * find scan with best quality
     */
    final double[] scores = scoreModel.calculateQualityScore(scans);
    int best = 0;
    for (int k = 1; k < scores.length; ++k) {
        if (scores[k] > scores[best]) {
            best = k;
        }
    }
    if (scores[best] <= 0)
        return MergedSpectrum.empty(totalNumberOfScans);
    final List<Scan> scansToMerge = new ArrayList<>();
    scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[best]));
    final Scan firstScan = scansToMerge.get(0);
    final MassList firstML = firstScan.getMassList(massList);
    if (firstML == null)
        throw new RuntimeException("Scan " + firstScan.getDataFile().getName() + "#" + firstScan.getScanNumber() + " does not have a mass list " + massList);
    if (firstML.getDataPoints().length <= 1)
        return MergedSpectrum.empty(totalNumberOfScans);
    /*
     * remove scans which are considerably worse than the best scan
     */
    final double scoreThreshold = best / 5d;
    for (int i = 1; i < scores.length; ++i) {
        int k = best - i;
        if (k >= 0 && scores[k] > scoreThreshold) {
            scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[k]));
        }
        k = best + i;
        if (k < scores.length && scores[k] > scoreThreshold) {
            scansToMerge.add(scans.origin.getScan(scans.ms2ScanNumbers[k]));
        }
    }
    if (scansToMerge.size() == 1) {
        final MergedSpectrum single = new MergedSpectrum(scansToMerge.get(0), massList);
        single.bestFragmentScanScore = scores[best];
        single.removedScansByLowQuality = scores.length - 1;
        return single;
    }
    /*
     * merge every scan if its cosine is above the cosine threshold
     */
    final double cosineThreshold = parameters.getParameter(MsMsSpectraMergeParameters.COSINE_PARAMETER).getValue();
    final MZTolerance mzTolerance = parameters.getParameter(MsMsSpectraMergeParameters.MASS_ACCURACY).getValue();
    final MzMergeMode mzMergeMode = parameters.getParameter(MsMsSpectraMergeParameters.MZ_MERGE_MODE).getValue();
    final IntensityMergeMode intensityMergeMode = parameters.getParameter(MsMsSpectraMergeParameters.INTENSITY_MERGE_MODE).getValue();
    MergedSpectrum initial = new MergedSpectrum(scansToMerge.get(0), massList);
    initial.bestFragmentScanScore = best;
    final double lowestMassToConsider = Math.min(50d, scans.feature.getMZ() - 50d);
    final DataPoint[] initialMostIntense = ScanUtils.extractMostIntensePeaksAcrossMassRange(initial.data, Range.closed(lowestMassToConsider, 150d), 6);
    double lowestIntensityToConsider;
    final int mostIntensPeakWithin = ScanUtils.findMostIntensePeakWithin(initialMostIntense, Range.closed(lowestMassToConsider, scans.feature.getMZ()));
    if (mostIntensPeakWithin >= 0)
        lowestIntensityToConsider = 0.005d * initialMostIntense[mostIntensPeakWithin].getIntensity();
    else
        lowestIntensityToConsider = 0d;
    Range<Double> cosineRange = Range.closed(lowestMassToConsider, scans.feature.getMZ() - 20);
    final double initialCosine = ScanUtils.probabilityProductUnnormalized(initialMostIntense, initialMostIntense, mzTolerance, lowestIntensityToConsider, cosineRange);
    for (int k = 1; k < scansToMerge.size(); ++k) {
        Scan scan = scansToMerge.get(k);
        if (!(scan.getPolarity().equals(initial.polarity) && scan.getPrecursorCharge() == initial.precursorCharge && mzTolerance.checkWithinTolerance(scan.getPrecursorMZ(), initial.precursorMz))) {
            LoggerFactory.getLogger(MsMsSpectraMergeModule.class).warn("Scan " + scan.getScanNumber() + " cannot be merged: it seems to belong to a different feature.");
            continue;
        }
        DataPoint[] dataPoints = scan.getMassList(massList).getDataPoints();
        final DataPoint[] mostIntense = ScanUtils.extractMostIntensePeaksAcrossMassRange(dataPoints, cosineRange, 6);
        final double norm = ScanUtils.probabilityProductUnnormalized(mostIntense, mostIntense, mzTolerance, lowestIntensityToConsider, cosineRange);
        final double cosine = ScanUtils.probabilityProductUnnormalized(initialMostIntense, mostIntense, mzTolerance, lowestIntensityToConsider, cosineRange) / Math.sqrt(norm * initialCosine);
        if (cosine >= cosineThreshold) {
            initial = merge(initial, scan, dataPoints, mzMergeMode, intensityMergeMode, mzTolerance);
        } else {
            initial.removedScansByLowCosine++;
        }
    }
    initial.removedScansByLowQuality += (totalNumberOfScans - scansToMerge.size());
    return initial;
}
Also used : MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) ArrayList(java.util.ArrayList) DataPoint(net.sf.mzmine.datamodel.DataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) Scan(net.sf.mzmine.datamodel.Scan) MassList(net.sf.mzmine.datamodel.MassList)

Example 2 with MZTolerance

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

the class MsMsSpectraMergeModule method mergeConsecutiveScans.

/**
 * Merge all consecutive MS/MS scans of the same feature within the same sample. Two scans are
 * consecutive, if there is no other MS/MS or MS in between. It is recommended to use
 * #merge(PeakListRow,String) instead. Note, that this method will not remove noise peaks from the
 * merged spectra.
 *
 * @param feature the feature which MS/MS should be merged
 * @param massList name of the mass list to use when extracting peaks
 * @return all merged spectra of consecutive MS/MS scans of the given feature
 */
public List<MergedSpectrum> mergeConsecutiveScans(ParameterSet parameters, Feature feature, String massList) {
    MZTolerance ppm = parameters.getParameter(MsMsSpectraMergeParameters.MASS_ACCURACY).getValue();
    final double isolationWindowOffset = parameters.getParameter(MsMsSpectraMergeParameters.ISOLATION_WINDOW_OFFSET).getValue();
    final double isolationWindowWidth = parameters.getParameter(MsMsSpectraMergeParameters.ISOLATION_WINDOW_WIDTH).getValue();
    FragmentScan[] allFragmentScans = FragmentScan.getAllFragmentScansFor(feature, massList, Range.closed(isolationWindowOffset - isolationWindowWidth, isolationWindowOffset + isolationWindowWidth), ppm);
    final List<MergedSpectrum> mergedSpec = new ArrayList<>();
    for (FragmentScan scan : allFragmentScans) {
        MergedSpectrum e = mergeConsecutiveScans(parameters, scan, massList, Ms2QualityScoreModel.SelectByLowChimericIntensityRelativeToMs1Intensity);
        if (e.data.length > 0)
            mergedSpec.add(e);
    }
    return mergedSpec;
}
Also used : MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) ArrayList(java.util.ArrayList)

Example 3 with MZTolerance

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

the class MzRangeFormulaCalculatorModule method getMzRangeFromFormula.

@Nullable
public static Range<Double> getMzRangeFromFormula(ParameterSet myParameters) {
    String formula = myParameters.getParameter(MzRangeFormulaCalculatorParameters.formula).getValue().trim();
    IonizationType ionType = myParameters.getParameter(MzRangeFormulaCalculatorParameters.ionType).getValue();
    MZTolerance mzTolerance = myParameters.getParameter(MzRangeFormulaCalculatorParameters.mzTolerance).getValue();
    Integer charge = myParameters.getParameter(MzRangeFormulaCalculatorParameters.charge).getValue();
    return getMzRangeFromFormula(formula, ionType, mzTolerance, charge);
}
Also used : MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) IonizationType(net.sf.mzmine.datamodel.IonizationType) Nullable(javax.annotation.Nullable)

Example 4 with MZTolerance

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

the class MzRangeMassCalculatorModule method showRangeCalculationDialog.

/**
 * Shows the calculation dialog and returns the calculated m/z range. May return null in case user
 * clicked Cancel.
 */
@Nullable
public static Range<Double> showRangeCalculationDialog() {
    ParameterSet myParameters = MZmineCore.getConfiguration().getModuleParameters(MzRangeMassCalculatorModule.class);
    if (myParameters == null)
        return null;
    ExitCode exitCode = myParameters.showSetupDialog(null, true);
    if (exitCode != ExitCode.OK)
        return null;
    Double mz = myParameters.getParameter(MzRangeMassCalculatorParameters.mz).getValue();
    MZTolerance mzTolerance = myParameters.getParameter(MzRangeMassCalculatorParameters.mzTolerance).getValue();
    if ((mz == null) || (mzTolerance == null))
        return null;
    Range<Double> mzRange = mzTolerance.getToleranceRange(mz);
    return mzRange;
}
Also used : ParameterSet(net.sf.mzmine.parameters.ParameterSet) MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) ExitCode(net.sf.mzmine.util.ExitCode) Nullable(javax.annotation.Nullable)

Example 5 with MZTolerance

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

the class RansacAlignerSetupDialog method getVectorAlignment.

/**
 * Create the vector which contains all the possible aligned peaks.
 *
 * @return vector which contains all the possible aligned peaks.
 */
private Vector<AlignStructMol> getVectorAlignment(PeakList peakListX, PeakList peakListY, RawDataFile file, RawDataFile file2) {
    Vector<AlignStructMol> alignMol = new Vector<AlignStructMol>();
    for (PeakListRow row : peakListX.getRows()) {
        // Calculate limits for a row with which the row can be aligned
        MZTolerance mzTolerance = super.parameterSet.getParameter(RansacAlignerParameters.MZTolerance).getValue();
        RTTolerance rtTolerance = super.parameterSet.getParameter(RansacAlignerParameters.RTToleranceBefore).getValue();
        Range<Double> mzRange = mzTolerance.getToleranceRange(row.getAverageMZ());
        Range<Double> rtRange = rtTolerance.getToleranceRange(row.getAverageRT());
        // Get all rows of the aligned peaklist within parameter limits
        PeakListRow[] candidateRows = peakListY.getRowsInsideScanAndMZRange(rtRange, mzRange);
        for (PeakListRow candidateRow : candidateRows) {
            if (file == null || file2 == null) {
                alignMol.addElement(new AlignStructMol(row, candidateRow));
            } else {
                if (candidateRow.getPeak(file2) != null) {
                    alignMol.addElement(new AlignStructMol(row, candidateRow, file, file2));
                }
            }
        }
    }
    return alignMol;
}
Also used : MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) RTTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.RTTolerance) Vector(java.util.Vector)

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