Search in sources :

Example 21 with Matrix

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

the class RunHierarchical method pclcluster.

/**
 * The pclcluster routine performs clustering, using pairwise centroid-linking
 * on a given set of gene expression data, using the distrance metric given by metric.
 *
 * @param matrix the data matrix containing the data and labels
 * @param distanceMatrix the distances that will be used to actually do the clustering.
 * @param metric the distance metric to be used.
 * @return the array of TreeNode's that describe the hierarchical clustering solution, or null if
 * it it files for some reason.
 */
private TreeNode[] pclcluster(CyMatrix matrix, double[][] distanceMatrix, DistanceMetric metric) {
    int nRows = matrix.nRows();
    int nColumns = matrix.nColumns();
    int nNodes = nRows - 1;
    double[][] mask = new double[matrix.nRows()][matrix.nColumns()];
    TreeNode[] nodeList = new TreeNode[nNodes];
    // Initialize
    CyMatrix newData = matrix.copy();
    // System.out.println("New matrix: ");
    // newData.printMatrix();
    int[] distID = new int[nRows];
    for (int row = 0; row < nRows; row++) {
        distID[row] = row;
        for (int col = 0; col < nColumns; col++) {
            if (newData.hasValue(row, col))
                mask[row][col] = 1.0;
            else
                mask[row][col] = 0.0;
        }
        if (row < nNodes)
            nodeList[row] = new TreeNode(Double.MAX_VALUE);
    }
    int[] pair = new int[2];
    for (int inode = 0; inode < nNodes; inode++) {
        // find the pair with the shortest distance
        pair[IS] = 1;
        pair[JS] = 0;
        double distance = findClosestPair(nRows - inode, distanceMatrix, pair);
        nodeList[inode].setDistance(distance);
        int is = pair[IS];
        int js = pair[JS];
        nodeList[inode].setLeft(distID[js]);
        nodeList[inode].setRight(distID[is]);
        // make node js the new node
        for (int col = 0; col < nColumns; col++) {
            double jsValue = newData.doubleValue(js, col);
            double isValue = newData.doubleValue(is, col);
            double newValue = 0.0;
            if (newData.hasValue(js, col))
                newValue = jsValue * mask[js][col];
            if (newData.hasValue(is, col))
                newValue += isValue * mask[is][col];
            if (newData.hasValue(js, col) || newData.hasValue(is, col)) {
                newData.setValue(js, col, newValue);
            }
            mask[js][col] += mask[is][col];
            if (mask[js][col] != 0) {
                newData.setValue(js, col, newValue / mask[js][col]);
            }
        }
        for (int col = 0; col < nColumns; col++) {
            mask[is][col] = mask[nNodes - inode][col];
            newData.setValue(is, col, newData.getValue(nNodes - inode, col));
        }
        // Fix the distances
        distID[is] = distID[nNodes - inode];
        for (int i = 0; i < is; i++) {
            distanceMatrix[is][i] = distanceMatrix[nNodes - inode][i];
        }
        for (int i = is + 1; i < nNodes - inode; i++) {
            distanceMatrix[i][is] = distanceMatrix[nNodes - inode][i];
        }
        distID[js] = -inode - 1;
        for (int i = 0; i < js; i++) {
            distanceMatrix[js][i] = metric.getMetric(newData, newData, js, i);
        }
        for (int i = js + 1; i < nNodes - inode; i++) {
            distanceMatrix[i][js] = metric.getMetric(newData, newData, js, i);
        }
    }
    return nodeList;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 22 with Matrix

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

the class RunAutoSOME method getNodeClusters.

private Map<NodeCluster, NodeCluster> getNodeClusters(clusterRun cr, Map<String, Integer> key, CyMatrix matrix, Settings s) {
    Map<NodeCluster, NodeCluster> cMap = new HashMap<NodeCluster, NodeCluster>();
    attrList = new ArrayList<String>();
    attrOrderList = new ArrayList<String>();
    nodeOrderList = new ArrayList<String>();
    for (int i = 0; i < matrix.nColumns(); i++) attrOrderList.add(matrix.getColumnLabel(i));
    for (int i = 0; i < clusterCount; i++) {
        if (cr.c[i].ids.isEmpty())
            continue;
        NodeCluster nc = new NodeCluster();
        nc.setClusterNumber(i);
        for (int j = 0; j < cr.c[i].ids.size(); j++) {
            int dataID = cr.c[i].ids.get(j).intValue();
            int nodeDataID = key.get(matrix.getRowLabels()[dataID]).intValue();
            CyNode cn = nodes.get(nodeDataID);
            nc.add(cn);
            attrList.add(ModelUtils.getNodeName(network, cn) + "\t" + i);
            nodeOrderList.add(ModelUtils.getNodeName(network, cn));
        }
        cMap.put(nc, nc);
    }
    return cMap;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) HashMap(java.util.HashMap) CyNode(org.cytoscape.model.CyNode)

Example 23 with Matrix

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

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

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