use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class SCPSCluster method run.
public void run(TaskMonitor monitor) {
monitor.setTitle("Performing SCPS cluster");
this.monitor = monitor;
if (network == null)
network = clusterManager.getNetwork();
// Make sure to update the context
context.setNetwork(network);
NodeCluster.init();
CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
if (matrix == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
return;
}
// Update our tunable results
clusterAttributeName = context.getClusterAttribute();
createGroups = context.advancedAttributes.createGroups;
if (canceled)
return;
// Cluster the nodes
runSCPS = new RunSCPS(matrix, context.epsilon, context.clusters, context.iterations, monitor);
monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
List<NodeCluster> clusterList = runSCPS.run(network, monitor);
// Canceled?
if (clusterList == null)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
// Remove any leftover groups from previous runs
removeGroups(network, GROUP_ATTRIBUTE);
monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
params = new ArrayList<String>();
context.edgeAttributeHandler.setParams(params);
List<List<CyNode>> nodeClusters = createGroups(network, clusterList, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, clusterList);
monitor.showMessage(TaskMonitor.Level.INFO, "Done. SCPS results:\n" + results);
if (context.vizProperties.showUI) {
monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.edgeAttributeHandler.selectedOnly));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class RunTransClust method run.
public List<NodeCluster> run(TaskMonitor monitor, CyNetwork network) {
DoubleMatrix2D matrix = this.distanceMatrix.getColtMatrix();
nodes = distanceMatrix.getRowNodes();
HashMap<String, CyNode> nodeHash = new HashMap<String, CyNode>();
for (CyNode node : nodes) {
nodeHash.put(ModelUtils.getNodeName(network, node), node);
}
HashMap<String, Integer> integers2proteins = new HashMap<String, Integer>();
HashMap<Integer, String> proteins2integers = new HashMap<Integer, String>();
int count = 0;
for (CyNode node : this.nodes) {
integers2proteins.put(ModelUtils.getNodeName(network, node), count);
proteins2integers.put(count, ModelUtils.getNodeName(network, node));
count++;
}
Edges es = new Edges(this.nodes.size() * this.nodes.size(), this.nodes.size());
count = 0;
for (int i = 0; i < this.nodes.size(); i++) {
CyNode cyNodeI = this.nodes.get(i);
es.startPositions[integers2proteins.get(cyNodeI.getSUID())] = count;
for (int j = 0; j < this.nodes.size(); j++) {
CyNode cyNodeJ = this.nodes.get(j);
es.sources[count] = i;
es.targets[count] = j;
Double val = distanceMatrix.getValue(i, j);
if (val != null) {
es.values[count] = val.floatValue();
count++;
}
}
es.endPositions[integers2proteins.get(cyNodeI.getSUID())] = count - 1;
}
Semaphore s = new Semaphore(1);
TaskConfig.mode = TaskConfig.COMPARISON_MODE;
TaskConfig.monitor = monitor;
IteratorThread it = new IteratorThread(es, integers2proteins, proteins2integers, s);
TaskConfig.minThreshold = threshold;
TaskConfig.maxThreshold = threshold;
try {
s.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
it.start();
monitor.showMessage(TaskMonitor.Level.INFO, "Executing TransClust Clustering...");
try {
s.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
String result = it.resultsStringBuffer.toString();
String[] clusters = result.split("\t")[2].split(";");
Map<Integer, NodeCluster> clusterMap = getClusterMap(clusters, nodeHash);
// Update node attributes in network to include clusters. Create cygroups from clustered nodes
monitor.showMessage(TaskMonitor.Level.INFO, "Created " + clusterMap.size() + " clusters");
if (clusterCount == 0) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Created 0 clusters!!!!");
return null;
}
int clusterNumber = 1;
Map<NodeCluster, NodeCluster> cMap = new HashMap();
for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
if (cMap.containsKey(cluster))
continue;
cMap.put(cluster, cluster);
cluster.setClusterNumber(clusterNumber);
clusterNumber++;
}
Set<NodeCluster> clusters2 = cMap.keySet();
return new ArrayList<NodeCluster>(clusters2);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class TransClustCluster method run.
/**
* Perform the actual clustering. For TransClust, there are really
* two steps:
* 1) Assign all of the connected components
* 2) Do the TransClust clustering.
*
* There is also an optional approach called evolutionary parameter
* tuning, which takes a lot longer and is probably less relevant for
* the Cytoscape integration.
*
* @param monitor the TaskMonitor to use
*/
public void run(TaskMonitor monitor) {
monitor.setTitle("Performing Transitivity clustering");
this.monitor = monitor;
if (network == null)
network = clusterManager.getNetwork();
// Make sure to update the context
context.setNetwork(network);
NodeCluster.init();
CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
if (matrix == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
return;
}
updateSettings();
runTransClust = new RunTransClust(matrix, context.edgeAttributeHandler.edgeCutOff.getValue(), monitor);
if (canceled)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
createGroups = context.advancedAttributes.createGroups;
// Cluster the nodes
List<NodeCluster> clusters = runTransClust.run(monitor, network);
// Canceled?
if (clusters == null)
return;
monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
// Remove any leftover groups from previous runs
removeGroups(network, GROUP_ATTRIBUTE);
monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
params = new ArrayList<String>();
context.edgeAttributeHandler.setParams(params);
List<List<CyNode>> nodeClusters = createGroups(network, clusters, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, clusters);
monitor.setStatusMessage("Done. TransClust results:\n" + results);
if (context.vizProperties.showUI) {
monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.edgeAttributeHandler.selectedOnly));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class RunSCPS method getSMat.
// Get Connected Components, cluster all components <= |5|, and connect the remaining components with random lowscoring edges
public DoubleMatrix2D getSMat(CyMatrix distanceMatrix) {
// Matrix prior to filtration modification
DoubleMatrix2D unfiltered_mat = distanceMatrix.getColtMatrix();
// Size of newly created Umat after filtering of small components
int sMat_rows = 0;
HashMap<Integer, List<CyNode>> filtered_cmap = new HashMap<Integer, List<CyNode>>();
// Connected Componets
Map<Integer, List<CyNode>> cMap = MatrixUtils.findConnectedComponents(distanceMatrix);
IntArrayList rowList = new IntArrayList();
IntArrayList columnList = new IntArrayList();
DoubleArrayList valueList = new DoubleArrayList();
// Iterate through connected components
int component_size_sum = 0;
for (List<CyNode> component : cMap.values()) {
numComponents += 1;
// Size <= 5. Automatically create cluster and increment clusterCount.
if (component.size() <= 5) {
NodeCluster iCluster = new NodeCluster(component);
iCluster.setClusterNumber(this.clusterCount);
// iCluster.add(component,this.clusterCount);
this.clusterMap.put(new Integer(clusterCount), iCluster);
this.clusterCount++;
} else {
// iterate through components and assign them index mappings in new uMatrix
component_size_sum += component.size();
System.out.println("Normal Component size " + component.size() + " Total Sum " + component_size_sum);
for (int i = 0; i < component.size(); i++) {
CyNode n = component.get(i);
int node_id = this.nodes.indexOf(n);
// set mapping of new matrix index to old index
setMap(node_id, sMat_rows);
sMat_rows++;
}
}
}
DoubleMatrix2D sMat = DoubleFactory2D.sparse.make(sMat_rows, sMat_rows);
// set diagnols of sMat to one
for (int i = 0; i < sMat_rows; i++) sMat.set(i, i, 1);
// iterate through nonzero edges. If both nodes in new index map, transfer the edge to new matrix
unfiltered_mat.getNonZeros(rowList, columnList, valueList);
for (int i = 0; i < rowList.size(); i++) {
int row_id = rowList.get(i);
int column_id = columnList.get(i);
int new_row_id = getMap_new(row_id);
int new_column_id = getMap_new(column_id);
double value = valueList.get(i);
// Set symmetrically the values in new matrix
if (new_row_id > -1 && new_column_id > -1) {
sMat.set(new_row_id, new_column_id, value);
sMat.set(new_column_id, new_row_id, value);
}
}
return sMat;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.
the class OjAlgoMatrix method like.
public Matrix like(int rows, int columns, double[][] initial) {
Matrix result = new OjAlgoMatrix();
result.initialize(rows, columns, initial);
return result;
}
Aggregations