Search in sources :

Example 26 with SimplePeakList

use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.

the class PeakListRowLearnerTask method run.

/**
 * @see Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running learner task on " + peakList);
    // Create a new results peakList which is added at the end
    resultPeakList = new SimplePeakList(peakList + " " + suffix, peakList.getRawDataFiles());
    /**
     * - A PeakList is a list of Features (peak in retention time dimension with accurate m/z)<br>
     * ---- contains one or multiple RawDataFiles <br>
     * ---- access mean retention time, mean m/z, maximum intensity, ...<br>
     */
    // get all rows and sort by m/z
    PeakListRow[] rows = peakList.getRows();
    Arrays.sort(rows, new PeakListRowSorter(SortingProperty.MZ, SortingDirection.Ascending));
    totalRows = rows.length;
    for (int i = 0; i < totalRows; i++) {
        // check for cancelled state and stop
        if (isCanceled())
            return;
        PeakListRow row = rows[i];
        // access details
        double mz = row.getAverageMZ();
        double intensity = row.getAverageHeight();
        double rt = row.getAverageRT();
        Feature peak = row.getBestPeak();
        // do stuff
        // ...
        // add row to peaklist result
        PeakListRow copy = copyPeakRow(row);
        resultPeakList.addRow(copy);
        // Update completion rate
        processedPeaks++;
    }
    // add to project
    addResultToProject();
    logger.info("Finished on " + peakList);
    setStatus(TaskStatus.FINISHED);
}
Also used : SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) Feature(net.sf.mzmine.datamodel.Feature) PeakListRowSorter(net.sf.mzmine.util.PeakListRowSorter)

Example 27 with SimplePeakList

use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.

the class StreamPeakListRowLearnerTask method run.

/**
 * @see Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running learner task on " + peakList);
    // Create a new results peakList which is added at the end
    resultPeakList = new SimplePeakList(peakList + " " + suffix, peakList.getRawDataFiles());
    /**
     * - A PeakList is a list of Features (peak in retention time dimension with accurate m/z)<br>
     * ---- contains one or multiple RawDataFiles <br>
     * ---- access mean retention time, mean m/z, maximum intensity, ...<br>
     */
    // use streams to filter, sort and create list
    List<PeakListRow> rowList = Arrays.stream(peakList.getRows()).filter(r -> r.getAverageHeight() > 5000).sorted(new PeakListRowSorter(SortingProperty.MZ, SortingDirection.Ascending)).collect(Collectors.toList());
    totalRows = rowList.size();
    // ###########################################################
    // OPTION 1: Streams
    // either use stream to process all rows
    rowList.stream().forEachOrdered(row -> {
        // check for cancelled state and stop
        if (isCanceled())
            return;
        // access details
        double mz = row.getAverageMZ();
        double intensity = row.getAverageHeight();
        double rt = row.getAverageRT();
        Feature peak = row.getBestPeak();
        // do stuff
        // ...
        // add row to peaklist result
        PeakListRow copy = copyPeakRow(row);
        resultPeakList.addRow(copy);
        // Update completion rate
        processedPeaks++;
    });
    // OPTION 2: For loop
    for (PeakListRow row : rowList) {
        // check for cancelled state and stop
        if (isCanceled())
            return;
        // access details
        double mz = row.getAverageMZ();
        double intensity = row.getAverageHeight();
        double rt = row.getAverageRT();
        Feature peak = row.getBestPeak();
        // do stuff
        // ...
        // add row to peaklist result
        PeakListRow copy = copyPeakRow(row);
        resultPeakList.addRow(copy);
        // Update completion rate
        processedPeaks++;
    }
    // add to project
    addResultToProject();
    logger.info("Finished on " + peakList);
    setStatus(TaskStatus.FINISHED);
}
Also used : SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) Feature(net.sf.mzmine.datamodel.Feature) PeakListRowSorter(net.sf.mzmine.util.PeakListRowSorter)

Example 28 with SimplePeakList

