Search in sources :

Example 6 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class SCPSCluster method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing SCPS 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
    runSCPS = new RunSCPS(matrix, context.epsilon, context.clusters, context.iterations, monitor);
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<NodeCluster> clusterList = runSCPS.run(network, monitor);
    // Canceled?
    if (clusterList == 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, clusterList, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusterList);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  SCPS 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 7 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class RunTransClust method run.

public List<NodeCluster> run(TaskMonitor monitor, CyNetwork network) {
    DoubleMatrix2D matrix = this.distanceMatrix.getColtMatrix();
    nodes = distanceMatrix.getRowNodes();
    HashMap<String, CyNode> nodeHash = new HashMap<String, CyNode>();
    for (CyNode node : nodes) {
        nodeHash.put(ModelUtils.getNodeName(network, node), node);
    }
    HashMap<String, Integer> integers2proteins = new HashMap<String, Integer>();
    HashMap<Integer, String> proteins2integers = new HashMap<Integer, String>();
    int count = 0;
    for (CyNode node : this.nodes) {
        integers2proteins.put(ModelUtils.getNodeName(network, node), count);
        proteins2integers.put(count, ModelUtils.getNodeName(network, node));
        count++;
    }
    Edges es = new Edges(this.nodes.size() * this.nodes.size(), this.nodes.size());
    count = 0;
    for (int i = 0; i < this.nodes.size(); i++) {
        CyNode cyNodeI = this.nodes.get(i);
        es.startPositions[integers2proteins.get(cyNodeI.getSUID())] = count;
        for (int j = 0; j < this.nodes.size(); j++) {
            CyNode cyNodeJ = this.nodes.get(j);
            es.sources[count] = i;
            es.targets[count] = j;
            Double val = distanceMatrix.getValue(i, j);
            if (val != null) {
                es.values[count] = val.floatValue();
                count++;
            }
        }
        es.endPositions[integers2proteins.get(cyNodeI.getSUID())] = count - 1;
    }
    Semaphore s = new Semaphore(1);
    TaskConfig.mode = TaskConfig.COMPARISON_MODE;
    TaskConfig.monitor = monitor;
    IteratorThread it = new IteratorThread(es, integers2proteins, proteins2integers, s);
    TaskConfig.minThreshold = threshold;
    TaskConfig.maxThreshold = threshold;
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    it.start();
    monitor.showMessage(TaskMonitor.Level.INFO, "Executing TransClust Clustering...");
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
    String result = it.resultsStringBuffer.toString();
    String[] clusters = result.split("\t")[2].split(";");
    Map<Integer, NodeCluster> clusterMap = getClusterMap(clusters, nodeHash);
    // 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();
    for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
        if (cMap.containsKey(cluster))
            continue;
        cMap.put(cluster, cluster);
        cluster.setClusterNumber(clusterNumber);
        clusterNumber++;
    }
    Set<NodeCluster> clusters2 = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters2);
}
Also used : HashMap(java.util.HashMap) IteratorThread(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.iterativeclustering.IteratorThread) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) CyNode(org.cytoscape.model.CyNode)

Example 8 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class TransClustCluster method run.

/**
 * Perform the actual clustering.  For TransClust, there are really
 * two steps:
 * 	1) Assign all of the connected components
 * 	2) Do the TransClust clustering.
 *
 * There is also an optional approach called evolutionary parameter
 * tuning, which takes a lot longer and is probably less relevant for
 * the Cytoscape integration.
 *
 * @param monitor the TaskMonitor to use
 */
public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing Transitivity clustering");
    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;
    }
    updateSettings();
    runTransClust = new RunTransClust(matrix, context.edgeAttributeHandler.edgeCutOff.getValue(), monitor);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    createGroups = context.advancedAttributes.createGroups;
    // Cluster the nodes
    List<NodeCluster> clusters = runTransClust.run(monitor, network);
    // 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.setStatusMessage("Done.  TransClust 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 9 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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 10 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class OjAlgoMatrix method like.

public Matrix like(int rows, int columns, double[][] initial) {
    Matrix result = new OjAlgoMatrix();
    result.initialize(rows, columns, initial);
    return result;
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)34 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)22 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)13 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)12 CyNode (org.cytoscape.model.CyNode)12 List (java.util.List)9 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)7 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)7 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)5 CyTable (org.cytoscape.model.CyTable)5 Test (org.junit.Test)4 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)3 ColtMatrix (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 DistanceMetric (edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)2 ScatterPlotDialog (edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)2 CyEdge (org.cytoscape.model.CyEdge)2 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)1 IntArrayList (cern.colt.list.tint.IntArrayList)1