Search in sources :

Example 51 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters 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)));
    }
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

Example 52 with Clusters

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

the class InOut method writeCostMatrices.

public static void writeCostMatrices(Edges es, Vector<Vector<Integer>> clusters, HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins) throws IOException {
    Vector<Vector<Integer>> complete = new Vector<Vector<Integer>>();
    int countCostMatrices = 1;
    double percentOld = 0;
    double numberOfProteins = proteins2integers.size();
    double alreadySolvedNumberOfProteins = 0;
    for (int i = 0; i < clusters.size(); i++) {
        Vector<Integer> cluster = clusters.get(i);
        int numberEdges = ((cluster.size() * (cluster.size() - 1)) / 2);
        int numberProteins = cluster.size();
        alreadySolvedNumberOfProteins += numberProteins;
        int countRealEdges = 0;
        if (Config.reducedMatrix) {
            CostMatrix cm = new CostMatrix(numberProteins);
            HashMap<String, Integer> CmIntegers2proteins = new HashMap<String, Integer>(numberProteins);
            HashMap<Integer, String> CmProteins2integers = new HashMap<Integer, String>(numberProteins);
            for (int j = 0; j < cluster.size(); j++) {
                int source = cluster.get(j);
                String protein = proteins2integers.get(source);
                CmProteins2integers.put(j, protein);
                CmIntegers2proteins.put(protein, j);
                for (int k = j + 1; k < cluster.size(); k++) {
                    int target = cluster.get(k);
                    float value = getEdgeValue(source, target, es);
                    cm.setEdgevalues(j, k, value);
                    cm.setEdgevalues(k, j, value);
                    if (value > Config.threshold)
                        countRealEdges++;
                }
            }
            cm.setIntegers2proteins(CmIntegers2proteins);
            cm.setProteins2integers(CmProteins2integers);
            if (countRealEdges == numberEdges) {
                complete.add(cluster);
            } else {
                String costMatrixFile = "";
                if (TaskConfig.mode == TaskConfig.HIERARICHAL_MODE) {
                    costMatrixFile = "costMatrix_size_" + cluster.size() + "_nr_" + countCostMatrices + "_" + new Random().nextDouble() + ".rcm";
                } else {
                    costMatrixFile = "costMatrix_size_" + cluster.size() + "_nr_" + countCostMatrices + ".rcm";
                }
                CostMatrix mergedCM = cm.mergeNodes();
                mergedCM.writeCostMatrix(Config.costMatrixDirectory + "/" + costMatrixFile);
                countCostMatrices++;
            }
            double percent = Math.rint((((double) alreadySolvedNumberOfProteins) / ((double) numberOfProteins)) * 10000) / 100;
            if (percent > percentOld + 0.5 || percent == 100) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("Writing costmatrices  " + percent + "%");
                }
            // else  System.out.print( percent + "%\t");
            }
            for (int j = 0; j < cluster.size(); j++) {
                String id = proteins2integers.get(cluster.get(j));
                integers2proteins.remove(id);
            }
        } else {
            int countPosition = 0;
            Edges edges = new Edges(numberEdges, numberProteins);
            // create edges for connected components
            for (int j = 0; j < cluster.size(); j++) {
                int source = cluster.get(j);
                edges.setStartPosition(j, countPosition);
                for (int k = j + 1; k < cluster.size(); k++) {
                    int target = cluster.get(k);
                    float value = getEdgeValue(source, target, es);
                    if (value > Config.threshold) {
                        countRealEdges++;
                    }
                    edges.setSource(countPosition, j);
                    edges.setTarget(countPosition, k);
                    edges.setValue(countPosition, value);
                    countPosition++;
                }
                edges.setEndPosition(j, countPosition - 1);
            }
            // divide between complete and incomplete connected components
            if (countRealEdges == numberEdges) {
                complete.add(cluster);
            } else {
                writeCostMatrix(edges, cluster, countCostMatrices, proteins2integers, integers2proteins);
                countCostMatrices++;
            }
            double percent = Math.rint((((double) alreadySolvedNumberOfProteins) / ((double) numberOfProteins)) * 10000) / 100;
            if (percent > percentOld + 0.5 || percent == 100) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("Writing costmatrices  " + percent + "%");
                }
            // else  System.out.print( percent + "%\t");
            }
            // remove proteins from list which are assigned to one cluster
            for (int j = 0; j < cluster.size(); j++) {
                String id = proteins2integers.get(cluster.get(j));
                integers2proteins.remove(id);
            }
        }
    }
    for (Iterator<String> iter = integers2proteins.keySet().iterator(); iter.hasNext(); ) {
        String element = iter.next();
        int id = integers2proteins.get(element);
        Vector<Integer> cluster = new Vector<Integer>();
        cluster.add(id);
        complete.add(cluster);
    }
    writeCompleteTable(complete, proteins2integers);
}
Also used : HashMap(java.util.HashMap) Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) Random(java.util.Random) Vector(java.util.Vector) CostMatrix(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.CostMatrix)

Example 53 with Clusters

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

the class RunFuzzifier method getFuzzyCenters.

/**
 * The method calculates the centers of fuzzy clusters
 *
 * @param cData matrix to store the data for cluster centers
 */
public void getFuzzyCenters(CyMatrix cData) {
    // To store the sum of memberships(raised to fuzziness index) corresponding to each cluster
    int nelements = distanceMatrix.nRows();
    for (NodeCluster cluster : Clusters) {
        int c = Clusters.indexOf(cluster);
        double numerator = 0;
        Double distance = 0.0;
        int i = 0;
        for (int e = 0; e < nelements; e++) {
            numerator = 0;
            for (CyNode node : cluster) {
                i = nodeList.indexOf(node);
                distance = distanceMatrix.doubleValue(i, e);
                numerator += distance;
            }
            cData.setValue(c, e, (numerator / cluster.size()));
        }
    }
}
Also used : FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyNode(org.cytoscape.model.CyNode)

Example 54 with Clusters

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters 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));
    }
}
Also used : NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) 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) NumberFormat(java.text.NumberFormat)

Example 55 with Clusters

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

the class RunSCPS method doComponentClustering.

// Store all components length greater then 5 in clusters, if number components is greater then K
public void doComponentClustering() {
    // Connected Componets
    Map<Integer, List<CyNode>> cMap = MatrixUtils.findConnectedComponents(distanceMatrix);
    // Iterate through connected components
    int component_size_sum = 0;
    for (List<CyNode> component : cMap.values()) {
        if (component.size() > 5) {
            NodeCluster iCluster = new NodeCluster(component);
            iCluster.setClusterNumber(this.clusterCount);
            this.clusterMap.put(new Integer(clusterCount), iCluster);
            this.clusterCount++;
        }
    }
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) 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)

Aggregations

ArrayList (java.util.ArrayList)30 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)27 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)22 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)21 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)16 HashMap (java.util.HashMap)16 List (java.util.List)15 CyNode (org.cytoscape.model.CyNode)13 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)10 Test (org.junit.Test)9 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)8 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)6 Random (java.util.Random)5 Vector (java.util.Vector)5 Semaphore (java.util.concurrent.Semaphore)5 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)3 ICCEdges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ICCEdges)3 IParameters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters)3 ClusteringManager (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager)3 PREdge (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.ranking.units.PREdge)3