Search in sources :

Example 26 with Matrix

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

the class RunPCA method calculateLoadingMatrix.

private Matrix calculateLoadingMatrix(CyMatrix matrix) {
    int rows = eigenVectors.length;
    int columns = eigenVectors[0].length;
    Matrix loading = CyMatrixFactory.makeSmallMatrix(matrix.getNetwork(), rows, columns);
    // loading.initialize(rows, columns, new double[rows][columns]);
    IntStream.range(0, rows).parallel().forEach(row -> {
        for (int column = columns - 1, newCol = 0; column >= 0; column--, newCol++) {
            loading.setValue(row, newCol, eigenVectors[row][column] * Math.sqrt(Math.abs(eigenValues[column])));
        }
    });
    loading.setRowLabels(Arrays.asList(matrix.getColumnLabels()));
    for (int column = 0; column < columns; column++) {
        loading.setColumnLabel(column, "PC " + (column + 1));
    }
    return loading;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) ColtMatrix(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)

Example 27 with Matrix

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

the class PCoA method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Running Principal Coordinate Analysis");
    this.monitor = monitor;
    long start = System.currentTimeMillis();
    if (network == null)
        network = clusterManager.getNetwork();
    context.setNetwork(network);
    CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
    if (matrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    if (canceled)
        return;
    PCoAContext.NegEigenHandling neg = context.neg.getSelectedValue();
    // Cluster the nodes
    runpcoa = new RunPCoA(manager, matrix, network, networkView, context, neg.getValue(), monitor);
    runpcoa.run();
    if (canceled)
        return;
    long duration = System.currentTimeMillis() - start;
    monitor.showMessage(TaskMonitor.Level.INFO, "PCoA completed in " + duration + "ms");
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 28 with Matrix

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

the class ResultPanelPCA method valueChanged.

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    // Get the rows
    int[] rowIndices = table.getSelectedRows();
    Map<CyNode, CyNode> selectedMap = new HashMap<CyNode, CyNode>();
    double threshold = 0.02;
    // Get the clusters
    for (int index = 0; index < rowIndices.length; index++) {
        int row = rowIndices[index];
        CyMatrix matrix = components[row];
        for (int i = 0; i < matrix.nRows(); i++) {
            for (int j = 0; j < matrix.nColumns(); j++) {
                if (matrix.getValue(i, j) > threshold) {
                    CyNode node = matrix.getRowNode(i);
                    selectedMap.put(node, node);
                    break;
                }
            }
        }
    // System.out.println("PC is selected");
    }
    // Select the nodes
    for (CyNode node : network.getNodeList()) {
        if (selectedMap.containsKey(node))
            network.getRow(node).set(CyNetwork.SELECTED, true);
        else
            network.getRow(node).set(CyNetwork.SELECTED, false);
    }
    // I wish we didn't need to do this, but if we don't, the selection
    // doesn't update
    networkView.updateView();
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) ListSelectionModel(javax.swing.ListSelectionModel) CyNode(org.cytoscape.model.CyNode) Point(java.awt.Point)

Example 29 with Matrix

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

the class RunBicFinder method getASR.