use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.

the class ADAPChromatogramBuilderTask method run.

/**
 * @see Runnable#run()
 */
public void run() {
    boolean writeChromCDF = true;
    setStatus(TaskStatus.PROCESSING);
    logger.info("Started chromatogram builder on " + dataFile);
    scans = scanSelection.getMatchingScans(dataFile);
    int[] allScanNumbers = scanSelection.getMatchingScanNumbers(dataFile);
    List<Double> rtListForChromCDF = new ArrayList<Double>();
    // Check if the scans are properly ordered by RT
    double prevRT = Double.NEGATIVE_INFINITY;
    for (Scan s : scans) {
        if (isCanceled()) {
            return;
        }
        if (writeChromCDF) {
            rtListForChromCDF.add(s.getRetentionTime());
        }
        if (s.getRetentionTime() < prevRT) {
            setStatus(TaskStatus.ERROR);
            final String msg = "Retention time of scan #" + s.getScanNumber() + " is smaller then the retention time of the previous scan." + " Please make sure you only use scans with increasing retention times." + " You can restrict the scan numbers in the parameters, or you can use the Crop filter module";
            setErrorMessage(msg);
            return;
        }
        prevRT = s.getRetentionTime();
    }
    // Check if the scans are MS1-only or MS2-only.
    int minMsLevel = Arrays.stream(scans).mapToInt(Scan::getMSLevel).min().orElseThrow(() -> new IllegalStateException("Cannot find the minimum MS level"));
    int maxMsLevel = Arrays.stream(scans).mapToInt(Scan::getMSLevel).max().orElseThrow(() -> new IllegalStateException("Cannot find the maximum MS level"));
    if (minMsLevel != maxMsLevel) {
        MZmineCore.getDesktop().displayMessage(null, "MZmine thinks that you are running ADAP Chromatogram builder on both MS1- and MS2-scans. " + "This will likely produce wrong results. " + "Please, set the scan filter parameter to a specific MS level");
    }
    // Create new feature list
    newPeakList = new SimplePeakList(dataFile + " " + suffix, dataFile);
    // make a list of all the data points
    // sort data points by intensity
    // loop through list
    // add data point to chromatogrm or make new one
    // update mz avg and other stuff
    // 
    // make a list of all the data points
    List<ExpandedDataPoint> allMzValues = new ArrayList<ExpandedDataPoint>();
    for (Scan scan : scans) {
        if (isCanceled())
            return;
        MassList massList = scan.getMassList(massListName);
        if (massList == null) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Scan " + dataFile + " #" + scan.getScanNumber() + " does not have a mass list " + massListName);
            return;
        }
        DataPoint[] mzValues = massList.getDataPoints();
        if (mzValues == null) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Mass list " + massListName + " does not contain m/z values for scan #" + scan.getScanNumber() + " of file " + dataFile);
            return;
        }
        for (DataPoint mzPeak : mzValues) {
            ExpandedDataPoint curDatP = new ExpandedDataPoint(mzPeak, scan.getScanNumber());
            allMzValues.add(curDatP);
        // corespondingScanNum.add(scan.getScanNumber());
        }
    }
    // Integer[] simpleCorespondingScanNums = new Integer[corespondingScanNum.size()];
    // corespondingScanNum.toArray(simpleCorespondingScanNums );
    ExpandedDataPoint[] simpleAllMzVals = new ExpandedDataPoint[allMzValues.size()];
    allMzValues.toArray(simpleAllMzVals);
    // sort data points by intensity
    Arrays.sort(simpleAllMzVals, new DataPointSorter(SortingProperty.Intensity, SortingDirection.Descending));
    // Exit if no peaks
    if (simpleAllMzVals.length == 0) {
        progress = 1.0;
        setStatus(TaskStatus.FINISHED);
        logger.info("Finished chromatogram builder with no peaks on " + dataFile);
        return;
    }
    double maxIntensity = simpleAllMzVals[0].getIntensity();
    // count starts at 1 since we already have added one with a single point.
    // Stopwatch stopwatch = Stopwatch.createUnstarted();
    // stopwatch2 = Stopwatch.createUnstarted();
    // Stopwatch stopwatch3 = Stopwatch.createUnstarted();
    progress = 0.0;
    double progressStep = (simpleAllMzVals.length > 0) ? 0.5 / simpleAllMzVals.length : 0.0;
    for (ExpandedDataPoint mzPeak : simpleAllMzVals) {
        progress += progressStep;
        if (isCanceled()) {
            return;
        }
        if (mzPeak == null || Double.isNaN(mzPeak.getMZ()) || Double.isNaN(mzPeak.getIntensity())) {
            continue;
        }
        // ////////////////////////////////////////////////
        Range<Double> containsPointRange = rangeSet.rangeContaining(mzPeak.getMZ());
        Range<Double> toleranceRange = mzTolerance.getToleranceRange(mzPeak.getMZ());
        if (containsPointRange == null) {
            // skip it entierly if the intensity is not high enough
            if (mzPeak.getIntensity() < minIntensityForStartChrom) {
                continue;
            }
            // look +- mz tolerance to see if ther is a range near by.
            // If there is use the proper boundry of that range for the
            // new range to insure than NON OF THE RANGES OVERLAP.
            Range<Double> plusRange = rangeSet.rangeContaining(toleranceRange.upperEndpoint());
            Range<Double> minusRange = rangeSet.rangeContaining(toleranceRange.lowerEndpoint());
            Double toBeLowerBound;
            Double toBeUpperBound;
            double cur_max_testing_mz = mzPeak.getMZ();
            // chromatogram so that none of the points are overlapping.
            if ((plusRange == null) && (minusRange == null)) {
                toBeLowerBound = toleranceRange.lowerEndpoint();
                toBeUpperBound = toleranceRange.upperEndpoint();
            } else if ((plusRange == null) && (minusRange != null)) {
                // the upper end point of the minus range will be the lower
                // range of the new one
                toBeLowerBound = minusRange.upperEndpoint();
                toBeUpperBound = toleranceRange.upperEndpoint();
            } else if ((minusRange == null) && (plusRange != null)) {
                toBeLowerBound = toleranceRange.lowerEndpoint();
                toBeUpperBound = plusRange.lowerEndpoint();
            // double tmp_this = plusRange.upperEndpoint();
            // System.out.println("tmp_this");
            } else if ((minusRange != null) && (plusRange != null)) {
                toBeLowerBound = minusRange.upperEndpoint();
                toBeUpperBound = plusRange.lowerEndpoint();
            } else {
                toBeLowerBound = 0.0;
                toBeUpperBound = 0.0;
            }
            if (toBeLowerBound < toBeUpperBound) {
                Range<Double> newRange = Range.open(toBeLowerBound, toBeUpperBound);
                ADAPChromatogram newChrom = new ADAPChromatogram(dataFile, allScanNumbers);
                newChrom.addMzPeak(mzPeak.getScanNumber(), mzPeak);
                newChrom.setHighPointMZ(mzPeak.getMZ());
                rangeToChromMap.put(newRange, newChrom);
                // also need to put it in the set -> this is where the range can be efficiently found.
                rangeSet.add(newRange);
            } else if (toBeLowerBound.equals(toBeUpperBound) && plusRange != null) {
                ADAPChromatogram curChrom = rangeToChromMap.get(plusRange);
                curChrom.addMzPeak(mzPeak.getScanNumber(), mzPeak);
            } else
                throw new IllegalStateException(String.format("Incorrect range [%f, %f] for m/z %f", toBeLowerBound, toBeUpperBound, mzPeak.getMZ()));
        } else {
            // In this case we do not need to update the rangeSet
            ADAPChromatogram curChrom = rangeToChromMap.get(containsPointRange);
            curChrom.addMzPeak(mzPeak.getScanNumber(), mzPeak);
            // update the entry in the map
            rangeToChromMap.put(containsPointRange, curChrom);
        }
    }
    // System.out.println("search chroms (ms): " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
    // System.out.println("making new chrom (ms): " + stopwatch2.elapsed(TimeUnit.MILLISECONDS));
    // finish chromatograms
    Set<Range<Double>> ranges = rangeSet.asRanges();
    Iterator<Range<Double>> RangeIterator = ranges.iterator();
    List<ADAPChromatogram> buildingChromatograms = new ArrayList<ADAPChromatogram>();
    progressStep = (ranges.size() > 0) ? 0.5 / ranges.size() : 0.0;
    while (RangeIterator.hasNext()) {
        if (isCanceled()) {
            return;
        }
        progress += progressStep;
        Range<Double> curRangeKey = RangeIterator.next();
        ADAPChromatogram chromatogram = rangeToChromMap.get(curRangeKey);
        chromatogram.finishChromatogram();
        // And remove chromatograms who dont have a certian number of continous points above the
        // IntensityThresh2 level.
        double numberOfContinuousPointsAboveNoise = chromatogram.findNumberOfContinuousPointsAboveNoise(IntensityThresh2);
        if (numberOfContinuousPointsAboveNoise < minimumScanSpan) {
            // requirements");
            continue;
        } else {
            buildingChromatograms.add(chromatogram);
        }
    }
    ADAPChromatogram[] chromatograms = buildingChromatograms.toArray(new ADAPChromatogram[0]);
    // Sort the final chromatograms by m/z
    Arrays.sort(chromatograms, new PeakSorter(SortingProperty.MZ, SortingDirection.Ascending));
    // Add the chromatograms to the new feature list
    for (Feature finishedPeak : chromatograms) {
        SimplePeakListRow newRow = new SimplePeakListRow(newPeakID);
        newPeakID++;
        newRow.addPeak(dataFile, finishedPeak);
        newPeakList.addRow(newRow);
    // finishedPeak.outputChromToFile();
    }
    // Add new peaklist to the project
    project.addPeakList(newPeakList);
    // Add quality parameters to peaks
    QualityParameters.calculateQualityParameters(newPeakList);
    progress = 1.0;
    setStatus(TaskStatus.FINISHED);
    logger.info("Finished chromatogram builder on " + dataFile);
}
Also used : Feature(net.sf.mzmine.datamodel.Feature) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) PeakSorter(net.sf.mzmine.util.PeakSorter) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) Range(com.google.common.collect.Range) DataPoint(net.sf.mzmine.datamodel.DataPoint) DataPointSorter(net.sf.mzmine.util.DataPointSorter) Scan(net.sf.mzmine.datamodel.Scan) MassList(net.sf.mzmine.datamodel.MassList)

