Search in sources :

Example 6 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster 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;
}
Also used : HashMap(java.util.HashMap) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) IntArrayList(cern.colt.list.tint.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode) IntArrayList(cern.colt.list.tint.IntArrayList)

Example 7 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster in project clusterMaker2 by RBVI.

the class RunAP method run.

public List<NodeCluster> run(CyNetwork network, TaskMonitor monitor) {
    double numClusters;
    monitor.setProgress(0.01);
    for (int i = 0; i < number_iterations; i++) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Exchanging messages: iteration " + i);
        iterate_message_exchange(monitor, i);
        if (canceled) {
            monitor.showMessage(TaskMonitor.Level.INFO, "canceled");
            return null;
        }
        monitor.setProgress((double) i / (double) number_iterations);
    }
    if (debug) {
        for (int i = 0; i < s_matrix.rows(); i++) {
            monitor.showMessage(TaskMonitor.Level.INFO, "Node " + nodes.get(i) + " has exemplar " + get_exemplar(i));
        }
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
    Map<Integer, NodeCluster> clusterMap = getClusterMap();
    // 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<NodeCluster, NodeCluster>();
    for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
        if (cMap.containsKey(cluster))
            continue;
        if (debug) {
            monitor.showMessage(TaskMonitor.Level.INFO, "Cluster " + clusterNumber);
            String s = "";
            for (CyNode node : cluster) {
                s += node.toString() + "\t";
            }
            monitor.showMessage(TaskMonitor.Level.INFO, s);
        }
        cMap.put(cluster, cluster);
        cluster.setClusterNumber(clusterNumber);
        clusterNumber++;
    }
    Set<NodeCluster> clusters = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters);
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode)

Example 8 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster in project clusterMaker2 by RBVI.

the class RunAP method getClusterMap.

private Map<Integer, NodeCluster> getClusterMap() {
    HashMap<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
    for (int i = 0; i < s_matrix.rows(); i++) {
        int exemplar = get_exemplar(i);
        if (clusterMap.containsKey(exemplar)) {
            if (i == exemplar)
                continue;
            // Already seen exemplar
            NodeCluster exemplarCluster = clusterMap.get(exemplar);
            if (clusterMap.containsKey(i)) {
                // We've already seen i also -- join them
                NodeCluster iCluster = clusterMap.get(i);
                if (iCluster != exemplarCluster) {
                    exemplarCluster.addAll(iCluster);
                    // System.out.println("Combining "+i+"["+iCluster+"] and "+exemplar+" ["+exemplarCluster+"]");
                    clusterCount--;
                    clusterMap.remove(i);
                }
            } else {
                exemplarCluster.add(nodes, i);
            // System.out.println("Adding "+i+" to ["+exemplarCluster+"]");
            }
            // Update Clusters
            updateClusters(exemplarCluster, clusterMap);
        } else {
            NodeCluster iCluster;
            // First time we've seen this "exemplar" -- have we already seen "i"?
            if (clusterMap.containsKey(i)) {
                if (i == exemplar)
                    continue;
                // Yes, just add exemplar to i's cluster
                iCluster = clusterMap.get(i);
                iCluster.add(nodes, exemplar);
            // System.out.println("Adding "+exemplar+" to ["+iCluster+"]");
            } else {
                // No create new cluster from scratch
                iCluster = new NodeCluster();
                iCluster.add(nodes, i);
                if (exemplar != i)
                    iCluster.add(nodes, exemplar);
                // System.out.println("New cluster ["+iCluster+"]");
                clusterCount++;
            }
            updateClusters(iCluster, clusterMap);
        }
    }
    return clusterMap;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap)

Example 9 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster in project clusterMaker2 by RBVI.

the class APCluster method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing AP 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
    runAP = new RunAP(matrix, context.lambda, context.preference, context.rNumber, monitor, debug);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<NodeCluster> clusters = runAP.run(network, monitor);
    // 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);
    setParams(params);
    List<List<CyNode>> nodeClusters = createGroups(network, clusters, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusters);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  AP 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));
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Example 10 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster in project clusterMaker2 by RBVI.

the class MCLCluster method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing MCL cluster");
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    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
    runMCL = new RunMCL(matrix, context.inflation_parameter, context.iterations, context.clusteringThresh, context.maxResidual, context.maxThreads, context.forceDecliningResidual, monitor);
    runMCL.setDebug(false);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    // results = runMCL.run(monitor);
    List<NodeCluster> clusters = runMCL.run(network, monitor);
    // 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.showMessage(TaskMonitor.Level.INFO, "MCL 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));
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Aggregations

NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)39 ArrayList (java.util.ArrayList)25 CyNode (org.cytoscape.model.CyNode)18 HashMap (java.util.HashMap)17 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)15 List (java.util.List)15 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)9 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)7 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)5 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)3 IntArrayList (cern.colt.list.tint.IntArrayList)3 PREdge (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.ranking.units.PREdge)3 PRNode (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.ranking.units.PRNode)3 CyEdge (org.cytoscape.model.CyEdge)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 HashSet (java.util.HashSet)2 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)1 IteratorThread (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.iterativeclustering.IteratorThread)1 ClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterResults)1 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)1