Search in sources :

Example 11 with SimplePeakIdentity

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

the class NistMsSearchTask method readSearchResults.

/**
 * Reads the search results file for a given feature list row.
 *
 * @param row the row.
 * @return a list of identities corresponding to the search results, or null if none is found.
 * @throws IOException if and i/o problem occurs.
 */
private List<PeakIdentity> readSearchResults(final PeakListRow row) throws IOException {
    // Search results.
    List<PeakIdentity> hitList = null;
    // Read the results file.
    final BufferedReader reader = new BufferedReader(new FileReader(new File(nistMsSearchDir, SEARCH_RESULTS_FILE_NAME)));
    try {
        // Read results.
        int lineCount = 1;
        String line = reader.readLine();
        while (line != null) {
            // Match the line.
            final Matcher scanMatcher = SEARCH_REGEX.matcher(line);
            final Matcher hitMatcher = HIT_REGEX.matcher(line);
            // Is this the start of a result block?
            if (scanMatcher.matches()) {
                // Is the row ID correct?
                final int rowID = row.getID();
                final int hitID = Integer.parseInt(scanMatcher.group(1));
                if (rowID == hitID) {
                    // Create a new list for the hits.
                    hitList = new ArrayList<PeakIdentity>(1);
                } else {
                    // Search results are for the wrong peak.
                    throw new IllegalArgumentException("Search results are for a different peak.  Expected peak: " + rowID + " but found: " + hitID);
                }
            } else if (hitMatcher.matches()) {
                if (hitList != null) {
                    // Do hit match factors exceed thresholds?
                    final String matchFactor = hitMatcher.group(3);
                    final String reverseMatchFactor = hitMatcher.group(4);
                    if (Integer.parseInt(matchFactor) >= minMatchFactor && Integer.parseInt(reverseMatchFactor) >= minReverseMatchFactor) {
                        // Extract identity from hit information.
                        final SimplePeakIdentity id = new SimplePeakIdentity(hitMatcher.group(1), hitMatcher.group(2), SEARCH_METHOD, hitMatcher.group(7), null);
                        id.setPropertyValue(MATCH_FACTOR_PROPERTY, matchFactor);
                        id.setPropertyValue(REVERSE_MATCH_FACTOR_PROPERTY, reverseMatchFactor);
                        id.setPropertyValue(CAS_PROPERTY, hitMatcher.group(5));
                        id.setPropertyValue(MOLECULAR_WEIGHT_PROPERTY, hitMatcher.group(6));
                        hitList.add(id);
                    }
                } else {
                    throw new IOException("Didn't find start of results block before listing hits at line " + lineCount);
                }
            } else {
                throw new IOException("Unrecognised results file text at line " + lineCount);
            }
            // Read the next line.
            line = reader.readLine();
            lineCount++;
        }
    } finally {
        reader.close();
    }
    return hitList;
}
Also used : SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) IOException(java.io.IOException) File(java.io.File)

Example 12 with SimplePeakIdentity

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

the class NistMsSearchTask method nistSearch.

/**
 * Run the NIST search.
 *
 * @throws IOException if there are i/o problems.
 */
