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