1 /*
2 * The baseCode project
3 *
4 * Copyright (c) 2008-2019 University of British Columbia
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 package ubic.basecode.util.r;
20
21 import org.apache.commons.collections4.Transformer;
22 import org.rosuda.REngine.REXP;
23 import ubic.basecode.dataStructure.matrix.DoubleMatrix;
24 import ubic.basecode.dataStructure.matrix.ObjectMatrix;
25 import ubic.basecode.math.linearmodels.LinearModelSummary;
26 import ubic.basecode.math.linearmodels.OneWayAnovaResult;
27 import ubic.basecode.math.linearmodels.TwoWayAnovaResult;
28
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * Abstraction of a connection to R
34 *
35 * @author Paul
36 *
37 */
38 public interface RClient {
39
40 /**
41 * @param argName
42 * @param arg
43 */
44 public void assign( String argName, double[] arg );
45
46 /*
47 * (non-Javadoc)
48 *
49 * @see org.rosuda.JRclient.Rconnection#assign(java.lang.String, int[])
50 */
51 public void assign( String arg0, int[] arg1 );
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see org.rosuda.JRclient.Rconnection#assign(java.lang.String, java.lang.String)
57 */
58 public void assign( String sym, String ct );
59
60 public void assign( String argName, String[] array );
61
62 /**
63 * @param strings
64 * @return the name of the factor generated.
65 */
66 public String assignFactor( List<String> strings );
67
68 /**
69 * @param factorName
70 * @param list
71 * @return the factor name
72 */
73 public String assignFactor( String factorName, List<String> list );
74
75 /**
76 * Assign a 2-d matrix.
77 *
78 * @param matrix
79 * @return the name of the variable by which the R matrix can be referred.
80 */
81 public String assignMatrix( double[][] matrix );
82
83 /**
84 * Assign a 2-d matrix.
85 *
86 * @param matrix
87 * @return the name of the variable by which the R matrix can be referred.
88 */
89 public String assignMatrix( DoubleMatrix<?, ?> matrix );
90
91 /**
92 * Assign a 2-d matrix.
93 *
94 * @param matrix
95 * @param rowNameExtractor
96 * @return the name of the variable by which the R matrix can be referred.
97 */
98 public String assignMatrix( DoubleMatrix<?, ?> matrix, Transformer rowNameExtractor );
99
100 /**
101 * Define a variable corresponding to a character array in the R context, given a List of Strings.
102 *
103 * @param objects, which will be stringified if they are not strings.
104 * @return the name of the variable in the R context.
105 */
106 public String assignStringList( List<?> objects );
107
108 /**
109 * Run a command that takes a double array as an argument and returns a boolean.
110 *
111 * @param command
112 * @param argName
113 * @param arg
114 * @return
115 */
116 public boolean booleanDoubleArrayEval( String command, String argName, double[] arg );
117
118 /**
119 * Convert an object matrix into an R data frame. Columns that look numeric are treated as numbers. Booleans and
120 * Strings are treated as factors.
121 *
122 * @param matrix
123 * @return variable name in R-land.
124 */
125 public String dataFrame( ObjectMatrix<String, String, Object> matrix );
126
127 /**
128 * Evaluate a command that returns a dataFrame
129 *
130 * @param command
131 * @return an ObjectMatrix representation of the data frame.
132 */
133 public ObjectMatrix<String, String, Object> dataFrameEval( String command );
134
135 /**
136 * Run a command that has a single double array parameter, and returns a double array.
137 *
138 * @param command
139 * @param argName
140 * @param arg
141 * @return
142 */
143 public double[] doubleArrayDoubleArrayEval( String command, String argName, double[] arg );
144
145 /**
146 * Run a command that returns a double array with no arguments.
147 *
148 * @param command
149 * @return
150 */
151 public double[] doubleArrayEval( String command );
152
153 /**
154 * Run a command that takes two double array arguments and returns a double array.
155 *
156 * @param command
157 * @param argName
158 * @param arg
159 * @param argName2
160 * @param arg2
161 * @return
162 */
163 public double[] doubleArrayTwoDoubleArrayEval( String command, String argName, double[] arg, String argName2,
164 double[] arg2 );
165
166 /**
167 * Run a command that takes two double arrays as arguments and returns a double value.
168 *
169 * @param command
170 * @param argName
171 * @param arg
172 * @param argName2
173 * @param arg2
174 * @return
175 */
176 public double doubleTwoDoubleArrayEval( String command, String argName, double[] arg, String argName2, double[] arg2 );
177
178 /**
179 * Evaluate the given command
180 *
181 * @param command
182 * @return
183 */
184 public abstract REXP eval( String command );
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see org.rosuda.JRclient.Rconnection#getLastError()
190 */
191 public String getLastError();
192
193 public int[] intArrayEval( String command );
194
195 public boolean isConnected();
196
197 /**
198 * Lower level access to linear model. Fairly simple. Factors are assigned in turn.
199 *
200 * @param data
201 * @param factors Map of factorNames to factors (which can be expressed as Strings or Doubles). If you care about
202 * the order the factors are introduced into the model, use a LinkedHashMap.
203 */
204 public LinearModelSummary linearModel( double[] data, Map<String, List<?>> factors );
205
206 /**
207 * @param data
208 * @param design which will be converted to factors or continuous covariates depending on whether the columns are
209 * booleans, strings or numerical. Names of factors are the column names of the design matrix, and the rows
210 * are assumed to be in the same order as the data.
211 * @return
212 */
213 public LinearModelSummary linearModel( double[] data, ObjectMatrix<String, String, Object> design );
214
215 /**
216 * @param listEntryType a hint about what type of object you want the list to contain. If you set this to be null,
217 * the method will try to guess, but caution is advised.
218 * @param command R command
219 * @return
220 */
221 public List<?> listEval( Class<?> listEntryType, String command );
222
223 public boolean loadLibrary( String libraryName );
224
225 /**
226 * Lower-level access to a simple one-way ANOVA
227 *
228 * @param data
229 * @param factor
230 * @return
231 */
232 public OneWayAnovaResult oneWayAnova( double[] data, List<String> factor );
233
234 public Map<String, OneWayAnovaResult> oneWayAnovaEval( String command );
235
236 /**
237 * Remove a variable from the R namespace
238 *
239 * @param variableName
240 */
241 public void remove( String variableName );
242
243 /**
244 * Get a matrix back out of the R context. Row and Column names are filled in for the resulting object, if they are
245 * present.
246 *
247 * @param variableName
248 * @return
249 */
250 public DoubleMatrix<String, String> retrieveMatrix( String variableName );
251
252 /**
253 * Run lm with anova on all the rows of a matrix
254 *
255 * @param dataMatrixVarName from an assignment of a matrix
256 * @param modelFormula and other options that will be passed as the argument to 'lm(...)', that refers to factor
257 * variables that have already been assigned, using x as the outcome. Example might be x ~ f1 + f2.
258 * @param names of the factors like {"f1", "f2"}.
259 * @return map of row identifiers to populated LinearModelSummaries.
260 */
261 public Map<String, LinearModelSummary> rowApplyLinearModel( String dataMatrixVarName, String modelFormula,
262 String[] factorNames );
263
264 /**
265 * Evaluate any command and return a string
266 *
267 * @param command
268 * @return string
269 */
270 public String stringEval( String command );
271
272 public List<String> stringListEval( String command );
273
274 /**
275 * Lower-level access to two-way ANOVA
276 *
277 * @param data
278 * @param factor1
279 * @param factor2
280 * @param includeInteraction
281 * @return result with interaction term information null if includeInteraction = false
282 */
283 public TwoWayAnovaResult twoWayAnova( double[] data, List<String> factor1, List<String> factor2,
284 boolean includeInteraction );
285
286 /**
287 * Evaluates two way anova commands of the form
288 * <p>
289 * apply(matrix,1,function(x){anova(aov(x~farea+ftreat))}
290 * </p>
291 * and
292 * <p>
293 * apply(matrix,1,function(x){anova(aov(x~farea+ftreat+farea*ftreat))}
294 * </p>
295 * where farea and ftreat have already been transposed and had factor called on them.
296 *
297 * @param command
298 * @return
299 */
300 public Map<String, TwoWayAnovaResult> twoWayAnovaEval( String command, boolean withInteractions );
301
302 public void voidEval( String command );
303
304 }