Search in sources :

Example 11 with SpectralDBEntry

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

the class JdxParser method parse.

@Override
public boolean parse(AbstractTask mainTask, File dataBaseFile) throws IOException {
    logger.info("Parsing jdx spectral library " + dataBaseFile.getAbsolutePath());
    boolean isData = false;
    Map<DBEntryField, Object> fields = new EnumMap<>(DBEntryField.class);
    List<DataPoint> dps = new ArrayList<>();
    // create db
    int sep = -1;
    try (BufferedReader br = new BufferedReader(new FileReader(dataBaseFile))) {
        for (String l; (l = br.readLine()) != null; ) {
            // main task was canceled?
            if (mainTask.isCanceled()) {
                return false;
            }
            try {
                // meta data?
                sep = isData ? -1 : l.indexOf("=");
                if (sep != -1) {
                    DBEntryField field = DBEntryField.forJdxID(l.substring(0, sep));
                    if (field != null) {
                        String content = l.substring(sep + 1, l.length());
                        if (content.length() > 0) {
                            try {
                                Object value = field.convertValue(content);
                                fields.put(field, value);
                            } catch (Exception e) {
                                logger.log(Level.WARNING, "Cannot convert value type of " + content + " to " + field.getObjectClass().toString(), e);
                            }
                        }
                    }
                } else {
                    // data?
                    String[] dataPairs = l.split(" ");
                    for (String dataPair : dataPairs) {
                        String[] data = dataPair.split(",");
                        if (data.length == 2) {
                            try {
                                dps.add(new SimpleDataPoint(Double.parseDouble(data[0]), Double.parseDouble(data[1])));
                                isData = true;
                            } catch (Exception e) {
                            }
                        }
                    }
                }
                if (l.contains("END")) {
                    // row with END
                    // add entry and reset
                    SpectralDBEntry entry = new SpectralDBEntry(fields, dps.toArray(new DataPoint[dps.size()]));
                    fields = new EnumMap<>(fields);
                    dps.clear();
                    addLibraryEntry(entry);
                    // reset
                    isData = false;
                }
            } catch (Exception ex) {
                logger.log(Level.WARNING, "Error for entry", ex);
            }
        }
    }
    // finish and push last 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)

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