Search in sources :

Example 6 with SimpleIsotopePattern

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

the class IsotopeGrouperTask method run.

/**
 * @see Runnable#run()
 */
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running isotopic peak grouper on " + peakList);
    // We assume source peakList contains one datafile
    RawDataFile dataFile = peakList.getRawDataFile(0);
    // Create a new deisotoped peakList
    deisotopedPeakList = new SimplePeakList(peakList + " " + suffix, peakList.getRawDataFiles());
    // Collect all selected charge states
    int[] charges = new int[maximumCharge];
    for (int i = 0; i < maximumCharge; i++) charges[i] = i + 1;
    // Sort peaks by descending height
    Feature[] sortedPeaks = peakList.getPeaks(dataFile);
    Arrays.sort(sortedPeaks, new PeakSorter(SortingProperty.Height, SortingDirection.Descending));
    // Loop through all peaks
    totalPeaks = sortedPeaks.length;
    for (int ind = 0; ind < totalPeaks; ind++) {
        if (isCanceled())
            return;
        Feature aPeak = sortedPeaks[ind];
        // Check if peak was already deleted
        if (aPeak == null) {
            processedPeaks++;
            continue;
        }
        // Check which charge state fits best around this peak
        int bestFitCharge = 0;
        int bestFitScore = -1;
        Vector<Feature> bestFitPeaks = null;
        for (int charge : charges) {
            Vector<Feature> fittedPeaks = new Vector<Feature>();
            fittedPeaks.add(aPeak);
            fitPattern(fittedPeaks, aPeak, charge, sortedPeaks);
            int score = fittedPeaks.size();
            if ((score > bestFitScore) || ((score == bestFitScore) && (bestFitCharge > charge))) {
                bestFitScore = score;
                bestFitCharge = charge;
                bestFitPeaks = fittedPeaks;
            }
        }
        PeakListRow oldRow = peakList.getPeakRow(aPeak);
        assert bestFitPeaks != null;
        // isotope, we skip this left the original peak in the feature list.
        if (bestFitPeaks.size() == 1) {
            deisotopedPeakList.addRow(oldRow);
            processedPeaks++;
            continue;
        }
        // Convert the peak pattern to array
        Feature[] originalPeaks = bestFitPeaks.toArray(new Feature[0]);
        // Create a new SimpleIsotopePattern
        DataPoint[] isotopes = new DataPoint[bestFitPeaks.size()];
        for (int i = 0; i < isotopes.length; i++) {
            Feature p = originalPeaks[i];
            isotopes[i] = new SimpleDataPoint(p.getMZ(), p.getHeight());
        }
        SimpleIsotopePattern newPattern = new SimpleIsotopePattern(isotopes, IsotopePatternStatus.DETECTED, aPeak.toString());
        // the lowest m/z peak
        if (chooseMostIntense) {
            Arrays.sort(originalPeaks, new PeakSorter(SortingProperty.Height, SortingDirection.Descending));
        } else {
            Arrays.sort(originalPeaks, new PeakSorter(SortingProperty.MZ, SortingDirection.Ascending));
        }
        Feature newPeak = new SimpleFeature(originalPeaks[0]);
        newPeak.setIsotopePattern(newPattern);
        newPeak.setCharge(bestFitCharge);
        // Keep old ID
        int oldID = oldRow.getID();
        SimplePeakListRow newRow = new SimplePeakListRow(oldID);
        PeakUtils.copyPeakListRowProperties(oldRow, newRow);
        newRow.addPeak(dataFile, newPeak);
        deisotopedPeakList.addRow(newRow);
        // Remove all peaks already assigned to isotope pattern
        for (int i = 0; i < sortedPeaks.length; i++) {
            if (bestFitPeaks.contains(sortedPeaks[i]))
                sortedPeaks[i] = null;
        }
        // Update completion rate
        processedPeaks++;
    }
    // Add new peakList to the project
    project.addPeakList(deisotopedPeakList);
    // Load previous applied methods
    for (PeakListAppliedMethod proc : peakList.getAppliedMethods()) {
        deisotopedPeakList.addDescriptionOfAppliedTask(proc);
    }
    // Add task description to peakList
    deisotopedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Isotopic peaks grouper", parameters));
    // Remove the original peakList if requested
    if (removeOriginal)
        project.removePeakList(peakList);
    logger.info("Finished isotopic peak grouper on " + peakList);
    setStatus(TaskStatus.FINISHED);
}
Also used : SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) PeakListAppliedMethod(net.sf.mzmine.datamodel.PeakList.PeakListAppliedMethod) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) Feature(net.sf.mzmine.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) PeakSorter(net.sf.mzmine.util.PeakSorter) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) Vector(java.util.Vector)

