Search in sources :

Example 1 with CyMatrix

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

the class CyMatrixFactory method makeMatrix.

public static CyMatrix makeMatrix(CyNetwork network, String[] attributes, boolean selectedOnly, boolean ignoreMissing, boolean transpose, boolean assymetric, MatrixType type) {
    // Create our local copy of the weightAtributes array
    String[] attributeArray = new String[attributes.length];
    if (attributes.length >= 1 && attributes[0].startsWith("node.")) {
        // Get rid of the leading type information
        for (int i = 0; i < attributes.length; i++) {
            attributeArray[i] = attributes[i].substring(5);
        }
        List<CyNode> nodeList = ModelUtils.getSortedNodeList(network, selectedOnly);
        Map<CyNode, Map<String, Double>> nodeCondMap = getNodeCondMap(network, nodeList, attributeArray, ignoreMissing);
        CyMatrix matrix = makeTypedMatrix(network, nodeCondMap.size(), attributeArray.length, transpose, type);
        matrix.setAssymetricalEdge(false);
        return makeAttributeMatrix(network, matrix, nodeList, nodeCondMap, attributeArray, transpose);
    } else if (attributes.length == 1 && attributes[0].startsWith("edge.")) {
        String weight = attributes[0].substring(5);
        if (!assymetric) {
            // Get the list of nodes and edges of interest
            List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
            List<CyNode> nodeList = getNodesFromEdges(network, edgeList, weight, ignoreMissing);
            Collections.sort(nodeList, new CyIdentifiableNameComparator(network));
            CyMatrix matrix = makeTypedMatrix(network, nodeList.size(), nodeList.size(), false, type);
            matrix.setAssymetricalEdge(false);
            return makeSymmetricalMatrix(network, matrix, nodeList, edgeList, weight);
        } else {
            List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
            List<CyNode> targetNodeList = new ArrayList<CyNode>();
            List<CyNode> sourceNodeList = getNodesFromEdges(network, edgeList, targetNodeList, weight, ignoreMissing);
            Collections.sort(targetNodeList, new CyIdentifiableNameComparator(network));
            Collections.sort(sourceNodeList, new CyIdentifiableNameComparator(network));
            CyMatrix matrix = makeTypedMatrix(network, sourceNodeList.size(), targetNodeList.size(), false, type);
            matrix.setAssymetricalEdge(true);
            return makeAssymmetricalMatrix(network, matrix, sourceNodeList, targetNodeList, edgeList, weight);
        }
    }
    return null;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CyIdentifiableNameComparator(edu.ucsf.rbvi.clusterMaker2.internal.utils.CyIdentifiableNameComparator) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CyMatrix

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

the class RunFCM method run.

/**
 * The method run has the actual implementation of the fuzzy c-means code
 * @param monitor, Task monitor for the process
 * @return List of FuzzyNodeCLusters
 */
public List<FuzzyNodeCluster> run(CyNetwork network, TaskMonitor monitor, int[] mostRelevantCluster) {
    Long networkID = network.getSUID();
    CyTable netAttributes = network.getDefaultNetworkTable();
    CyTable nodeAttributes = network.getDefaultNodeTable();
    long startTime = System.currentTimeMillis();
    random = null;
    int nelements = distanceMatrix.nRows();
    // Matrix to store the temporary cluster membership values of elements
    double[][] tClusterMemberships = new double[nelements][number_clusters];
    // Initializing all membership values to 0
    for (int i = 0; i < nelements; i++) {
        for (int j = 0; j < number_clusters; j++) {
            tClusterMemberships[i][j] = 0;
        }
    }
    // Matrix to store cluster memberships of the previous iteration
    double[][] prevClusterMemberships = new double[nelements][number_clusters];
    // This matrix will store the centroid data
    CyMatrix cData = CyMatrixFactory.makeSmallMatrix(network, number_clusters, nelements);
    int iteration = 0;
    boolean end = false;
    do {
        if (monitor != null)
            monitor.setProgress(((double) iteration / (double) number_iterations));
        // Initializing the membership values by randomly assigning a cluster to each element
        if (iteration == 0 && number_iterations != 0) {
            randomAssign(tClusterMemberships);
            prevClusterMemberships = tClusterMemberships;
            // Find the centers
            getFuzzyCenters(cData, tClusterMemberships);
        }
        // Calculate Fuzzy Memberships
        getClusterMemberships(cData, tClusterMemberships);
        // Now calculate the new fuzzy centers
        getFuzzyCenters(cData, tClusterMemberships);
        end = checkEndCriterion(tClusterMemberships, prevClusterMemberships);
        if (end) {
            break;
        }
    } while (++iteration < number_iterations);
    HashMap<CyNode, double[]> membershipMap = createMembershipMap(tClusterMemberships);
    List<FuzzyNodeCluster> fuzzyClusters = new ArrayList<FuzzyNodeCluster>();
    List<CyNode> clusterNodes = distanceMatrix.getRowNodes();
    // List<CyNode> fuzzyNodeList;
    for (int i = 0; i < number_clusters; i++) {
        List<CyNode> fuzzyNodeList = new ArrayList<CyNode>();
        HashMap<CyNode, Double> clusterMembershipMap = new HashMap<CyNode, Double>();
        for (CyNode node : clusterNodes) {
            if (membershipMap.get(node)[i] > membershipThreshold) {
                fuzzyNodeList.add(node);
                clusterMembershipMap.put(node, membershipMap.get(node)[i]);
            }
        }
        fuzzyClusters.add(new FuzzyNodeCluster(fuzzyNodeList, clusterMembershipMap));
    }
    // Setting up the most relevant cluster per node
    for (int i = 0; i < nelements; i++) {
        // List tempMemList = Arrays.asList(ArrayUtils.toObject(tClusterMemberships[i]));
        int maxIndex = 0;
        for (int j = 1; j < number_clusters; j++) {
            if (tClusterMemberships[i][j] > tClusterMemberships[i][maxIndex]) {
                maxIndex = j;
            }
        }
        mostRelevantCluster[i] = maxIndex + 1;
    }
    clusterMemberships = tClusterMemberships;
    return fuzzyClusters;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)

Example 3 with CyMatrix

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

the class Fuzzifier method run.

/**
 * The method run creates an instance of the RunFuzzifier and creates the fuzzy clusters
 * by calling the fuzzifier algorithm.
 * Also creates fuzzy groups and the Fuzzy Cluster Table
 *
 * @param Task Monitor
 */
public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing Fuzzifier clustering");
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    super.network = network;
    this.Clusters = getClusters();
    this.cNumber = Clusters.size();
    // Make sure to update the context
    context.setNetwork(network);
    Long networkID = network.getSUID();
    CyTable nodeAttributes = network.getDefaultNodeTable();
    CyMatrix distanceMatrix = context.edgeAttributeHandler.getMatrix();
    if (distanceMatrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    // Update our tunable results
    clusterAttributeName = context.getClusterAttribute();
    runFuzzifier = new RunFuzzifier(Clusters, distanceMatrix, cNumber, context.membershipThreshold.getValue(), context.maxThreads, monitor);
    runFuzzifier.setDebug(debug);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<FuzzyNodeCluster> FuzzyClusters = runFuzzifier.run(network, monitor);
    // Canceled?
    if (FuzzyClusters == null)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
    // Remove any leftover groups from previous runs
    removeGroups(network, GROUP_ATTRIBUTE);
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
    params = new ArrayList<String>();
    context.edgeAttributeHandler.setParams(params);
    List<List<CyNode>> nodeClusters = createFuzzyGroups(network, FuzzyClusters, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, FuzzyClusters);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  Fuzzifier results:\n" + results);
    if (context.vizProperties.showUI) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.edgeAttributeHandler.selectedOnly));
    } else {
        monitor.showMessage(TaskMonitor.Level.INFO, "Done.  Fizzifier results:\n" + results);
    }
    System.out.println("Creating fuzzy table");
    createFuzzyTable(FuzzyClusters);
    System.out.println("Done");
}
Also used : CyTable(org.cytoscape.model.CyTable) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)

