Search in sources :

Example 16 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class InOut method writeCostMatrices.

public static void writeCostMatrices(Edges es, Vector<Vector<Integer>> clusters, HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins) throws IOException {
    Vector<Vector<Integer>> complete = new Vector<Vector<Integer>>();
    int countCostMatrices = 1;
    double percentOld = 0;
    double numberOfProteins = proteins2integers.size();
    double alreadySolvedNumberOfProteins = 0;
    for (int i = 0; i < clusters.size(); i++) {
        Vector<Integer> cluster = clusters.get(i);
        int numberEdges = ((cluster.size() * (cluster.size() - 1)) / 2);
        int numberProteins = cluster.size();
        alreadySolvedNumberOfProteins += numberProteins;
        int countRealEdges = 0;
        if (Config.reducedMatrix) {
            CostMatrix cm = new CostMatrix(numberProteins);
            HashMap<String, Integer> CmIntegers2proteins = new HashMap<String, Integer>(numberProteins);
            HashMap<Integer, String> CmProteins2integers = new HashMap<Integer, String>(numberProteins);
            for (int j = 0; j < cluster.size(); j++) {
                int source = cluster.get(j);
                String protein = proteins2integers.get(source);
                CmProteins2integers.put(j, protein);
                CmIntegers2proteins.put(protein, j);
                for (int k = j + 1; k < cluster.size(); k++) {
                    int target = cluster.get(k);
                    float value = getEdgeValue(source, target, es);
                    cm.setEdgevalues(j, k, value);
                    cm.setEdgevalues(k, j, value);
                    if (value > Config.threshold)
                        countRealEdges++;
                }
            }
            cm.setIntegers2proteins(CmIntegers2proteins);
            cm.setProteins2integers(CmProteins2integers);
            if (countRealEdges == numberEdges) {
                complete.add(cluster);
            } else {
                String costMatrixFile = "";
                if (TaskConfig.mode == TaskConfig.HIERARICHAL_MODE) {
                    costMatrixFile = "costMatrix_size_" + cluster.size() + "_nr_" + countCostMatrices + "_" + new Random().nextDouble() + ".rcm";
                } else {
                    costMatrixFile = "costMatrix_size_" + cluster.size() + "_nr_" + countCostMatrices + ".rcm";
                }
                CostMatrix mergedCM = cm.mergeNodes();
                mergedCM.writeCostMatrix(Config.costMatrixDirectory + "/" + costMatrixFile);
                countCostMatrices++;
            }
            double percent = Math.rint((((double) alreadySolvedNumberOfProteins) / ((double) numberOfProteins)) * 10000) / 100;
            if (percent > percentOld + 0.5 || percent == 100) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("Writing costmatrices  " + percent + "%");
                }
            // else  System.out.print( percent + "%\t");
            }
            for (int j = 0; j < cluster.size(); j++) {
                String id = proteins2integers.get(cluster.get(j));
                integers2proteins.remove(id);
            }
        } else {
            int countPosition = 0;
            Edges edges = new Edges(numberEdges, numberProteins);
            // create edges for connected components
            for (int j = 0; j < cluster.size(); j++) {
                int source = cluster.get(j);
                edges.setStartPosition(j, countPosition);
                for (int k = j + 1; k < cluster.size(); k++) {
                    int target = cluster.get(k);
                    float value = getEdgeValue(source, target, es);
                    if (value > Config.threshold) {
                        countRealEdges++;
                    }
                    edges.setSource(countPosition, j);
                    edges.setTarget(countPosition, k);
                    edges.setValue(countPosition, value);
                    countPosition++;
                }
                edges.setEndPosition(j, countPosition - 1);
            }
            // divide between complete and incomplete connected components
            if (countRealEdges == numberEdges) {
                complete.add(cluster);
            } else {
                writeCostMatrix(edges, cluster, countCostMatrices, proteins2integers, integers2proteins);
                countCostMatrices++;
            }
            double percent = Math.rint((((double) alreadySolvedNumberOfProteins) / ((double) numberOfProteins)) * 10000) / 100;
            if (percent > percentOld + 0.5 || percent == 100) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("Writing costmatrices  " + percent + "%");
                }
            // else  System.out.print( percent + "%\t");
            }
            // remove proteins from list which are assigned to one cluster
            for (int j = 0; j < cluster.size(); j++) {
                String id = proteins2integers.get(cluster.get(j));
                integers2proteins.remove(id);
            }
        }
    }
    for (Iterator<String> iter = integers2proteins.keySet().iterator(); iter.hasNext(); ) {
        String element = iter.next();
        int id = integers2proteins.get(element);
        Vector<Integer> cluster = new Vector<Integer>();
        cluster.add(id);
        complete.add(cluster);
    }
    writeCompleteTable(complete, proteins2integers);
}
Also used : HashMap(java.util.HashMap) Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) Random(java.util.Random) Vector(java.util.Vector) CostMatrix(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.CostMatrix)

