Search in sources :

Example 16 with SimplePeakListRow

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

the class GnpsFbmnMgfExportTask method copyPeakRow.

/**
 * Create a copy of a feature list row.
 */
private static PeakListRow copyPeakRow(final PeakListRow row) {
    // Copy the feature list row.
    final PeakListRow newRow = new SimplePeakListRow(row.getID());
    PeakUtils.copyPeakListRowProperties(row, newRow);
    // Copy the peaks.
    for (final Feature peak : row.getPeaks()) {
        final Feature newPeak = new SimpleFeature(peak);
        PeakUtils.copyPeakProperties(peak, newPeak);
        newRow.addPeak(peak.getDataFile(), newPeak);
    }
    return newRow;
}
Also used : SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) Feature(net.sf.mzmine.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature)

Example 17 with SimplePeakListRow

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

the class HierarAlignerGCTask method run.

/**
 * @see Runnable#run()
 */
public void run() {
    // Check options validity
    if ((Math.abs(mzWeight) < EPSILON) && (Math.abs(rtWeight) < EPSILON)) {
        setStatus(TaskStatus.ERROR);
        setErrorMessage("Cannot run alignment, all the weight parameters are zero!");
        return;
    }
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running join aligner");
    // TIME STUFF
    long startTime, endTime;
    float ms;
    // 
    if (DEBUG)
        startTime = System.currentTimeMillis();
    // MEMORY STUFF
    Runtime run_time = Runtime.getRuntime();
    Long prevTotal = 0l;
    Long prevFree = run_time.freeMemory();
    if (DEBUG)
        printMemoryUsage(logger, run_time, prevTotal, prevFree, "START TASK...");
    // - third for actual alignment
    for (int i = 0; i < peakLists.length; i++) {
        totalRows += peakLists[i].getNumberOfRows() * 3;
    }
    // Collect all data files
    Vector<RawDataFile> allDataFiles = new Vector<RawDataFile>();
    for (PeakList peakList : peakLists) {
        for (RawDataFile dataFile : peakList.getRawDataFiles()) {
            // Each data file can only have one column in aligned feature list
            if (allDataFiles.contains(dataFile)) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Cannot run alignment, because file " + dataFile + " is present in multiple feature lists");
                return;
            }
            allDataFiles.add(dataFile);
        }
    }
    // Create a new aligned feature list
    alignedPeakList = new SimplePeakList(peakListName, allDataFiles.toArray(new RawDataFile[0]));
    if (DEBUG)
        printMemoryUsage(logger, run_time, prevTotal, prevFree, "COMPOUND DETECTED");
    /**
     * Alignment mapping *
     */
    // Iterate source feature lists
    Hashtable<SimpleFeature, Double> rtPeaksBackup = new Hashtable<SimpleFeature, Double>();
    Hashtable<PeakListRow, Object[]> infoRowsBackup = new Hashtable<PeakListRow, Object[]>();
    // Since clustering is now order independent, option removed!
    // Build comparison order
    ArrayList<Integer> orderIds = new ArrayList<Integer>();
    for (int i = 0; i < peakLists.length; ++i) {
        orderIds.add(i);
    }
    Integer[] newIds = orderIds.toArray(new Integer[orderIds.size()]);
    // 
    // TriangularMatrix distances = null;
    DistanceMatrix distancesGNF_Tri = null;
    DistanceMatrix distancesGNF_Tri_Bkp = null;
    int nbPeaks = 0;
    for (int i = 0; i < newIds.length; ++i) {
        PeakList peakList = peakLists[newIds[i]];
        nbPeaks += peakList.getNumberOfRows();
    }
    // If 'Hybrid' or no distance matrix: no need for a matrix
    if (CLUSTERER_TYPE == ClustererType.HYBRID || !saveRAMratherThanCPU_1) {
        // distances = new double[nbPeaks][nbPeaks];
        int nRowCount = nbPeaks;
        distancesGNF_Tri = new DistanceMatrixTriangular1D2D(nRowCount);
    }
    full_rows_list = new ArrayList<>();
    for (int i = 0; i < newIds.length; ++i) {
        PeakList peakList = peakLists[newIds[i]];
        PeakListRow[] allRows = peakList.getRows();
        for (int j = 0; j < allRows.length; ++j) {
            PeakListRow row = allRows[j];
            full_rows_list.add(row);
        }
    }
    RowVsRowDistanceProvider distProvider = new RowVsRowDistanceProvider(project, // rtAdjustementMapping,
    full_rows_list, mzWeight, rtWeight, // rtToleranceAfter,
    maximumScore);
    // If 'Hybrid' or no distance matrix: no need for a matrix
    if (CLUSTERER_TYPE == ClustererType.HYBRID || !saveRAMratherThanCPU_1) {
        for (int x = 0; x < nbPeaks; ++x) {
            for (int y = x; y < nbPeaks; ++y) {
                float dist = (float) distProvider.getRankedDistance(x, y, mzTolerance.getMzTolerance(), rtTolerance.getTolerance(), minScore);
                // if (CLUSTERER_TYPE == ClustererType.CLASSIC_OLD)
                // distances.set(x, y , dist);
                // else
                distancesGNF_Tri.setValue(x, y, dist);
            }
            processedRows++;
            if (DEBUG)
                logger.info("Treating lists: " + (Math.round(100 * processedRows / (double) nbPeaks)) + " %");
        }
    }
    if (DEBUG)
        printMemoryUsage(logger, run_time, prevTotal, prevFree, "DISTANCES COMPUTED");
    // ////
    // Math.abs(row.getBestPeak().getRT() -
    double max_dist = maximumScore;
    // k_row.getBestPeak().getRT()) /
    // ((RangeUtils.rangeLength(rtRange) /
    // 2.0));
    // String newickCluster;
    List<List<Integer>> gnfClusters = null;
    // ////
    boolean do_verbose = true;
    boolean do_cluster = true;
    boolean do_print = (exportDendrogramAsTxt);
    boolean do_data = false;
    org.gnf.clustering.Node[] arNodes = null;
    int nRowCount = full_rows_list.size();
    String[] rowNames = null;
    if (do_print) {
        rowNames = new String[nRowCount];
        for (int i = 0; i < nRowCount; i++) {
            // rowNames[i] = "ID_" + i + "_" +
            // full_rows_list.get(i).getID();
            Feature peak = full_rows_list.get(i).getBestPeak();
            double rt = peak.getRT();
            int end = peak.getDataFile().getName().indexOf(" ");
            String short_fname = peak.getDataFile().getName().substring(0, end);
            rowNames[i] = "@" + rtFormat.format(rt) + "^[" + short_fname + "]";
        }
    }
    String outputPrefix = null;
    if (CLUSTERER_TYPE == ClustererType.CLASSIC) {
        // Pure Hierar!
        outputPrefix = "hierar_0";
        throw new IllegalStateException("'" + ClustererType.CLASSIC.toString() + "' algorithm not yet implemented!");
    } else if (CLUSTERER_TYPE == ClustererType.CACHED) {
        // TODO: ...!
        if (DEBUG_2)
            logger.info(distancesGNF_Tri.toString());
        if (saveRAMratherThanCPU_2) {
            // Requires: distances values will be
            // recomputed on demand during
            // "getValidatedClusters_3()"
            // No duplicate backup storage!
            distancesGNF_Tri_Bkp = null;
        } else {
            // Otherwise, backing up the distance matrix (matrix being
            // deeply changed during "clusterDM()", then no more
            // exploitable)
            distancesGNF_Tri_Bkp = new DistanceMatrixTriangular1D2D(distancesGNF_Tri);
            if (DEBUG)
                printMemoryUsage(logger, run_time, prevTotal, prevFree, "GNF CLUSTERER BACKUP MATRIX");
        }
        if (DEBUG)
            logger.info("Clustering...");
        if (distancesGNF_Tri != null)
            arNodes = org.gnf.clustering.sequentialcache.SequentialCacheClustering.clusterDM(distancesGNF_Tri, linkageStartegyType, null, nRowCount);
        distancesGNF_Tri = null;
        System.gc();
        if (DEBUG)
            printMemoryUsage(logger, run_time, prevTotal, prevFree, "GNF CLUSTERER DONE");
        if (DEBUG_2)
            logger.info(distancesGNF_Tri.toString());
        if (DEBUG_2)
            for (int i = 0; i < arNodes.length; i++) {
                logger.info("Node " + i + ": " + arNodes[i]);
            }
        // TODO: Use usual interfacing ...
        // ClusteringResult<org.gnf.clustering.Node> clust_res = new
        // ClusteringResult<>(
        // Arrays.asList(arNodes), null, 0, null);
        outputPrefix = "hierar_1";
    } else if (CLUSTERER_TYPE == ClustererType.HYBRID) {
        throw new IllegalStateException("'" + ClustererType.HYBRID.toString() + "' algorithm not yet implemented!");
    }
    // Sort Nodes by correlation score (Required in
    // 'getValidatedClusters_3')
    int[] rowOrder = new int[nRowCount];
    if (DEBUG)
        logger.info("Sorting tree nodes...");
    org.gnf.clustering.Utils.NodeSort(arNodes, nRowCount - 2, 0, rowOrder);
    if (do_cluster) {
        gnfClusters = getValidatedClusters_3(arNodes, 0.0f, newIds.length, max_dist, distancesGNF_Tri_Bkp, distProvider);
        // -- Print
        if (DEBUG_2 && do_verbose)
            for (int i = 0; i < gnfClusters.size(); i++) {
                List<Integer> cl = gnfClusters.get(i);
                String str = "";
                for (int j = 0; j < cl.size(); j++) {
                    int r = cl.get(j);
                    str += cl.get(j) + "^(" + full_rows_list.get(r).getID() + ", " + full_rows_list.get(r).getAverageRT() + ")" + " ";
                }
                logger.info(str);
            }
    }
    // File output
    int ext_pos = dendrogramTxtFilename.getAbsolutePath().lastIndexOf(".");
    outputPrefix = dendrogramTxtFilename.getAbsolutePath().substring(0, ext_pos);
    String outGtr = outputPrefix + ".gtr";
    String outCdt = outputPrefix + ".cdt";
    if (DEBUG)
        logger.info("Writing output to file...");
    int nColCount = 1;
    String[] colNames = new String[nColCount];
    colNames[nColCount - 1] = "Id";
    String sep = "\t";
    if (do_print) {
        try {
            float[] arFloats = new float[nRowCount];
            for (int i = 0; i < arFloats.length; i++) {
                arFloats[i] = i / 2.0f;
            }
            DataSource source = (do_data) ? new FloatSource1D(arFloats, nRowCount, nColCount) : null;
            /* org.gnf.clustering.Utils. */
            HierarAlignerGCTask.GenerateCDT(outCdt, source, /* null */
            nRowCount, nColCount, sep, rowNames, colNames, rowOrder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        org.gnf.clustering.Utils.WriteTreeToFile(outGtr, nRowCount - 1, arNodes, true);
        if (DEBUG)
            printMemoryUsage(logger, run_time, prevTotal, prevFree, "GNF CLUSTERER FILES PRINTED");
    }
    // //// Arrange row clustered list with method 0,1,2
    List<List<PeakListRow>> clustersList = new ArrayList<>();
    // Build feature list row clusters
    for (List<Integer> cl : gnfClusters) {
        List<PeakListRow> rows_cluster = new ArrayList<>();
        for (int i = 0; i < cl.size(); i++) {
            rows_cluster.add(full_rows_list.get(cl.get(i)));
        }
        clustersList.add(rows_cluster);
        // 
        processedRows += rows_cluster.size();
    }
    if (DEBUG)
        printMemoryUsage(logger, run_time, prevTotal, prevFree, "GNF CLUSTERER CLUSTER_LIST");
    // Fill alignment table: One row per cluster
    for (List<PeakListRow> cluster : clustersList) {
        if (isCanceled())
            return;
        PeakListRow targetRow = new SimplePeakListRow(newRowID);
        newRowID++;
        alignedPeakList.addRow(targetRow);
        // 
        infoRowsBackup.put(targetRow, new Object[] { new HashMap<RawDataFile, Double[]>(), new HashMap<RawDataFile, PeakIdentity>(), new HashMap<RawDataFile, Double>() });
        for (PeakListRow row : cluster) {
            // Add all non-existing identities from the original row to the
            // aligned row
            // Set the preferred identity
            targetRow.setPreferredPeakIdentity(row.getPreferredPeakIdentity());
            // for (RawDataFile file : row.getRawDataFiles()) {
            for (RawDataFile file : alignedPeakList.getRawDataFiles()) {
                if (Arrays.asList(row.getRawDataFiles()).contains(file)) {
                    Feature originalPeak = row.getPeak(file);
                    if (originalPeak != null) {
                        targetRow.addPeak(file, originalPeak);
                    } else {
                        setStatus(TaskStatus.ERROR);
                        setErrorMessage("Cannot run alignment, no originalPeak");
                        return;
                    }
                }
            }
            // present
            for (PeakIdentity identity : row.getPeakIdentities()) {
                PeakIdentity clonedIdentity = (PeakIdentity) identity.clone();
                if (!PeakUtils.containsIdentity(targetRow, clonedIdentity))
                    targetRow.addPeakIdentity(clonedIdentity, false);
            }
        // processedRows++;
        }
    }
    // of the "targetRow.update()" used down there
    for (SimpleFeature peak : rtPeaksBackup.keySet()) {
        peak.setRT((double) rtPeaksBackup.get(peak));
    }
    /**
     * Post-processing... *
     */
    // Build reference RDFs index: We need an ordered reference here, to be
    // able to parse
    // correctly while reading back stored info
    RawDataFile[] rdf_sorted = alignedPeakList.getRawDataFiles().clone();
    Arrays.sort(rdf_sorted, new RawDataFileSorter(SortingDirection.Ascending));
    // Process
    for (PeakListRow targetRow : infoRowsBackup.keySet()) {
        if (isCanceled())
            return;
        // Refresh averaged RTs...
        ((SimplePeakListRow) targetRow).update();
    }
    // 
    if (DEBUG) {
        endTime = System.currentTimeMillis();
        ms = (endTime - startTime);
        logger.info("## >> Whole JoinAlignerGCTask processing took " + Float.toString(ms) + " ms.");
    }
    // ----------------------------------------------------------------------
    // Add new aligned feature list to the project
    this.project.addPeakList(alignedPeakList);
    if (DEBUG) {
        for (RawDataFile rdf : alignedPeakList.getRawDataFiles()) logger.info("RDF: " + rdf);
    }
    // Add task description to peakList
    alignedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod(HierarAlignerGCTask.TASK_NAME, parameters));
    logger.info("Finished join aligner GC");
    setStatus(TaskStatus.FINISHED);
}
Also used : ArrayList(java.util.ArrayList) 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) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) ArrayList(java.util.ArrayList) List(java.util.List) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) DistanceMatrix(org.gnf.clustering.DistanceMatrix) Vector(java.util.Vector) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) DataSource(org.gnf.clustering.DataSource) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) FloatSource1D(org.gnf.clustering.FloatSource1D) Hashtable(java.util.Hashtable) IOException(java.io.IOException) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList)