Example 7 with SimpleIsotopePattern

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

the class DPPIsotopeGrouperTask method compressIsotopeDataSets.

/**
 * This method generates a single IsotopesDataSet from all detected isotope patterns in the
 * results.
 *
 * @param dataPoints
 * @return
 */
private IsotopesDataSet compressIsotopeDataSets(ProcessedDataPoint[] dataPoints) {
    List<IsotopePattern> list = new ArrayList<>();
    for (ProcessedDataPoint dp : dataPoints) {
        if (dp.resultTypeExists(ResultType.ISOTOPEPATTERN)) {
            list.add(((DPPIsotopePatternResult) dp.getFirstResultByType(ResultType.ISOTOPEPATTERN)).getValue());
        }
    }
    if (list.isEmpty())
        return null;
    List<DataPoint> dpList = new ArrayList<>();
    for (IsotopePattern pattern : list) {
        for (DataPoint dp : pattern.getDataPoints()) dpList.add(dp);
    }
    if (dpList.isEmpty())
        return null;
    IsotopePattern full = new SimpleIsotopePattern(dpList.toArray(new DataPoint[0]), IsotopePatternStatus.DETECTED, "Isotope patterns");
    return new IsotopesDataSet(full);
}
Also used : ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint) ArrayList(java.util.ArrayList) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) IsotopesDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.IsotopesDataSet)

Example 8 with SimpleIsotopePattern

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

the class IsotopePatternUtils method mergeIsotopePatternResults.

// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// old-new
public static void mergeIsotopePatternResults(ProcessedDataPoint dp) {
    if (!dp.resultTypeExists(ResultType.ISOTOPEPATTERN))
        return;
    List<DPPIsotopePatternResult> patternResults = getIsotopePatternResults(dp);
    List<DPPResult<?>> newResults = new ArrayList<>();
    for (DPPIsotopePatternResult dpPatternResult : patternResults) {
        ProcessedDataPoint[] dpPattern = dpPatternResult.getLinkedDataPoints();
        int patternCharge = dpPatternResult.getCharge();
        for (ProcessedDataPoint p : dpPattern) {
            List<DPPIsotopePatternResult> pPatternResults = getIsotopePatternResults(p);
            for (DPPIsotopePatternResult pPatternResult : pPatternResults) {
                if (pPatternResult.getCharge() != patternCharge)
                    continue;
                ProcessedDataPoint[] dataPoints = pPatternResult.getLinkedDataPoints();
                p.removeResult(pPatternResult);
                newResults.add(new DPPIsotopePatternResult(new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.DETECTED, ""), dataPoints, patternCharge));
            }
        }
    }
    dp.getAllResultsByType(ResultType.ISOTOPEPATTERN);
    dp.addAllResults(newResults);
    logger.finest("-------------------------");
    for (DPPResult<?> result : newResults) logger.finest("FINAL: " + format.format(dp.getMZ()) + " pattern: " + getResultIsoComp((DPPIsotopePatternResult) result));
// TODO: test
}
Also used : DPPResult(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.results.DPPResult) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint) DPPIsotopePatternResult(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.results.DPPIsotopePatternResult) ArrayList(java.util.ArrayList) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint)

Example 9 with SimpleIsotopePattern

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

the class IsotopePatternUtils method checkOverlappingIsotopes.