Example 17 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class CyMatrixFactory method makeLargeMatrix.

/**
 * Create a large, possibly sparse matrix populated with data from
 * the indicated edge attribute
 *
 * @param network the network that will be the source of the data
 * @param edgeAttribute the edge attribute to pull the data from
 * @param selectedOnly only include selected edges
 * @param converter the edge weight converter to use
 * @param unDirected if true, the edges are undirected
 * @param cutOff the minimum edge value to consider
 * @return the resulting matrix
 */
public static CyMatrix makeLargeMatrix(CyNetwork network, String edgeAttribute, boolean selectedOnly, EdgeWeightConverter converter, boolean unDirected, double cutOff) {
    List<CyNode> nodes;
    List<CyEdge> edges;
    double maxAttribute = Double.MIN_VALUE;
    double minAttribute = Double.MAX_VALUE;
    if (!selectedOnly) {
        nodes = network.getNodeList();
        edges = network.getEdgeList();
    } else {
        nodes = new ArrayList<CyNode>();
        edges = new ArrayList<CyEdge>();
        nodes.addAll(CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true));
        edges.addAll(ModelUtils.getConnectingEdges(network, nodes));
    }
    CyMatrix matrix = makeTypedMatrix(network, nodes.size(), nodes.size(), false, MatrixType.LARGE);
    matrix.setRowNodes(nodes);
    matrix.setColumnNodes(nodes);
    Map<CyNode, Integer> nodeMap = new HashMap<CyNode, Integer>(nodes.size());
    for (int row = 0; row < nodes.size(); row++) {
        CyNode node = nodes.get(row);
        nodeMap.put(node, row);
        matrix.setRowLabel(row, ModelUtils.getNodeName(network, node));
        matrix.setColumnLabel(row, ModelUtils.getNodeName(network, node));
    }
    matrix.setSymmetrical(unDirected);
    CyTable edgeAttributes = network.getDefaultEdgeTable();
    // First, we need the min and max values for our converter
    if (edgeAttributes.getColumn(edgeAttribute) == null) {
        minAttribute = 1.0;
        maxAttribute = 1.0;
    } else {
        for (CyEdge edge : edges) {
            if (network.getRow(edge).getRaw(edgeAttribute) == null)
                continue;
            double edgeWeight = ModelUtils.getNumericValue(network, edge, edgeAttribute);
            if (edgeWeight < cutOff)
                continue;
            minAttribute = Math.min(minAttribute, edgeWeight);
            maxAttribute = Math.max(maxAttribute, edgeWeight);
        }
    }
    for (CyEdge edge : edges) {
        double value;
        if (minAttribute == 1.0 && maxAttribute == 1.0) {
            value = 1.0;
        } else {
            Double val = ModelUtils.getNumericValue(network, edge, edgeAttribute);
            if (val == null)
                continue;
            value = val.doubleValue();
        }
        double weight = converter.convert(value, minAttribute, maxAttribute);
        if (weight < cutOff)
            continue;
        int sourceIndex = nodeMap.get(edge.getSource());
        int targetIndex = nodeMap.get(edge.getTarget());
        matrix.setValue(targetIndex, sourceIndex, weight);
        // TODO: should we consider maybe doing this on the getValue side?
        if (unDirected)
            matrix.setValue(sourceIndex, targetIndex, weight);
    }
    // System.out.println("distance matrix: "+matrix.printMatrix());
    return matrix;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode)

Example 18 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class DensityFilter method doFilter.

public NodeCluster doFilter(List<CyNode> nodeList, Map<NodeCluster, List<CyNode>> addedNodeMap) {
    // Get the number of edges within the cluster
    List<CyEdge> edgeList = ModelUtils.getConnectingEdges(network, nodeList);
    if (edgeList == null)
        return null;
    int edgeCount = edgeList.size();
    int nodeCount = nodeList.size();
    // Calculate the density
    double density = (double) (edgeCount * 2) / (double) (nodeCount * (nodeCount - 1));
    if (density >= context.minimumDensity)
        return new NodeCluster(nodeList);
    return null;
}
Also used : NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) CyEdge(org.cytoscape.model.CyEdge)

Example 19 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class NewNetworkView method createClusteredNetwork.