Example 18 with SimplePeakListRow

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

the class JoinAlignerTask method run.

/**
 * @see Runnable#run()
 */
@Override
public void run() {
    if ((mzWeight == 0) && (rtWeight == 0)) {
        setStatus(TaskStatus.ERROR);
        setErrorMessage("Cannot run alignment, all the weight parameters are zero");
        return;
    }
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running join aligner");
    // twice, first for score calculation, second for actual alignment.
    for (int i = 0; i < peakLists.length; i++) {
        totalRows += peakLists[i].getNumberOfRows() * 2;
    }
    // Collect all data files
    Vector<RawDataFile> allDataFiles = new Vector<RawDataFile>();
    for (PeakList peakList : peakLists) {
        for (RawDataFile dataFile : peakList.getRawDataFiles()) {
            // Each data file can only have one column in aligned feature list
            if (allDataFiles.contains(dataFile)) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Cannot run alignment, because file " + dataFile + " is present in multiple feature lists");
                return;
            }
            allDataFiles.add(dataFile);
        }
    }
    // Create a new aligned feature list
    alignedPeakList = new SimplePeakList(peakListName, allDataFiles.toArray(new RawDataFile[0]));
    // Iterate source feature lists
    for (PeakList peakList : peakLists) {
        // Create a sorted set of scores matching
        TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();
        PeakListRow[] allRows = peakList.getRows();
        // Calculate scores for all possible alignments of this row
        for (PeakListRow row : allRows) {
            if (isCanceled())
                return;
            // Calculate limits for a row with which the row can be aligned
            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 = alignedPeakList.getRowsInsideScanAndMZRange(rtRange, mzRange);
            // Calculate scores and store them
            for (PeakListRow candidate : candidateRows) {
                if (sameChargeRequired) {
                    if (!PeakUtils.compareChargeState(row, candidate))
                        continue;
                }
                if (sameIDRequired) {
                    if (!PeakUtils.compareIdentities(row, candidate))
                        continue;
                }
                if (compareIsotopePattern) {
                    IsotopePattern ip1 = row.getBestIsotopePattern();
                    IsotopePattern ip2 = candidate.getBestIsotopePattern();
                    if ((ip1 != null) && (ip2 != null)) {
                        ParameterSet isotopeParams = parameters.getParameter(JoinAlignerParameters.compareIsotopePattern).getEmbeddedParameters();
                        if (!IsotopePatternScoreCalculator.checkMatch(ip1, ip2, isotopeParams)) {
                            continue;
                        }
                    }
                }
                // compare the similarity of spectra mass lists on MS1 or MS2 level
                if (compareSpectraSimilarity) {
                    DataPoint[] rowDPs = null;
                    DataPoint[] candidateDPs = null;
                    SpectralSimilarity sim = null;
                    // get data points of mass list of the representative scans
                    if (msLevel == 1) {
                        rowDPs = row.getBestPeak().getRepresentativeScan().getMassList(massList).getDataPoints();
                        candidateDPs = candidate.getBestPeak().getRepresentativeScan().getMassList(massList).getDataPoints();
                    }
                    // get data points of mass list of the best fragmentation scans
                    if (msLevel == 2) {
                        if (row.getBestFragmentation() != null && candidate.getBestFragmentation() != null) {
                            rowDPs = row.getBestFragmentation().getMassList(massList).getDataPoints();
                            candidateDPs = candidate.getBestFragmentation().getMassList(massList).getDataPoints();
                        } else
                            continue;
                    }
                    // compare mass list data points of selected scans
                    if (rowDPs != null && candidateDPs != null) {
                        // calculate similarity using SimilarityFunction
                        sim = createSimilarity(rowDPs, candidateDPs);
                        // user set threshold
                        if (sim == null) {
                            continue;
                        }
                    }
                }
                RowVsRowScore score = new RowVsRowScore(row, candidate, RangeUtils.rangeLength(mzRange) / 2.0, mzWeight, RangeUtils.rangeLength(rtRange) / 2.0, rtWeight);
                scoreSet.add(score);
            }
            processedRows++;
        }
        // Create a table of mappings for best scores
        Hashtable<PeakListRow, PeakListRow> alignmentMapping = new Hashtable<PeakListRow, PeakListRow>();
        // Iterate scores by descending order
        Iterator<RowVsRowScore> scoreIterator = scoreSet.iterator();
        while (scoreIterator.hasNext()) {
            RowVsRowScore score = scoreIterator.next();
            // Check if the row is already mapped
            if (alignmentMapping.containsKey(score.getPeakListRow()))
                continue;
            // Check if the aligned row is already filled
            if (alignmentMapping.containsValue(score.getAlignedRow()))
                continue;
            alignmentMapping.put(score.getPeakListRow(), score.getAlignedRow());
        }
        // Align all rows using mapping
        for (PeakListRow row : allRows) {
            PeakListRow targetRow = alignmentMapping.get(row);
            // If we have no mapping for this row, add a new one
            if (targetRow == null) {
                targetRow = new SimplePeakListRow(newRowID);
                newRowID++;
                alignedPeakList.addRow(targetRow);
            }
            // Add all peaks from the original row to the aligned row
            for (RawDataFile file : row.getRawDataFiles()) {
                targetRow.addPeak(file, row.getPeak(file));
            }
            // Add all non-existing identities from the original row to the
            // aligned row
            PeakUtils.copyPeakListRowProperties(row, targetRow);
            processedRows++;
        }
    }
    // Next feature list
    // Add new aligned feature list to the project
    project.addPeakList(alignedPeakList);
    // Add task description to peakList
    alignedPeakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Join aligner", parameters));
    logger.info("Finished join aligner");
    setStatus(TaskStatus.FINISHED);
}
Also used : ParameterSet(net.sf.mzmine.parameters.ParameterSet) Hashtable(java.util.Hashtable) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SpectralSimilarity(net.sf.mzmine.util.scans.similarity.SpectralSimilarity) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) 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) TreeSet(java.util.TreeSet) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) Vector(java.util.Vector)

