Search in sources :

Example 26 with NodeCluster

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

the class ResultsPanelTask method getClusters.

public List<NodeCluster> getClusters() {
    List<NodeCluster> clusters = new ArrayList<NodeCluster>();
    // List<List<CyNode>> clusterList = new ArrayList<List<CyNode>>();
    /*
		System.out.println(network.NAME);
		System.out.println(CyNetwork.LOCAL_ATTRS);
		System.out.println(ClusterManager.CLUSTER_ATTRIBUTE);
		*/
    clusterAttribute = network.getRow(network, CyNetwork.LOCAL_ATTRS).get(ClusterManager.CLUSTER_ATTRIBUTE, String.class);
    // Create a temporary cluster map
    Map<Integer, ArrayList<CyNode>> clusterMap = new HashMap<Integer, ArrayList<CyNode>>();
    for (CyNode node : (List<CyNode>) network.getNodeList()) {
        // For each node -- see if it's in a cluster.  If so, add it to our map
        if (ModelUtils.hasAttribute(network, node, clusterAttribute)) {
            Integer cluster = network.getRow(node).get(clusterAttribute, Integer.class);
            if (!clusterMap.containsKey(cluster))
                clusterMap.put(cluster, new ArrayList<CyNode>());
            clusterMap.get(cluster).add(node);
        }
    }
    // See if this algorithm provided it's own scores
    List<Double> scores = null;
    if (network.getDefaultNetworkTable().getColumn(clusterAttribute + "_Scores") != null) {
        scores = network.getRow(network, CyNetwork.LOCAL_ATTRS).getList(clusterAttribute + "_Scores", Double.class);
    }
    for (int clustNum : clusterMap.keySet()) {
        NodeCluster cluster = new NodeCluster(clusterMap.get(clustNum));
        cluster.setClusterNumber(clustNum);
        if (scores != null)
            cluster.setClusterScore(scores.get(clustNum - 1));
        clusters.add(cluster);
    }
    // calculating the scores for each cluster
    clusterResults = new AbstractClusterResults(network, clusters);
    List<Double> modularityList;
    if (scores == null) {
        modularityList = clusterResults.getModularityList();
    } else {
        modularityList = scores;
    }
    for (int i = 0; i < clusters.size(); i++) {
        clusters.get(i).setClusterScore(modularityList.get(i));
    }
    return clusters;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Example 27 with NodeCluster

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

the class ClusterUtils method fetchClusters.

public static List<NodeCluster> fetchClusters(CyNetwork network) {
    List<NodeCluster> clusters = new ArrayList<>();
    String clusterAttribute = getClusterAttribute(network);
    Map<Integer, ArrayList<CyNode>> clusterMap = new HashMap<>();
    for (CyNode node : network.getNodeList()) {
        if (ModelUtils.hasAttribute(network, node, clusterAttribute)) {
            Integer cluster = network.getRow(node).get(clusterAttribute, Integer.class);
            if (!clusterMap.containsKey(cluster)) {
                clusterMap.put(cluster, new ArrayList<>());
            }
            clusterMap.get(cluster).add(node);
        }
    }
    for (int clusterNum : clusterMap.keySet()) {
        NodeCluster cluster = new NodeCluster(clusterMap.get(clusterNum));
        cluster.setClusterNumber(clusterNum);
        clusters.add(cluster);
    }
    return clusters;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 28 with NodeCluster

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

the class ClusterUtils method setNodeScoresInCluster.

public static List<NodeCluster> setNodeScoresInCluster(CyNetwork network, List<NodeCluster> clusters, List<String> nodeAttributes, String clusterColumnName, boolean multiplicative) {
    List<CyNode> nodes = network.getNodeList();
    CyTable table = network.getDefaultNodeTable();
    for (String nodeAttr : nodeAttributes) {
        for (CyNode node : nodes) {
            CyRow row = table.getRow(node.getSUID());
            int nodeClusterNumber = row.get(clusterColumnName, Integer.class, -1);
            for (NodeCluster cluster : clusters) {
                if (cluster.getClusterNumber() == nodeClusterNumber) {
                    if (multiplicative) {
                        setRankScoreMultiplicative(nodeAttr, row, cluster);
                    } else {
                        setRankScore(nodeAttr, row, cluster);
                    }
                }
            }
        }
    }
    return clusters;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)

Example 29 with NodeCluster

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

the class RunMCL method run.

public List<NodeCluster> run(CyNetwork network, TaskMonitor monitor) {
    // String networkID = network.getIdentifier();
    Long networkID = network.getSUID();
    long startTime = System.currentTimeMillis();
    // Matrix matrix;
    double numClusters;
    debugln("Initial matrix:", matrix);
    // Normalize
    normalize(matrix, clusteringThresh, false);
    debugln("Normalized matrix:", matrix);
    double residual = 1.0;
    double progress = 1.0;
    // IntIntDoubleFunction myPow = new MatrixPow(inflationParameter);
    debugln("residual = " + residual + " maxResidual = " + maxResidual);
    for (int i = 0; (i < number_iterations) && (residual > maxResidual); i++) {
        progress = (double) (i * 3) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        // Expand
        {
            long t = System.currentTimeMillis();
            // monitor.setStatus();
            monitor.setStatusMessage("Iteration: " + (i + 1) + " expanding ");
            debugln("Iteration: " + (i + 1) + " expanding ");
            debugln("matrix: ", matrix);
            Matrix multiMatrix = matrix.ops().multiplyMatrix(matrix);
            matrix = matrix.copy(multiMatrix);
            // Normalize
            normalize(matrix, clusteringThresh, false);
            monitor.showMessage(TaskMonitor.Level.INFO, "Expansion " + (i + 1) + " took " + (System.currentTimeMillis() - t) + "ms");
        }
        debugln("^ " + (i + 1) + " after expansion");
        progress = (double) (i * 3 + 1) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        // Inflate
        // DoubleMatrix2D m = matrix.getColtMatrix();
        {
            long t = System.currentTimeMillis();
            // monitor.setStatusMessage
            monitor.setStatusMessage("Iteration: " + (i + 1) + " inflating");
            debugln("Iteration: " + (i + 1) + " inflating");
            matrix.ops().powScalar(inflationParameter);
            // Normalize
            normalize(matrix, clusteringThresh, true);
        }
        debugln("^ " + (i + 1) + " after inflation");
        progress = (double) (i * 3 + 2) / (double) (number_iterations * 3);
        monitor.setProgress(progress);
        /*
			double newResidual  = calculateResiduals(matrix);
			if (newResidual >= residual) break;
			residual = newResidual;
			*/
        double newResidual = calculateResiduals(matrix);
        if (forceDecliningResidual && newResidual >= residual)
            break;
        residual = newResidual;
        debugln("Iteration: " + (i + 1) + " residual: " + residual);
        monitor.showMessage(TaskMonitor.Level.INFO, "Iteration " + (i + 1) + " complete.  Residual=" + residual);
        if (canceled) {
            // monitor.setStatusMessage
            monitor.setStatusMessage("canceled");
            return null;
        }
    }
    // If we're in debug mode, output the matrix
    debugln("Matrix: ", matrix);
    // monitor.setStatusMessage
    monitor.setStatusMessage("Assigning nodes to clusters");
    clusterCount = 0;
    HashMap<Integer, NodeCluster> clusterMap = new HashMap<Integer, NodeCluster>();
    ClusterMatrix clusterMat = new ClusterMatrix(clusterMap);
    for (int row = 0; row < matrix.nRows(); row++) {
        for (int col = 0; col < matrix.nColumns(); col++) {
            clusterMat.apply(row, col, matrix.doubleValue(row, col));
        }
    }
    // Update node attributes in network to include clusters. Create cygroups from clustered nodes
    monitor.setStatusMessage("Created " + clusterCount + " clusters");
    monitor.setStatusMessage("Cluster map has " + clusterMap.keySet().size() + " clusters");
    // 
    if (clusterCount == 0) {
        monitor.setStatusMessage("Created 0 clusters!!!!");
        monitor.setStatusMessage("Cluster map has " + clusterMap.keySet().size() + " clusters");
        return null;
    }
    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++;
    }
    monitor.setStatusMessage("Total runtime = " + (System.currentTimeMillis() - startTime) + "ms");
    Set<NodeCluster> clusters = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)

Example 30 with NodeCluster

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

the class MCODEAlgorithm method findClusters.

/**
 * Step 2: Find all clusters given a scored graph.  If the input network has not been scored,
 * this method will return null.  This method is called when the user selects network scope or
 * single node scope.
 *
 * @param inputNetwork The scored network to find clusters in.
 * @param resultId Title of the result
 * @return An array containing an MCODEClusterObj object for each cluster.
 */
public List<NodeCluster> findClusters(CyNetwork inputNetwork, int resultId) {
    SortedMap<Double, List<CyNode>> nodeScoreSortedMap;
    Map<CyNode, NodeInfo> nodeInfoHashMap;
    // under the title of this result set for later use
    if (!nodeScoreResultsMap.containsKey(resultId)) {
        nodeScoreSortedMap = currentNodeScoreSortedMap;
        nodeInfoHashMap = currentNodeInfoHashMap;
        nodeScoreResultsMap.put(resultId, nodeScoreSortedMap);
        nodeInfoResultsMap.put(resultId, nodeInfoHashMap);
    } else {
        nodeScoreSortedMap = (TreeMap) nodeScoreResultsMap.get(resultId);
        nodeInfoHashMap = (HashMap) nodeInfoResultsMap.get(resultId);
    }
    params = getParams();
    String callerID = "MCODEAlgorithm.findClusters";
    if (inputNetwork == null) {
        taskMonitor.showMessage(TaskMonitor.Level.ERROR, "In " + callerID + ": inputNetwork was null.");
        return (null);
    }
    if ((nodeInfoHashMap == null) || (nodeScoreSortedMap == null)) {
        taskMonitor.showMessage(TaskMonitor.Level.ERROR, "In " + callerID + ": nodeInfoHashMap or nodeScoreSortedMap was null.");
        return (null);
    }
    // initialization
    long msTimeBefore = System.currentTimeMillis();
    Map<CyNode, Boolean> nodeSeenHashMap = // key is nodeIndex, value is true/false
    new HashMap<CyNode, Boolean>();
    int findingProgress = 0;
    int findingTotal = 0;
    // returns a Collection sorted by key order (descending)
    Collection<List<CyNode>> values = nodeScoreSortedMap.values();
    // and no progress is reported) we count all the scored nodes and track those instead
    for (List<CyNode> nodeList : values) {
        findingTotal += nodeList.size();
    }
    // stores the list of clusters as ArrayLists of node indices in the input Network
    List<MCODEClusterObj> alClusters = new ArrayList<MCODEClusterObj>();
    // iterate over node indices sorted descending by their score
    for (List<CyNode> alNodesWithSameScore : values) {
        for (CyNode currentNode : alNodesWithSameScore) {
            if (!nodeSeenHashMap.containsKey(currentNode)) {
                // Store the list of all the nodes that have already been seen and incorporated in other clusters
                Map<CyNode, Boolean> nodeSeenHashMapSnapShot = new HashMap<CyNode, Boolean>(nodeSeenHashMap);
                List<CyNode> alCluster = getClusterCore(currentNode, nodeSeenHashMap, params.getNodeScoreCutoff(), params.getMaxDepthFromStart(), // here we use the original node score cutoff
                nodeInfoHashMap);
                if (alCluster.size() > 0) {
                    // make sure seed node is part of cluster, if not already in there
                    if (!alCluster.contains(currentNode)) {
                        alCluster.add(currentNode);
                    }
                    // create an input graph for the filter and haircut methods
                    CyNetwork clusterGraph = createCyNetwork(alCluster, inputNetwork);
                    if (!filterCluster(clusterGraph)) {
                        if (params.isHaircut()) {
                            haircutCluster(clusterGraph, alCluster, inputNetwork);
                        }
                        if (params.isFluff()) {
                            fluffClusterBoundary(alCluster, nodeSeenHashMap, nodeInfoHashMap);
                        }
                        clusterGraph = createCyNetwork(alCluster, inputNetwork);
                        final double score = scoreCluster(clusterGraph);
                        MCODEClusterObj currentCluster = new MCODEClusterObj(resultId, currentNode, clusterGraph, score, alCluster, nodeSeenHashMapSnapShot);
                        alClusters.add(currentCluster);
                    }
                }
            }
            if (taskMonitor != null) {
                findingProgress++;
                // We want to be sure that only progress changes are reported and not
                // miniscule decimal increments so that the taskMonitor isn't overwhelmed
                double newProgress = (double) findingProgress / (double) findingTotal;
                double oldProgress = (double) (findingProgress - 1) / (double) findingTotal;
                if (newProgress != oldProgress) {
                    taskMonitor.setProgress(newProgress);
                }
            }
            if (cancelled) {
                break;
            }
        }
    }
    // Once the clusters have been found we either return them or in the case of selection scope, we select only
    // the ones that contain the selected node(s) and return those
    ArrayList selectedALClusters = new ArrayList();
    if (!params.getScope().equals(MCODEParameterSet.NETWORK)) {
        for (MCODEClusterObj cluster : alClusters) {
            List<CyNode> alCluster = cluster.getALCluster();
            List<CyNode> alSelectedNodes = new ArrayList<CyNode>(params.getSelectedNodes());
            // method for returning only clusters that contain all of the selected noes
            /*
				if (alCluster.containsAll(alSelectedNodes)) {
					selectedAlClusters.add(cluster);
				}
				*/
            // method for returning all clusters that contain any of the selected nodes
            boolean hit = false;
            for (CyNode node : alSelectedNodes) {
                if (alCluster.contains(node))
                    hit = true;
            }
            if (hit)
                selectedALClusters.add(cluster);
        }
        alClusters = selectedALClusters;
    }
    // Finally convert the arraylist into a fixed array
    List<NodeCluster> clusters = new ArrayList<NodeCluster>();
    for (MCODEClusterObj c : alClusters) {
        clusters.add(c.getNodeCluster());
    }
    long msTimeAfter = System.currentTimeMillis();
    lastFindTime = msTimeAfter - msTimeBefore;
    return clusters;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyNode(org.cytoscape.model.CyNode)

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