Search in sources :

Example 1 with SpectralSimilarity

use of net.sf.mzmine.util.scans.similarity.SpectralSimilarity in project mzmine2 by mzmine.

the class SpectralMatchTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    // check for mass list
    DataPoint[] spectraMassList;
    try {
        spectraMassList = getDataPoints(currentScan);
    } catch (MissingMassListException e) {
        // no mass list
        setStatus(TaskStatus.ERROR);
        setErrorMessage(MessageFormat.format("No masslist for name: {0} in scan {1} of raw file {2}", massListName, currentScan.getScanNumber(), currentScan.getDataFile().getName()));
        return;
    }
    // remove 13C isotopes
    if (removeIsotopes)
        spectraMassList = removeIsotopes(spectraMassList);
    setStatus(TaskStatus.PROCESSING);
    try {
        totalSteps = list.size();
        matches = new ArrayList<>();
        for (SpectralDBEntry ident : list) {
            if (isCanceled()) {
                logger.info("Added " + count + " spectral library matches (before being cancelled)");
                repaintWindow();
                return;
            }
            SpectralSimilarity sim = spectraDBMatch(spectraMassList, ident);
            if (sim != null && (!needsIsotopePattern || checkForIsotopePattern(sim, mzToleranceSpectra, minMatchedIsoSignals))) {
                count++;
                // use SpectralDBPeakIdentity to store all results similar to peaklist method
                matches.add(new SpectralDBPeakIdentity(currentScan, massListName, ident, sim, SpectraIdentificationSpectralDatabaseModule.MODULE_NAME));
            }
            // next row
            finishedSteps++;
        }
        addIdentities(matches);
        logger.info("Added " + count + " spectral library matches");
        // check if no match was found
        if (count == 0) {
            logger.log(Level.WARNING, "No data base matches found");
            setErrorMessage("No data base matches found. Spectral data base matching failed");
            list = null;
            return;
        }
    } catch (Exception e) {
        setStatus(TaskStatus.ERROR);
        logger.log(Level.SEVERE, "Spectral data base matching failed", e);
        setErrorMessage("Spectral data base matching failed");
        return;
    }
    // Repaint the window to reflect the change in the feature list
    repaintWindow();
    list = null;
    setStatus(TaskStatus.FINISHED);
}
Also used : DataPoint(net.sf.mzmine.datamodel.DataPoint) SpectralDBPeakIdentity(net.sf.mzmine.util.spectraldb.entry.SpectralDBPeakIdentity) SpectralSimilarity(net.sf.mzmine.util.scans.similarity.SpectralSimilarity) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException)

Example 2 with SpectralSimilarity

use of net.sf.mzmine.util.scans.similarity.SpectralSimilarity in project mzmine2 by mzmine.

the class RowsSpectralMatchTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    for (PeakListRow row : rows) {
        if (isCanceled()) {
            logger.info("Added " + count + " spectral library matches (before being cancelled)");
            repaintWindow();
            return;
        }
        try {
            // All MS2 or only best MS2 scan
            // best MS1 scan
            // check for MS1 or MSMS scan
            List<Scan> scans = getScans(row);
            List<DataPoint[]> rowMassLists = new ArrayList<>();
            for (Scan scan : scans) {
                // get mass list and perform deisotoping if active
                DataPoint[] rowMassList = getDataPoints(scan, true);
                if (removeIsotopes)
                    rowMassList = removeIsotopes(rowMassList);
                rowMassLists.add(rowMassList);
            }
            // match against all library entries
            for (SpectralDBEntry ident : list) {
                SpectralDBPeakIdentity best = null;
                // match all scans against this ident to find best match
                for (int i = 0; i < scans.size(); i++) {
                    SpectralSimilarity sim = spectraDBMatch(row, rowMassLists.get(i), ident);
                    if (sim != null && (!needsIsotopePattern || SpectralMatchTask.checkForIsotopePattern(sim, mzToleranceSpectra, minMatchedIsoSignals)) && (best == null || best.getSimilarity().getScore() < sim.getScore())) {
                        best = new SpectralDBPeakIdentity(scans.get(i), massListName, ident, sim, METHOD);
                    }
                }
                // has match?
                if (best != null) {
                    addIdentity(row, best);
                    count++;
                }
            }
            // sort identities based on similarity score
            SortSpectralDBIdentitiesTask.sortIdentities(row);
        } catch (MissingMassListException e) {
            logger.log(Level.WARNING, "No mass list in spectrum for rowID=" + row.getID(), e);
            errorCounter++;
        }
        // check for max error (missing masslist)
        if (errorCounter > MAX_ERROR) {
            logger.log(Level.WARNING, "Data base matching failed. To many missing mass lists ");
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Data base matching failed. To many missing mass lists ");
            list = null;
            return;
        }
        // next row
        finishedRows++;
    }
    if (count > 0)
        logger.info("Added " + count + " spectral library matches");
    // Repaint the window to reflect the change in the feature list
    repaintWindow();
    list = null;
    setStatus(TaskStatus.FINISHED);
}
Also used : PeakListRow(net.sf.mzmine.datamodel.PeakListRow) DataPoint(net.sf.mzmine.datamodel.DataPoint) SpectralDBPeakIdentity(net.sf.mzmine.util.spectraldb.entry.SpectralDBPeakIdentity) ArrayList(java.util.ArrayList) SpectralSimilarity(net.sf.mzmine.util.scans.similarity.SpectralSimilarity) Scan(net.sf.mzmine.datamodel.Scan) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) DataPoint(net.sf.mzmine.datamodel.DataPoint) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException)

