Search in sources :

Example 1 with EdgeWeightConverter

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter 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 2 with EdgeWeightConverter

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter in project clusterMaker2 by RBVI.

the class EdgeAttributeHandler method initializeTunables.

public void initializeTunables() {
    attribute = ModelUtils.updateEdgeAttributeList(network, attribute);
    EdgeWeightConverter[] edgeWeightConverters = converters.toArray(new EdgeWeightConverter[1]);
    if (edgeWeightConverters.length > 0) {
        edgeWeighter = new ListSingleSelection<EdgeWeightConverter>(edgeWeightConverters);
        edgeWeighter.setSelectedValue(edgeWeightConverters[0]);
    } else {
        edgeWeighter = new ListSingleSelection<EdgeWeightConverter>();
    }
    edgeCutOff = new BoundedDouble(0.0, 0.0, 100.0, false, false);
}
Also used : EdgeWeightConverter(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter) BoundedDouble(org.cytoscape.work.util.BoundedDouble)

Example 3 with EdgeWeightConverter

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter 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

EdgeWeightConverter (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter)2 HashMap (java.util.HashMap)2 CyEdge (org.cytoscape.model.CyEdge)2 CyNode (org.cytoscape.model.CyNode)2 CyTable (org.cytoscape.model.CyTable)2 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)1 VisualStyle (org.cytoscape.view.vizmap.VisualStyle)1 BoundedDouble (org.cytoscape.work.util.BoundedDouble)1