Search in sources :

Example 26 with CyMatrix

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

the class ChengChurch 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();
    clusterAttributeName = "CnC_Bicluster";
    if (network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().getColumn(ClusterManager.CLUSTER_ATTRIBUTE) == null) {
        network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().createColumn(ClusterManager.CLUSTER_ATTRIBUTE, String.class, false);
    }
    network.getRow(network, CyNetwork.LOCAL_ATTRS).set(ClusterManager.CLUSTER_ATTRIBUTE, clusterAttributeName);
    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
    RunChengChurch algorithm = new RunChengChurch(network, attributeArray, monitor, context);
    String resultsString = "ChengChurch results:";
    // Cluster the nodes
    monitor.setStatusMessage("Clustering nodes");
    Integer[] rowOrder = algorithm.cluster(false);
    CyMatrix biclusterMatrix = algorithm.getBiclusterMatrix();
    createGroups(network, biclusterMatrix, context.nClusters, algorithm.getRowClustersArray(), SHORTNAME);
    updateAttributes(network, SHORTNAME, rowOrder, attributeArray, getAttributeList(), algorithm.getBiclusterMatrix());
    // Build our attribute clustesrs
    List<String> arrayList = buildClusterHeaders(algorithm.getColClustersArray(), algorithm.getBiclusterMatrix(), true);
    ModelUtils.createAndSetLocal(network, network, ClusterManager.CLUSTER_ATTR_ATTRIBUTE, arrayList, List.class, String.class);
    updateParams(network, context.getParams());
    // System.out.println(resultsString);
    if (context.showUI) {
        insertTasksAfterCurrentTask(new BiclusterView(clusterManager));
    }
}
Also used : BiclusterView(edu.ucsf.rbvi.clusterMaker2.internal.ui.BiclusterView) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 27 with CyMatrix

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

the class BiMine 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();
    clusterAttributeName = "BiMine_Bicluster";
    if (network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().getColumn(ClusterManager.CLUSTER_ATTRIBUTE) == null) {
        network.getRow(network, CyNetwork.LOCAL_ATTRS).getTable().createColumn(ClusterManager.CLUSTER_ATTRIBUTE, String.class, false);
    }
    network.getRow(network, CyNetwork.LOCAL_ATTRS).set(ClusterManager.CLUSTER_ATTRIBUTE, clusterAttributeName);
    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
    RunBiMine algorithm = new RunBiMine(network, attributeArray, monitor, context);
    String resultsString = "BiMine results:";
    // Cluster the nodes
    monitor.setStatusMessage("Clustering nodes");
    Integer[] rowOrder = algorithm.cluster(false);
    CyMatrix biclusterMatrix = algorithm.getBiclusterMatrix();
    int[] clusters = new int[biclusterMatrix.nRows()];
    createGroups(network, biclusterMatrix, 1, clusters, "bimine");
    updateAttributes(network, SHORTNAME, rowOrder, attributeArray, getAttributeList(), algorithm.getBiclusterMatrix());
    createBiclusterTable(algorithm.getClusterNodes(), algorithm.getClusterAttrs());
    // System.out.println(resultsString);
    if (context.showUI) {
        insertTasksAfterCurrentTask(new BiclusterView(clusterManager));
    }
}
Also used : BiclusterView(edu.ucsf.rbvi.clusterMaker2.internal.ui.BiclusterView) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 28 with CyMatrix

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

Example 29 with CyMatrix

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

the class RunFFT method kcluster.

