Search in sources :

Example 16 with CyMatrix

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

the class RunAutoSOME method getNodeClustersFCN.

private Map<NodeCluster, NodeCluster> getNodeClustersFCN(clusterRun cr, CyMatrix matrix, Settings s) {
    attrList = new ArrayList<String>();
    attrOrderList = new ArrayList<String>();
    nodeOrderList = new ArrayList<String>();
    HashMap<NodeCluster, NodeCluster> cMap = new HashMap<NodeCluster, NodeCluster>();
    storeNodes = new HashMap<String, CyNode>();
    storeClust = new HashMap<String, String>();
    int currClust = -1;
    NodeCluster nc = new NodeCluster();
    Map<String, CyNode> storeOrigNodes = new HashMap<String, CyNode>();
    for (int i = 0; i < nodes.size(); i++) {
        CyNode cn = (CyNode) nodes.get(i);
        storeOrigNodes.put(ModelUtils.getNodeName(network, cn), cn);
    }
    if (!s.FCNrows)
        for (int i = 1; i < s.columnHeaders.length; i++) attrOrderList.add(s.columnHeaders[i]);
    else {
        for (int i = 0; i < matrix.nColumns(); i++) attrOrderList.add(matrix.getColumnLabel(i));
    }
    for (int i = 0; i < cr.fcn_nodes.length; i++) {
        String[] fcn = cr.fcn_nodes[i];
        if (currClust != Integer.valueOf(fcn[1])) {
            if (nc.size() > 0)
                cMap.put(nc, nc);
            nc = new NodeCluster();
            currClust = Integer.valueOf(fcn[1]);
            nc.setClusterNumber(currClust);
        // System.out.println(currClust+"\t"+nc.getClusterNumber());
        }
        String temp = fcn[0];
        // System.out.println(temp);
        String[] tokens = temp.split("_");
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < tokens.length - 1; j++) sb.append(tokens[j] + "_");
        temp = sb.substring(0, sb.length() - 1);
        CyNode cn = network.addNode();
        network.getRow(cn).set(CyNetwork.NAME, temp);
        network.getRow(cn).set(CyRootNetwork.SHARED_NAME, temp);
        nodeOrderList.add(temp);
        attrList.add(temp + "\t" + currClust);
        if (s.FCNrows) {
            CyNode orig = (CyNode) storeOrigNodes.get(fcn[2]);
            CyTable nodeAttrs = network.getDefaultNodeTable();
            Set<String> atts = CyTableUtil.getColumnNames(nodeAttrs);
            for (String attribute : atts) {
                Class type = nodeAttrs.getColumn(attribute).getType();
                Object att = nodeAttrs.getRow(orig).getRaw(attribute);
                if (att == null)
                    continue;
                nodeAttrs.getRow(cn).set(attribute, att);
            }
        }
        storeNodes.put(fcn[0], cn);
        storeClust.put(fcn[0], fcn[1]);
        nc.add(cn);
    /*
			CyAttributes netAttr = Cytoscape.getNetworkAttributes();
			String netID = Cytoscape.getCurrentNetwork().getIdentifier();
			netAttr.setListAttribute(netID, ClusterMaker.CLUSTER_NODE_ATTRIBUTE, attrList);
			netAttr.setListAttribute(netID, ClusterMaker.ARRAY_ORDER_ATTRIBUTE, attrOrderList);
			netAttr.setListAttribute(netID, ClusterMaker.NODE_ORDER_ATTRIBUTE, nodeOrderList);
			*/
    }
    if (nc.size() > 0)
        cMap.put(nc, nc);
    return cMap;
}
Also used : HashMap(java.util.HashMap) CyTable(org.cytoscape.model.CyTable) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyNode(org.cytoscape.model.CyNode)

Example 17 with CyMatrix

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

the class PAMTest method testSingletonCluster.

@Test
public void testSingletonCluster() {
    Double[] data = { .9, .9 };
    int k = 1;
    int[] ans = { 0 };
    CyMatrix mat = CyMatrixFactory.makeSmallMatrix(1, 2, data);
    PAM pam = new PAM(null, mat, DistanceMetric.CITYBLOCK);
    Clusters c = pam.cluster(k);
    assertEquals(c.getNumberOfClusters(), k);
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters) Test(org.junit.Test)

Example 18 with CyMatrix

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

the class PAMTest method testLarge.

