Search in sources :

Example 46 with SimplePeakList

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

the class StandardCompoundNormalizerTask method run.

public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.finest("Starting standard compound normalization of " + originalPeakList + " using " + normalizationType + " (total " + standardRows.length + " standard peaks)");
    // Check if we have standards
    if (standardRows.length == 0) {
        setErrorMessage("No internal standard peaks selected");
        setStatus(TaskStatus.ERROR);
        return;
    }
    // Initialize new alignment result for the normalized result
    normalizedPeakList = new SimplePeakList(originalPeakList + " " + suffix, originalPeakList.getRawDataFiles());
    // Copy raw data files from original alignment result to new alignment
    // result
    totalRows = originalPeakList.getNumberOfRows();
    // Loop through all rows
    rowIteration: for (PeakListRow row : originalPeakList.getRows()) {
        // Cancel ?
        if (isCanceled()) {
            return;
        }
        // Do not add the standard rows to the new peaklist
        for (int i = 0; i < standardRows.length; i++) {
            if (row == standardRows[i]) {
                processedRows++;
                continue rowIteration;
            }
        }
        // Copy comment and identification
        SimplePeakListRow normalizedRow = new SimplePeakListRow(row.getID());
        PeakUtils.copyPeakListRowProperties(row, normalizedRow);
        // Get m/z and RT of the current row
        double mz = row.getAverageMZ();
        double rt = row.getAverageRT();
        // Loop through all raw data files
        for (RawDataFile file : originalPeakList.getRawDataFiles()) {
            double[] normalizationFactors = null;
            double[] normalizationFactorWeights = null;
            if (normalizationType == StandardUsageType.Nearest) {
                // Search for nearest standard
                PeakListRow nearestStandardRow = null;
                double nearestStandardRowDistance = Double.MAX_VALUE;
                for (int standardRowIndex = 0; standardRowIndex < standardRows.length; standardRowIndex++) {
                    PeakListRow standardRow = standardRows[standardRowIndex];
                    double stdMZ = standardRow.getAverageMZ();
                    double stdRT = standardRow.getAverageRT();
                    double distance = MZvsRTBalance * Math.abs(mz - stdMZ) + Math.abs(rt - stdRT);
                    if (distance <= nearestStandardRowDistance) {
                        nearestStandardRow = standardRow;
                        nearestStandardRowDistance = distance;
                    }
                }
                assert nearestStandardRow != null;
                // Calc and store a single normalization factor
                normalizationFactors = new double[1];
                normalizationFactorWeights = new double[1];
                Feature standardPeak = nearestStandardRow.getPeak(file);
                if (standardPeak == null) {
                    // What to do if standard peak is not available?
                    normalizationFactors[0] = 1.0;
                } else {
                    if (peakMeasurementType == PeakMeasurementType.HEIGHT) {
                        normalizationFactors[0] = standardPeak.getHeight();
                    } else {
                        normalizationFactors[0] = standardPeak.getArea();
                    }
                }
                logger.finest("Normalizing row #" + row.getID() + " using standard peak " + standardPeak + ", factor " + normalizationFactors[0]);
                normalizationFactorWeights[0] = 1.0f;
            }
            if (normalizationType == StandardUsageType.Weighted) {
                // Add all standards as factors, and use distance as weight
                normalizationFactors = new double[standardRows.length];
                normalizationFactorWeights = new double[standardRows.length];
                for (int standardRowIndex = 0; standardRowIndex < standardRows.length; standardRowIndex++) {
                    PeakListRow standardRow = standardRows[standardRowIndex];
                    double stdMZ = standardRow.getAverageMZ();
                    double stdRT = standardRow.getAverageRT();
                    double distance = MZvsRTBalance * Math.abs(mz - stdMZ) + Math.abs(rt - stdRT);
                    Feature standardPeak = standardRow.getPeak(file);
                    if (standardPeak == null) {
                        // What to do if standard peak is not available?
                        normalizationFactors[standardRowIndex] = 1.0;
                        normalizationFactorWeights[standardRowIndex] = 0.0;
                    } else {
                        if (peakMeasurementType == PeakMeasurementType.HEIGHT) {
                            normalizationFactors[standardRowIndex] = standardPeak.getHeight();
                        } else {
                            normalizationFactors[standardRowIndex] = standardPeak.getArea();
                        }
                        normalizationFactorWeights[standardRowIndex] = 1 / distance;
                    }
                }
            }
            assert normalizationFactors != null;
            assert normalizationFactorWeights != null;
            // Calculate a single normalization factor as weighted average
            // of all factors
            double weightedSum = 0.0f;
            double sumOfWeights = 0.0f;
            for (int factorIndex = 0; factorIndex < normalizationFactors.length; factorIndex++) {
                weightedSum += normalizationFactors[factorIndex] * normalizationFactorWeights[factorIndex];
                sumOfWeights += normalizationFactorWeights[factorIndex];
            }
            double normalizationFactor = weightedSum / sumOfWeights;
            // For simple scaling of the normalized values
            normalizationFactor = normalizationFactor / 100.0f;
            logger.finest("Normalizing row #" + row.getID() + "[" + file + "] using factor " + normalizationFactor);
            // How to handle zero normalization factor?
            if (normalizationFactor == 0.0)
                normalizationFactor = Double.MIN_VALUE;
            // Normalize peak
            Feature originalPeak = row.getPeak(file);
            if (originalPeak != null) {
                SimpleFeature normalizedPeak = new SimpleFeature(originalPeak);
                PeakUtils.copyPeakProperties(originalPeak, normalizedPeak);
                double normalizedHeight = originalPeak.getHeight() / normalizationFactor;
                double normalizedArea = originalPeak.getArea() / normalizationFactor;
                normalizedPeak.setHeight(normalizedHeight);
                normalizedPeak.setArea(normalizedArea);
                normalizedRow.addPeak(file, normalizedPeak);
            }
        }
        normalizedPeakList.addRow(normalizedRow);
        processedRows++;
    }
    // Add new peaklist to the project
    project.addPeakList(normalizedPeakList);
    // Load previous applied methods
    for (PeakListAppliedMethod proc : originalPeakList.getAppliedMethods()) {
        normalizedPeakList.addDescriptionOfAppliedTask(proc);
    }
    // Add task description to peakList
    normalizedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Standard compound normalization", parameters));
    // Remove the original peaklist if requested
    if (removeOriginal)
        project.removePeakList(originalPeakList);
    logger.info("Finished standard compound normalizer");
    setStatus(TaskStatus.FINISHED);
}
Also used : SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) PeakListAppliedMethod(net.sf.mzmine.datamodel.PeakList.PeakListAppliedMethod) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) Feature(net.sf.mzmine.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature)