@Override
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterID) {
    random = null;
    int nelements = matrix.nRows();
    int[] tclusterid = new int[nelements];
    int[] saved = new int[nelements];
    int[] mapping = new int[nClusters];
    int[] counts = new int[nClusters];
    CyMatrix distanceMatrix = matrix.getDistanceMatrix(metric);
    HashMap<Integer, Integer> centers = new HashMap<Integer, Integer>();
    double error = Double.MAX_VALUE;
    if (monitor != null)
        monitor.setProgress(0);
    for (int i = 0; i < nelements; i++) clusterID[i] = 0;
    // the first center
    Random randomGenerator = new Random();
    centers.put(0, randomGenerator.nextInt(nelements));
    // now find the remaining centers
    for (int i = 1; i < nClusters; i++) {
        int y = getMaxMin(centers, distanceMatrix);
        centers.put(i, y);
        clusterID[y] = i;
    }
    /*
		for (int i = 0; i < nClusters; i++)
			System.out.printf("Center for %d = %d\n", i, centers.get(i));
		*/
    // assign clusters now
    int k = centers.get(0);
    for (int i = 0; i < nelements; i++) {
        // Is this one of our centers?
        if (centers.containsValue(i))
            continue;
        double minDistance = Double.MAX_VALUE;
        int center = 0;
        for (int cluster = 0; cluster < nClusters; cluster++) {
            double dist = distanceMatrix.doubleValue(i, centers.get(cluster));
            if (dist < minDistance) {
                center = cluster;
                minDistance = dist;
            }
        }
        clusterID[i] = center;
    }
    return nClusters;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Random(java.util.Random) HashMap(java.util.HashMap)

Example 30 with CyMatrix

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

the class RunAutoSOME method run.

public List<NodeCluster> run(TaskMonitor monitor) {
    long startTime = System.currentTimeMillis();
    debugln("Initial matrix:");
    // printMatrix(matrix);
    // Normalize
    // normalize(matrix, clusteringThresh, false);
    debugln("Normalized matrix:");
    if (dataAttributes == null || dataAttributes.isEmpty()) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
        return null;
    }
    if (selectedOnly && network.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED, true) == 0) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no nodes selected from network");
        return null;
    }
    Settings s = new Settings();
    // get parameter settings
    s.ensemble_runs = settings.ensemble_runs;
    s.mst_pval = settings.mst_pval;
    s.threads = settings.threads;
    s.logNorm = settings.logNorm;
    s.unitVar = settings.unitVar;
    s.medCenter = settings.medCenter;
    s.medCenterCol = settings.medCenterCol;
    s.sumSqrRows = settings.sumSqrRows;
    s.sumSqrCol = settings.sumSqrCol;
    s.som_iters = settings.som_iters;
    s.de_resolution = settings.de_resolution;
    s.distMatrix = settings.distMatrix;
    s.dmDist = settings.dmDist;
    s.FCNrows = settings.FCNrows;
    s.som_maxGrid = 20;
    s.htmlOut = false;
    s.textOut = false;
    // System.out.println("network "+network.toString()+" has "+network.getNodeCount()+" nodes");
    // System.out.println("attributes: "+dataAttributes);
    String[] attrArray = new String[dataAttributes.size()];
    int att = 0;
    for (String attribute : dataAttributes) {
        attrArray[att++] = "node." + attribute;
    }
    // Create the matrix
    CyMatrix matrix = CyMatrixFactory.makeSmallMatrix(network, attrArray, selectedOnly, ignoreMissing, false, false);
    if (!selectedOnly) {
        nodes = network.getNodeList();
    } else {
        nodes = CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true);
    }
    s.input = new dataItem[matrix.nRows()];
    // matrix.printMatrix();
    Map<String, Integer> key = new HashMap<String, Integer>();
    for (int i = 0; i < nodes.size(); i++) {
        String id = ModelUtils.getNodeName(network, nodes.get(i));
        if (!key.containsKey(id))
            key.put(id, i);
    }
    for (int k = 0, itor = 0; k < matrix.nRows(); k++) {
        float[] f = new float[matrix.nColumns()];
        // System.out.println(matrix.getRowLabels()[k]+" "+nodes.get(k).getIdentifier());
        if (k == 0) {
            s.columnHeaders = new String[f.length + 1];
            s.columnHeaders[0] = "NAME";
        }
        for (int l = 0; l < f.length; l++) {
            if (k == 0) {
                s.columnHeaders[l + 1] = matrix.getColumnLabel(l);
                s.columnHeaders[l + 1] = s.columnHeaders[l + 1].replace("\"", "");
                s.columnHeaders[l + 1] = s.columnHeaders[l + 1].replace(",", "");
            // System.out.println(s.columnHeaders[l+1]);
            }
            // System.out.println(matrix.getValue(k,l).floatValue());
            if (matrix.getValue(k, l) != null) {
                f[l] = matrix.getValue(k, l).floatValue();
            } else {
                f[l] = -99999999;
                s.fillMissing = true;
            }
        }
        s.input[itor++] = new dataItem(f, matrix.getRowLabel(k));
    }
    if (s.FCNrows && s.distMatrix)
        s = transpose(s);
    if (s.input == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
        return null;
    } else if (s.input.length < 2) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
        return null;
    } else if (s.input[0].getValues() == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
        return null;
    } else if (s.input[0].getValues().length < 2) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
        return null;
    }
    autRun = new Run();
    cr = autRun.runAutoSOMEBasic(s, monitor);
    if (cr == null) {
        monitor.setStatusMessage("Clustering failed!");
        return null;
    }
    monitor.setStatusMessage("Assigning nodes to clusters");
    clusterCount = cr.c.length;
    Map<NodeCluster, NodeCluster> cMap = (!s.distMatrix) ? getNodeClusters(cr, key, matrix, s) : getNodeClustersFCN(cr, matrix, s);
    if (canceled) {
        monitor.setStatusMessage("canceled");
        return null;
    }
    // Update node attributes in network to include clusters. Create cygroups from clustered nodes
    monitor.setStatusMessage("Created " + clusterCount + " clusters");
    // 
    if (clusterCount == 0) {
        monitor.showMessage(TaskMonitor.Level.WARN, "Created 0 clusters!!!!");
        return null;
    }
    Set<NodeCluster> clusters = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters);
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)

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