@Test
public void testLarge() {
    System.out.println("testLarge begin");
    Double[] data;
    int k = 8;
    int[] ans = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
    BufferedReader reader = new BufferedReader(new InputStreamReader(PAMTest.class.getResourceAsStream("/pam_data.txt")));
    String line;
    ArrayList<Double> vectors = new ArrayList<Double>();
    int vectorWidth = 0;
    try {
        while ((line = reader.readLine()) != null) {
            String[] vector = line.split("\t");
            vectorWidth = vector.length;
            for (String v : vector) vectors.add(Double.parseDouble(v));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    data = new Double[vectors.size()];
    data = vectors.toArray(data);
    // BaseMatrix mat = new BaseMatrix(0, vectorWidth, data);
    CyMatrix mat = CyMatrixFactory.makeSmallMatrix(data.length / vectorWidth, vectorWidth, data);
    PAM pam = new PAM(null, mat, DistanceMetric.EUCLIDEAN);
    Clusters c = pam.cluster(k);
    System.out.println("testLarge end");
    assertEquals(c.getNumberOfClusters(), k);
    for (int i = 0; i < c.size(); ++i) {
        assertEquals(c.getClusterIndex(i), ans[i]);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) Clusters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters) Test(org.junit.Test)

Example 19 with CyMatrix

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

the class RunPCA method runOnNodeToAttributeMatrix.

// this method assumes that eigen values
// are sorted in increasing order
public void runOnNodeToAttributeMatrix() {
    // System.out.println("runOnNodeToAttributeMatrix");
    CyMatrix matrix = CyMatrixFactory.makeLargeMatrix(network, weightAttributes, context.selectedOnly, context.ignoreMissing, false, false);
    // System.out.println("Computing principle components");
    components = computePCs(matrix);
    final Matrix loadingMatrix = calculateLoadingMatrix(matrix);
    if (context.pcaResultPanel) {
        CyServiceRegistrar registrar = manager.getService(CyServiceRegistrar.class);
        CySwingApplication swingApplication = manager.getService(CySwingApplication.class);
        ResultPanelPCA panel = new ResultPanelPCA(components, variance, network, networkView);
        CytoPanel cytoPanel = swingApplication.getCytoPanel(CytoPanelName.EAST);
        registrar.registerService(panel, CytoPanelComponent.class, new Properties());
        if (cytoPanel.getState() == CytoPanelState.HIDE)
            cytoPanel.setState(CytoPanelState.DOCK);
    }
    if (context.pcaPlot) {
        if (components.length < 2) {
            monitor.showMessage(TaskMonitor.Level.ERROR, "Only found " + components.length + " components. Need 2 for scatterplot. " + "Perhaps minimum variance is set too high?");
            return;
        }
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                // System.out.println("Scatter plot dialog call");
                ScatterPlotDialog dialog = new ScatterPlotDialog(manager, "PCA", monitor, components, loadingMatrix, variance);
            }
        });
    }
}
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) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CySwingApplication(org.cytoscape.application.swing.CySwingApplication) ScatterPlotDialog(edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog) CytoPanel(org.cytoscape.application.swing.CytoPanel) Properties(java.util.Properties) CyServiceRegistrar(org.cytoscape.service.util.CyServiceRegistrar)

Example 20 with CyMatrix

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

the class RunPCA method computePCs.

public CyMatrix[] computePCs(CyMatrix matrix) /*, Matrix loadingMatrix*/
{
    // matrix.writeMatrix("output.txt");
    Matrix C;
    if (standardize) {
        for (int column = 0; column < matrix.nColumns(); column++) {
            matrix.ops().standardizeColumn(column);
        }
    }
    // System.out.println("centralizing columns");
    matrix.ops().centralizeColumns();
    if (matrixType.equals("correlation")) {
        // System.out.println("Creating correlation matrix");
        C = matrix.ops().correlation();
    } else {
        // Covariance
        // System.out.println("Creating covariance matrix");
        C = matrix.ops().covariance();
    }
    C.ops().eigenInit();
    // System.out.println("Finding eigenValues");
    eigenValues = C.ops().eigenValues(true);
    // System.out.println("Finding eigenVectors");
    eigenVectors = C.ops().eigenVectors();
    monitor.showMessage(TaskMonitor.Level.INFO, "Found " + eigenValues.length + " EigenValues");
    monitor.showMessage(TaskMonitor.Level.INFO, "Found " + eigenVectors.length + " EigenVectors of length " + eigenVectors[0].length);
    variance = computeVariance(eigenValues);
    CyMatrix[] components = new CyMatrix[variance.length];
    for (int j = eigenValues.length - 1, k = 0; j >= 0 && k < variance.length; j--, k++) {
        // double[] w = new double[vectors.length];
        // vector
        CyMatrix result = CyMatrixFactory.makeLargeMatrix(matrix.getNetwork(), eigenValues.length, 1);
        for (int i = 0; i < eigenVectors.length; i++) {
            result.setValue(i, 0, eigenVectors[i][j]);
        }
        Matrix mat = matrix.ops().multiplyMatrix(result);
        // System.out.println("After vector multiply: "+mat.printMatrixInfo());
        components[k] = matrix.copy(mat);
    }
    return components;
}
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) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)47 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 CyNode (org.cytoscape.model.CyNode)13 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)11 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)10 Test (org.junit.Test)10 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)9 List (java.util.List)8 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)6 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)6 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)4 CyTable (org.cytoscape.model.CyTable)4 ColtMatrix (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)3 BiclusterView (edu.ucsf.rbvi.clusterMaker2.internal.ui.BiclusterView)3 Map (java.util.Map)3 CyNetwork (org.cytoscape.model.CyNetwork)3 MedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer)2 ScatterPlotDialog (edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)2 CyEdge (org.cytoscape.model.CyEdge)2