Search in sources :

Example 6 with AbstractTask

use of net.sf.mzmine.taskcontrol.AbstractTask in project mzmine2 by mzmine.

the class GnpsGcExportAndSubmitTask method run.

@Override
public void run() {
    final AbstractTask thistask = this;
    setStatus(TaskStatus.PROCESSING);
    List<AbstractTask> list = new ArrayList<>(3);
    // add mgf export task
    list.add(addAdapMgfTask(parameters));
    // add csv quant table
    list.add(addQuantTableTask(parameters, null));
    // finish listener to submit
    final File fileName = file;
    final File folder = file.getParentFile();
    new AllTasksFinishedListener(list, true, // succeed
    l -> {
        try {
            LOG.info("succeed" + thistask.getStatus().toString());
            if (submit) {
                GnpsGcSubmitParameters param = parameters.getParameter(GnpsGcExportAndSubmitParameters.SUBMIT).getEmbeddedParameters();
                submit(fileName, param);
            }
            // open folder
            try {
                if (openFolder && Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(folder);
                }
            } catch (Exception ex) {
            }
        } finally {
            // finish task
            if (thistask.getStatus() == TaskStatus.PROCESSING)
                thistask.setStatus(TaskStatus.FINISHED);
        }
    }, lerror -> {
        setErrorMessage("GNPS-GC submit was not started due too errors while file export");
        thistask.setStatus(TaskStatus.ERROR);
        throw new MSDKRuntimeException("GNPS-GC submit was not started due too errors while file export");
    }, // cancel if one was cancelled
    listCancelled -> cancel()) {

        @Override
        public void taskStatusChanged(Task task, TaskStatus newStatus, TaskStatus oldStatus) {
            super.taskStatusChanged(task, newStatus, oldStatus);
            // show progress
            progress.getAndSet(getProgress());
        }
    };
    MZmineCore.getTaskController().addTasks(list.toArray(new AbstractTask[list.size()]));
    // wait till finish
    while (!(isCanceled() || isFinished())) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            LOG.log(Level.SEVERE, "Error in GNPS-GC export/submit task", e);
        }
    }
}
Also used : CSVExportTask(net.sf.mzmine.modules.peaklistmethods.io.csvexport.CSVExportTask) AdapMgfExportTask(net.sf.mzmine.modules.peaklistmethods.io.adap.mgfexport.AdapMgfExportTask) Task(net.sf.mzmine.taskcontrol.Task) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) ArrayList(java.util.ArrayList) AllTasksFinishedListener(net.sf.mzmine.taskcontrol.AllTasksFinishedListener) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) File(java.io.File) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException)

Example 7 with AbstractTask

use of net.sf.mzmine.taskcontrol.AbstractTask in project mzmine2 by mzmine.

the class LocalSpectralDBSearchTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    int count = 0;
    try {
        tasks = parseFile(dataBaseFile);
        totalTasks = tasks.size();
        if (!tasks.isEmpty()) {
            // wait for the tasks to finish
            while (!isCanceled() && !tasks.isEmpty()) {
                for (int i = 0; i < tasks.size(); i++) {
                    if (tasks.get(i).isFinished() || tasks.get(i).isCanceled()) {
                        count += tasks.get(i).getCount();
                        tasks.remove(i);
                        i--;
                    }
                }
                // wait for all sub tasks to finish
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    cancel();
                }
            }
            // cancelled
            if (isCanceled()) {
                tasks.stream().forEach(AbstractTask::cancel);
            }
        } else {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("DB file was empty - or error while parsing " + dataBaseFile);
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Could not read file " + dataBaseFile, e);
        setStatus(TaskStatus.ERROR);
        setErrorMessage(e.toString());
    }
    logger.info("Added " + count + " spectral library matches");
    // Add task description to peakList
    peakList.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod("Peak identification using MS/MS spectral database " + dataBaseFile, parameters));
    // Repaint the window to reflect the change in the feature list
    Desktop desktop = MZmineCore.getDesktop();
    if (!(desktop instanceof HeadLessDesktop))
        desktop.getMainWindow().repaint();
    setStatus(TaskStatus.FINISHED);
}
Also used : AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) HeadLessDesktop(net.sf.mzmine.desktop.impl.HeadLessDesktop) Desktop(net.sf.mzmine.desktop.Desktop) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod) IOException(java.io.IOException) UnsupportedFormatException(net.sf.mzmine.util.spectraldb.parser.UnsupportedFormatException) HeadLessDesktop(net.sf.mzmine.desktop.impl.HeadLessDesktop)