Example 4 with CyMatrix

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

the class RunFuzzifier method run.

/**
 * The method run has the actual implementation of the fuzzifier code
 * @param monitor, Task monitor for the process
 * @return List of FuzzyNodeCLusters
 */
public List<FuzzyNodeCluster> run(CyNetwork network, TaskMonitor monitor) {
    Long networkID = network.getSUID();
    long startTime = System.currentTimeMillis();
    int nelements = distanceMatrix.nRows();
    nodeList = distanceMatrix.getRowNodes();
    // Matrix to store the temporary cluster membership values of elements
    double[][] ClusterMemberships = new double[nelements][number_clusters];
    // Initializing all membership values to 0
    for (int i = 0; i < nelements; i++) {
        for (int j = 0; j < number_clusters; j++) {
            ClusterMemberships[i][j] = 0;
        }
    }
    // This matrix will store the centroid data
    CyMatrix cData = CyMatrixFactory.makeSmallMatrix(network, number_clusters, nelements);
    getFuzzyCenters(cData);
    for (CyNode node : nodeList) {
        int nodeIndex = nodeList.indexOf(node);
        double sumDistances = 0;
        for (int i = 0; i < Clusters.size(); i++) {
            sumDistances += cData.doubleValue(i, nodeIndex);
        }
        for (int i = 0; i < Clusters.size(); i++) {
            ClusterMemberships[nodeIndex][i] = cData.doubleValue(i, nodeIndex) / sumDistances;
        }
    }
    HashMap<CyNode, double[]> membershipMap = createMembershipMap(ClusterMemberships);
    List<FuzzyNodeCluster> fuzzyClusters = new ArrayList<FuzzyNodeCluster>();
    // Adding the nodes which have memberships greater than the threshold to fuzzy node clusters
    List<CyNode> fuzzyNodeList;
    for (int i = 0; i < number_clusters; i++) {
        fuzzyNodeList = new ArrayList<CyNode>();
        HashMap<CyNode, Double> clusterMembershipMap = new HashMap<CyNode, Double>();
        for (CyNode node : nodeList) {
            if (membershipMap.get(node)[i] > membershipThreshold) {
                fuzzyNodeList.add(node);
                clusterMembershipMap.put(node, membershipMap.get(node)[i]);
            }
        }
        fuzzyClusters.add(new FuzzyNodeCluster(fuzzyNodeList, clusterMembershipMap));
    }
    return fuzzyClusters;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)

