Search in sources :

Example 1 with DBEntryField

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

the class GnpsJsonParser method getDBEntry.

public SpectralDBEntry getDBEntry(JsonObject main) {
    // extract dps
    DataPoint[] dps = getDataPoints(main);
    if (dps == null)
        return null;
    // extract meta data
    Map<DBEntryField, Object> map = new EnumMap<>(DBEntryField.class);
    for (DBEntryField f : DBEntryField.values()) {
        String id = f.getGnpsJsonID();
        if (id != null && !id.isEmpty()) {
            try {
                Object o = null;
                if (f.getObjectClass() == Double.class || f.getObjectClass() == Integer.class || f.getObjectClass() == Float.class) {
                    o = main.getJsonNumber(id);
                } else {
                    o = main.getString(id, null);
                    if (o != null && o.equals("N/A"))
                        o = null;
                }
                // add value
                if (o != null) {
                    if (o instanceof JsonNumber) {
                        if (f.getObjectClass().equals(Integer.class)) {
                            o = ((JsonNumber) o).intValue();
                        } else {
                            o = ((JsonNumber) o).doubleValue();
                        }
                    }
                    // add
                    map.put(f, o);
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Cannot convert value to its type", e);
            }
        }
    }
    return new SpectralDBEntry(map, dps);
}
Also used : DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry) DBEntryField(net.sf.mzmine.util.spectraldb.entry.DBEntryField) EnumMap(java.util.EnumMap) IOException(java.io.IOException)

Example 2 with DBEntryField

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

the class SpectralMatchPanel method extractMetaData.

private JPanel extractMetaData(String title, SpectralDBEntry entry, DBEntryField[] other) {
    JPanel panelOther = new JPanel();
    panelOther.setLayout(new BoxLayout(panelOther, BoxLayout.Y_AXIS));
    panelOther.setBackground(Color.WHITE);
    panelOther.setAlignmentY(Component.TOP_ALIGNMENT);
    panelOther.setAlignmentX(Component.TOP_ALIGNMENT);
    for (DBEntryField db : other) {
        Object o = entry.getField(db).orElse("N/A");
        if (!o.equals("N/A")) {
            CustomTextPane textPane = new CustomTextPane(true);
            textPane.setText(db.toString() + ": " + o.toString());
            panelOther.add(textPane);
        }
    }
    JLabel otherInfo = new JLabel(title);
    otherInfo.setFont(headerFont);
    JPanel pn = new JPanel(new BorderLayout());
    pn.setBackground(Color.WHITE);
    pn.add(otherInfo, BorderLayout.NORTH);
    pn.add(panelOther, BorderLayout.CENTER);
    JPanel pn1 = new JPanel(new BorderLayout());
    pn1.add(pn, BorderLayout.NORTH);
    pn1.setBackground(Color.WHITE);
    return pn1;
}
Also used : JPanel(javax.swing.JPanel) BorderLayout(java.awt.BorderLayout) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) DBEntryField(net.sf.mzmine.util.spectraldb.entry.DBEntryField) CustomTextPane(net.sf.mzmine.framework.CustomTextPane)

Example 3 with DBEntryField

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

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

the class NistMspParser method extractMetaData.

/**
 * Extracts metadata from a line which is separated by ': ' and inserts the metadata inta a map
 *
 * @param fields The map of metadata fields
 * @param line String with metadata
 * @param sep index of the separation char ':'
 */
private void extractMetaData(Map<DBEntryField, Object> fields, String line, int sep) {
    String key = line.substring(0, sep);
    DBEntryField field = DBEntryField.forMspID(key);
    if (field != null) {
        // spe +2 for colon and space
        String content = line.substring(sep + 2, line.length());
        if (content.length() > 0) {
            try {
                // convert into value type
                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);
            }
        }
    }
}
Also used : DBEntryField(net.sf.mzmine.util.spectraldb.entry.DBEntryField) IOException(java.io.IOException)

Example 5 with DBEntryField

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

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