public static IsotopePattern checkOverlappingIsotopes(IsotopePattern pattern, IIsotope[] isotopes, double mergeWidth, double minAbundance) {
    DataPoint[] dp = pattern.getDataPoints();
    double basemz = dp[0].getMZ();
    List<DataPoint> newPeaks = new ArrayList<DataPoint>();
    double isotopeBaseMass = 0d;
    for (IIsotope isotope : isotopes) {
        if (isotope.getNaturalAbundance() > minAbundance) {
            isotopeBaseMass = isotope.getExactMass();
            logger.info("isotopeBaseMass of " + isotope.getSymbol() + " = " + isotopeBaseMass);
            break;
        }
    }
    // loop all new isotopes
    for (IIsotope isotope : isotopes) {
        if (isotope.getNaturalAbundance() < minAbundance)
            continue;
        // the difference added by the heavier isotope peak
        double possiblemzdiff = isotope.getExactMass() - isotopeBaseMass;
        if (possiblemzdiff < 0.000001)
            continue;
        boolean add = true;
        for (DataPoint patternDataPoint : dp) {
            // here check for every peak in the pattern, if a new peak would overlap
            // if it overlaps good, we dont need to add a new peak
            int i = 1;
            do {
                if (Math.abs(patternDataPoint.getMZ() * i - possiblemzdiff) <= mergeWidth) {
                    // TODO: maybe we should do a average of the masses? i can'T say if it makes sense,
                    // since
                    // we're just looking for isotope mass differences and dont look at the total
                    // composition,
                    // so we dont know the intensity ratios
                    logger.info("possible overlap found: " + i + " * pattern dp = " + patternDataPoint.getMZ() + "\toverlaps with " + isotope.getMassNumber() + isotope.getSymbol() + " (" + (isotopeBaseMass - isotope.getExactMass()) + ")\tdiff: " + Math.abs(patternDataPoint.getMZ() * i - possiblemzdiff));
                    add = false;
                }
                i++;
            // logger.info("do");
            } while (patternDataPoint.getMZ() * i <= possiblemzdiff + mergeWidth && patternDataPoint.getMZ() != 0.0);
        }
        if (add)
            newPeaks.add(new SimpleDataPoint(possiblemzdiff, 1));
    }
    // DataPoint[] newDataPoints = new SimpleDataPoint[dp.length + newPeaks.size()];
    for (DataPoint p : dp) {
        newPeaks.add(p);
    }
    newPeaks.sort((o1, o2) -> {
        return Double.compare(o1.getMZ(), o2.getMZ());
    });
    return new SimpleIsotopePattern(newPeaks.toArray(new DataPoint[0]), IsotopePatternStatus.PREDICTED, "");
}
Also used : IIsotope(org.openscience.cdk.interfaces.IIsotope) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint) ArrayList(java.util.ArrayList) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) ProcessedDataPoint(net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint)

Example 10 with SimpleIsotopePattern

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

the class CameraSearchTask method groupPeaksByIsotope.

/**
 * Uses Isotope-field in PeakIdentity to group isotopes and build spectrum
 *
 * @param peakList PeakList object
 * @return new PeakList object
 */
