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