use of edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterManager in project clusterMaker2 by RBVI.
the class BiMine 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 = "BiMine_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
RunBiMine algorithm = new RunBiMine(network, attributeArray, monitor, context);
String resultsString = "BiMine results:";
// Cluster the nodes
monitor.setStatusMessage("Clustering nodes");
Integer[] rowOrder = algorithm.cluster(false);
CyMatrix biclusterMatrix = algorithm.getBiclusterMatrix();
int[] clusters = new int[biclusterMatrix.nRows()];
createGroups(network, biclusterMatrix, 1, clusters, "bimine");
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.ClusterManager in project clusterMaker2 by RBVI.
the class FeatureVectorCluster method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing " + getName());
List<String> nodeAttributeList = context.nodeAttributeList.getSelectedValues();
// Sanity check all of our settings
if (nodeAttributeList == null || nodeAttributeList.size() == 0) {
if (monitor != null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
}
return;
}
String[] attributeArray = new String[nodeAttributeList.size()];
int index = 0;
for (String attr : nodeAttributeList) {
attributeArray[index++] = "node." + attr;
}
// To make debugging easier, sort the attribute array
Arrays.sort(attributeArray);
if (monitor != null) {
monitor.setProgress(0.0);
monitor.setStatusMessage("Initializaing");
}
// Create the matrix
// Matrix matrix = new Matrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly);
CyMatrix matrix = CyMatrixFactory.makeSmallMatrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly, false);
if (monitor != null) {
monitor.setProgress(0.1);
monitor.setStatusMessage("Calculating edge distances");
if (canceled)
return;
}
// Handle special cases
if (context.zeroMissing)
matrix.setMissingToZero();
int nNodes = matrix.nRows();
// For each node, get the distance to all other nodes
double maxdistance = Double.MIN_VALUE;
double mindistance = Double.MAX_VALUE;
double[][] distanceMatrix = new double[nNodes][nNodes];
for (int i = 0; i < nNodes; i++) {
for (int j = i + 1; j < nNodes; j++) {
double distance = context.metric.getSelectedValue().getMetric(matrix, matrix, i, j);
maxdistance = Math.max(maxdistance, distance);
mindistance = Math.min(mindistance, distance);
distanceMatrix[i][j] = distance;
}
if (canceled)
return;
monitor.setProgress((double) i / ((double) nNodes * 4));
}
monitor.setStatusMessage("Min distance = " + mindistance + ", max distance = " + maxdistance);
monitor.setStatusMessage("Assigning values to edges");
List<CyEdge> edgeList = new ArrayList<CyEdge>();
double scale = maxdistance - mindistance;
CyNetwork newNet = null;
if (context.createNewNetwork) {
newNet = ModelUtils.createChildNetwork(clusterManager, network, network.getNodeList(), null, "--clustered");
}
for (int i = 0; i < nNodes; i++) {
for (int j = i + 1; j < nNodes; j++) {
// time = System.currentTimeMillis();
// This scales the distances to be between 0.0 and 1.0
double distance = (distanceMatrix[i][j] - mindistance) / scale;
if (context.createNewNetwork == true && distance > context.edgeCutoff && context.edgeCutoff != 0.0)
continue;
CyNode source = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(i), CyNode.class);
CyNode target = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(j), CyNode.class);
// time = System.currentTimeMillis();
if (context.createNewNetwork == true) {
CyEdge edge = newNet.addEdge(source, target, false);
ModelUtils.createAndSet(newNet, edge, context.edgeAttribute, distance, Double.class, null);
edgeList.add(edge);
} else {
List<CyEdge> connectingEdges = network.getConnectingEdgeList(source, target, CyEdge.Type.ANY);
// time = System.currentTimeMillis();
if (connectingEdges == null || connectingEdges.size() == 0)
continue;
CyEdge edge = connectingEdges.get(0);
ModelUtils.createAndSet(network, edge, context.edgeAttribute, distance, Double.class, null);
}
}
if (canceled)
return;
monitor.setProgress(25.0 + (75.0 * (double) i / (double) nNodes) / 100.0);
}
System.out.println("Network created -- creating view");
// If we're supposed to, create the new network
if (context.createNewNetwork) {
VisualStyle style = ViewUtils.getCurrentVisualStyle(clusterManager);
CyNetworkView view = ViewUtils.createView(clusterManager, newNet, false);
ViewUtils.doLayout(clusterManager, view, monitor, "force-directed");
ViewUtils.setVisualStyle(clusterManager, view, style);
ViewUtils.registerView(clusterManager, view);
}
/*
System.out.println("Created "+newEdges+" edges");
System.out.println("Edge creation time: "+edgeCreateTime+"ms");
System.out.println("Network add time: "+networkAddTime+"ms");
System.out.println("Edge fetch time: "+edgeFetchTime+"ms");
System.out.println("Node fetch time: "+nodeFetchTime+"ms");
System.out.println("Set attribute time: "+setAttributeTime+"ms");
*/
monitor.setStatusMessage("Complete");
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterManager in project clusterMaker2 by RBVI.
the class MCODECluster method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing " + getName());
updateSettings();
if (network == null)
network = clusterManager.getNetwork();
context.setNetwork(network);
NodeCluster.init();
if (currentParamsCopy.getScope().equals(MCODEParameterSet.SELECTION)) {
List<CyNode> selectedNodes = CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true);
currentParamsCopy.setSelectedNodes(selectedNodes);
}
MCODECurrentParameters.getInstance().setParams(currentParamsCopy, "MCODE Result", ModelUtils.getNetworkName(network));
runMCODE = new RunMCODE(RESCORE, 1, network, monitor);
List<NodeCluster> clusters = runMCODE.run(monitor);
if (canceled) {
monitor.showMessage(TaskMonitor.Level.INFO, "Canceled by user");
return;
}
monitor.showMessage(TaskMonitor.Level.INFO, "Found " + clusters.size() + " clusters");
if (clusters == null || clusters.size() == 0) {
monitor.showMessage(TaskMonitor.Level.WARN, "Didn't find any clusters!");
return;
}
// Now, sort our list of clusters by score
clusters = NodeCluster.rankListByScore(clusters);
List<Double> scoreList = NodeCluster.getScoreList(clusters);
clusterAttributeName = context.getClusterAttribute();
createGroups = context.advancedAttributes.createGroups;
monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
// Remove any leftover groups from previous runs
removeGroups(network, GROUP_ATTRIBUTE);
monitor.setStatusMessage("Creating groups");
List<List<CyNode>> nodeClusters = createGroups(network, clusters, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, clusters);
monitor.setStatusMessage("Done. MCODE results:\n" + results);
if (context.vizProperties.showUI) {
monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !currentParamsCopy.getScope().equals(MCODEParameterSet.SELECTION)));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterManager in project clusterMaker2 by RBVI.
the class GLayCluster method run.
public void run(TaskMonitor monitor) {
this.monitor = monitor;
monitor.setTitle("Performing community clustering (GLay)");
createGroups = context.advancedAttributes.createGroups;
clusterAttributeName = context.getClusterAttribute();
if (network == null)
network = clusterManager.getNetwork();
// Make sure to update the context
context.setNetwork(network);
NodeCluster.init();
GSimpleGraphData simpleGraph = new GSimpleGraphData(network, context.selectedOnly, context.undirectedEdges);
fa = new FastGreedyAlgorithm();
// fa.partition(simpleGraph);
fa.execute(simpleGraph, monitor);
NumberFormat nf = NumberFormat.getInstance();
String modularityString = nf.format(fa.getModularity());
List<NodeCluster> clusterList = new ArrayList<NodeCluster>();
for (int cluster = 0; cluster < fa.getClusterNumber(); cluster++) {
clusterList.add(new NodeCluster());
}
int[] membership = fa.getMembership();
for (int index = 0; index < simpleGraph.graphIndices.length; index++) {
int cluster = membership[index];
clusterList.get(cluster).add(simpleGraph.graphIndices[index]);
}
monitor.showMessage(TaskMonitor.Level.INFO, "Found " + clusterList.size() + " clusters");
// Remove any leftover groups from previous runs
removeGroups(network, GROUP_ATTRIBUTE);
monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
List<List<CyNode>> nodeClusters = createGroups(network, clusterList, GROUP_ATTRIBUTE);
results = new AbstractClusterResults(network, clusterList);
monitor.showMessage(TaskMonitor.Level.INFO, "Done. Community Clustering results:\n" + results);
if (context.vizProperties.showUI) {
monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.selectedOnly));
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterManager in project clusterMaker2 by RBVI.
the class ConnectedComponentsCluster method run.
public void run(TaskMonitor monitor) {
monitor.setTitle("Performing ConnectedComponents 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;
Map<Integer, List<CyNode>> components = MatrixUtils.findConnectedComponents(matrix);
// Create the NodeClusters
Map<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
for (Integer cluster : components.keySet()) {
clusterMap.put(cluster, new NodeCluster(components.get(cluster)));
}
// Now get the sorted cluster map
int clusterNumber = 1;
HashMap<NodeCluster, NodeCluster> cMap = new HashMap<NodeCluster, NodeCluster>();
for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
if (cMap.containsKey(cluster))
continue;
cMap.put(cluster, cluster);
cluster.setClusterNumber(clusterNumber);
clusterNumber++;
}
List<NodeCluster> clusters = new ArrayList<NodeCluster>(cMap.keySet());
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.showMessage(TaskMonitor.Level.INFO, "ConnectedComponent 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));
}
}
Aggregations