org.fhcrc.cpl.toolbox.statistics
Class RInterface

java.lang.Object
  extended by org.fhcrc.cpl.toolbox.statistics.RInterface

public class RInterface
extends java.lang.Object

A class with generic methods for interfacing with R. Hides all kinds of exceptions, both from R and from file IO


Nested Class Summary
static class RInterface.RErrorReaderThread
          Manages the R error inputstream.
static class RInterface.RReaderThread
          Latches onto the R process' input stream and doesn't let go TODO: provide a way to get access to just whatever was read since last time you checked
static class RInterface.RWriterThread
          Handles a single write of a byte array to R
 
Field Summary
protected static int DEFAULT_MAX_R_WAIT_MILLIS
           
protected static int expressionNumber
           
protected static int R_SLEEP_INCREMENT_MILLIS
           
 
Constructor Summary
RInterface()
           
 
Method Summary
protected static java.lang.String collectInput(RInterface.RReaderThread readerThread, java.lang.Process p, java.lang.String endResponseSentinel, int maxMillisToWaitForResponse)
          Talks to the ReaderThread to check periodically for the specified sentinel.
static java.lang.String evaluateRCommand(java.lang.String rCommand)
          Evaluate an R command, giving it the default amount of time to respond
static java.lang.String evaluateRCommand(java.lang.String rCommand, int maxMillisToWaitForResponse)
          Evaluate an R command, or series of commands.
static java.lang.String evaluateRExpression(java.lang.String rExpression, java.util.Map<java.lang.String,double[]> vectorVariableValues, java.util.Map<java.lang.String,double[][]> matrixVariableValues, java.lang.String[] dependentPackageNames)
          Cover method with default wait time
static java.lang.String evaluateRExpression(java.lang.String rExpression, java.util.Map<java.lang.String,double[]> vectorVariableValues, java.util.Map<java.lang.String,double[][]> matrixVariableValues, java.lang.String[] dependentPackageNames, int maxMillisToWaitForResponse)
           
static java.lang.String evaluateRExpression(java.lang.String rExpression, java.util.Map<java.lang.String,double[]> variableValues, java.lang.String[] dependentPackageNames)
          cover method with default wait time, vector variables only
static java.lang.String evaluateRExpression(java.lang.String rExpression, java.util.Map<java.lang.String,double[]> variableValues, java.lang.String[] dependentPackageNames, int maxMillisToWaitForResponse)
          Generic method for running an R expression and feeding the output to a file.
static java.lang.String evaluateRExpression(java.lang.String rExpression, java.util.Map<java.lang.String,java.lang.Object> scalarVariableValues, java.util.Map<java.lang.String,double[]> vectorVariableValues, java.util.Map<java.lang.String,double[][]> matrixVariableValues, java.lang.String[] dependentPackageNames, int maxMillisToWaitForResponse)
          Generic method for running an R expression and feeding the output to a file.
static java.util.Map<java.lang.String,java.lang.String> extractVariableStringsFromListOutput(java.lang.String listOutput)
          Given R output, extract a map from variable names provided by a "list" command, to the strings that contain the values
static java.lang.String generateRFriendlyPath(java.io.File file)
          R doesn't like backslashes, even on Windows.
static java.lang.String generateRFriendlyPath(java.lang.String filePath)
           
protected static java.lang.String getRCommandStringForOS()
          Choose the right name of the command for running R for this OS
static java.util.List<java.lang.Float> parseNumericList(java.lang.String varString)
          Parse an R list result as a list of Floats
protected static java.util.List<java.lang.Float> processCoefficientOnlyLine(java.lang.String coefficientLine)
          Parse the important bits of a response from R's 'coeff' command
static double[] processRCoefficientResponse(java.lang.String rResponse)
          cute little method that handles a n R coefficients() response by ignoring the first line and splitting the second around whitespace.
static float[][] processRMatrixResponse(java.lang.String rResponse)
          First index in response indicates row.