Example 3 with SpectralSimilarity

use of net.sf.mzmine.util.scans.similarity.SpectralSimilarity in project mzmine2 by mzmine.

the class RowsSpectralMatchTask method spectraDBMatch.

/**
 * @param row
 * @param ident
 * @return spectral similarity or null if no match
 */
private SpectralSimilarity spectraDBMatch(PeakListRow row, DataPoint[] rowMassList, SpectralDBEntry ident) {
    // MS level 1 or check precursorMZ
    if (checkRT(row, ident) && (msLevel == 1 || checkPrecursorMZ(row, ident))) {
        DataPoint[] library = ident.getDataPoints();
        if (removeIsotopes)
            library = removeIsotopes(library);
        // crop the spectra to their overlapping mz range
        // helpful when comparing spectra, acquired with different fragmentation energy
        DataPoint[] query = rowMassList;
        if (cropSpectraToOverlap) {
            DataPoint[][] cropped = ScanAlignment.cropToOverlap(mzToleranceSpectra, library, query);
            library = cropped[0];
            query = cropped[1];
        }
        // check spectra similarity
        SpectralSimilarity sim = createSimilarity(library, query);
        if (sim != null) {
            return sim;
        }
    }
    return null;
}
Also used : DataPoint(net.sf.mzmine.datamodel.DataPoint) SpectralSimilarity(net.sf.mzmine.util.scans.similarity.SpectralSimilarity)

Example 4 with SpectralSimilarity

use of net.sf.mzmine.util.scans.similarity.SpectralSimilarity 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 5 with SpectralSimilarity

use of net.sf.mzmine.util.scans.similarity.SpectralSimilarity in project mzmine2 by mzmine.

the class CompositeCosineSpectralSimilarity method getSimilarity.

/**
 * Returns mass and intensity values detected in given scan
 */
@Override
public SpectralSimilarity getSimilarity(ParameterSet parameters, MZTolerance mzTol, int minMatch, DataPoint[] library, DataPoint[] query) {
    Weights weights = parameters.getParameter(CompositeCosineSpectralSimilarityParameters.weight).getValue();
    double minCos = parameters.getParameter(CompositeCosineSpectralSimilarityParameters.minCosine).getValue();
    boolean removeUnmatched = parameters.getParameter(CompositeCosineSpectralSimilarityParameters.removeUnmatched).getValue();
    // align
    List<DataPoint[]> aligned = alignDataPoints(mzTol, library, query);
    // removes all signals which were not found in both masslists
    if (removeUnmatched)
        aligned = removeUnaligned(aligned);
    int queryN = query.length;
    int overlap = calcOverlap(aligned);
    if (overlap >= minMatch) {
        // relative factor ranges from 0-1
        double relativeFactor = calcRelativeNeighbourFactor(aligned);
        // weighted cosine
        double[][] diffArray = ScanAlignment.toIntensityMatrixWeighted(aligned, weights.getIntensity(), weights.getMz());
        double diffCosine = Similarity.COSINE.calc(diffArray);
        // composite dot product identity score
        // NIST search similar
        double composite = (queryN * diffCosine + overlap * relativeFactor) / (queryN + overlap);
        if (composite >= minCos)
            return new SpectralSimilarity(getName(), composite, overlap, library, query, aligned);
        else
            return null;
    }
    return null;
}
Also used : Weights(net.sf.mzmine.util.scans.similarity.Weights) SpectralSimilarity(net.sf.mzmine.util.scans.similarity.SpectralSimilarity) DataPoint(net.sf.mzmine.datamodel.DataPoint)

Aggregations

DataPoint (net.sf.mzmine.datamodel.DataPoint)6 SpectralSimilarity (net.sf.mzmine.util.scans.similarity.SpectralSimilarity)6 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)2 MissingMassListException (net.sf.mzmine.util.exceptions.MissingMassListException)2 Weights (net.sf.mzmine.util.scans.similarity.Weights)2 SpectralDBEntry (net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)2 SpectralDBPeakIdentity (net.sf.mzmine.util.spectraldb.entry.SpectralDBPeakIdentity)2 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 TreeSet (java.util.TreeSet)1 Vector (java.util.Vector)1 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)1 PeakList (net.sf.mzmine.datamodel.PeakList)1 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)1 Scan (net.sf.mzmine.datamodel.Scan)1 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)1 SimplePeakListAppliedMethod (net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)1 SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)1 ParameterSet (net.sf.mzmine.parameters.ParameterSet)1