use of de.unijena.bioinf.ChemistryBase.chem.FormulaConstraints in project mzmine2 by mzmine.
the class SiriusThread method run.
@Override
public void run() {
List<MsSpectrum> ms1list = new ArrayList<>(), ms2list = new ArrayList<>();
try {
Scan ms1Scan = peakListRow.getBestPeak().getRepresentativeScan();
Collection<Scan> top10ms2Scans = ScanUtils.selectBestMS2Scans(peakListRow, massListName, 10);
// Convert to MSDK data model
ms1list.add(new MZmineToMSDKMsScan(ms1Scan));
for (Scan s : top10ms2Scans) {
ms2list.add(new MZmineToMSDKMsScan(s));
}
} catch (MissingMassListException f) {
releaseResources();
task.remoteCancel("Scan does not have requested Mass List name [" + massListName + "]");
return;
}
FormulaConstraints constraints = ConstraintsGenerator.generateConstraint(range);
IonType siriusIon = IonTypeUtil.createIonType(ionType.toString());
List<IonAnnotation> siriusResults = null;
SiriusIdentificationMethod siriusMethod = null;
/*
* Code block below gives SiriusMethod specific amount of time to be executed, if it expires ->
* log error and continue
*/
try {
final SiriusIdentificationMethod method = new SiriusIdentificationMethod(ms1list, ms2list, peakListRow.getAverageMZ(), siriusIon, siriusCandidates, constraints, deviationPpm);
// On some spectra it may never stop (halting problem), that's why interruptable thread is
// used
final Future<List<IonAnnotation>> f = service.submit(() -> {
return method.execute();
});
siriusResults = f.get(siriusTimer, TimeUnit.SECONDS);
siriusMethod = method;
if (ms2list.isEmpty()) {
/* If no MSMS spectra - add sirius results */
addSiriusCompounds(siriusResults, peakListRow, siriusCandidates);
} else {
/* Initiate FingerId processing */
Ms2Experiment experiment = siriusMethod.getExperiment();
for (int index = 0; index < siriusCandidates; index++) {
SiriusIonAnnotation annotation = (SiriusIonAnnotation) siriusResults.get(index);
try {
FingerIdWebMethodTask task = new FingerIdWebMethodTask(annotation, experiment, fingeridCandidates, peakListRow);
MZmineCore.getTaskController().addTask(task, TaskPriority.NORMAL);
Thread.sleep(1000);
} catch (InterruptedException interrupt) {
logger.error("Processing of FingerWebMethods were interrupted");
/* If interrupted, store last item */
List<IonAnnotation> lastItem = new LinkedList<>();
lastItem.add(annotation);
addSiriusCompounds(lastItem, peakListRow, 1);
}
}
}
} catch (InterruptedException | TimeoutException ie) {
logger.error("Timeout on Sirius method expired, abort. Row id = {}", peakListRow.getID());
} catch (ExecutionException ce) {
logger.error("Concurrency error during Sirius method. Row id = {}", peakListRow.getID());
} finally {
// Do not forget to release resources!
releaseResources();
}
}
use of de.unijena.bioinf.ChemistryBase.chem.FormulaConstraints in project mzmine2 by mzmine.
the class SingleRowIdentificationTask method run.
/**
* @see Runnable#run()
*/
public void run() {
setStatus(TaskStatus.PROCESSING);
NumberFormat massFormater = MZmineCore.getConfiguration().getMZFormat();
ResultWindow window = new ResultWindow(peakListRow, this);
window.setTitle("SIRIUS/CSI-FingerID identification of " + massFormater.format(parentMass) + " m/z");
window.setVisible(true);
List<MsSpectrum> ms1list = new ArrayList<>(), ms2list = new ArrayList<>();
try {
Scan ms1Scan = peakListRow.getBestPeak().getRepresentativeScan();
Collection<Scan> top10ms2Scans = ScanUtils.selectBestMS2Scans(peakListRow, massListName, 10);
logger.debug("Adding MS1 scan " + ScanUtils.scanToString(ms1Scan, true) + " for SIRIUS identification");
// Convert to MSDK data model
ms1list.add(buildMSDKSpectrum(ms1Scan, massListName));
for (Scan ms2Scan : top10ms2Scans) {
logger.debug("Adding MS/MS scan " + ScanUtils.scanToString(ms2Scan, true) + " for SIRIUS identification");
ms2list.add(buildMSDKSpectrum(ms2Scan, massListName));
}
} catch (MissingMassListException f) {
showError(window, "Scan does not contain Mass List with requested name. [" + massListName + "]");
return;
}
// Use executor to run Sirius Identification Method as an Interruptable thread.
// Otherwise it may compute for too long (or even forever).
final ExecutorService service = Executors.newSingleThreadExecutor();
SiriusIdentificationMethod siriusMethod = null;
List<IonAnnotation> siriusResults = null;
/* Sirius processing */
try {
FormulaConstraints constraints = ConstraintsGenerator.generateConstraint(range);
IonType type = IonTypeUtil.createIonType(ionType.toString());
final SiriusIdentificationMethod method = new SiriusIdentificationMethod(ms1list, ms2list, parentMass, type, siriusCandidates, constraints, deviationPpm);
final Future<List<IonAnnotation>> f = service.submit(() -> {
return method.execute();
});
siriusResults = f.get(timer, TimeUnit.SECONDS);
siriusMethod = method;
} catch (InterruptedException | TimeoutException ie) {
logger.error("Timeout on Sirius method expired, abort.");
showError(window, String.format("Processing of the peaklist with mass %.2f by Sirius module expired.\n", parentMass) + "Reinitialize the task with larger Sirius Timer value.");
return;
} catch (ExecutionException ce) {
ce.printStackTrace();
logger.error("Concurrency error during Sirius method: " + ce.getMessage());
showError(window, String.format("Sirius failed to predict compounds from row with id = %d", peakListRow.getID()));
return;
}
/* FingerId processing */
if (!ms2list.isEmpty()) {
try {
latch = new CountDownLatch(siriusResults.size());
Ms2Experiment experiment = siriusMethod.getExperiment();
fingerTasks = new LinkedList<>();
/* Create a new FingerIdWebTask for each Sirius result */
for (IonAnnotation ia : siriusResults) {
SiriusIonAnnotation annotation = (SiriusIonAnnotation) ia;
FingerIdWebMethodTask task = new FingerIdWebMethodTask(annotation, experiment, fingerCandidates, window);
task.setLatch(latch);
fingerTasks.add(task);
MZmineCore.getTaskController().addTask(task, TaskPriority.NORMAL);
}
// Sleep for not overloading boecker-labs servers
Thread.sleep(1000);
} catch (InterruptedException interrupt) {
logger.error("Processing of FingerWebMethods were interrupted");
}
} else {
/* MS/MS spectrum is not present */
window.addListofItems(siriusMethod.getResult());
}
// If there was a FingerId processing, wait until subtasks finish
try {
if (latch != null)
latch.await();
} catch (InterruptedException e) {
}
setStatus(TaskStatus.FINISHED);
}
Aggregations