static java.lang.String readResourceFile(java.lang.String resourcePath)
          Read in a fully-qualified resource on the classpath, i.e., an R script
static boolean runRScript(java.io.File rScriptFile, java.lang.Object caller)
          Run an R script, setting the R directory to be the temp dir.
protected static void tellRToQuit(java.io.DataOutputStream rOut)
           
static void writeToR(java.io.DataOutputStream rOut, byte[] bytesToWrite)
          Cover method to start up a writer thread, send it some bytes, and make sure they got written
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expressionNumber

protected static int expressionNumber

DEFAULT_MAX_R_WAIT_MILLIS

protected static final int DEFAULT_MAX_R_WAIT_MILLIS
See Also:
Constant Field Values

R_SLEEP_INCREMENT_MILLIS

protected static final int R_SLEEP_INCREMENT_MILLIS
See Also:
Constant Field Values
Constructor Detail

RInterface

public RInterface()
Method Detail

getRCommandStringForOS

protected static java.lang.String getRCommandStringForOS()
Choose the right name of the command for running R for this OS

Returns:

runRScript

public static boolean runRScript(java.io.File rScriptFile,
                                 java.lang.Object caller)
                          throws java.lang.RuntimeException
Run an R script, setting the R directory to be the temp dir. Also mark the Rout file generated by R for deletion when tempfiles for the caller are cleaned up (don't want to do it here because a user might want to view the Rout file). This is the easy and stable way to invoke R. The alternative is to interact with the input, output, and error streams of the process, which provides some advantages.

Parameters:
rScriptFile -
Returns:
true if successful, false otherwise
Throws:
java.lang.RuntimeException

processRMatrixResponse

public static float[][] processRMatrixResponse(java.lang.String rResponse)
First index in response indicates row. Second indicates column.

Parameters:
rResponse -
Returns:

processRCoefficientResponse

public static double[] processRCoefficientResponse(java.lang.String rResponse)
                                            throws java.lang.NumberFormatException
cute little method that handles a n R coefficients() response by ignoring the first line and splitting the second around whitespace. Brittle, don't give it anything at all funky

Parameters:
rResponse -
Returns:
Throws:
java.lang.NumberFormatException

processCoefficientOnlyLine

protected static java.util.List<java.lang.Float> processCoefficientOnlyLine(java.lang.String coefficientLine)
Parse the important bits of a response from R's 'coeff' command

Parameters:
coefficientLine -
Returns:

parseNumericList

public static java.util.List<java.lang.Float> parseNumericList(java.lang.String varString)
Parse an R list result as a list of Floats

Parameters:
varString -
Returns:

extractVariableStringsFromListOutput

public static java.util.Map<java.lang.String,java.lang.String> extractVariableStringsFromListOutput(java.lang.String listOutput)
Given R output, extract a map from variable names provided by a "list" command, to the strings that contain the values

Parameters:
listOutput -
Returns:

evaluateRCommand

public static java.lang.String evaluateRCommand(java.lang.String rCommand)
Evaluate an R command, giving it the default amount of time to respond

Parameters:
rCommand - command to execute
Returns:
results from R

collectInput

protected static java.lang.String collectInput(RInterface.RReaderThread readerThread,
                                               java.lang.Process p,
                                               java.lang.String endResponseSentinel,
                                               int maxMillisToWaitForResponse)
                                        throws java.io.IOException
Talks to the ReaderThread to check periodically for the specified sentinel. TODO: make this more efficient. Right now, at every read, it pulls back the whole response

Parameters:
readerThread -
endResponseSentinel -
maxMillisToWaitForResponse -
Returns:
Throws:
java.io.IOException

writeToR

public static void writeToR(java.io.DataOutputStream rOut,
                            byte[] bytesToWrite)
                     throws java.io.IOException
Cover method to start up a writer thread, send it some bytes, and make sure they got written

Parameters:
rOut -
bytesToWrite -
Throws:
java.io.IOException

readResourceFile

public static java.lang.String readResourceFile(java.lang.String resourcePath)
                                         throws java.io.IOException
Read in a fully-qualified resource on the classpath, i.e., an R script

Parameters:
resourcePath -
Returns:
Throws:
java.io.IOException

evaluateRCommand

public static java.lang.String evaluateRCommand(java.lang.String rCommand,
                                                int maxMillisToWaitForResponse)
Evaluate an R command, or series of commands. Time out if we wait longer than maxMillis... Return a every single thing that R gives back, with whitespace trimmed from start and end. In order to make sure we wait the appropriate amount of time, and to make sure that we return only R's response and nothing else, I do the following: 1. Place a sentinel that will be echoed before the command response 2. Place a sentinel (that can be evaluated by R) after a newline, so that R will echo it after the command completes. That way we know to ignore everything after the second sentinel, and we know when we're done 3. When we get back the response, take everything between the two sentinel responses. Then, if that response contains any "Package loaded" lines, take them out, too If R fails for any reason, throw a RuntimeException

Parameters:
rCommand -
maxMillisToWaitForResponse -
Returns:
the result from R

tellRToQuit

protected static void tellRToQuit(java.io.DataOutputStream rOut)
                           throws java.io.IOException
Throws:
java.io.IOException

evaluateRExpression

public static java.lang.String evaluateRExpression(java.lang.String rExpression,
                                                   java.util.Map<java.lang.String,double[]> variableValues,
                                                   java.lang.String[] dependentPackageNames)
cover method with default wait time, vector variables only

Parameters:
rExpression -
variableValues -
dependentPackageNames -
Returns:

evaluateRExpression

public static java.lang.String evaluateRExpression(java.lang.String rExpression,
                                                   java.util.Map<java.lang.String,double[]> variableValues,
                                                   java.lang.String[] dependentPackageNames,
                                                   int maxMillisToWaitForResponse)
Generic method for running an R expression and feeding the output to a file. Populates variable values and loads packages if necessary

Parameters:
rExpression -
Returns:

evaluateRExpression

public static java.lang.String evaluateRExpression(java.lang.String rExpression,
                                                   java.util.Map<java.lang.String,double[]> vectorVariableValues,
                                                   java.util.Map<java.lang.String,double[][]> matrixVariableValues,
                                                   java.lang.String[] dependentPackageNames)
Cover method with default wait time

Parameters:
rExpression -
vectorVariableValues -
matrixVariableValues -
dependentPackageNames -
Returns:

evaluateRExpression

public static java.lang.String evaluateRExpression(java.lang.String rExpression,
                                                   java.util.Map<java.lang.String,double[]> vectorVariableValues,
                                                   java.util.Map<java.lang.String,double[][]> matrixVariableValues,
                                                   java.lang.String[] dependentPackageNames,
                                                   int maxMillisToWaitForResponse)

generateRFriendlyPath

public static java.lang.String generateRFriendlyPath(java.io.File file)
R doesn't like backslashes, even on Windows. So replace them with forward slashes

Parameters:
file -
Returns:

generateRFriendlyPath

public static java.lang.String generateRFriendlyPath(java.lang.String filePath)

evaluateRExpression

public static java.lang.String evaluateRExpression(java.lang.String rExpression,
                                                   java.util.Map<java.lang.String,java.lang.Object> scalarVariableValues,
                                                   java.util.Map<java.lang.String,double[]> vectorVariableValues,
                                                   java.util.Map<java.lang.String,double[][]> matrixVariableValues,
                                                   java.lang.String[] dependentPackageNames,
                                                   int maxMillisToWaitForResponse)
Generic method for running an R expression and feeding the output to a file. Populates variable values and loads packages if necessary

Parameters:
rExpression -
Returns:


Fred Hutchinson Cancer Research Center