Example 29 with SimplePeakList

use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.

the class RowsFilterTask method filterPeakListRows.

/**
 * Filter the feature list rows.
 *
 * @param peakList feature list to filter.
 * @return a new feature list with rows of the original feature list that pass the filtering.
 */
private PeakList filterPeakListRows(final PeakList peakList) {
    // Create new feature list.
    final PeakList newPeakList = new SimplePeakList(peakList.getName() + ' ' + parameters.getParameter(RowsFilterParameters.SUFFIX).getValue(), peakList.getRawDataFiles());
    // Copy previous applied methods.
    for (final PeakListAppliedMethod method : peakList.getAppliedMethods()) {
        newPeakList.addDescriptionOfAppliedTask(method);
    }
    // Add task description to peakList.
    newPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod(getTaskDescription(), parameters));
    // Get parameters.
    final boolean onlyIdentified = parameters.getParameter(RowsFilterParameters.HAS_IDENTITIES).getValue();
    final boolean filterByIdentityText = parameters.getParameter(RowsFilterParameters.IDENTITY_TEXT).getValue();
    final boolean filterByCommentText = parameters.getParameter(RowsFilterParameters.COMMENT_TEXT).getValue();
    final String groupingParameter = (String) parameters.getParameter(RowsFilterParameters.GROUPSPARAMETER).getValue();
    final boolean filterByMinPeakCount = parameters.getParameter(RowsFilterParameters.MIN_PEAK_COUNT).getValue();
    final boolean filterByMinIsotopePatternSize = parameters.getParameter(RowsFilterParameters.MIN_ISOTOPE_PATTERN_COUNT).getValue();
    final boolean filterByMzRange = parameters.getParameter(RowsFilterParameters.MZ_RANGE).getValue();
    final boolean filterByRtRange = parameters.getParameter(RowsFilterParameters.RT_RANGE).getValue();
    final boolean filterByDuration = parameters.getParameter(RowsFilterParameters.PEAK_DURATION).getValue();
    final boolean filterByFWHM = parameters.getParameter(RowsFilterParameters.FWHM).getValue();
    final boolean filterByCharge = parameters.getParameter(RowsFilterParameters.CHARGE).getValue();
    final boolean filterByKMD = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getValue();
    final boolean filterByMS2 = parameters.getParameter(RowsFilterParameters.MS2_Filter).getValue();
    final String removeRowString = parameters.getParameter(RowsFilterParameters.REMOVE_ROW).getValue();
    Double minCount = parameters.getParameter(RowsFilterParameters.MIN_PEAK_COUNT).getEmbeddedParameter().getValue();
    final boolean renumber = parameters.getParameter(RowsFilterParameters.Reset_ID).getValue();
    int rowsCount = 0;
    boolean removeRow = false;
    if (removeRowString.equals(RowsFilterParameters.removeRowChoices[0]))
        removeRow = false;
    else
        removeRow = true;
    // Keep rows that don't match any criteria. Keep by default.
    boolean filterRowCriteriaFailed = false;
    // Handle < 1 values for minPeakCount
    if ((minCount == null) || (minCount < 1))
        minCount = 1.0;
    // Round value down to nearest hole number
    int intMinCount = minCount.intValue();
    // Filter rows.
    final PeakListRow[] rows = peakList.getRows();
    totalRows = rows.length;
    for (processedRows = 0; !isCanceled() && processedRows < totalRows; processedRows++) {
        filterRowCriteriaFailed = false;
        final PeakListRow row = rows[processedRows];
        final int peakCount = getPeakCount(row, groupingParameter);
        // Check number of peaks.
        if (filterByMinPeakCount) {
            if (peakCount < intMinCount)
                filterRowCriteriaFailed = true;
        }
        // Check identities.
        if (onlyIdentified) {
            if (row.getPreferredPeakIdentity() == null)
                filterRowCriteriaFailed = true;
        }
        // Check average m/z.
        if (filterByMzRange) {
            final Range<Double> mzRange = parameters.getParameter(RowsFilterParameters.MZ_RANGE).getEmbeddedParameter().getValue();
            if (!mzRange.contains(row.getAverageMZ()))
                filterRowCriteriaFailed = true;
        }
        // Check average RT.
        if (filterByRtRange) {
            final Range<Double> rtRange = parameters.getParameter(RowsFilterParameters.RT_RANGE).getEmbeddedParameter().getValue();
            if (!rtRange.contains(row.getAverageRT()))
                filterRowCriteriaFailed = true;
        }
        // Search peak identity text.
        if (filterByIdentityText) {
            if (row.getPreferredPeakIdentity() == null)
                filterRowCriteriaFailed = true;
            if (row.getPreferredPeakIdentity() != null) {
                final String searchText = parameters.getParameter(RowsFilterParameters.IDENTITY_TEXT).getEmbeddedParameter().getValue().toLowerCase().trim();
                int numFailedIdentities = 0;
                PeakIdentity[] identities = row.getPeakIdentities();
                for (int index = 0; !isCanceled() && index < identities.length; index++) {
                    String rowText = identities[index].getName().toLowerCase().trim();
                    if (!rowText.contains(searchText))
                        numFailedIdentities += 1;
                }
                if (numFailedIdentities == identities.length)
                    filterRowCriteriaFailed = true;
            }
        }
        // Search peak comment text.
        if (filterByCommentText) {
            if (row.getComment() == null)
                filterRowCriteriaFailed = true;
            if (row.getComment() != null) {
                final String searchText = parameters.getParameter(RowsFilterParameters.COMMENT_TEXT).getEmbeddedParameter().getValue().toLowerCase().trim();
                final String rowText = row.getComment().toLowerCase().trim();
                if (!rowText.contains(searchText))
                    filterRowCriteriaFailed = true;
            }
        }
        // Calculate average duration and isotope pattern count.
        int maxIsotopePatternSizeOnRow = 1;
        double avgDuration = 0.0;
        final Feature[] peaks = row.getPeaks();
        for (final Feature p : peaks) {
            final IsotopePattern pattern = p.getIsotopePattern();
            if (pattern != null && maxIsotopePatternSizeOnRow < pattern.getNumberOfDataPoints()) {
                maxIsotopePatternSizeOnRow = pattern.getNumberOfDataPoints();
            }
            avgDuration += RangeUtils.rangeLength(p.getRawDataPointsRTRange());
        }
        // Check isotope pattern count.
        if (filterByMinIsotopePatternSize) {
            final int minIsotopePatternSize = parameters.getParameter(RowsFilterParameters.MIN_ISOTOPE_PATTERN_COUNT).getEmbeddedParameter().getValue();
            if (maxIsotopePatternSizeOnRow < minIsotopePatternSize)
                filterRowCriteriaFailed = true;
        }
        // Check average duration.
        avgDuration /= peakCount;
        if (filterByDuration) {
            final Range<Double> durationRange = parameters.getParameter(RowsFilterParameters.PEAK_DURATION).getEmbeddedParameter().getValue();
            if (!durationRange.contains(avgDuration))
                filterRowCriteriaFailed = true;
        }
        // Filter by FWHM range
        if (filterByFWHM) {
            final Range<Double> FWHMRange = parameters.getParameter(RowsFilterParameters.FWHM).getEmbeddedParameter().getValue();
            // If any of the peaks fail the FWHM criteria,
            Double FWHM_value = row.getBestPeak().getFWHM();
            if (FWHM_value != null && !FWHMRange.contains(FWHM_value))
                filterRowCriteriaFailed = true;
        }
        // Filter by charge range
        if (filterByCharge) {
            final Range<Integer> chargeRange = parameters.getParameter(RowsFilterParameters.CHARGE).getEmbeddedParameter().getValue();
            int charge = row.getBestPeak().getCharge();
            if (charge == 0 || !chargeRange.contains(charge))
                filterRowCriteriaFailed = true;
        }
        // Filter by KMD or RKM range
        if (filterByKMD) {
            // get embedded parameters
            final Range<Double> rangeKMD = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.kendrickMassDefectRange).getValue();
            final String kendrickMassBase = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.kendrickMassBase).getValue();
            final double shift = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.shift).getValue();
            final int charge = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.charge).getValue();
            final int divisor = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.divisor).getValue();
            final boolean useRemainderOfKendrickMass = parameters.getParameter(RowsFilterParameters.KENDRICK_MASS_DEFECT).getEmbeddedParameters().getParameter(KendrickMassDefectFilterParameters.useRemainderOfKendrickMass).getValue();
            // get m/z
            Double valueMZ = row.getBestPeak().getMZ();
            // calc exact mass of Kendrick mass base
            double exactMassFormula = FormulaUtils.calculateExactMass(kendrickMassBase);
            // calc exact mass of Kendrick mass factor
            double kendrickMassFactor = Math.round(exactMassFormula / divisor) / (exactMassFormula / divisor);
            double defectOrRemainder = 0.0;
            if (!useRemainderOfKendrickMass) {
                // calc Kendrick mass defect
                defectOrRemainder = Math.ceil(charge * (valueMZ * kendrickMassFactor)) - charge * (valueMZ * kendrickMassFactor);
            } else {
                // calc Kendrick mass remainder
                defectOrRemainder = (charge * (divisor - Math.round(FormulaUtils.calculateExactMass(kendrickMassBase))) * valueMZ) / // 
                FormulaUtils.calculateExactMass(kendrickMassBase) - Math.floor((charge * (divisor - Math.round(FormulaUtils.calculateExactMass(kendrickMassBase))) * valueMZ) / FormulaUtils.calculateExactMass(kendrickMassBase));
            }
            // shift Kendrick mass defect or remainder of Kendrick mass
            double kendrickMassDefectShifted = defectOrRemainder + shift - Math.floor(defectOrRemainder + shift);
            // check if shifted Kendrick mass defect or remainder of Kendrick mass is in range
            if (!rangeKMD.contains(kendrickMassDefectShifted))
                filterRowCriteriaFailed = true;
        }
        // Check ms2 filter .
        if (filterByMS2) {
            // iterates the peaks
            int failCounts = 0;
            for (int i = 0; i < peakCount; i++) {
                if (row.getPeaks()[i].getMostIntenseFragmentScanNumber() < 1) {
                    failCounts++;
                // filterRowCriteriaFailed = true;
                // break;
                }
            }
            if (failCounts == peakCount) {
                filterRowCriteriaFailed = true;
            }
        }
        if (!filterRowCriteriaFailed && !removeRow) {
            // Only add the row if none of the criteria have failed.
            rowsCount++;
            PeakListRow resetRow = copyPeakRow(row);
            if (renumber) {
                resetRow.setID(rowsCount);
            }
            newPeakList.addRow(resetRow);
        }
        if (filterRowCriteriaFailed && removeRow) {
            // Only remove rows that match *all* of the criteria, so add
            // rows that fail any of the criteria.
            rowsCount++;
            PeakListRow resetRow = copyPeakRow(row);
            if (renumber) {
                resetRow.setID(rowsCount);
            }
            newPeakList.addRow(resetRow);
        }
    }
    return newPeakList;
}
Also used : SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) PeakListAppliedMethod(net.sf.mzmine.datamodel.PeakList.PeakListAppliedMethod) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) Feature(net.sf.mzmine.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList)