@SuppressWarnings("unchecked")
private void createClusteredNetwork(String clusterAttribute, TaskMonitor monitor) {
    boolean isFuzzy = isFuzzy(clusterAttribute);
    // System.out.println("isFuzzy = "+isFuzzy);
    // Get the clustering parameters
    Map<String, String> params = getParams();
    List<CyNode> nodeList = new ArrayList<CyNode>();
    Map<Integer, List<CyNode>> clusterMap = getClusterMap(clusterAttribute, nodeList);
    // Special handling for edge weight thresholds
    EdgeWeightConverter converter = edgeConverterList.getConverter(getParam(params, "converter"));
    String dataAttribute = getParam(params, "dataAttribute");
    double cutOff = 0.0;
    if (getParam(params, "edgeCutOff") != null)
        cutOff = Double.parseDouble(getParam(params, "edgeCutOff"));
    HashMap<CyEdge, CyEdge> edgeMap = new HashMap<CyEdge, CyEdge>();
    List<CyEdge> edgeList = new ArrayList<CyEdge>();
    // System.out.println("Getting the edges");
    for (Integer cluster : clusterMap.keySet()) {
        // Get the list of nodes
        List<CyNode> clusterNodes = clusterMap.get(cluster);
        // Get the list of edges
        List<CyEdge> connectingEdges = ModelUtils.getConnectingEdges(network, clusterNodes);
        for (CyEdge edge : connectingEdges) {
            if (converter != null && dataAttribute != null) {
                if (edgeWeightCheck(edge, dataAttribute, converter, cutOff))
                    continue;
            }
            edgeMap.put(edge, edge);
            // Add the cluster attribute to the edge so we can style it later
            ModelUtils.createAndSetLocal(network, edge, clusterAttribute, new Integer(1), Integer.class, null);
            edgeList.add(edge);
        }
    }
    // System.out.println("Getting the style");
    VisualStyle style = ViewUtils.getCurrentVisualStyle(manager);
    // System.out.println("Creating the network");
    CyNetwork newNetwork = ModelUtils.createChildNetwork(manager, network, nodeList, edgeList, "--clustered");
    // Now, copy the cluster attribute from the original network to this one
    ModelUtils.copyLocalColumn(network, newNetwork, CyNode.class, clusterAttribute);
    // Copy the clustering information over
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterType");
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterAttribute");
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterParams");
    // Finally, if we're fuzzy, see if we had an initial seed and copy that over
    if (isFuzzy && ModelUtils.hasAttribute(network, network, "__fuzzifierSeed")) {
        ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__fuzzifierSeed");
        String seedAttribute = network.getRow(network, CyNetwork.LOCAL_ATTRS).get("__fuzzifierSeed", String.class);
        ModelUtils.copyLocalColumn(network, newNetwork, CyNode.class, seedAttribute);
    }
    // System.out.println("Getting the view");
    networkView = ViewUtils.createView(manager, newNetwork, false);
    ViewUtils.doLayout(manager, networkView, monitor, "force-directed");
    // Now, if we're supposed to, restore the inter-cluster edges
    if (restoreEdges || (context != null && context.restoreEdges)) {
        for (CyEdge edge : network.getEdgeList()) {
            if (!edgeMap.containsKey(edge)) {
                ((CySubNetwork) networkView.getModel()).addEdge(edge);
                ModelUtils.createAndSetLocal(networkView.getModel(), edge, clusterAttribute, new Integer(0), Integer.class, null);
            }
        }
        style = styleNewView(style, clusterAttribute);
    }
    // System.out.println("Setting the style");
    ViewUtils.setVisualStyle(manager, networkView, style);
    if (isFuzzy) {
        long fuzzyClusterTableSUID = network.getRow(network).get(clusterAttribute + "_Table.SUID", Long.class);
        newNetwork.getRow(newNetwork).set(clusterAttribute + "_Table.SUID", fuzzyClusterTableSUID);
        // System.out.println("NetworkName: "+ network.getRow(network).get(CyNetwork.NAME, String.class));
        // System.out.println("Fuzzy Table SUID: " + fuzzyClusterTableSUID );
        CyTable fuzzyClusterTable = manager.getTableManager().getTable(fuzzyClusterTableSUID);
        // System.out.println("Creating membership edges");
        createMembershipEdges(newNetwork, networkView, manager, fuzzyClusterTable);
    }
    ViewUtils.registerView(manager, networkView);
    return;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) EdgeWeightConverter(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Aggregations

CyNode (org.cytoscape.model.CyNode)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 CyEdge (org.cytoscape.model.CyEdge)8 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)6 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)4 List (java.util.List)4 Vector (java.util.Vector)4 CyNetwork (org.cytoscape.model.CyNetwork)4 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)3 Random (java.util.Random)3 Semaphore (java.util.concurrent.Semaphore)3 CyNetworkView (org.cytoscape.view.model.CyNetworkView)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 Point (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.autosome.cluststruct.Point)2 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)2 ICCEdges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ICCEdges)2 ClusteringManager (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager)2 HeaderInfo (edu.ucsf.rbvi.clusterMaker2.internal.treeview.HeaderInfo)2 Hashtable (java.util.Hashtable)2