Example 5 with CyMatrix

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

the class SCPSCluster method run.

public void run(TaskMonitor monitor) {
    monitor.setTitle("Performing SCPS cluster");
    this.monitor = monitor;
    if (network == null)
        network = clusterManager.getNetwork();
    // Make sure to update the context
    context.setNetwork(network);
    NodeCluster.init();
    CyMatrix matrix = context.edgeAttributeHandler.getMatrix();
    if (matrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    // Update our tunable results
    clusterAttributeName = context.getClusterAttribute();
    createGroups = context.advancedAttributes.createGroups;
    if (canceled)
        return;
    // Cluster the nodes
    runSCPS = new RunSCPS(matrix, context.epsilon, context.clusters, context.iterations, monitor);
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<NodeCluster> clusterList = runSCPS.run(network, monitor);
    // Canceled?
    if (clusterList == null)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
    // Remove any leftover groups from previous runs
    removeGroups(network, GROUP_ATTRIBUTE);
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
    params = new ArrayList<String>();
    context.edgeAttributeHandler.setParams(params);
    List<List<CyNode>> nodeClusters = createGroups(network, clusterList, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusterList);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  SCPS results:\n" + results);
    if (context.vizProperties.showUI) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !context.edgeAttributeHandler.selectedOnly));
    }
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)

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