Search in sources :

Example 6 with SpectralDBEntry

use of net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry in project mzmine2 by mzmine.

the class PrecursorDBSearchTask method parseFile.

/**
 * Load all library entries from data base file
 *
 * @param dataBaseFile
 * @return
 */
private List<AbstractTask> parseFile(File dataBaseFile) throws UnsupportedFormatException, IOException {
    // 
    List<AbstractTask> tasks = new ArrayList<>();
    AutoLibraryParser parser = new AutoLibraryParser(100, new LibraryEntryProcessor() {

        @Override
        public void processNextEntries(List<SpectralDBEntry> list, int alreadyProcessed) {
            AbstractTask task = new AbstractTask() {

                private int total = peakList.getNumberOfRows();

                private int done = 0;

                @Override
                public void run() {
                    for (PeakListRow row : peakList.getRows()) {
                        if (this.isCanceled())
                            break;
                        for (SpectralDBEntry db : list) {
                            if (this.isCanceled())
                                break;
                            if (checkRT(row, (Double) db.getField(DBEntryField.RT).orElse(null)) && checkMZ(row, db.getPrecursorMZ())) {
                                // add identity
                                row.addPeakIdentity(new PrecursorDBPeakIdentity(db, PrecursorDBSearchModule.MODULE_NAME), false);
                                matches.getAndIncrement();
                            }
                        }
                        done++;
                    }
                    if (!this.isCanceled())
                        setStatus(TaskStatus.FINISHED);
                }

                @Override
                public String getTaskDescription() {
                    return "Checking for precursors: " + alreadyProcessed + 1 + " - " + alreadyProcessed + list.size();
                }

                @Override
                public double getFinishedPercentage() {
                    if (total == 0)
                        return 0;
                    return done / (double) total;
                }
            };
            // start last task
            MZmineCore.getTaskController().addTask(task);
            tasks.add(task);
        }
    });
    // return tasks
    parser.parse(this, dataBaseFile);
    return tasks;
}
Also used : PrecursorDBPeakIdentity(net.sf.mzmine.util.spectraldb.entry.PrecursorDBPeakIdentity) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) AutoLibraryParser(net.sf.mzmine.util.spectraldb.parser.AutoLibraryParser) ArrayList(java.util.ArrayList) LibraryEntryProcessor(net.sf.mzmine.util.spectraldb.parser.LibraryEntryProcessor) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)

Example 7 with SpectralDBEntry

use of net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry in project mzmine2 by mzmine.

the class LocalSpectralDBSearchTask method parseFile.

/**
 * Load all library entries from data base file
 *
 * @param dataBaseFile
 * @return
 */
private List<RowsSpectralMatchTask> parseFile(File dataBaseFile) throws UnsupportedFormatException, IOException {
    // 
    List<RowsSpectralMatchTask> tasks = new ArrayList<>();
    AutoLibraryParser parser = new AutoLibraryParser(100, new LibraryEntryProcessor() {

        @Override
        public void processNextEntries(List<SpectralDBEntry> list, int alreadyProcessed) {
            // start last task
            RowsSpectralMatchTask task = new RowsSpectralMatchTask(peakList.getName(), rows, parameters, alreadyProcessed + 1, list);
            MZmineCore.getTaskController().addTask(task);
            tasks.add(task);
        }
    });
    // return tasks
    parser.parse(this, dataBaseFile);
    return tasks;
}
Also used : AutoLibraryParser(net.sf.mzmine.util.spectraldb.parser.AutoLibraryParser) ArrayList(java.util.ArrayList) LibraryEntryProcessor(net.sf.mzmine.util.spectraldb.parser.LibraryEntryProcessor) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)

Example 8 with SpectralDBEntry

use of net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry in project mzmine2 by mzmine.

the class GnpsMgfParser method parse.

