Search in sources :

Example 6 with MassDetector

use of net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector 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

DataPoint (net.sf.mzmine.datamodel.DataPoint)6 MassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.MassDetector)6 ArrayList (java.util.ArrayList)5 CentroidMassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetector)5 CentroidMassDetectorParameters (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.centroid.CentroidMassDetectorParameters)5 ExactMassDetector (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetector)5 ExactMassDetectorParameters (net.sf.mzmine.modules.rawdatamethods.peakpicking.massdetection.exactmass.ExactMassDetectorParameters)5 DataPointsDataSet (net.sf.mzmine.modules.visualization.spectra.simplespectra.datasets.DataPointsDataSet)4 SpectraDatabaseSearchLabelGenerator (net.sf.mzmine.modules.visualization.spectra.simplespectra.spectraidentification.SpectraDatabaseSearchLabelGenerator)4 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)4 Scan (net.sf.mzmine.datamodel.Scan)2 Range (com.google.common.collect.Range)1 Color (java.awt.Color)1 FileReader (java.io.FileReader)1 NumberFormat (java.text.NumberFormat)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Logger (java.util.logging.Logger)1 IonizationType (net.sf.mzmine.datamodel.IonizationType)1