-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathComputePCA.java
More file actions
56 lines (48 loc) · 1.51 KB
/
ComputePCA.java
File metadata and controls
56 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
*
*/
package edu.iastate.metnet.metaomgraph;
import org.renjin.primitives.matrix.Matrix;
import org.renjin.script.*;
import org.renjin.sexp.Vector;
import edu.iastate.metnet.metaomgraph.utils.RenjinUtils;
import javax.script.*;
/**
* @author sumanth
*
* Class to compute the PCA
* uses the R packages to compute the PCA
*/
public class ComputePCA {
private double[][] data;
/**
* Constructor
* @param data 2d array with m rows(selected samples) and n columns(selected feature/gene list)
*/
public ComputePCA(double[][] data) {
this.data = data;
}
/**
* Calculates pca vectors of the given matrix by reducing it into the number of dimensions.
* @param numOfDims the number of components on which to project the features
* @param normalize whether to normalize (set features to have zero mean)
* @return the reduced parameters of the data
*/
public double[][] projectData(int numOfDims, boolean normalize){
ScriptEngine engine = RenjinUtils.getScriptEngine();
String dataRMatrix = RenjinUtils.fillRMatrixFrom2DArray("dataRMatrix", data, engine);
String center = "TRUE";
if(!normalize) {
center = "FALSE";
}
Vector pcaRVector = null;
try {
engine.eval("pca <- prcomp(" + dataRMatrix + ", scale. = TRUE, center = " + center + ")");
pcaRVector = (Vector)engine.eval("pca$x");
} catch (ScriptException e) {
e.printStackTrace();
}
double[][] plotData = RenjinUtils.get2DArrayFromRenjinVector(pcaRVector, data.length, numOfDims);
return plotData;
}
}