private PeakList groupPeaksByIsotope(PeakList peakList) {
    // Create new feature list.
    final PeakList combinedPeakList = new SimplePeakList(peakList + " " + parameters.getParameter(CameraSearchParameters.SUFFIX).getValue(), peakList.getRawDataFiles());
    // Load previous applied methods.
    for (final PeakList.PeakListAppliedMethod method : peakList.getAppliedMethods()) {
        combinedPeakList.addDescriptionOfAppliedTask(method);
    }
    // Add task description to feature list.
    combinedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Bioconductor CAMERA", parameters));
    // ------------------------------------------------
    // Find unique isotopes belonging to the same group
    // ------------------------------------------------
    Set<String> isotopeGroups = new HashSet<>();
    for (PeakListRow row : peakList.getRows()) {
        PeakIdentity identity = row.getPreferredPeakIdentity();
        if (identity == null)
            continue;
        String isotope = identity.getPropertyValue("Isotope");
        if (isotope == null)
            continue;
        String isotopeGroup = isotope.substring(1, isotope.indexOf("]"));
        if (isotopeGroup == null || isotopeGroup.length() == 0)
            continue;
        isotopeGroups.add(isotopeGroup);
    }
    List<PeakListRow> groupRows = new ArrayList<>();
    Set<String> groupNames = new HashSet<>();
    Map<Double, Double> spectrum = new HashMap<>();
    List<PeakListRow> newPeakListRows = new ArrayList<>();
    for (String isotopeGroup : isotopeGroups) {
        // -----------------------------------------
        // Find all peaks belonging to isotopeGroups
        // -----------------------------------------
        groupRows.clear();
        groupNames.clear();
        spectrum.clear();
        int minLength = Integer.MAX_VALUE;
        PeakListRow groupRow = null;
        for (PeakListRow row : peakList.getRows()) {
            PeakIdentity identity = row.getPreferredPeakIdentity();
            if (identity == null)
                continue;
            String isotope = identity.getPropertyValue("Isotope");
            if (isotope == null)
                continue;
            String isoGroup = isotope.substring(1, isotope.indexOf("]"));
            if (isoGroup == null)
                continue;
            if (isoGroup.equals(isotopeGroup)) {
                groupRows.add(row);
                groupNames.add(identity.getName());
                spectrum.put(row.getAverageMZ(), row.getAverageHeight());
                if (isoGroup.length() < minLength) {
                    minLength = isoGroup.length();
                    groupRow = row;
                }
            }
        }
        // Skip peaks that have different identity names (belong to different pcgroup)
        if (groupRow == null || groupNames.size() != 1)
            continue;
        if (groupRow == null)
            continue;
        PeakIdentity identity = groupRow.getPreferredPeakIdentity();
        if (identity == null)
            continue;
        DataPoint[] dataPoints = new DataPoint[spectrum.size()];
        int count = 0;
        for (Entry<Double, Double> e : spectrum.entrySet()) dataPoints[count++] = new SimpleDataPoint(e.getKey(), e.getValue());
        IsotopePattern pattern = new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, "Spectrum");
        groupRow.getBestPeak().setIsotopePattern(pattern);
        // combinedPeakList.addRow(groupRow);
        newPeakListRows.add(groupRow);
    }
    if (includeSingletons) {
        for (PeakListRow row : peakList.getRows()) {
            PeakIdentity identity = row.getPreferredPeakIdentity();
            if (identity == null)
                continue;
            String isotope = identity.getPropertyValue("Isotope");
            if (isotope == null || isotope.length() == 0) {
                DataPoint[] dataPoints = new DataPoint[1];
                dataPoints[0] = new SimpleDataPoint(row.getAverageMZ(), row.getAverageHeight());
                IsotopePattern pattern = new SimpleIsotopePattern(dataPoints, IsotopePatternStatus.PREDICTED, "Spectrum");
                row.getBestPeak().setIsotopePattern(pattern);
                newPeakListRows.add(row);
            }
        }
    }
    // ------------------------------------
    // Sort new peak rows by retention time
    // ------------------------------------
    Collections.sort(newPeakListRows, new Comparator<PeakListRow>() {

        @Override
        public int compare(PeakListRow row1, PeakListRow row2) {
            double retTime1 = row1.getAverageRT();
            double retTime2 = row2.getAverageRT();
            return Double.compare(retTime1, retTime2);
        }
    });
    for (PeakListRow row : newPeakListRows) combinedPeakList.addRow(row);
    return combinedPeakList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimpleIsotopePattern(net.sf.mzmine.datamodel.impl.SimpleIsotopePattern) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) HashSet(java.util.HashSet)

Aggregations

DataPoint (net.sf.mzmine.datamodel.DataPoint)17 SimpleIsotopePattern (net.sf.mzmine.datamodel.impl.SimpleIsotopePattern)17 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)15 ArrayList (java.util.ArrayList)11 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)6 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)6 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)5 ExtendedIsotopePattern (net.sf.mzmine.datamodel.impl.ExtendedIsotopePattern)5 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)5 SimplePeakIdentity (net.sf.mzmine.datamodel.impl.SimplePeakIdentity)5 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)5 HashMap (java.util.HashMap)4 SimplePeakListAppliedMethod (net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)4 ProcessedDataPoint (net.sf.mzmine.modules.visualization.spectra.simplespectra.datapointprocessing.datamodel.ProcessedDataPoint)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 IOException (java.io.IOException)3 Feature (net.sf.mzmine.datamodel.Feature)3 FeatureStatus (net.sf.mzmine.datamodel.Feature.FeatureStatus)3 PeakIdentity (net.sf.mzmine.datamodel.PeakIdentity)3