Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 10 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>bedatadriven</id>
<name>bedatadriven public repo</name>
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -185,45 +190,6 @@
<artifactId>flatlaf-intellij-themes</artifactId>
<version>0.42</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>1.0.0-beta7</version>
<exclusions>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>mkl-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.nd4j</groupId>
<artifactId>jackson</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
</exclusion>
<exclusion>
<groupId>net.ericaro</groupId>
<artifactId>neoitertools</artifactId>
</exclusion>
<exclusion>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
</exclusion>
<exclusion>
<groupId>org.nd4j</groupId>
<artifactId>protobuf</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.jakewharton.byteunits</groupId>
<artifactId>byteunits</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.lejon.T-SNE-Java</groupId>
<artifactId>tsne</artifactId>
Expand All @@ -234,6 +200,11 @@
<artifactId>hierarchical-clustering</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

</project>
67 changes: 28 additions & 39 deletions src/edu/iastate/metnet/metaomgraph/ComputePCA.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,54 @@
*/
package edu.iastate.metnet.metaomgraph;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dimensionalityreduction.PCA;
import org.nd4j.linalg.factory.Nd4j;
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 library nd4j to compute the PCA
* uses the R packages to compute the PCA
*/
public class ComputePCA {
private INDArray dataArray;
private double[][] data;
/**
* Constructor
* @param data 2d array with m rows(selected samples) and n columns(selected feature/gene list)
*/
public ComputePCA(double[][] data) {
dataArray = Nd4j.createFromArray(data);
this.data = data;
}

/**
* Calculates pca reduced value of a matrix, for a given variance. A larger variance (99%)
* will result in a higher order feature set.
* The returned matrix is a projection of A onto principal components
*
* If PCA is computed directly, the library is somehow modifying the original data, hence
* compute the pca factors first and multiply the factors with the data to get the
* reduced dimensions.
*
* @param variance the amount of variance to preserve as a float 0 - 1
* @param normalize whether to normalize (set features to have zero mean)
* @return the matrix representing a reduced feature set
*/
public double[][] projectData(double variance, boolean normalize){
INDArray tempArr = dataArray.dup();
INDArray pcaFactors = PCA.pca_factor(dataArray, variance, normalize);
INDArray reducedDimensions = tempArr.mmul(pcaFactors);
double[][] plotData = reducedDimensions.toDoubleMatrix();
return plotData;
}

/**
* Calculates pca vectors of a matrix, for a flags number of reduced features
* returns the reduced feature set
* The return is a projection of A onto principal nDims components
*
* If PCA is computed directly, the library is somehow modifying the original data, hence
* compute the pca factors first and multiply the factors with the data to get the
* reduced dimensions.
*
* 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){
INDArray tempArr = dataArray.dup();
INDArray pcaFactors = PCA.pca_factor(dataArray, numOfDims, normalize);
INDArray reducedDimensions = tempArr.mmul(pcaFactors);
double[][] plotData = reducedDimensions.toDoubleMatrix();
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;
}
}
8 changes: 4 additions & 4 deletions src/edu/iastate/metnet/metaomgraph/ui/ThirdPartyLibs.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public ThirdPartyLibs() {
addThirdPartyLibrary(name, websiteUrl, licenseUrl);
rangePanel.add(new JSeparator(SwingConstants.HORIZONTAL));

// nd4j
name = "nd4j";
websiteUrl = "https://github.com/deeplearning4j/nd4j";
licenseUrl = "https://github.com/deeplearning4j/nd4j/blob/master/LICENSE";
// Renjin
name = "Renjin";
websiteUrl = "https://www.renjin.org/";
licenseUrl = "https://github.com/bedatadriven/renjin/blob/master/LICENSE.txt";
addThirdPartyLibrary(name, websiteUrl, licenseUrl);
rangePanel.add(new JSeparator(SwingConstants.HORIZONTAL));

Expand Down
77 changes: 77 additions & 0 deletions src/edu/iastate/metnet/metaomgraph/utils/RenjinUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
*
*/
package edu.iastate.metnet.metaomgraph.utils;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.renjin.primitives.matrix.Matrix;
import org.renjin.script.RenjinScriptEngineFactory;
import org.renjin.sexp.Vector;

/**
* @author sumanth
* This class contains all the utilities related to Renjin (https://www.renjin.org/)
*/
public class RenjinUtils {

/**
* Initiate and return the script engine for further usage.
* @return ScriptEngine
*/
public static ScriptEngine getScriptEngine() {
// create a script engine manager:
RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
// create a Renjin engine:
ScriptEngine engine = factory.getScriptEngine();

return engine;
}

/**
* Fill the R's matrix using 2d array.
* this method uses and returns the same matrixVariableName passed to it
* @param matrixVariableName name of the matrix variable
* @param data 2D array data
* @param engine renjins scriptengine
* @return matrixVariableName the same variable name passed to the method
*/
public static String fillRMatrixFrom2DArray(final String matrixVariableName,
final double[][] data, final ScriptEngine engine) {
engine.put(matrixVariableName, data[0]);
try {
engine.eval(matrixVariableName + " <- matrix(" + matrixVariableName + ", nr=1)");
for(int i = 1; i < data.length; i++) {
engine.put("temp", data[i]);
engine.eval(matrixVariableName + " <- rbind(" + matrixVariableName + ", matrix(temp, nr=1))");
}
} catch (ScriptException e) {
e.printStackTrace();
}

return matrixVariableName;
}

/**
* Return the data in Vector form to 2D array.
*
* Note: This method is exception prone, call this carefully only if you know that the vector is of matrix type,
* and by giving the proper number of rows and columns you require in the output data.
*
* @param vector the vector datatype returned by r's script
* @param numOfRows num of rows in the return data type, should be less than or equal to the number of rows in vector
* @param numOfCols num of columns in the return data type, should be less than or equal to the number of columns in vector
* @return data 2D array
*/
public static double[][] get2DArrayFromRenjinVector(Vector vector, int numOfRows, int numOfCols){
double[][] data = new double[numOfRows][numOfCols];
Matrix rMatrix = new Matrix(vector);
for(int i = 0; i < numOfRows; i++) {
for(int j = 0; j < numOfCols; j++) {
data[i][j] = rMatrix.getElementAsDouble(i, j);
}
}
return data;
}
}