Search in sources :

Example 1 with Ms2Experiment

use of de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment 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();
    }
}
Also used : MsSpectrum(io.github.msdk.datamodel.MsSpectrum) IonType(io.github.msdk.datamodel.IonType) ArrayList(java.util.ArrayList) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException) SiriusIonAnnotation(io.github.msdk.id.sirius.SiriusIonAnnotation) FormulaConstraints(de.unijena.bioinf.ChemistryBase.chem.FormulaConstraints) Ms2Experiment(de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment) Scan(net.sf.mzmine.datamodel.Scan) MZmineToMSDKMsScan(net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MZmineToMSDKMsScan(net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan) ExecutionException(java.util.concurrent.ExecutionException) SiriusIonAnnotation(io.github.msdk.id.sirius.SiriusIonAnnotation) IonAnnotation(io.github.msdk.datamodel.IonAnnotation) SiriusIdentificationMethod(io.github.msdk.id.sirius.SiriusIdentificationMethod) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with Ms2Experiment

use of de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment 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);
}
Also used : IonType(io.github.msdk.datamodel.IonType) ArrayList(java.util.ArrayList) FormulaConstraints(de.unijena.bioinf.ChemistryBase.chem.FormulaConstraints) Ms2Experiment(de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MassList(net.sf.mzmine.datamodel.MassList) ExecutionException(java.util.concurrent.ExecutionException) SiriusIdentificationMethod(io.github.msdk.id.sirius.SiriusIdentificationMethod) TimeoutException(java.util.concurrent.TimeoutException) SimpleMsSpectrum(io.github.msdk.datamodel.SimpleMsSpectrum) MsSpectrum(io.github.msdk.datamodel.MsSpectrum) CountDownLatch(java.util.concurrent.CountDownLatch) MissingMassListException(net.sf.mzmine.util.exceptions.MissingMassListException) SiriusIonAnnotation(io.github.msdk.id.sirius.SiriusIonAnnotation) ExecutorService(java.util.concurrent.ExecutorService) Scan(net.sf.mzmine.datamodel.Scan) SiriusIonAnnotation(io.github.msdk.id.sirius.SiriusIonAnnotation) IonAnnotation(io.github.msdk.datamodel.IonAnnotation) NumberFormat(java.text.NumberFormat)

Aggregations

FormulaConstraints (de.unijena.bioinf.ChemistryBase.chem.FormulaConstraints)2 Ms2Experiment (de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment)2 IonAnnotation (io.github.msdk.datamodel.IonAnnotation)2 IonType (io.github.msdk.datamodel.IonType)2 MsSpectrum (io.github.msdk.datamodel.MsSpectrum)2 SiriusIdentificationMethod (io.github.msdk.id.sirius.SiriusIdentificationMethod)2 SiriusIonAnnotation (io.github.msdk.id.sirius.SiriusIonAnnotation)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Scan (net.sf.mzmine.datamodel.Scan)2 MissingMassListException (net.sf.mzmine.util.exceptions.MissingMassListException)2 SimpleMsSpectrum (io.github.msdk.datamodel.SimpleMsSpectrum)1 NumberFormat (java.text.NumberFormat)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 MassList (net.sf.mzmine.datamodel.MassList)1 MZmineToMSDKMsScan (net.sf.mzmine.datamodel.impl.MZmineToMSDKMsScan)1