use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class HopachablePAMTest method testCollapse.
@Test
public void testCollapse() {
Double[] data = { .9, .9, .8, .8, .4, .4, .5, .5, .1, .1, .0, .0 };
int k = 3;
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(6, 2, data);
HopachablePAM pam = new HopachablePAM(null, mat, DistanceMetric.CITYBLOCK);
Clusters c1 = pam.cluster(k);
Clusters c2 = pam.collapse(0, 1, c1);
Clusters c3 = pam.collapse(1, 2, c1);
Clusters c4 = pam.collapse(0, 2, c1);
// check that the size has reduced
--k;
assertEquals(c2.getSizes().length, k);
assertEquals(c3.getSizes().length, k);
assertEquals(c4.getSizes().length, k);
Clusters c5 = pam.collapse(0, 1, c2);
--k;
assertEquals(c5.getSizes().length, k);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class HopachablePAMTest method testSubset.
@Test
public void testSubset() {
Double[] data = { .9, .9, .8, .8, .4, .4, .5, .5, .1, .1, .0, .0 };
int k = 3;
// new order
int[] index = { 0, 1, 5, 2, 4, 3 };
// expected results based on new order
int[] ans = { 0, 0, 1, 2, 1, 2 };
CyMatrix mat = CyMatrixFactory.makeSmallMatrix(6, 2, data);
HopachablePAM pam = new HopachablePAM(null, mat, DistanceMetric.CITYBLOCK);
// permute sample order
Hopachable pamPermuted = pam.subset(index);
Clusters c = pamPermuted.cluster(k);
// the number of clusters should not change because it should always
// return the specified number of clusters
assertEquals(c.getNumberOfClusters(), k);
// check that the clustering results match
for (int i = 0; i < c.size(); ++i) {
assertEquals(c.getClusterIndex(i), ans[i]);
}
// minor test case
// subset the last 4 elements
int[] subsetIndex = { 2, 3, 4, 5 };
int[] subsetAns = { 0, 0, 1, 1 };
int subsetK = 2;
Hopachable pamSubset = pam.subset(subsetIndex);
Clusters c2 = pamSubset.cluster(subsetK);
// check number of clusters
assertEquals(c2.getNumberOfClusters(), subsetK);
// check cluster assignments
for (int i = 0; i < c2.size(); ++i) {
assertEquals(c2.getClusterIndex(i), subsetAns[i]);
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class HierarchicalCluster 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();
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 && nodeAttributeList != null && nodeAttributeList.size() > 1 && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
return;
}
// Get our attributes we're going to use for the cluster
String[] attributeArray;
if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
Collections.sort(nodeAttributeList);
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.showMessage(TaskMonitor.Level.INFO, "Initializing");
// System.out.println("Initializing");
resetAttributes(network, SHORTNAME);
// Create a new clusterer
DistanceMetric metric = context.metric.getSelectedValue();
RunHierarchical algorithm = new RunHierarchical(network, attributeArray, metric, clusterMethod, monitor, context);
// Cluster the attributes, if requested
if (context.clusterAttributes && (attributeArray.length > 1 || context.isAssymetric())) {
monitor.setStatusMessage("Clustering attributes");
// System.out.println("Clustering attributes");
Integer[] rowOrder = algorithm.cluster(true);
attributeTree = algorithm.getAttributeList();
CyMatrix matrix = algorithm.getMatrix();
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, attributeTree, matrix);
attributeOrder = new ArrayList<String>();
for (int i = 0; i < rowOrder.length; i++) {
attributeOrder.add(matrix.getRowLabel(rowOrder[i]));
}
}
monitor.setStatusMessage("Clustering nodes");
// Cluster the nodes
// System.out.println("Clustering nodes");
Integer[] rowOrder = algorithm.cluster(false);
nodeTree = algorithm.getAttributeList();
CyMatrix matrix = algorithm.getMatrix();
updateAttributes(network, SHORTNAME, rowOrder, attributeArray, nodeTree, matrix);
nodeOrder = new ArrayList<CyNode>();
for (int i = 0; i < rowOrder.length; i++) {
nodeOrder.add(matrix.getRowNode(rowOrder[i]));
}
// TODO: Deal with params!
List<String> params = context.getParams(algorithm.getMatrix());
updateParams(network, params);
if (context.showUI) {
insertTasksAfterCurrentTask(new TreeView(clusterManager));
}
monitor.setStatusMessage("Done");
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class RunHierarchical method pclcluster.
/**
* The pclcluster routine performs clustering, using pairwise centroid-linking
* on a given set of gene expression data, using the distrance metric given by metric.
*
* @param matrix the data matrix containing the data and labels
* @param distanceMatrix the distances that will be used to actually do the clustering.
* @param metric the distance metric to be used.
* @return the array of TreeNode's that describe the hierarchical clustering solution, or null if
* it it files for some reason.
*/
private TreeNode[] pclcluster(CyMatrix matrix, double[][] distanceMatrix, DistanceMetric metric) {
int nRows = matrix.nRows();
int nColumns = matrix.nColumns();
int nNodes = nRows - 1;
double[][] mask = new double[matrix.nRows()][matrix.nColumns()];
TreeNode[] nodeList = new TreeNode[nNodes];
// Initialize
CyMatrix newData = matrix.copy();
// System.out.println("New matrix: ");
// newData.printMatrix();
int[] distID = new int[nRows];
for (int row = 0; row < nRows; row++) {
distID[row] = row;
for (int col = 0; col < nColumns; col++) {
if (newData.hasValue(row, col))
mask[row][col] = 1.0;
else
mask[row][col] = 0.0;
}
if (row < nNodes)
nodeList[row] = new TreeNode(Double.MAX_VALUE);
}
int[] pair = new int[2];
for (int inode = 0; inode < nNodes; inode++) {
// find the pair with the shortest distance
pair[IS] = 1;
pair[JS] = 0;
double distance = findClosestPair(nRows - inode, distanceMatrix, pair);
nodeList[inode].setDistance(distance);
int is = pair[IS];
int js = pair[JS];
nodeList[inode].setLeft(distID[js]);
nodeList[inode].setRight(distID[is]);
// make node js the new node
for (int col = 0; col < nColumns; col++) {
double jsValue = newData.doubleValue(js, col);
double isValue = newData.doubleValue(is, col);
double newValue = 0.0;
if (newData.hasValue(js, col))
newValue = jsValue * mask[js][col];
if (newData.hasValue(is, col))
newValue += isValue * mask[is][col];
if (newData.hasValue(js, col) || newData.hasValue(is, col)) {
newData.setValue(js, col, newValue);
}
mask[js][col] += mask[is][col];
if (mask[js][col] != 0) {
newData.setValue(js, col, newValue / mask[js][col]);
}
}
for (int col = 0; col < nColumns; col++) {
mask[is][col] = mask[nNodes - inode][col];
newData.setValue(is, col, newData.getValue(nNodes - inode, col));
}
// Fix the distances
distID[is] = distID[nNodes - inode];
for (int i = 0; i < is; i++) {
distanceMatrix[is][i] = distanceMatrix[nNodes - inode][i];
}
for (int i = is + 1; i < nNodes - inode; i++) {
distanceMatrix[i][is] = distanceMatrix[nNodes - inode][i];
}
distID[js] = -inode - 1;
for (int i = 0; i < js; i++) {
distanceMatrix[js][i] = metric.getMetric(newData, newData, js, i);
}
for (int i = js + 1; i < nNodes - inode; i++) {
distanceMatrix[i][js] = metric.getMetric(newData, newData, js, i);
}
}
return nodeList;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix in project clusterMaker2 by RBVI.
the class RunAutoSOME method getNodeClusters.
private Map<NodeCluster, NodeCluster> getNodeClusters(clusterRun cr, Map<String, Integer> key, CyMatrix matrix, Settings s) {
Map<NodeCluster, NodeCluster> cMap = new HashMap<NodeCluster, NodeCluster>();
attrList = new ArrayList<String>();
attrOrderList = new ArrayList<String>();
nodeOrderList = new ArrayList<String>();
for (int i = 0; i < matrix.nColumns(); i++) attrOrderList.add(matrix.getColumnLabel(i));
for (int i = 0; i < clusterCount; i++) {
if (cr.c[i].ids.isEmpty())
continue;
NodeCluster nc = new NodeCluster();
nc.setClusterNumber(i);
for (int j = 0; j < cr.c[i].ids.size(); j++) {
int dataID = cr.c[i].ids.get(j).intValue();
int nodeDataID = key.get(matrix.getRowLabels()[dataID]).intValue();
CyNode cn = nodes.get(nodeDataID);
nc.add(cn);
attrList.add(ModelUtils.getNodeName(network, cn) + "\t" + i);
nodeOrderList.add(ModelUtils.getNodeName(network, cn));
}
cMap.put(nc, nc);
}
return cMap;
}
Aggregations