@Override
public boolean parse(AbstractTask mainTask, File dataBaseFile) throws IOException {
    logger.info("Parsing mgf spectral library " + dataBaseFile.getAbsolutePath());
    // BEGIN IONS
    // meta data
    // SCANS=1 .... n (the scan ID; could be used to put all spectra of the same entry together)
    // data
    // END IONS
    int correct = 0;
    State state = State.WAIT_FOR_META;
    Map<DBEntryField, Object> fields = new EnumMap<>(DBEntryField.class);
    List<DataPoint> dps = new ArrayList<>();
    int sep = -1;
    // create db
    try (BufferedReader br = new BufferedReader(new FileReader(dataBaseFile))) {
        for (String l; (l = br.readLine()) != null; ) {
            // main task was canceled?
            if (mainTask != null && mainTask.isCanceled()) {
                return false;
            }
            try {
                if (l.length() > 1) {
                    // meta data start?
                    if (state.equals(State.WAIT_FOR_META)) {
                        if (l.equalsIgnoreCase("BEGIN IONS")) {
                            fields = new EnumMap<>(fields);
                            dps.clear();
                            state = State.META;
                        }
                    } else {
                        if (l.equalsIgnoreCase("END IONS")) {
                            // add entry and reset
                            if (fields.size() > 1 && dps.size() > 1) {
                                SpectralDBEntry entry = new SpectralDBEntry(fields, dps.toArray(new DataPoint[dps.size()]));
                                // add and push
                                addLibraryEntry(entry);
                                correct++;
                            }
                            state = State.WAIT_FOR_META;
                        } else if (l.toLowerCase().startsWith("scans")) {
                            // belongs to the previously created entry and is another spectrum
                            // data starts
                            state = State.DATA;
                        } else {
                            switch(state) {
                                case WAIT_FOR_META:
                                    // wait for next entry
                                    break;
                                case DATA:
                                    String[] data = l.split("\t");
                                    dps.add(new SimpleDataPoint(Double.parseDouble(data[0]), Double.parseDouble(data[1])));
                                    break;
                                case META:
                                    sep = l.indexOf('=');
                                    if (sep != -1 && sep < l.length() - 1) {
                                        DBEntryField field = DBEntryField.forMgfID(l.substring(0, sep));
                                        if (field != null) {
                                            String content = l.substring(sep + 1, l.length());
                                            if (!content.isEmpty()) {
                                                try {
                                                    Object value = field.convertValue(content);
                                                    // name
                                                    if (field.equals(DBEntryField.NAME)) {
                                                        String name = ((String) value);
                                                        int lastSpace = name.lastIndexOf(' ');
                                                        if (lastSpace != -1 && lastSpace < name.length() - 2) {
                                                            String adductCandidate = name.substring(lastSpace + 1);
                                                            // check for valid adduct with the adduct parser from export
                                                            // use as adduct
                                                            String adduct = AdductParser.parse(adductCandidate);
                                                            if (adduct != null && !adduct.isEmpty())
                                                                fields.put(DBEntryField.ION_TYPE, adduct);
                                                        }
                                                    }
                                                    fields.put(field, value);
                                                } catch (Exception e) {
                                                    logger.log(Level.WARNING, "Cannot convert value type of " + content + " to " + field.getObjectClass().toString(), e);
                                                }
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                logger.log(Level.WARNING, "Error for entry", ex);
                state = State.WAIT_FOR_META;
            }
        }
        // finish and process all entries
        finish();
        return true;
    }
}
Also used : ArrayList(java.util.ArrayList) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) DBEntryField(net.sf.mzmine.util.spectraldb.entry.DBEntryField) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) IOException(java.io.IOException) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) EnumMap(java.util.EnumMap)

Example 9 with SpectralDBEntry

use of net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry in project mzmine2 by mzmine.

the class NistMspParser method parse.

@Override
public boolean parse(AbstractTask mainTask, File dataBaseFile) throws IOException {
    logger.info("Parsing NIST msp spectral library " + dataBaseFile.getAbsolutePath());
    // metadata fields and data points
    Map<DBEntryField, Object> fields = new EnumMap<>(DBEntryField.class);
    List<DataPoint> dps = new ArrayList<>();
    // separation index (metadata is separated by ': '
    int sep = -1;
    // currently loading data?
    boolean isData = false;
    // read DB file
    try (BufferedReader br = new BufferedReader(new FileReader(dataBaseFile))) {
        for (String l; (l = br.readLine()) != null; ) {
            // main task was canceled?
            if (mainTask != null && mainTask.isCanceled()) {
                return false;
            }
            try {
                if (l.length() > 1) {
                    // meta data?
                    sep = isData ? -1 : l.indexOf(": ");
                    if (sep != -1 && sep < l.length() - 2) {
                        extractMetaData(fields, l, sep);
                    } else {
                        // data?
                        DataPoint dp = extractDataPoint(l);
                        if (dp != null) {
                            dps.add(dp);
                            isData = true;
                        } else
                            isData = false;
                    }
                } else {
                    // empty row
                    if (isData) {
                        // empty row after data
                        // add entry and reset
                        SpectralDBEntry entry = new SpectralDBEntry(fields, dps.toArray(new DataPoint[dps.size()]));
                        // add and push
                        addLibraryEntry(entry);
                        // reset
                        fields = new EnumMap<>(fields);
                        dps.clear();
                        isData = false;
                    }
                }
            } catch (Exception ex) {
                logger.log(Level.WARNING, "Error for entry", ex);
                // reset on error
                isData = false;
                fields = new EnumMap<>(fields);
                dps.clear();
            }
        }
        // finish and process all entries
        finish();
        return true;
    }
}
Also used : ArrayList(java.util.ArrayList) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) DBEntryField(net.sf.mzmine.util.spectraldb.entry.DBEntryField) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) IOException(java.io.IOException) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) EnumMap(java.util.EnumMap)

Example 10 with SpectralDBEntry

use of net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry in project mzmine2 by mzmine.

the class GnpsJsonParser method parse.

@Override
public boolean parse(AbstractTask mainTask, File dataBaseFile) throws IOException {
    logger.info("Parsing GNPS spectral library " + dataBaseFile.getAbsolutePath());
    int correct = 0;
    int error = 0;
    // create db
    try (BufferedReader br = new BufferedReader(new FileReader(dataBaseFile))) {
        for (String l; (l = br.readLine()) != null; ) {
            // main task was canceled?
            if (mainTask != null && mainTask.isCanceled()) {
                return false;
            }
            JsonReader reader = null;
            try {
                reader = Json.createReader(new StringReader(l));
                JsonObject json = reader.readObject();
                SpectralDBEntry entry = getDBEntry(json);
                if (entry != null) {
                    correct++;
                    // add entry and process
                    addLibraryEntry(entry);
                } else
                    error++;
            } catch (Exception ex) {
                error++;
                logger.log(Level.WARNING, "Error for entry", ex);
            } finally {
                if (reader != null)
                    reader.close();
            }
            // to many errors? wrong data format?
            if (error > 5 && correct < 5) {
                logger.log(Level.WARNING, "This file was no GNPS spectral json library");
                return false;
            }
        }
    }
    // finish and process last entries
    finish();
    return true;
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) FileReader(java.io.FileReader) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) IOException(java.io.IOException)

Aggregations

SpectralDBEntry (net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)11 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 DataPoint (net.sf.mzmine.datamodel.DataPoint)7 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)5 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 EnumMap (java.util.EnumMap)4 DBEntryField (net.sf.mzmine.util.spectraldb.entry.DBEntryField)4 AutoLibraryParser (net.sf.mzmine.util.spectraldb.parser.AutoLibraryParser)4 LibraryEntryProcessor (net.sf.mzmine.util.spectraldb.parser.LibraryEntryProcessor)4 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)3 JsonObject (javax.json.JsonObject)2 AbstractTask (net.sf.mzmine.taskcontrol.AbstractTask)2 MissingMassListException (net.sf.mzmine.util.exceptions.MissingMassListException)2 SpectralSimilarity (net.sf.mzmine.util.scans.similarity.SpectralSimilarity)2 SpectralDBPeakIdentity (net.sf.mzmine.util.spectraldb.entry.SpectralDBPeakIdentity)2 UnsupportedFormatException (net.sf.mzmine.util.spectraldb.parser.UnsupportedFormatException)2 File (java.io.File)1 StringReader (java.io.StringReader)1