Example 47 with SimplePeakList

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

the class ADAP3DTask method run.

/**
 * @see Runnable#run()
 */
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Started ADAP3D on " + dataFile);
    List<Scan> selectedScans = Arrays.asList(scanSelection.getMatchingScans(dataFile));
    // Check if we have any scans
    if (selectedScans.size() == 0) {
        setStatus(TaskStatus.ERROR);
        setErrorMessage("No scans match the selected criteria");
        return;
    }
    // Check if the scans are properly ordered by RT
    double prevRT = Double.NEGATIVE_INFINITY;
    for (Scan s : selectedScans) {
        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();
    }
    // Run MSDK module
    MZmineToMSDKRawDataFile msdkRawDataFile = new MZmineToMSDKRawDataFile(dataFile);
    Predicate<MsScan> scanSelectionPredicate = scan -> selectedScans.contains(((MZmineToMSDKMsScan) scan).getMzmineScan());
    msdkADAP3DMethod = new ADAP3DFeatureDetectionMethod(msdkRawDataFile, scanSelectionPredicate, new ADAP3DFeatureDetectionParameters());
    List<Feature> features = null;
    try {
        if (isCanceled())
            return;
        features = msdkADAP3DMethod.execute();
        if (isCanceled())
            return;
    } catch (Exception e) {
        e.printStackTrace();
        setStatus(TaskStatus.ERROR);
        setErrorMessage("Error in ADAP3D: " + e.getMessage());
    }
    if (features == null)
        features = new ArrayList<>(0);
    logger.info("ADAP3D detected " + features.size() + " features in " + dataFile + ", converting to MZmine peaklist");
    // Create new MZmine 2 feature list
    SimplePeakList newPeakList = new SimplePeakList(dataFile + " " + suffix, dataFile);
    int rowId = 1;
    for (Feature msdkFeature : features) {
        if (isCanceled())
            return;
        SimpleFeature mzmineFeature = new SimpleFeature(dataFile, FeatureStatus.DETECTED, msdkFeature);
        PeakListRow row = new SimplePeakListRow(rowId);
        row.addPeak(dataFile, mzmineFeature);
        newPeakList.addRow(row);
        rowId++;
    }
    // Add new peaklist to the project
    project.addPeakList(newPeakList);
    // Add quality parameters to peaks
    QualityParameters.calculateQualityParameters(newPeakList);
    setStatus(TaskStatus.FINISHED);
    logger.info("Finished ADAP3D feature detection on " + dataFile);
}
Also used : FeatureStatus(net.sf.mzmine.datamodel.Feature.FeatureStatus) Scan(net.sf.mzmine.datamodel.Scan) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) Arrays(java.util.Arrays) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) Feature(io.github.msdk.datamodel.Feature) Predicate(java.util.function.Predicate) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) Logger(java.util.logging.Logger) MZmineToMSDKRawDataFile(net.sf.mzmine.datamodel.impl.MZmineToMSDKRawDataFile) ArrayList(java.util.ArrayList) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) MZmineToMSDKMsScan(net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan) MsScan(io.github.msdk.datamodel.MsScan) List(java.util.List) ParameterSet(net.sf.mzmine.parameters.ParameterSet) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) QualityParameters(net.sf.mzmine.modules.peaklistmethods.qualityparameters.QualityParameters) MZmineProject(net.sf.mzmine.datamodel.MZmineProject) ADAP3DFeatureDetectionMethod(io.github.msdk.featuredetection.adap3d.ADAP3DFeatureDetectionMethod) ADAP3DFeatureDetectionParameters(io.github.msdk.featuredetection.adap3d.ADAP3DFeatureDetectionParameters) ScanSelection(net.sf.mzmine.parameters.parametertypes.selectors.ScanSelection) MZmineToMSDKMsScan(net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan) MsScan(io.github.msdk.datamodel.MsScan) ArrayList(java.util.ArrayList) MZmineToMSDKRawDataFile(net.sf.mzmine.datamodel.impl.MZmineToMSDKRawDataFile) Feature(io.github.msdk.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) ADAP3DFeatureDetectionParameters(io.github.msdk.featuredetection.adap3d.ADAP3DFeatureDetectionParameters) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) Scan(net.sf.mzmine.datamodel.Scan) MZmineToMSDKMsScan(net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan) MsScan(io.github.msdk.datamodel.MsScan) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) ADAP3DFeatureDetectionMethod(io.github.msdk.featuredetection.adap3d.ADAP3DFeatureDetectionMethod)

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