Search in sources :

Example 36 with NodeCluster

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

the class AbstractNetworkClusterer method createGroups.

protected List<List<CyNode>> createGroups(CyNetwork network, List<NodeCluster> clusters, String group_attr) {
    // List of node lists
    List<List<CyNode>> clusterList = new ArrayList<List<CyNode>>();
    // keep track of the groups we create
    List<Long> groupList = new ArrayList<Long>();
    List<Double> clusterScores = new ArrayList<Double>(clusters.size());
    // Initialize
    for (NodeCluster cluster : clusters) {
        clusterScores.add(null);
    }
    boolean haveScores = NodeCluster.getScoreList(clusters) != null;
    // Remove the old column, if it's there.  Some of the algorithms don't put
    // all nodes into clusters, so we might wind up with old data lingering
    ModelUtils.deleteColumnLocal(network, CyNode.class, clusterAttributeName);
    for (NodeCluster cluster : clusters) {
        int clusterNumber = cluster.getClusterNumber();
        if (cluster.hasScore()) {
            clusterScores.set(clusterNumber - 1, cluster.getClusterScore());
            haveScores = true;
        }
        String groupName = clusterAttributeName + "_" + clusterNumber;
        List<CyNode> nodeList = new ArrayList<CyNode>();
        for (CyNode node : cluster) {
            nodeList.add(node);
            ModelUtils.createAndSetLocal(network, node, clusterAttributeName, clusterNumber, Integer.class, null);
        }
        if (createGroups) {
            CyGroup group = clusterManager.createGroup(network, clusterAttributeName + "_" + clusterNumber, nodeList, null, true);
            if (group != null) {
                groupList.add(group.getGroupNode().getSUID());
                if (NodeCluster.hasScore()) {
                    ModelUtils.createAndSetLocal(network, group.getGroupNode(), clusterAttributeName + "_Score", cluster.getClusterScore(), Double.class, null);
                }
            }
        }
        clusterList.add(nodeList);
    }
    if (haveScores)
        ModelUtils.createAndSetLocal(network, network, clusterAttributeName + "_Scores", clusterScores, List.class, Double.class);
    ModelUtils.createAndSetLocal(network, network, group_attr, groupList, List.class, Long.class);
    ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_TYPE_ATTRIBUTE, getShortName(), String.class, null);
    ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_ATTRIBUTE, clusterAttributeName, String.class, null);
    if (params != null)
        ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_PARAMS_ATTRIBUTE, params, List.class, String.class);
    return clusterList;
}
Also used : ArrayList(java.util.ArrayList) CyGroup(org.cytoscape.group.CyGroup) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode)

Example 37 with NodeCluster

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

the class AbstractNetworkClusterer method getNodeClusters.

@SuppressWarnings("unchecked")
public static List<NodeCluster> getNodeClusters(CyNetwork net, String clusterAttribute) {
    // List of node lists
    List<NodeCluster> clusterList = new ArrayList<NodeCluster>();
    // Create the cluster Map
    HashMap<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
    for (CyNode node : net.getNodeList()) {
        // For each node -- see if it's in a cluster.  If so, add it to our map
        if (net.getRow(node).get(clusterAttribute, Integer.class) != null) {
            Integer clusterNumber = net.getRow(node).get(clusterAttribute, Integer.class);
            if (!clusterMap.containsKey(clusterNumber)) {
                NodeCluster cluster = new NodeCluster();
                cluster.setClusterNumber(clusterNumber);
                clusterMap.put(clusterNumber, cluster);
            }
            clusterMap.get(clusterNumber).add(node);
        }
    }
    for (int i = 0; i < clusterMap.size(); i++) {
        if (clusterMap.containsKey(i + 1))
            clusterList.add(clusterMap.get(i + 1));
        else {
            // This shouldn't happen, but...
            clusterList.add(new NodeCluster(i + 1, new ArrayList<CyNode>()));
        }
    }
    return clusterList;
}
Also used : FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode)

Example 38 with NodeCluster

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster 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));
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Example 39 with NodeCluster

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

the class DensityFilter method doFilter.

public NodeCluster doFilter(List<CyNode> nodeList, Map<NodeCluster, List<CyNode>> addedNodeMap) {
    // Get the number of edges within the cluster
    List<CyEdge> edgeList = ModelUtils.getConnectingEdges(network, nodeList);
    if (edgeList == null)
        return null;
    int edgeCount = edgeList.size();
    int nodeCount = nodeList.size();
    // Calculate the density
    double density = (double) (edgeCount * 2) / (double) (nodeCount * (nodeCount - 1));
    if (density >= context.minimumDensity)
        return new NodeCluster(nodeList);
    return null;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyEdge(org.cytoscape.model.CyEdge)

Example 40 with NodeCluster

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

the class AbstractNetworkFilter method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Filtering using " + getName());
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    clusterAttributeName = getClusterAttributeName();
    // get the cluster list
    List<NodeCluster> clusterList = AbstractNetworkClusterer.getNodeClusters(network, getClusterAttribute());
    List<NodeCluster> newClusterList = new ArrayList<NodeCluster>();
    Map<NodeCluster, List<CyNode>> addedNodeMap = new HashMap<NodeCluster, List<CyNode>>();
    // Iterate over clusters and build a new clusterList
    for (NodeCluster nodeList : clusterList) {
        NodeCluster newCluster = doFilter(nodeList, addedNodeMap);
        if (newCluster != null && newCluster.size() > 0)
            newClusterList.add(newCluster);
    }
    // Fixup our clusters
    if (addedNodeMap.size() > 0) {
        // until this stabelized...
        for (NodeCluster addedCluster : addedNodeMap.keySet()) {
            for (NodeCluster cluster : newClusterList) {
                if (cluster.equals(addedCluster))
                    continue;
                removeNodes(cluster, addedNodeMap.get(addedCluster));
            }
        }
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
    // Remove any leftover groups from previous runs
    removeGroups(network, groupAttribute);
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
    List<List<CyNode>> nodeClusters = createGroups(network, newClusterList, groupAttribute);
    results = new AbstractClusterResults(network, clusterList);
    ClusterResults results2 = new AbstractClusterResults(network, newClusterList);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  Results:\n\nBefore Filter:\n" + results + "\n\nAfter Filter:\n" + results2);
    if (showUI()) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, restoreEdges(), false));
    }
}
Also used : FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) ClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.api.ClusterResults)

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