Search in sources :

Example 6 with DataPointsDataSet

use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.

the class SpectralMatchTask method addIdentities.

private void addIdentities(List<SpectralDBPeakIdentity> matches) {
    for (SpectralDBPeakIdentity match : matches) {
        try {
            // TODO put into separate method and add comments
            // get data points of matching scans
            DataPoint[] spectraMassList = getDataPoints(currentScan);
            List<DataPoint[]> alignedDataPoints = ScanAlignment.align(mzToleranceSpectra, match.getEntry().getDataPoints(), spectraMassList);
            alignedSignals = ScanAlignment.removeUnaligned(alignedDataPoints);
            // add new mass list to the spectra for match
            DataPoint[] dataset = new DataPoint[alignedSignals.size()];
            for (int i = 0; i < dataset.length; i++) {
                dataset[i] = alignedSignals.get(i)[1];
            }
            String compoundName = match.getEntry().getField(DBEntryField.NAME).toString();
            String shortName = compoundName;
            // TODO remove or specify more - special naming format?
            int start = compoundName.indexOf("[");
            int end = compoundName.indexOf("]");
            if (start != -1 && start + 1 < compoundName.length() && end != -1 && end < compoundName.length())
                shortName = compoundName.substring(start + 1, end);
            DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet(shortName + " " + "Score: " + COS_FORM.format(match.getSimilarity().getScore()), dataset);
            spectraPlot.addDataSet(detectedCompoundsDataset, new Color((int) (Math.random() * 0x1000000)), true);
        } catch (MissingMassListException e) {
            logger.log(Level.WARNING, "No mass list for the selected spectrum", e);
            errorCounter++;
        }
    }
    resultWindow.addMatches(matches);
    resultWindow.revalidate();
    resultWindow.repaint();
    setStatus(TaskStatus.FINISHED);
}
Also used : SpectralDBPeakIdentity(net.sf.mzmine.util.spectraldb.entry.SpectralDBPeakIdentity) DataPoint(net.sf.mzmine.datamodel.DataPoint) Color(java.awt.Color) DataPoint(net.sf.mzmine.datamodel.DataPoint) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException)

Example 7 with DataPointsDataSet

use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.

the class SpectraIdentificationSumFormulaTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.finest("Starting search for formulas for " + massRange + " Da");
    // create mass list for scan
    DataPoint[] massList = null;
    ArrayList<DataPoint> massListAnnotated = new ArrayList<>();
    MassDetector massDetector = null;
    ArrayList<String> allCompoundIDs = new ArrayList<>();
    // Create a new mass list for MS/MS scan. Check if sprectrum is profile or centroid mode
    if (currentScan.getSpectrumType() == MassSpectrumType.CENTROIDED) {
        massDetector = new CentroidMassDetector();
        CentroidMassDetectorParameters parameters = new CentroidMassDetectorParameters();
        CentroidMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    } else {
        massDetector = new ExactMassDetector();
        ExactMassDetectorParameters parameters = new ExactMassDetectorParameters();
        ExactMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    }
    numItems = massList.length;
    // loop through every peak in mass list
    if (getStatus() != TaskStatus.PROCESSING) {
        return;
    }
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
    for (int i = 0; i < massList.length; i++) {
        massRange = mzTolerance.getToleranceRange((massList[i].getMZ() - ionType.getAddedMass()) / charge);
        generator = new MolecularFormulaGenerator(builder, massRange.lowerEndpoint(), massRange.upperEndpoint(), elementCounts);
        IMolecularFormula cdkFormula;
        String annotation = "";
        // create a map to store ResultFormula and relative mass deviation for sorting
        Map<Double, String> possibleFormulas = new TreeMap<>();
        while ((cdkFormula = generator.getNextFormula()) != null) {
            if (isCanceled())
                return;
            // Mass is ok, so test other constraints
            if (checkConstraints(cdkFormula) == true) {
                String formula = MolecularFormulaManipulator.getString(cdkFormula);
                // calc rel mass deviation
                Double relMassDev = ((((// 
                massList[i].getMZ() - ionType.getAddedMass()) / // 
                charge) - (// 
                FormulaUtils.calculateExactMass(MolecularFormulaManipulator.getString(cdkFormula))) / charge) / ((// 
                massList[i].getMZ() - ionType.getAddedMass()) / charge)) * 1000000;
                // write to map
                possibleFormulas.put(relMassDev, formula);
            }
        }
        Map<Double, String> treeMap = new TreeMap<>((Comparator<Double>) (o1, o2) -> Double.compare(Math.abs(o1), Math.abs(o2)));
        treeMap.putAll(possibleFormulas);
        // get top 3
        int ctr = 0;
        for (Map.Entry<Double, String> entry : treeMap.entrySet()) {
            int number = ctr + 1;
            if (ctr > 2)
                break;
            annotation = annotation + number + ". " + entry.getValue() + " Δ " + NumberFormat.getInstance().format(entry.getKey()) + " ppm; ";
            ctr++;
            if (isCanceled())
                return;
        }
        if (annotation != "") {
            allCompoundIDs.add(annotation);
            massListAnnotated.add(massList[i]);
        }
        logger.finest("Finished formula search for " + massRange + " m/z, found " + foundFormulas + " formulas");
    }
    // new mass list
    DataPoint[] annotatedMassList = new DataPoint[massListAnnotated.size()];
    massListAnnotated.toArray(annotatedMassList);
    String[] annotations = new String[annotatedMassList.length];
    allCompoundIDs.toArray(annotations);
    DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet("Detected compounds", annotatedMassList);
    // Add label generator for the dataset
    SpectraDatabaseSearchLabelGenerator labelGenerator = new SpectraDatabaseSearchLabelGenerator(annotations, spectraPlot);
    spectraPlot.addDataSet(detectedCompoundsDataset, Color.orange, true, labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setSeriesItemLabelGenerator(spectraPlot.getXYPlot().getSeriesCount(), labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT, TextAnchor.BOTTOM_CENTER, 0.0), true);
    setStatus(TaskStatus.FINISHED);
}
Also used : Color(java.awt.Color) Scan(net.sf.mzmine.datamodel.Scan) MZmineCore(net.sf.mzmine.main.MZmineCore) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) CentroidMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) RDBERestrictionChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.rdbe.RDBERestrictionChecker) TextAnchor(org.jfree.chart.ui.TextAnchor) MolecularFormulaManipulator(org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) SpectraDatabaseSearchLabelGenerator(net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator) NumberFormat(java.text.NumberFormat) DataPoint(net.sf.mzmine.datamodel.DataPoint) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) ArrayList(java.util.ArrayList) MassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector) ParameterSet(net.sf.mzmine.parameters.ParameterSet) Map(java.util.Map) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) FormulaUtils(net.sf.mzmine.util.FormulaUtils) MZTolerance(net.sf.mzmine.parameters.parametertypes.tolerances.MZTolerance) SilentChemObjectBuilder(org.openscience.cdk.silent.SilentChemObjectBuilder) Range(com.google.common.collect.Range) Logger(java.util.logging.Logger) IonizationType(net.sf.mzmine.datamodel.IonizationType) ElementalHeuristicChecker(net.sf.mzmine.modules.peaklistmethods.identification.formulaprediction.restrictions.elements.ElementalHeuristicChecker) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) TreeMap(java.util.TreeMap) MolecularFormulaRange(org.openscience.cdk.formula.MolecularFormulaRange) Comparator(java.util.Comparator) ItemLabelAnchor(org.jfree.chart.labels.ItemLabelAnchor) ExactMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters) SpectraPlot(net.sf.mzmine.modules.visualization.spectra.simplespectra.SpectraPlot) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) ArrayList(java.util.ArrayList) IMolecularFormula(org.openscience.cdk.interfaces.IMolecularFormula) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) MolecularFormulaGenerator(org.openscience.cdk.formula.MolecularFormulaGenerator) DataPoint(net.sf.mzmine.datamodel.DataPoint) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) MassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) CentroidMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters) TreeMap(java.util.TreeMap) IChemObjectBuilder(org.openscience.cdk.interfaces.IChemObjectBuilder) DataPoint(net.sf.mzmine.datamodel.DataPoint) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) ExactMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters) Map(java.util.Map) TreeMap(java.util.TreeMap) SpectraDatabaseSearchLabelGenerator(net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator)

Example 8 with DataPointsDataSet

use of net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet in project mzmine2 by mzmine.

the class SpectraIdentificationCustomDatabaseTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    // create mass list for scan
    DataPoint[] massList = null;
    ArrayList<DataPoint> massListAnnotated = new ArrayList<>();
    MassDetector massDetector = null;
    ArrayList<String> allCompoundIDs = new ArrayList<>();
    // Create a new mass list for MS/MS scan. Check if sprectrum is profile or centroid mode
    if (currentScan.getSpectrumType() == MassSpectrumType.CENTROIDED) {
        massDetector = new CentroidMassDetector();
        CentroidMassDetectorParameters parameters = new CentroidMassDetectorParameters();
        CentroidMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    } else {
        massDetector = new ExactMassDetector();
        ExactMassDetectorParameters parameters = new ExactMassDetectorParameters();
        ExactMassDetectorParameters.noiseLevel.setValue(noiseLevel);
        massList = massDetector.getMassValues(currentScan.getDataPoints(), parameters);
    }
    numItems = massList.length;
    // load custom database
    try {
        // read database contents in memory
        FileReader dbFileReader = new FileReader(dataBaseFile);
        databaseValues = CSVParser.parse(dbFileReader, fieldSeparator.charAt(0));
        if (ignoreFirstLine)
            finishedLines++;
        for (; finishedLines < databaseValues.length; finishedLines++) {
            if (isCanceled()) {
                dbFileReader.close();
                return;
            }
            int numOfColumns = Math.min(fieldOrder.length, databaseValues[finishedLines].length);
            String lineName = null;
            double lineMZ = 0;
            for (int i = 0; i < numOfColumns; i++) {
                if (fieldOrder[i] == FieldItem.FIELD_NAME)
                    lineName = databaseValues[finishedLines][i].toString();
                if (fieldOrder[i] == FieldItem.FIELD_MZ)
                    lineMZ = Double.parseDouble(databaseValues[finishedLines][i].toString());
            }
            for (int i = 0; i < massList.length; i++) {
                // loop through every peak in mass list
                if (getStatus() != TaskStatus.PROCESSING) {
                    return;
                }
                double searchedMass = massList[i].getMZ();
                Range<Double> mzRange = mzTolerance.getToleranceRange(searchedMass);
                boolean mzMatches = (lineMZ == 0d) || mzRange.contains(lineMZ);
                String annotation = "";
                if (mzMatches) {
                    // calc rel mass deviation
                    double relMassDev = ((searchedMass - lineMZ) / searchedMass) * 1000000;
                    logger.finest("Found compound " + lineName + " m/z " + NumberFormat.getInstance().format(searchedMass) + " Δ " + NumberFormat.getInstance().format(relMassDev) + " ppm");
                    annotation = lineName + " Δ " + NumberFormat.getInstance().format(relMassDev) + " ppm";
                }
                if (annotation != "") {
                    allCompoundIDs.add(annotation);
                    massListAnnotated.add(massList[i]);
                }
            }
            finishedLines++;
        }
        // close the file reader
        dbFileReader.close();
    } catch (Exception e) {
        logger.log(Level.WARNING, "Could not read file " + dataBaseFile, e);
        setStatus(TaskStatus.ERROR);
        setErrorMessage(e.toString());
        return;
    }
    // new mass list
    DataPoint[] annotatedMassList = new DataPoint[massListAnnotated.size()];
    massListAnnotated.toArray(annotatedMassList);
    String[] annotations = new String[annotatedMassList.length];
    allCompoundIDs.toArray(annotations);
    DataPointsDataSet detectedCompoundsDataset = new DataPointsDataSet("Detected compounds", annotatedMassList);
    // Add label generator for the dataset
    SpectraDatabaseSearchLabelGenerator labelGenerator = new SpectraDatabaseSearchLabelGenerator(annotations, spectraPlot);
    spectraPlot.addDataSet(detectedCompoundsDataset, Color.orange, true, labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setSeriesItemLabelGenerator(spectraPlot.getXYPlot().getSeriesCount(), labelGenerator);
    spectraPlot.getXYPlot().getRenderer().setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_LEFT, TextAnchor.BOTTOM_CENTER, 0.0), true);
    setStatus(TaskStatus.FINISHED);
}
Also used : CentroidMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters) ArrayList(java.util.ArrayList) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) DataPoint(net.sf.mzmine.datamodel.DataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) FileReader(java.io.FileReader) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) CentroidMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector) ExactMassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector) MassDetector(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector) ExactMassDetectorParameters(net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters) DataPointsDataSet(net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet) SpectraDatabaseSearchLabelGenerator(net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator)

Aggregations

DataPointsDataSet (net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet)8 DataPoint (net.sf.mzmine.datamodel.DataPoint)7 ArrayList (java.util.ArrayList)6 MassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector)4 CentroidMassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector)4 CentroidMassDetectorParameters (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters)4 ExactMassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector)4 ExactMassDetectorParameters (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters)4 SpectraDatabaseSearchLabelGenerator (net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator)4 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)4 Color (java.awt.Color)3 Scan (net.sf.mzmine.datamodel.Scan)2 SpectraPlot (net.sf.mzmine.modules.visualization.spectra.simplespectra.SpectraPlot)2 ScanDataSet (net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.ScanDataSet)2 Range (com.google.common.collect.Range)1 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 FileReader (java.io.FileReader)1 NumberFormat (java.text.NumberFormat)1 Comparator (java.util.Comparator)1