private void nistSearch() throws IOException {
    // Waiting to get the SEMAPHORE: only one instance of NIST MS Search can
    // run at a time.
    setStatus(TaskStatus.WAITING);
    synchronized (SEMAPHORE) {
        File locatorFile2 = null;
        try {
            if (!isCanceled()) {
                setStatus(TaskStatus.PROCESSING);
                // Configure locator files.
                final File locatorFile1 = new File(nistMsSearchDir, PRIMARY_LOCATOR_FILE_NAME);
                locatorFile2 = getSecondLocatorFile(locatorFile1);
                if (locatorFile2 == null) {
                    throw new IOException("Primary locator file " + locatorFile1 + " doesn't contain the name of a valid file.");
                }
                // Is MS Search already running?
                if (locatorFile2.exists()) {
                    throw new IllegalStateException("NIST MS Search appears to be busy - please wait until it finishes its current task and then try again.  Alternatively, try manually deleting the file " + locatorFile2);
                }
            }
            // Single or multiple row search?
            final PeakListRow[] peakListRows;
            final Map<PeakListRow, Set<PeakListRow>> rowHoods;
            if (peakListRow == null) {
                peakListRows = peakList.getRows();
                rowHoods = groupPeakRows();
            } else {
                peakListRows = new PeakListRow[] { peakListRow };
                rowHoods = new HashMap<PeakListRow, Set<PeakListRow>>(1);
                rowHoods.put(peakListRow, findPeakRowGroup());
            }
            // Reduce neighbourhoods to maximum number of peaks.
            trimNeighbours(rowHoods);
            // Store search results for each neighbourhood - to avoid repeat
            // searches.
            final int numRows = peakListRows.length;
            final Map<Set<PeakListRow>, List<PeakIdentity>> rowIdentities = new HashMap<Set<PeakListRow>, List<PeakIdentity>>(numRows);
            // Search command string.
            final String command = nistMsSearchExe.getAbsolutePath() + ' ' + COMMAND_LINE_ARGS;
            // Perform searches for each feature list row..
            progress = 0;
            progressMax = numRows;
            for (final PeakListRow row : peakListRows) {
                // Get the row's neighbours.
                final Set<PeakListRow> neighbours = rowHoods.get(row);
                // Has this neighbourhood's search been run already?
                if (!rowIdentities.containsKey(neighbours)) {
                    if (!isCanceled()) {
                        // Write spectra file.
                        final File spectraFile = writeSpectraFile(row, neighbours);
                        // Write locator file.
                        writeSecondaryLocatorFile(locatorFile2, spectraFile);
                        // Run the search.
                        runNistMsSearch(command);
                        // Read the search results file and store the
                        // results.
                        rowIdentities.put(neighbours, readSearchResults(row));
                    }
                }
                // Get the search results.
                final List<PeakIdentity> identities = rowIdentities.get(neighbours);
                if (identities != null) {
                    // Add (copy of) identities to peak row.
                    int maxMatchFactor = -1;
                    for (final PeakIdentity identity : identities) {
                        // Copy the identity.
                        final PeakIdentity id = new SimplePeakIdentity((Hashtable<String, String>) identity.getAllProperties());
                        // Best match factor?
                        final boolean isPreferred;
                        final int matchFactor = Integer.parseInt(id.getPropertyValue(MATCH_FACTOR_PROPERTY));
                        if (matchFactor > maxMatchFactor) {
                            maxMatchFactor = matchFactor;
                            isPreferred = true;
                        } else {
                            isPreferred = false;
                        }
                        // Add peak identity.
                        row.addPeakIdentity(id, isPreferred);
                    }
                    // Notify the GUI about the change in the project
                    MZmineCore.getProjectManager().getCurrentProject().notifyObjectChanged(row, false);
                }
                progress++;
            }
        } finally {
            // Clean up.
            if (locatorFile2 != null) {
                locatorFile2.delete();
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) ParameterSet(net.sf.mzmine.parameters.ParameterSet) Set(java.util.Set) HashMap(java.util.HashMap) IOException(java.io.IOException) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) PeakList(net.sf.mzmine.datamodel.PeakList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 13 with SimplePeakIdentity

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

the class ResultWindow method actionPerformed.

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("EXPORT")) {
        // Ask for filename
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setApproveButtonText("Export");
        int result = fileChooser.showSaveDialog(MZmineCore.getDesktop().getMainWindow());
        if (result != JFileChooser.APPROVE_OPTION)
            return;
        File outputFile = fileChooser.getSelectedFile();
        try {
            FileWriter fileWriter = new FileWriter(outputFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write("Formula,Mass,RDBE,Isotope pattern score,MS/MS score");
            writer.newLine();
            for (int row = 0; row < resultsTable.getRowCount(); row++) {
                int modelRow = resultsTable.convertRowIndexToModel(row);
                ResultFormula formula = resultsTableModel.getFormula(modelRow);
                writer.write(formula.getFormulaAsString());
                writer.write(",");
                writer.write(String.valueOf(formula.getExactMass()));
                writer.write(",");
                if (formula.getRDBE() != null)
                    writer.write(String.valueOf(formula.getRDBE()));
                writer.write(",");
                if (formula.getIsotopeScore() != null)
                    writer.write(String.valueOf(formula.getIsotopeScore()));
                writer.write(",");
                if (formula.getMSMSScore() != null)
                    writer.write(String.valueOf(formula.getMSMSScore()));
                writer.newLine();
            }
            writer.close();
        } catch (Exception ex) {
            MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Error writing to file " + outputFile + ": " + ExceptionUtils.exceptionToString(ex));
        }
        return;
    }
    // The following actions require a single row to be selected
    int index = resultsTable.getSelectedRow();
    if (index < 0) {
        MZmineCore.getDesktop().displayMessage(MZmineCore.getDesktop().getMainWindow(), "Please select one result");
        return;
    }
    index = resultsTable.convertRowIndexToModel(index);
    ResultFormula formula = resultsTableModel.getFormula(index);
    if (command.equals("ADD")) {
        SimplePeakIdentity newIdentity = new SimplePeakIdentity(formula.getFormulaAsString());
        peakListRow.addPeakIdentity(newIdentity, false);
        // Notify the GUI about the change in the project
        MZmineCore.getProjectManager().getCurrentProject().notifyObjectChanged(peakListRow, false);
        // Repaint the window to reflect the change in the feature list
        MZmineCore.getDesktop().getMainWindow().repaint();
        dispose();
    }
    if (command.equals("COPY")) {
        String formulaString = formula.getFormulaAsString();
        StringSelection stringSelection = new StringSelection(formulaString);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);
    }
    if (command.equals("SHOW_ISOTOPES")) {
        logger.finest("Showing isotope pattern for formula " + formula.getFormulaAsString());
        IsotopePattern predictedPattern = formula.getPredictedIsotopes();
        if (predictedPattern == null)
            return;
        Feature peak = peakListRow.getBestPeak();
        RawDataFile dataFile = peak.getDataFile();
        int scanNumber = peak.getRepresentativeScanNumber();
        SpectraVisualizerModule.showNewSpectrumWindow(dataFile, scanNumber, null, peak.getIsotopePattern(), predictedPattern);
    }
    if (command.equals("SHOW_MSMS")) {
        Feature bestPeak = peakListRow.getBestPeak();
        RawDataFile dataFile = bestPeak.getDataFile();
        int msmsScanNumber = bestPeak.getMostIntenseFragmentScanNumber();
        if (msmsScanNumber < 1)
            return;
        SpectraVisualizerWindow msmsPlot = SpectraVisualizerModule.showNewSpectrumWindow(dataFile, msmsScanNumber);
        if (msmsPlot == null)
            return;
        Map<DataPoint, String> annotation = formula.getMSMSannotation();
        if (annotation == null)
            return;
        msmsPlot.addAnnotation(annotation);
    }
}
Also used : FileWriter(java.io.FileWriter) IsotopePattern(net.sf.mzmine.datamodel.IsotopePattern) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) Feature(net.sf.mzmine.datamodel.Feature) DataPoint(net.sf.mzmine.datamodel.DataPoint) BufferedWriter(java.io.BufferedWriter) StringSelection(java.awt.datatransfer.StringSelection) SpectraVisualizerWindow(net.sf.mzmine.modules.visualization.spectra.simplespectra.SpectraVisualizerWindow) JFileChooser(javax.swing.JFileChooser) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) DataPoint(net.sf.mzmine.datamodel.DataPoint) Clipboard(java.awt.datatransfer.Clipboard) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File)

Aggregations

SimplePeakIdentity (net.sf.mzmine.datamodel.impl.SimplePeakIdentity)13 DataPoint (net.sf.mzmine.datamodel.DataPoint)7 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)6 IOException (java.io.IOException)5 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)5 Scan (net.sf.mzmine.datamodel.Scan)5 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)5 Feature (net.sf.mzmine.datamodel.Feature)4 FeatureStatus (net.sf.mzmine.datamodel.Feature.FeatureStatus)4 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 File (java.io.File)3 PeakIdentity (net.sf.mzmine.datamodel.PeakIdentity)3 SimpleIsotopePattern (net.sf.mzmine.datamodel.impl.SimpleIsotopePattern)3 SAXException (org.xml.sax.SAXException)3 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 IsotopePattern (net.sf.mzmine.datamodel.IsotopePattern)2 PeakList (net.sf.mzmine.datamodel.PeakList)2