Example 19 with SimplePeakListRow

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

the class PeakLearnerTask method copyPeakRow.

/**
 * Create a copy of a feature list row.
 *
 * @param row the row to copy.
 * @return the newly created copy.
 */
private static PeakListRow copyPeakRow(final PeakListRow row) {
    // Copy the feature list row.
    final PeakListRow newRow = new SimplePeakListRow(row.getID());
    PeakUtils.copyPeakListRowProperties(row, newRow);
    // Copy the peaks.
    for (final Feature peak : row.getPeaks()) {
        final Feature newPeak = new SimpleFeature(peak);
        PeakUtils.copyPeakProperties(peak, newPeak);
        newRow.addPeak(peak.getDataFile(), newPeak);
    }
    return newRow;
}
Also used : SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) 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 20 with SimplePeakListRow

use of net.sf.mzmine.datamodel.impl.SimplePeakListRow 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)

Aggregations

SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)38 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)31 Feature (net.sf.mzmine.datamodel.Feature)29 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)22 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)19 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)17 DataPoint (net.sf.mzmine.datamodel.DataPoint)15 SimplePeakListAppliedMethod (net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)14 Scan (net.sf.mzmine.datamodel.Scan)9 ArrayList (java.util.ArrayList)8 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)8 PeakListAppliedMethod (net.sf.mzmine.datamodel.PeakList.PeakListAppliedMethod)7 PeakList (net.sf.mzmine.datamodel.PeakList)6 PeakIdentity (net.sf.mzmine.datamodel.PeakIdentity)5 Hashtable (java.util.Hashtable)3 List (java.util.List)3 Vector (java.util.Vector)3 ParameterSet (net.sf.mzmine.parameters.ParameterSet)3 PeakSorter (net.sf.mzmine.util.PeakSorter)3 SAXException (org.xml.sax.SAXException)3