Search in sources :

Example 6 with DBEntryField

use of net.sf.mzmine.util.spectraldb.entry.DBEntryField 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

DBEntryField (net.sf.mzmine.util.spectraldb.entry.DBEntryField)6 IOException (java.io.IOException)5 EnumMap (java.util.EnumMap)4 DataPoint (net.sf.mzmine.datamodel.DataPoint)4 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)4 SpectralDBEntry (net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)3 BorderLayout (java.awt.BorderLayout)1 JsonNumber (javax.json.JsonNumber)1 JsonObject (javax.json.JsonObject)1 BoxLayout (javax.swing.BoxLayout)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 CustomTextPane (net.sf.mzmine.framework.CustomTextPane)1