private double getASR(List<Integer> genes, List<Integer> conditions) {
    CyMatrix data = CyMatrixFactory.makeSmallMatrix(network, genes.size(), conditions.size());
    // Matrix data_t = new Matrix(network,conditions.size(),genes.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 += getSpearmansRho(data, i, j);
        }
    }
    asr_g /= genes.size() * (genes.size() - 1);
    /*
		for(int i = 0; i < genes.size(); i++){
			for(int j = i+1; j < genes.size(); j++){
				asr_c += conditionRho[conditions.get(i)][conditions.get(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 30 with Matrix

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

the class FeatureVectorCluster method run.

public void run(TaskMonitor monitor) {
    this.monitor = monitor;
    monitor.setTitle("Performing " + getName());
    List<String> nodeAttributeList = context.nodeAttributeList.getSelectedValues();
    // Sanity check all of our settings
    if (nodeAttributeList == null || nodeAttributeList.size() == 0) {
        if (monitor != null) {
            monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
        }
        return;
    }
    String[] attributeArray = new String[nodeAttributeList.size()];
    int index = 0;
    for (String attr : nodeAttributeList) {
        attributeArray[index++] = "node." + attr;
    }
    // To make debugging easier, sort the attribute array
    Arrays.sort(attributeArray);
    if (monitor != null) {
        monitor.setProgress(0.0);
        monitor.setStatusMessage("Initializaing");
    }
    // Create the matrix
    // Matrix matrix = new Matrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly);
    CyMatrix matrix = CyMatrixFactory.makeSmallMatrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly, false);
    if (monitor != null) {
        monitor.setProgress(0.1);
        monitor.setStatusMessage("Calculating edge distances");
        if (canceled)
            return;
    }
    // Handle special cases
    if (context.zeroMissing)
        matrix.setMissingToZero();
    int nNodes = matrix.nRows();
    // For each node, get the distance to all other nodes
    double maxdistance = Double.MIN_VALUE;
    double mindistance = Double.MAX_VALUE;
    double[][] distanceMatrix = new double[nNodes][nNodes];
    for (int i = 0; i < nNodes; i++) {
        for (int j = i + 1; j < nNodes; j++) {
            double distance = context.metric.getSelectedValue().getMetric(matrix, matrix, i, j);
            maxdistance = Math.max(maxdistance, distance);
            mindistance = Math.min(mindistance, distance);
            distanceMatrix[i][j] = distance;
        }
        if (canceled)
            return;
        monitor.setProgress((double) i / ((double) nNodes * 4));
    }
    monitor.setStatusMessage("Min distance = " + mindistance + ", max distance = " + maxdistance);
    monitor.setStatusMessage("Assigning values to edges");
    List<CyEdge> edgeList = new ArrayList<CyEdge>();
    double scale = maxdistance - mindistance;
    CyNetwork newNet = null;
    if (context.createNewNetwork) {
        newNet = ModelUtils.createChildNetwork(clusterManager, network, network.getNodeList(), null, "--clustered");
    }
    for (int i = 0; i < nNodes; i++) {
        for (int j = i + 1; j < nNodes; j++) {
            // time = System.currentTimeMillis();
            // This scales the distances to be between 0.0 and 1.0
            double distance = (distanceMatrix[i][j] - mindistance) / scale;
            if (context.createNewNetwork == true && distance > context.edgeCutoff && context.edgeCutoff != 0.0)
                continue;
            CyNode source = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(i), CyNode.class);
            CyNode target = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(j), CyNode.class);
            // time = System.currentTimeMillis();
            if (context.createNewNetwork == true) {
                CyEdge edge = newNet.addEdge(source, target, false);
                ModelUtils.createAndSet(newNet, edge, context.edgeAttribute, distance, Double.class, null);
                edgeList.add(edge);
            } else {
                List<CyEdge> connectingEdges = network.getConnectingEdgeList(source, target, CyEdge.Type.ANY);
                // time = System.currentTimeMillis();
                if (connectingEdges == null || connectingEdges.size() == 0)
                    continue;
                CyEdge edge = connectingEdges.get(0);
                ModelUtils.createAndSet(network, edge, context.edgeAttribute, distance, Double.class, null);
            }
        }
        if (canceled)
            return;
        monitor.setProgress(25.0 + (75.0 * (double) i / (double) nNodes) / 100.0);
    }
    System.out.println("Network created -- creating view");
    // If we're supposed to, create the new network
    if (context.createNewNetwork) {
        VisualStyle style = ViewUtils.getCurrentVisualStyle(clusterManager);
        CyNetworkView view = ViewUtils.createView(clusterManager, newNet, false);
        ViewUtils.doLayout(clusterManager, view, monitor, "force-directed");
        ViewUtils.setVisualStyle(clusterManager, view, style);
        ViewUtils.registerView(clusterManager, view);
    }
    /*
		System.out.println("Created "+newEdges+" edges");
		System.out.println("Edge creation time: "+edgeCreateTime+"ms");
		System.out.println("Network add time: "+networkAddTime+"ms");
		System.out.println("Edge fetch time: "+edgeFetchTime+"ms");
		System.out.println("Node fetch time: "+nodeFetchTime+"ms");
		System.out.println("Set attribute time: "+setAttributeTime+"ms");
		*/
    monitor.setStatusMessage("Complete");
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyEdge(org.cytoscape.model.CyEdge) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

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