Example 8 with AbstractTask

use of net.sf.mzmine.taskcontrol.AbstractTask in project mzmine2 by mzmine.

the class PeakListBlankSubtractionMasterTask method run.

@Override
public void run() {
    if (!checkBlankSelection(alignedFeatureList, blankRaws)) {
        setErrorMessage("Peak list " + alignedFeatureList.getName() + " does no contain all selected blank raw data files.");
        setStatus(TaskStatus.ERROR);
        return;
    }
    setStatus(TaskStatus.PROCESSING);
    // PeakListRow[] rowsInBlanks =
    // getFeatureRowsContainedBlanks(alignedFeatureList, blankRaws, minBlankDetections);
    PeakListRow[] rows = PeakUtils.copyPeakRows(alignedFeatureList.getRows());
    rows = PeakUtils.sortRowsMzAsc(rows);
    for (RawDataFile raw : alignedFeatureList.getRawDataFiles()) {
        // only create a task for every file that is not a blank
        if (Arrays.asList(blankRaws).contains(raw))
            continue;
        // these tasks will access the passed array and remove the features that appear in their raw
        // data file and the blanks from these rows
        AbstractTask task = new PeakListBlankSubtractionSingleTask(parameters, raw, rows);
        MZmineCore.getTaskController().addTask(task);
        subTasks.add(task);
        if (getStatus() == TaskStatus.CANCELED)
            return;
    }
    // wait for tasks to finish
    boolean allTasksFinished = false;
    while (!allTasksFinished) {
        allTasksFinished = true;
        for (AbstractTask task : subTasks) {
            if (task.getStatus() != TaskStatus.FINISHED)
                allTasksFinished = false;
        }
        try {
            TimeUnit.MILLISECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
            setErrorMessage(e.getMessage());
            setStatus(TaskStatus.ERROR);
            return;
        }
        if (getStatus() == TaskStatus.CANCELED)
            return;
    }
    // remove rows that only contain blankRaws
    List<RawDataFile> blankRawsList = Arrays.asList(blankRaws);
    int onlyBlankRows = 0;
    for (int i = 0; i < rows.length; i++) {
        PeakListRow row = rows[i];
        if (blankRawsList.containsAll(Arrays.asList(row.getRawDataFiles()))) {
            onlyBlankRows++;
            rows[i] = null;
        }
        if (getStatus() == TaskStatus.CANCELED)
            return;
    }
    logger.finest("Removed " + onlyBlankRows + " rows that only existed in blankfiles.");
    PeakList result = new SimplePeakList(alignedFeatureList.getName() + " sbtrctd", alignedFeatureList.getRawDataFiles());
    for (PeakListRow row : rows) {
        if (row != null) {
            result.addRow(row);
        }
    }
    PeakListUtils.copyPeakListAppliedMethods(alignedFeatureList, result);
    result.addDescriptionOfAppliedTask(new SimplePeakListAppliedMethod(PeakListBlankSubtractionModule.MODULE_NAME, parameters));
    project.addPeakList(result);
    setStatus(TaskStatus.FINISHED);
}
Also used : PeakListRow(net.sf.mzmine.datamodel.PeakListRow) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) SimplePeakListAppliedMethod(net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)

Example 9 with AbstractTask

use of net.sf.mzmine.taskcontrol.AbstractTask in project mzmine2 by mzmine.

the class GnpsFbmnExportAndSubmitTask method run.