Example 30 with SimplePeakList

use of net.sf.mzmine.datamodel.impl.SimplePeakList in project mzmine2 by mzmine.

the class PeakFinderTask method run.

public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running gap filler on " + peakList);
    // Calculate total number of scans in all files
    for (RawDataFile dataFile : peakList.getRawDataFiles()) {
        totalScans += dataFile.getNumOfScans(1);
    }
    processedScans = new AtomicInteger();
    // Create new feature list
    processedPeakList = new SimplePeakList(peakList + " " + suffix, peakList.getRawDataFiles());
    // Fill new feature list with empty rows
    for (int row = 0; row < peakList.getNumberOfRows(); row++) {
        PeakListRow sourceRow = peakList.getRow(row);
        PeakListRow newRow = new SimplePeakListRow(sourceRow.getID());
        newRow.setComment(sourceRow.getComment());
        for (PeakIdentity ident : sourceRow.getPeakIdentities()) {
            newRow.addPeakIdentity(ident, false);
        }
        if (sourceRow.getPreferredPeakIdentity() != null) {
            newRow.setPreferredPeakIdentity(sourceRow.getPreferredPeakIdentity());
        }
        processedPeakList.addRow(newRow);
    }
    if (rtCorrection) {
        totalScans *= 2;
        // Fill the gaps of a random sample using all the other samples and
        // take it as master list
        // to fill the gaps of the other samples
        masterSample = (int) Math.floor(Math.random() * peakList.getNumberOfRawDataFiles());
        fillList(MASTERLIST);
        // Process all raw data files
        fillList(!MASTERLIST);
    } else {
        // Process all raw data files
        IntStream rawStream = IntStream.range(0, peakList.getNumberOfRawDataFiles());
        if (useParallelStream)
            rawStream = rawStream.parallel();
        rawStream.forEach(i -> {
            // Canceled?
            if (isCanceled()) {
                // inside stream - only skips this element
                return;
            }
            RawDataFile dataFile = peakList.getRawDataFile(i);
            List<Gap> gaps = new ArrayList<Gap>();
            // if necessary
            for (int row = 0; row < peakList.getNumberOfRows(); row++) {
                // Canceled?
                if (isCanceled()) {
                    // inside stream - only skips this element
                    return;
                }
                PeakListRow sourceRow = peakList.getRow(row);
                PeakListRow newRow = processedPeakList.getRow(row);
                Feature sourcePeak = sourceRow.getPeak(dataFile);
                if (sourcePeak == null) {
                    // Create a new gap
                    Range<Double> mzRange = mzTolerance.getToleranceRange(sourceRow.getAverageMZ());
                    Range<Double> rtRange = rtTolerance.getToleranceRange(sourceRow.getAverageRT());
                    Gap newGap = new Gap(newRow, dataFile, mzRange, rtRange, intTolerance);
                    gaps.add(newGap);
                } else {
                    newRow.addPeak(dataFile, sourcePeak);
                }
            }
            // Stop processing this file if there are no gaps
            if (gaps.size() == 0) {
                processedScans.addAndGet(dataFile.getNumOfScans());
                return;
            }
            // Get all scans of this data file
            int[] scanNumbers = dataFile.getScanNumbers(1);
            // Process each scan
            for (int scanNumber : scanNumbers) {
                // Canceled?
                if (isCanceled()) {
                    // inside stream - only skips this element
                    return;
                }
                // Get the scan
                Scan scan = dataFile.getScan(scanNumber);
                // Feed this scan to all gaps
                for (Gap gap : gaps) {
                    gap.offerNextScan(scan);
                }
                processedScans.incrementAndGet();
            }
            // Finalize gaps
            for (Gap gap : gaps) {
                gap.noMoreOffers();
            }
        });
    }
    // terminate - stream only skips all elements
    if (isCanceled())
        return;
    // Append processed feature list to the project
    project.addPeakList(processedPeakList);
    // Add quality parameters to peaks
    QualityParameters.calculateQualityParameters(processedPeakList);
    // Add task description to peakList
    processedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Gap filling ", parameters));
    // Remove the original peaklist if requested
    if (removeOriginal)
        project.removePeakList(peakList);
    logger.info("Finished gap-filling on " + peakList);
    setStatus(TaskStatus.FINISHED);
}
Also used : ArrayList(java.util.ArrayList) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) Feature(net.sf.mzmine.datamodel.Feature) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scan(net.sf.mzmine.datamodel.Scan) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) IntStream(java.util.stream.IntStream)

Aggregations

SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)47 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)39 SimplePeakListAppliedMethod (net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)29 SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)29 Feature (net.sf.mzmine.datamodel.Feature)25 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)25 DataPoint (net.sf.mzmine.datamodel.DataPoint)19 PeakList (net.sf.mzmine.datamodel.PeakList)18 PeakListAppliedMethod (net.sf.mzmine.datamodel.PeakList.PeakListAppliedMethod)14 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)14 ArrayList (java.util.ArrayList)11 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)10 PeakListRowSorter (net.sf.mzmine.util.PeakListRowSorter)10 Scan (net.sf.mzmine.datamodel.Scan)8 Vector (java.util.Vector)7 PeakIdentity (net.sf.mzmine.datamodel.PeakIdentity)7 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)5 Desktop (net.sf.mzmine.desktop.Desktop)5 HeadLessDesktop (net.sf.mzmine.desktop.impl.HeadLessDesktop)5 SimpleIsotopePattern (net.sf.mzmine.datamodel.impl.SimpleIsotopePattern)4