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;
}
Aggregations