@Override
public void run() {
    final AbstractTask thistask = this;
    setStatus(TaskStatus.PROCESSING);
    boolean openFolder = parameters.getParameter(GnpsFbmnExportAndSubmitParameters.OPEN_FOLDER).getValue();
    boolean submit = parameters.getParameter(GnpsFbmnExportAndSubmitParameters.SUBMIT).getValue();
    File file = parameters.getParameter(GnpsFbmnExportAndSubmitParameters.FILENAME).getValue();
    file = FileAndPathUtil.eraseFormat(file);
    parameters.getParameter(GnpsFbmnExportAndSubmitParameters.FILENAME).setValue(file);
    List<AbstractTask> list = new ArrayList<>(3);
    GnpsFbmnMgfExportTask task = new GnpsFbmnMgfExportTask(parameters);
    list.add(task);
    // add csv quant table
    list.add(addQuantTableTask(parameters, null));
    // finish listener to submit
    final File fileName = file;
    final File folder = file.getParentFile();
    new AllTasksFinishedListener(list, true, // succeed
    l -> {
        try {
            LOG.info("succeed" + thistask.getStatus().toString());
            if (submit) {
                GnpsFbmnSubmitParameters param = parameters.getParameter(GnpsFbmnExportAndSubmitParameters.SUBMIT).getEmbeddedParameters();
                submit(fileName, param);
            }
            // open folder
            try {
                if (openFolder && Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(folder);
                }
            } catch (Exception ex) {
            }
        } finally {
            // finish task
            if (thistask.getStatus() == TaskStatus.PROCESSING)
                thistask.setStatus(TaskStatus.FINISHED);
        }
    }, lerror -> {
        setErrorMessage("GNPS submit was not started due too errors while file export");
        thistask.setStatus(TaskStatus.ERROR);
        throw new MSDKRuntimeException("GNPS submit was not started due too errors while file export");
    }, // cancel if one was cancelled
    listCancelled -> cancel()) {

        @Override
        public void taskStatusChanged(Task task, TaskStatus newStatus, TaskStatus oldStatus) {
            super.taskStatusChanged(task, newStatus, oldStatus);
            // show progress
            progress.getAndSet(getProgress());
        }
    };
    MZmineCore.getTaskController().addTasks(list.toArray(new AbstractTask[list.size()]));
    // wait till finish
    while (!(isCanceled() || isFinished())) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            LOG.log(Level.SEVERE, "Error in GNPS export/submit task", e);
        }
    }
}
Also used : CSVExportTask(net.sf.mzmine.modules.peaklistmethods.io.csvexport.CSVExportTask) Task(net.sf.mzmine.taskcontrol.Task) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) ArrayList(java.util.ArrayList) AllTasksFinishedListener(net.sf.mzmine.taskcontrol.AllTasksFinishedListener) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) File(java.io.File)

Example 10 with AbstractTask

use of net.sf.mzmine.taskcontrol.AbstractTask 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;
}
Also used : PrecursorDBPeakIdentity(net.sf.mzmine.util.spectraldb.entry.PrecursorDBPeakIdentity) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) AutoLibraryParser(net.sf.mzmine.util.spectraldb.parser.AutoLibraryParser) ArrayList(java.util.ArrayList) LibraryEntryProcessor(net.sf.mzmine.util.spectraldb.parser.LibraryEntryProcessor) SpectralDBEntry(net.sf.mzmine.util.spectraldb.entry.SpectralDBEntry)

Aggregations

AbstractTask (net.sf.mzmine.taskcontrol.AbstractTask)10 IOException (java.io.IOException)4 Desktop (net.sf.mzmine.desktop.Desktop)4 HeadLessDesktop (net.sf.mzmine.desktop.impl.HeadLessDesktop)4 Task (net.sf.mzmine.taskcontrol.Task)4 UnsupportedFormatException (net.sf.mzmine.util.spectraldb.parser.UnsupportedFormatException)4 ArrayList (java.util.ArrayList)3 SimplePeakListAppliedMethod (net.sf.mzmine.datamodel.impl.SimplePeakListAppliedMethod)3 TaskStatus (net.sf.mzmine.taskcontrol.TaskStatus)3 MSDKRuntimeException (io.github.msdk.MSDKRuntimeException)2 File (java.io.File)2 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)2 CSVExportTask (net.sf.mzmine.modules.peaklistmethods.io.csvexport.CSVExportTask)2 SpectraIdentificationResultsWindow (net.sf.mzmine.modules.visualization.spectra.spectralmatchresults.SpectraIdentificationResultsWindow)2 AllTasksFinishedListener (net.sf.mzmine.taskcontrol.AllTasksFinishedListener)2 PeakList (net.sf.mzmine.datamodel.PeakList)1 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)1 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)1 AdapMgfExportTask (net.sf.mzmine.modules.peaklistmethods.io.adap.mgfexport.AdapMgfExportTask)1 TaskStatusListener (net.sf.mzmine.taskcontrol.TaskStatusListener)1