Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #37 from golgauth/rserve-integration-4.1
Browse files Browse the repository at this point in the history
Rserve integration 4.1
  • Loading branch information
tomas-pluskal committed May 31, 2015
2 parents 06f6c61 + fff87b7 commit a59e958
Show file tree
Hide file tree
Showing 27 changed files with 1,193 additions and 884 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.taskcontrol.AbstractTask;
import net.sf.mzmine.taskcontrol.TaskStatus;
import net.sf.mzmine.util.R.RSessionWrapper;
import net.sf.mzmine.util.R.RSessionWrapperException;

public class DeconvolutionTask extends AbstractTask {

Expand All @@ -59,6 +61,10 @@ public class DeconvolutionTask extends AbstractTask {
// User parameters
private final ParameterSet parameters;

private RSessionWrapper rSession;
private String errorMsg;


/**
* Create the task.
*
Expand All @@ -77,7 +83,7 @@ public DeconvolutionTask(final MZmineProject project, final PeakList list,
newPeakList = null;
processedRows = 0;
totalRows = 0;
}
}

@Override
public String getTaskDescription() {
Expand All @@ -95,6 +101,8 @@ public double getFinishedPercentage() {
@Override
public void run() {

errorMsg = null;

if (!isCanceled()) {

setStatus(TaskStatus.PROCESSING);
Expand All @@ -110,8 +118,23 @@ public void run() {

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();
this.rSession = new RSessionWrapper(callerFeatureName, /*this.rEngineType,*/ reqPackages, reqPackagesVersions);
this.rSession.open();
} else {
this.rSession = null;
}

// Deconvolve peaks.
newPeakList = resolvePeaks(originalPeakList);
newPeakList = resolvePeaks(originalPeakList, this.rSession);

if (!isCanceled()) {

Expand All @@ -128,12 +151,41 @@ public void run() {
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);
}
}
}
}
Expand All @@ -144,8 +196,9 @@ public void run() {
* @param peakList
* holds the chromatogram to deconvolve.
* @return a new peak list holding the resolved peaks.
* @throws RSessionWrapperException
*/
private PeakList resolvePeaks(final PeakList peakList) {
private PeakList resolvePeaks(final PeakList peakList, RSessionWrapper rSession) throws RSessionWrapperException {

// Get data file information.
final RawDataFile dataFile = peakList.getRawDataFile(0);
Expand Down Expand Up @@ -203,7 +256,8 @@ private PeakList resolvePeaks(final PeakList peakList) {
final PeakResolver resolverModule = resolver.getModule();
final ParameterSet resolverParams = resolver.getParameterSet();
final Feature[] peaks = resolverModule.resolvePeaks(chromatogram,
scanNumbers, retentionTimes, intensities, resolverParams);
scanNumbers, retentionTimes, intensities, resolverParams,
rSession);

// Add peaks to the new peak list.
for (final Feature peak : peaks) {
Expand All @@ -217,5 +271,18 @@ private PeakList resolvePeaks(final PeakList peakList) {
}

return resolvedPeaks;
}

@Override
public void cancel() {

super.cancel();
// Turn off R instance, if already existing.
try {
if (this.rSession != null) this.rSession.close(true);
}
catch (RSessionWrapperException e) {
// Silent, always...
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,34 @@
import net.sf.mzmine.datamodel.Feature;
import net.sf.mzmine.modules.MZmineModule;
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.util.R.RSessionWrapper;
import net.sf.mzmine.util.R.RSessionWrapperException;

public interface PeakResolver extends MZmineModule {

/**
* Gets if resolver requires R, if applicable
*/
public boolean getRequiresR();
/**
* Gets R required packages for the resolver's method, if applicable
*/
public String[] getRequiredRPackages();
/**
* Gets R required packages versions for the resolver's method, if applicable
*/
public String[] getRequiredRPackagesVersions();

/**
* Resolve a peaks found within given chromatogram. For easy use, three
* arrays (scanNumbers, retentionTimes and intensities) are provided,
* although the contents of these arrays can also be obtained from the
* chromatogram itself. The size of these arrays must be same, and must be
* equal to the number of scans covered by given chromatogram.
* @throws RSessionWrapperException
*/
public Feature[] resolvePeaks(Feature chromatogram, int scanNumbers[],
double retentionTimes[], double intensities[],
ParameterSet parameters);
ParameterSet parameters, RSessionWrapper rSession) throws RSessionWrapperException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.parameters.dialogs.ParameterSetupDialog;
import net.sf.mzmine.util.GUIUtils;
import net.sf.mzmine.util.R.RSessionWrapper;
import net.sf.mzmine.util.R.RSessionWrapperException;

import org.jfree.data.xy.XYDataset;

Expand Down Expand Up @@ -233,10 +235,31 @@ public void parametersChanged() {

// 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();
rSession = new RSessionWrapper(callerFeatureName, /*this.rEngineType,*/ reqPackages, reqPackagesVersions);
rSession.open();
} else {
rSession = null;
}

resolvedPeaks = peakResolver.resolvePeaks(previewPeak,
scanNumbers, retentionTimes, intensities,
parameters);
parameters, rSession);

// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.sf.mzmine.modules.peaklistmethods.peakpicking.deconvolution.PeakResolver;
import net.sf.mzmine.modules.peaklistmethods.peakpicking.deconvolution.ResolvedPeak;
import net.sf.mzmine.parameters.ParameterSet;
import net.sf.mzmine.util.R.RSessionWrapper;

import com.google.common.collect.Range;

Expand All @@ -49,7 +50,8 @@ public class BaselinePeakDetector implements PeakResolver {
@Override
public Feature[] resolvePeaks(final Feature chromatogram,
final int[] scanNumbers, final double[] retentionTimes,
final double[] intensities, ParameterSet parameters) {
final double[] intensities, ParameterSet parameters,
RSessionWrapper rSession) {

// Get parameters.
final double minimumPeakHeight = parameters.getParameter(
Expand Down Expand Up @@ -116,4 +118,19 @@ public Feature[] resolvePeaks(final Feature chromatogram,
public @Nonnull Class<? extends ParameterSet> getParameterSetClass() {
return BaselinePeakDetectorParameters.class;
}

@Override
public boolean getRequiresR() {
return false;
}

@Override
public String[] getRequiredRPackages() {
return null;
}

@Override
public String[] getRequiredRPackagesVersions() {
return null;
}
}
Loading

0 comments on commit a59e958

Please sign in to comment.