use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class RunPCA method calculateLoadingMatrix.
private Matrix calculateLoadingMatrix(CyMatrix matrix) {
int rows = eigenVectors.length;
int columns = eigenVectors[0].length;
Matrix loading = CyMatrixFactory.makeSmallMatrix(matrix.getNetwork(), rows, columns);
// loading.initialize(rows, columns, new double[rows][columns]);
IntStream.range(0, rows).parallel().forEach(row -> {
for (int column = columns - 1, newCol = 0; column >= 0; column--, newCol++) {
loading.setValue(row, newCol, eigenVectors[row][column] * Math.sqrt(Math.abs(eigenValues[column])));
}
});
loading.setRowLabels(Arrays.asList(matrix.getColumnLabels()));
for (int column = 0; column < columns; column++) {
loading.setColumnLabel(column, "PC " + (column + 1));
}
return loading;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class PCoA method run.
public void run(TaskMonitor monitor) {
monitor.setTitle("Running Principal Coordinate Analysis");
this.monitor = monitor;
long start = System.currentTimeMillis();
if (network == null)
network = clusterManager.getNetwork();
context.setNetwork(network);
CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
if (matrix == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
return;
}
if (canceled)
return;
PCoAContext.NegEigenHandling neg = context.neg.getSelectedValue();
// Cluster the nodes
runpcoa = new RunPCoA(manager, matrix, network, networkView, context, neg.getValue(), monitor);
runpcoa.run();
if (canceled)
return;
long duration = System.currentTimeMillis() - start;
monitor.showMessage(TaskMonitor.Level.INFO, "PCoA completed in " + duration + "ms");
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class ResultPanelPCA method valueChanged.
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
// Get the rows
int[] rowIndices = table.getSelectedRows();
Map<CyNode, CyNode> selectedMap = new HashMap<CyNode, CyNode>();
double threshold = 0.02;
// Get the clusters
for (int index = 0; index < rowIndices.length; index++) {
int row = rowIndices[index];
CyMatrix matrix = components[row];
for (int i = 0; i < matrix.nRows(); i++) {
for (int j = 0; j < matrix.nColumns(); j++) {
if (matrix.getValue(i, j) > threshold) {
CyNode node = matrix.getRowNode(i);
selectedMap.put(node, node);
break;
}
}
}
// System.out.println("PC is selected");
}
// Select the nodes
for (CyNode node : network.getNodeList()) {
if (selectedMap.containsKey(node))
network.getRow(node).set(CyNetwork.SELECTED, true);
else
network.getRow(node).set(CyNetwork.SELECTED, false);
}
// I wish we didn't need to do this, but if we don't, the selection
// doesn't update
networkView.updateView();
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class BicFinder method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing " + getName());
List<String> nodeAttributeList = context.attributeList.getNodeAttributeList();
String edgeAttribute = context.attributeList.getEdgeAttribute();
clusterAttributeName = "BicFinder_Bicluster";
if (network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().getColumn(ClusterManager.CLUSTER_ATTRIBUTE) == null) {
network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().createColumn(ClusterManager.CLUSTER_ATTRIBUTE, String.class, false);
}
network.getRow(network, CyNetwork.LOCAL_ATTRS).set(ClusterManager.CLUSTER_ATTRIBUTE, clusterAttributeName);
if (nodeAttributeList == null && edgeAttribute == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must select either one edge column or two or more node columns");
return;
}
if (nodeAttributeList != null && nodeAttributeList.size() > 0 && edgeAttribute != null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't have both node and edge columns selected");
return;
}
if (context.selectedOnly && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
return;
}
createGroups = context.createGroups;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
// To make debugging easier, sort the attribute list
Collections.sort(nodeAttributeList);
}
// Get our attributes we're going to use for the cluster
String[] attributeArray;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
attributeArray = new String[nodeAttributeList.size()];
int i = 0;
for (String attr : nodeAttributeList) {
attributeArray[i++] = "node." + attr;
}
} else {
attributeArray = new String[1];
attributeArray[0] = "edge." + edgeAttribute;
}
monitor.setStatusMessage("Initializing");
resetAttributes(network, SHORTNAME);
// Create a new clusterer
RunBicFinder algorithm = new RunBicFinder(network, attributeArray, monitor, context);
String resultsString = "BicFinder results:";
// Cluster the nodes
monitor.setStatusMessage("Clustering nodes");
Integer[] rowOrder = algorithm.cluster(false);
CyMatrix biclusterMatrix = algorithm.getBiclusterMatrix();
int[] clusters = new int[biclusterMatrix.nRows()];
createBiclusterGroups(algorithm.getClusterNodes());
// createGroups(network,biclusterMatrix,1, clusters, "bicfinder");
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, getAttributeList(), algorithm.getBiclusterMatrix());
createBiclusterTable(algorithm.getClusterNodes(), algorithm.getClusterAttrs());
// System.out.println(resultsString);
if (context.showUI) {
insertTasksAfterCurrentTask(new BiclusterView(clusterManager));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class RunBicFinder method getASR.
private double getASR(List<Integer> genes, List<Integer> conditions) {
CyMatrix data = CyMatrixFactory.makeSmallMatrix(network, genes.size(), conditions.size());
// Matrix data_t = new Matrix(network,conditions.size(),genes.size());
for (int i = 0; i < genes.size(); i++) {
for (int j = 0; j < conditions.size(); j++) {
data.setValue(i, j, matrix.getValue(genes.get(i), conditions.get(j)));
// data_t.setValue(j, i, matrix.getValue(genes.get(i), conditions.get(j)));
}
}
double asr = 0.0;
double asr_g = 0.0;
double asr_c = 0.0;
for (int i = 0; i < genes.size(); i++) {
for (int j = i + 1; j < genes.size(); j++) {
asr_g += getSpearmansRho(data, i, j);
}
}
asr_g /= genes.size() * (genes.size() - 1);
/*
for(int i = 0; i < genes.size(); i++){
for(int j = i+1; j < genes.size(); j++){
asr_c += conditionRho[conditions.get(i)][conditions.get(j)];
}
}
asr_c /= conditions.size()*(conditions.size()-1);
asr = 2*Math.max(asr_g, asr_c);
*/
asr = 2 * asr_g;
return asr;
}
Aggregations