Search in sources :

Example 1 with DistanceMetric

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

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

the class KMedoidCluster 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 && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
        return;
    }
    createGroups = context.createGroups;
    if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
        // To make debugging easier, sort the attribute list
        Collections.sort(nodeAttributeList);
    }
    // Get our attributes we're going to use for the cluster
    String[] attributeArray;
    if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
        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.setStatusMessage("Initializing");
    // System.out.println("Initializing");
    resetAttributes(network, SHORTNAME);
    // Create a new clusterer
    RunKMedoidCluster algorithm = new RunKMedoidCluster(network, attributeArray, distanceMetric, monitor, context, this);
    // System.out.println("Algorithm defined");
    String resultsString = "K-Medoid results:";
    // Cluster the attributes, if requested
    if (context.clusterAttributes && attributeArray.length > 1) {
        monitor.setStatusMessage("Clustering attributes");
        // System.out.println("Clustering attributes");
        Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, context.iterations, true, SHORTNAME, context.kcluster, false);
        attributeList = algorithm.getAttributeList();
        updateAttributes(network, SHORTNAME, rowOrder, attributeArray, attributeList, algorithm.getMatrix());
        attributeSilhouette = algorithm.getSilhouettes();
        attributeOrder = getOrder(rowOrder, algorithm.getMatrix());
    }
    // Cluster the nodes
    monitor.setStatusMessage("Clustering nodes");
    // System.out.println("Clustering nodes");
    Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, context.iterations, false, SHORTNAME, context.kcluster, createGroups);
    nodeList = algorithm.getAttributeList();
    updateAttributes(network, SHORTNAME, rowOrder, attributeArray, algorithm.getAttributeList(), algorithm.getMatrix());
    nodeSilhouette = algorithm.getSilhouettes();
    nodeOrder = getOrder(rowOrder, algorithm.getMatrix());
    // System.out.println(resultsString);
    if (context.showUI) {
        insertTasksAfterCurrentTask(new KnnView(clusterManager));
    }
}
Also used : KnnView(edu.ucsf.rbvi.clusterMaker2.internal.ui.KnnView)

Example 3 with DistanceMetric

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

Example 4 with DistanceMetric

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

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

the class FFT 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 && CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true).size() < 3) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Must have at least three nodes to cluster");
        return;
    }
    createGroups = context.createGroups;
    if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
        // To make debugging easier, sort the attribute list
        Collections.sort(nodeAttributeList);
    }
    // Get our attributes we're going to use for the cluster
    String[] attributeArray;
    if (nodeAttributeList != null && nodeAttributeList.size() > 0) {
        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.setStatusMessage("Initializing");
    resetAttributes(network, SHORTNAME);
    // Create a new clusterer
    RunFFT algorithm = new RunFFT(network, attributeArray, distanceMetric, monitor, context, this);
    // System.out.println("Algorithm defined");
    String resultsString = "FFT results:";
    // Cluster the attributes, if requested
    if (context.clusterAttributes && attributeArray.length > 1) {
        monitor.setStatusMessage("Clustering attributes");
        Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, 1, true, "fft", context.kcluster, false);
        updateAttributes(network, SHORTNAME, rowOrder, attributeArray, algorithm.getAttributeList(), algorithm.getMatrix());
    }
    // Cluster the nodes
    monitor.setStatusMessage("Clustering nodes");
    Integer[] rowOrder = algorithm.cluster(clusterManager, context.kcluster.kNumber, 1, false, "fft", context.kcluster, createGroups);
    updateAttributes(network, SHORTNAME, rowOrder, attributeArray, algorithm.getAttributeList(), algorithm.getMatrix());
    // System.out.println(resultsString);
    if (context.showUI) {
        insertTasksAfterCurrentTask(new KnnView(clusterManager));
    }
}
Also used : KnnView(edu.ucsf.rbvi.clusterMaker2.internal.ui.KnnView)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)7 KnnView (edu.ucsf.rbvi.clusterMaker2.internal.ui.KnnView)4 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)3 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)2 DistanceMetric (edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)2 HopachablePAM (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.pam.HopachablePAM)1 MeanSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MeanSummarizer)1 MedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.MedianSummarizer)1 PrimitiveMeanSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMeanSummarizer)1 PrimitiveMedianSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveMedianSummarizer)1 PrimitiveSummarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.PrimitiveSummarizer)1 Summarizer (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.numeric.Summarizer)1 TreeView (edu.ucsf.rbvi.clusterMaker2.internal.ui.TreeView)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 CyNode (org.cytoscape.model.CyNode)1