Search in sources :

Example 1 with REngineType

use of net.sf.mzmine.util.R.REngineType in project mzmine2 by mzmine.

the class DeconvolutionTask method run.

@Override
public void run() {
    errorMsg = null;
    if (!isCanceled()) {
        setStatus(TaskStatus.PROCESSING);
        LOG.info("Started peak deconvolution on " + originalPeakList);
        // Check raw data files.
        if (originalPeakList.getNumberOfRawDataFiles() > 1) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Peak deconvolution can only be performed on feature lists with a single raw data file");
        } else {
            try {
                // Peak resolver.
                final MZmineProcessingStep<PeakResolver> resolver = parameters.getParameter(PEAK_RESOLVER).getValue();
                if (resolver.getModule().getRequiresR()) {
                    // Check R availability, by trying to open the
                    // connection.
                    String[] reqPackages = resolver.getModule().getRequiredRPackages();
                    String[] reqPackagesVersions = resolver.getModule().getRequiredRPackagesVersions();
                    String callerFeatureName = resolver.getModule().getName();
                    REngineType rEngineType = resolver.getModule().getREngineType(resolver.getParameterSet());
                    this.rSession = new RSessionWrapper(rEngineType, callerFeatureName, reqPackages, reqPackagesVersions);
                    this.rSession.open();
                } else {
                    this.rSession = null;
                }
                // Deconvolve peaks.
                newPeakList = resolvePeaks(originalPeakList, this.rSession);
                if (!isCanceled()) {
                    // Add new peaklist to the project.
                    project.addPeakList(newPeakList);
                    // Add quality parameters to peaks
                    QualityParameters.calculateQualityParameters(newPeakList);
                    // Remove the original peaklist if requested.
                    if (parameters.getParameter(AUTO_REMOVE).getValue()) {
                        project.removePeakList(originalPeakList);
                    }
                    setStatus(TaskStatus.FINISHED);
                    LOG.info("Finished peak recognition on " + originalPeakList);
                }
                // Turn off R instance.
                if (this.rSession != null)
                    this.rSession.close(false);
            } catch (RSessionWrapperException e) {
                errorMsg = "'R computing error' during CentWave detection. \n" + e.getMessage();
            } catch (Exception e) {
                errorMsg = "'Unknown error' during CentWave detection. \n" + e.getMessage();
            } catch (Throwable t) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage(t.getMessage());
                LOG.log(Level.SEVERE, "Peak deconvolution error", t);
            }
            // Turn off R instance, once task ended UNgracefully.
            try {
                if (this.rSession != null && !isCanceled())
                    rSession.close(isCanceled());
            } catch (RSessionWrapperException e) {
                if (!isCanceled()) {
                    // Do not override potential previous error message.
                    if (errorMsg == null) {
                        errorMsg = e.getMessage();
                    }
                } else {
                // User canceled: Silent.
                }
            }
            // Report error.
            if (errorMsg != null) {
                setErrorMessage(errorMsg);
                setStatus(TaskStatus.ERROR);
            }
        }
    }
}
Also used : RSessionWrapper(net.sf.mzmine.util.R.RSessionWrapper) REngineType(net.sf.mzmine.util.R.REngineType) RSessionWrapperException(net.sf.mzmine.util.R.RSessionWrapperException) RSessionWrapperException(net.sf.mzmine.util.R.RSessionWrapperException)

Example 2 with REngineType

use of net.sf.mzmine.util.R.REngineType in project mzmine2 by mzmine.

the class PeakResolverSetupDialog method parametersChanged.

