Search in sources :

Example 16 with Matrix

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

the class RunBiMine method getASR.

private double getASR(BETNode<Integer> node) {
    List<Integer> genes = node.getGenes();
    List<Integer> conditions = node.getConditions();
    // Matrix data = new Matrix(network,genes.size(),conditions.size());
    CyMatrix data = CyMatrixFactory.makeSmallMatrix(network, genes.size(), conditions.size());
    for (int i = 0; i < genes.size(); i++) {
        for (int j = 0; j < conditions.size(); j++) {
            data.setValue(i, j, matrix.getValue(genes.get(i), conditions.get(j)));
        // data_t.setValue(j, i, matrix.getValue(genes.get(i), conditions.get(j)));
        }
    }
    double asr = 0.0;
    double asr_g = 0.0;
    double asr_c = 0.0;
    for (int i = 0; i < genes.size(); i++) {
        for (int j = i + 1; j < genes.size(); j++) {
            // asr_g += geneRho[genes.get(i)][genes.get(j)];
            asr_g += getSpearmansRho(data, i, j);
        }
    }
    asr_g /= genes.size() * (genes.size() - 1);
    /*
		for(int i = 0; i < conditions.size(); i++){			
			for(int j = i+1; j < conditions.size(); j++){
				//asr_c += conditionRho[conditions.get(i)][conditions.get(j)];
				asr_c += getSpearmansRho(data_t,i,j);
			}
		}
		asr_c /= conditions.size()*(conditions.size()-1);
		*/
    // asr = 2*Math.max(asr_g, asr_c);
    asr = 2 * asr_g;
    return asr;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 17 with Matrix

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

the class RunDBSCAN method cluster.

public int[] cluster(boolean transpose) {
    // Create the matrix
    matrix = CyMatrixFactory.makeSmallMatrix(network, weightAttributes, selectedOnly, ignoreMissing, transpose, false);
    monitor.showMessage(TaskMonitor.Level.INFO, "cluster matrix has " + matrix.nRows() + " rows");
    DistanceMetric metric = context.metric.getSelectedValue();
    if (monitor != null)
        monitor.setStatusMessage("Clustering...");
    int nelements = matrix.nRows();
    int ifound = 1;
    int currentC = -1;
    int[] clusters = new int[nelements];
    // calculate the distances and store in distance matrix
    // Do we want to normalize the matrix?
    // Matrix normMatrix = matrix.getDistanceMatrix(metric);
    // normMatrix.ops().normalize();
    // distanceMatrix = normMatrix.toArray();
    distanceMatrix = matrix.getDistanceMatrix(metric).toArray();
    unvisited = new ArrayList<Integer>();
    // Initializing all nodes as unvisited and clusters to -1
    for (int i = 0; i < nelements; i++) {
        unvisited.add(i);
        clusters[i] = -1;
    }
    while (unvisited.size() > 0) {
        int p = unvisited.get(0);
        unvisited.remove(0);
        ArrayList<Integer> neighborPts = regionQuery(p);
        if (neighborPts.size() < minPts) {
            clusters[p] = -1;
        } else {
            currentC += 1;
            expandCluster(p, neighborPts, currentC, clusters);
        // System.out.println("Node "+p+" has "+neighborPts.size()+" neighbors after expansion");
        }
    }
    nClusters = currentC + 1;
    // System.out.println("nClusters = "+nClusters);
    return clusters;
}
Also used : DistanceMetric(edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)

Example 18 with Matrix

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

the class MatrixTest method covTest.

@Test
public void covTest() {
    initialize();
    timeStart();
    Matrix simpleCov = simpleMatrix.ops().covariance();
    timeEnd("simple matrix covariance");
    timeStart();
    Matrix coltCov = coltMatrix.ops().covariance();
    timeEnd("colt matrix covariance");
    assertArrayEquals(simpleCov.toArray(), coltCov.toArray(), DELTA);
    timeStart();
    Matrix ojAlgoCov = ojAlgoMatrix.ops().covariance();
    timeEnd("ojAlgo matrix covariance");
    assertArrayEquals(simpleCov.toArray(), ojAlgoCov.toArray(), DELTA);
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) Test(org.junit.Test)

Example 19 with Matrix

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

the class MatrixTest method corrTest.

@Test
public void corrTest() {
    initialize();
    timeStart();
    Matrix simpleCorr = simpleMatrix.ops().correlation();
    timeEnd("simple matrix correlation");
    timeStart();
    Matrix coltCorr = coltMatrix.ops().correlation();
    timeEnd("colt matrix correlation");
    assertArrayEquals(simpleCorr.toArray(), coltCorr.toArray(), DELTA);
    timeStart();
    Matrix ojAlgoCorr = ojAlgoMatrix.ops().correlation();
    timeEnd("ojAlgo matrix correlation");
    assertArrayEquals(simpleCorr.toArray(), ojAlgoCorr.toArray(), DELTA);
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) Test(org.junit.Test)

Example 20 with Matrix

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

the class HierarchicalCluster method run.

public void run(TaskMonitor monitor) {
    this.monitor = monitor;
    monitor.setTitle("Performing " + getName());
    List<String> nodeAttributeList = context.attributeList.getNodeAttributeList();
    String edgeAttribute = context.attributeList.getEdgeAttribute();
    if (nodeAttributeList == null && edgeAttribute == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Must select either one edge column or two or more node columns");
        return;
    }
    if (nodeAttributeList != null && nodeAttributeList.size() > 0 && edgeAttribute != null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't have both node and edge columns selected");
        return;
    }
    if (context.selectedOnly && nodeAttributeList != null && nodeAttributeList.size() > 1 && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
        return;
    }
    // Get our attributes we're going to use for the cluster
    String[] attributeArray;
    if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
        Collections.sort(nodeAttributeList);
        attributeArray = new String[nodeAttributeList.size()];
        int i = 0;
        for (String attr : nodeAttributeList) {
            attributeArray[i++] = "node." + attr;
        }
    } else {
        attributeArray = new String[1];
        attributeArray[0] = "edge." + edgeAttribute;
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Initializing");
    // System.out.println("Initializing");
    resetAttributes(network, SHORTNAME);
    // Create a new clusterer
    DistanceMetric metric = context.metric.getSelectedValue();
    RunHierarchical algorithm = new RunHierarchical(network, attributeArray, metric, clusterMethod, monitor, context);
    // Cluster the attributes, if requested
    if (context.clusterAttributes && (attributeArray.length > 1 || context.isAssymetric())) {
        monitor.setStatusMessage("Clustering attributes");
        // System.out.println("Clustering attributes");
        Integer[] rowOrder = algorithm.cluster(true);
        attributeTree = algorithm.getAttributeList();
        CyMatrix matrix = algorithm.getMatrix();
        updateAttributes(network, SHORTNAME, rowOrder, attributeArray, attributeTree, matrix);
        attributeOrder = new ArrayList<String>();
        for (int i = 0; i < rowOrder.length; i++) {
            attributeOrder.add(matrix.getRowLabel(rowOrder[i]));
        }
    }
    monitor.setStatusMessage("Clustering nodes");
    // Cluster the nodes
    // System.out.println("Clustering nodes");
    Integer[] rowOrder = algorithm.cluster(false);
    nodeTree = algorithm.getAttributeList();
    CyMatrix matrix = algorithm.getMatrix();
    updateAttributes(network, SHORTNAME, rowOrder, attributeArray, nodeTree, matrix);
    nodeOrder = new ArrayList<CyNode>();
    for (int i = 0; i < rowOrder.length; i++) {
        nodeOrder.add(matrix.getRowNode(rowOrder[i]));
    }
    // TODO: Deal with params!
    List<String> params = context.getParams(algorithm.getMatrix());
    updateParams(network, params);
    if (context.showUI) {
        insertTasksAfterCurrentTask(new TreeView(clusterManager));
    }
    monitor.setStatusMessage("Done");
}
Also used : DistanceMetric(edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) TreeView(edu.ucsf.rbvi.clusterMaker2.internal.ui.TreeView) CyNode(org.cytoscape.model.CyNode)

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