@Override
public void parametersChanged() {
    if (preview != null && preview.isSelected()) {
        final PeakListRow previewRow = (PeakListRow) comboPeak.getSelectedItem();
        if (previewRow != null) {
            LOG.finest("Loading new preview peak " + previewRow);
            ticPlot.removeAllTICDataSets();
            ticPlot.addTICDataset(new ChromatogramTICDataSet(previewRow.getPeaks()[0]));
            // Auto-range to axes.
            ticPlot.getXYPlot().getDomainAxis().setAutoRange(true);
            ticPlot.getXYPlot().getDomainAxis().setAutoTickUnitSelection(true);
            ticPlot.getXYPlot().getRangeAxis().setAutoRange(true);
            ticPlot.getXYPlot().getRangeAxis().setAutoTickUnitSelection(true);
            updateParameterSetFromComponents();
            // If there is some illegal value, do not load the preview but
            // just exit.
            ArrayList<String> errors = new ArrayList<String>();
            if (!parameterSet.checkParameterValues(errors)) {
                LOG.fine("Illegal parameter value: " + errors);
                return;
            }
            // Load the intensities and RTs into array.
            final Feature previewPeak = previewRow.getPeaks()[0];
            // Resolve peaks.
            Feature[] resolvedPeaks = {};
            RSessionWrapper rSession;
            try {
                if (peakResolver.getRequiresR()) {
                    // Check R availability, by trying to open the
                    // connection.
                    String[] reqPackages = peakResolver.getRequiredRPackages();
                    String[] reqPackagesVersions = peakResolver.getRequiredRPackagesVersions();
                    String callerFeatureName = peakResolver.getName();
                    REngineType rEngineType = peakResolver.getREngineType(parameters);
                    rSession = new RSessionWrapper(rEngineType, callerFeatureName, reqPackages, reqPackagesVersions);
                    rSession.open();
                } else {
                    rSession = null;
                }
                CenterFunction mzCenterFunction = new CenterFunction(CenterMeasure.MEDIAN);
                // preview doesn't show msms scans
                // set it to be default searching range
                resolvedPeaks = peakResolver.resolvePeaks(previewPeak, parameters, rSession, mzCenterFunction, 0, 0);
                // Turn off R instance.
                if (rSession != null)
                    rSession.close(false);
            } catch (RSessionWrapperException e) {
                throw new IllegalStateException(e.getMessage());
            } catch (Throwable t) {
                LOG.log(Level.SEVERE, "Peak deconvolution error", t);
                MZmineCore.getDesktop().displayErrorMessage(this, t.toString());
            }
            // Add resolved peaks to TIC plot.
            final int peakCount = Math.min(MAX_PEAKS, resolvedPeaks.length);
            for (int i = 0; i < peakCount; i++) {
                final XYDataset peakDataSet = new PeakDataSet(resolvedPeaks[i]);
                ticPlot.addPeakDataset(peakDataSet);
            }
            // Check peak count.
            if (resolvedPeaks.length > MAX_PEAKS) {
                // MZmineCore.getDesktop().displayMessage(this,
                // "Too many peaks detected, please adjust parameter values");
                MZmineCore.getDesktop().displayMessage(this, "Too many peaks detected. Not all of the peaks might be displayed");
            }
        }
    }
}
Also used : PeakDataSet(net.sf.mzmine.modules.visualization.tic.PeakDataSet) RSessionWrapper(net.sf.mzmine.util.R.RSessionWrapper) REngineType(net.sf.mzmine.util.R.REngineType) ArrayList(java.util.ArrayList) Feature(net.sf.mzmine.datamodel.Feature) RSessionWrapperException(net.sf.mzmine.util.R.RSessionWrapperException) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) XYDataset(org.jfree.data.xy.XYDataset) CenterFunction(net.sf.mzmine.util.maths.CenterFunction)

Aggregations

REngineType (net.sf.mzmine.util.R.REngineType)2 RSessionWrapper (net.sf.mzmine.util.R.RSessionWrapper)2 RSessionWrapperException (net.sf.mzmine.util.R.RSessionWrapperException)2 ArrayList (java.util.ArrayList)1 Feature (net.sf.mzmine.datamodel.Feature)1 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)1 PeakDataSet (net.sf.mzmine.modules.visualization.tic.PeakDataSet)1 CenterFunction (net.sf.mzmine.util.maths.CenterFunction)1 XYDataset (org.jfree.data.xy.XYDataset)1