Search in sources :

Example 66 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class CyGroupImpl method expand.

/**
 * @see org.cytoscape.group.CyGroup#expand()
 */
@Override
public void expand(CyNetwork net) {
    synchronized (lock) {
        if (!isCollapsed(net))
            // Already expanded
            return;
        if (!networkSet.contains(net))
            // We're not in that network
            return;
    }
    cyEventHelper.fireEvent(new GroupAboutToCollapseEvent(CyGroupImpl.this, net, false));
    expanding = true;
    CySubNetwork subnet = (CySubNetwork) net;
    List<CyNode> nodes;
    synchronized (lock) {
        // Get the list of nodes we collapsed in this net
        nodes = collapsedNodes.get(net.getSUID());
        // initialized.  Handle this special case here
        if (nodes == null) {
            nodes = getNodeList();
        }
    }
    boolean groupNodeShown = nodeShownSet.contains(net.getSUID());
    // Make sure the group node isn't selected
    if (!groupNodeShown && net.containsNode(groupNode)) {
        cyEventHelper.silenceEventSource(net.getDefaultNodeTable());
        net.getRow(groupNode).set(CyNetwork.SELECTED, Boolean.FALSE);
        cyEventHelper.unsilenceEventSource(net.getDefaultNodeTable());
    }
    // Expand it.
    final CyNetworkViewManager netViewMgr = mgr.getService(CyNetworkViewManager.class);
    final Collection<CyNetworkView> netViewList = netViewMgr.getNetworkViews(net);
    final Set<CyIdentifiable> addedElements = new HashSet<CyIdentifiable>();
    synchronized (lock) {
        // the group node did not go away.
        if (memberEdges.size() == 0 && !groupNodeShown) {
            // First, get all of the group node's edges
            List<CyEdge> groupEdges = subnet.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
            // Save group node's locked visual properties values before they are removed
            lvpMgr.saveLockedValues(groupNode, netViewList);
            saveLocalAttributes(net, groupNode);
            for (CyEdge e : groupEdges) {
                lvpMgr.saveLockedValues(e, netViewList);
                saveLocalAttributes(net, e);
            }
            groupNodeEdges = new HashSet<CyEdge>();
            // Now, see which of these groupEdges aren't meta-edges
            for (CyEdge edge : groupEdges) {
                if (!isMeta(edge))
                    groupNodeEdges.add(edge);
            }
            // Remove the group node, if we're supposed to
            if (!nodeShownSet.contains(subnet.getSUID())) {
                List<CyEdge> groupNodeEdges = subnet.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
                subnet.removeNodes(Collections.singletonList(groupNode));
                rootNetwork.restoreNode(groupNode);
                for (CyEdge e : groupNodeEdges) rootNetwork.restoreEdge(e);
            }
        }
        // Add all of the member nodes and edges in
        for (CyNode n : nodes) {
            subnet.addNode(n);
            addedElements.add(n);
        }
    }
    // Make sure everyone knows about all of our changes!
    cyEventHelper.flushPayloadEvents();
    synchronized (lock) {
        // Add all of the interior edges in
        for (CyEdge e : getInternalEdgeList()) {
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // Add all of the exterior edges in
        for (CyEdge e : getExternalEdgeList()) {
            // collapsed group
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // our nodes to collapsed groups.  Add those in
        for (CyEdge e : getMetaEdgeList()) {
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
            }
        }
        // Add all of the member edges in
        for (CyEdge e : memberEdges) {
            subnet.addEdge(e);
            addedElements.add(e);
        }
    }
    // Make sure everyone knows about all of our changes!
    cyEventHelper.flushPayloadEvents();
    // Restore locked visual property values of added nodes/edges
    lvpMgr.setLockedValues(netViewList, addedElements);
    restoreLocalAttributes(net, addedElements);
    synchronized (lock) {
        collapseSet.remove(net.getSUID());
        collapsedNodes.remove(net.getSUID());
    }
    // Finish up
    expanding = false;
    cyEventHelper.fireEvent(new GroupCollapsedEvent(CyGroupImpl.this, net, false));
}
Also used : CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) CyEdge(org.cytoscape.model.CyEdge) GroupAboutToCollapseEvent(org.cytoscape.group.events.GroupAboutToCollapseEvent) CyNode(org.cytoscape.model.CyNode) GroupCollapsedEvent(org.cytoscape.group.events.GroupCollapsedEvent) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyIdentifiable(org.cytoscape.model.CyIdentifiable) HashSet(java.util.HashSet)

Example 67 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class GraphMLParser method createNode.

private void createNode(final Attributes atts) {
    // Parse node entry.
    String currentAttributeID = atts.getValue(ID.getTag());
    // If nested, add them to all parent networks.
    if (networkStack.size() > 1) {
        final CyNetwork rootNetwork = networkStack.get(0);
        currentObject = nodeid2CyNodeMap.get(currentAttributeID);
        if (currentObject == null) {
            currentObject = rootNetwork.addNode();
            rootNetwork.getRow(currentObject).set(CyNetwork.NAME, currentAttributeID);
            nodeid2CyNodeMap.put(currentAttributeID, currentObject);
        }
        for (CyNetwork network : networkStack) {
            if (network != rootNetwork) {
                ((CySubNetwork) network).addNode((CyNode) currentObject);
            }
        }
    } else {
        // Root
        currentObject = nodeid2CyNodeMap.get(currentAttributeID);
        if (currentObject == null) {
            currentObject = currentNetwork.addNode();
            currentNetwork.getRow(currentObject).set(CyNetwork.NAME, currentAttributeID);
            nodeid2CyNodeMap.put(currentAttributeID, currentObject);
        }
    }
    lastNode = (CyNode) currentObject;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Example 68 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class GroupViewCollapseHandler method handleEvent.

@Override
public void handleEvent(GroupCollapsedEvent e) {
    getServices();
    CyNetwork network = e.getNetwork();
    CyGroup group = e.getSource();
    final Collection<CyNetworkView> views = cyNetworkViewManager.getNetworkViews(network);
    CyNetworkView view = null;
    if (views.size() == 0) {
        return;
    }
    for (CyNetworkView v : views) {
        if (v.getRendererId().equals("org.cytoscape.ding")) {
            view = v;
        }
    }
    if (view == null)
        return;
    CyRootNetwork rootNetwork = group.getRootNetwork();
    GroupViewType groupViewType = cyGroupSettings.getGroupViewType(group);
    if (e.collapsed()) {
        // System.out.println("Got collapse event for "+group);
        // Get the location to move the group node to
        Dimension d = ViewUtils.getLocation(network, group);
        if (d != null) {
            // Move it.
            ViewUtils.moveNode(view, group.getGroupNode(), d);
        }
        View<CyNode> nView = view.getNodeView(group.getGroupNode());
        if (cyGroupSettings.getUseNestedNetworks(group)) {
            // Now, if we're displaying the nested network, create it....
            CyNetwork nn = group.getGroupNetwork();
            CyNetworkView nnView = null;
            // If we've already registered the network, don't do it again
            if (!cyNetworkManager.networkExists(nn.getSUID())) {
                cyNetworkManager.addNetwork(nn, false);
                nnView = cyNetworkViewFactory.createNetworkView(nn);
                cyNetworkViewManager.addNetworkView(nnView, false);
                // Apply our current style to the nested network
                VisualStyle style = cyStyleManager.getVisualStyle(view);
                cyStyleManager.setVisualStyle(style, nnView);
                style.apply(nnView);
            } else if (cyNetworkViewManager.viewExists(nn)) {
                nnView = cyNetworkViewManager.getNetworkViews(nn).iterator().next();
            }
            if (nnView != null) {
                ViewUtils.moveNodes(group, nnView, d);
                nnView.updateView();
                // Allow the nested network image to be displayed
                nView.clearValueLock(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE);
            }
        } else {
            // Make sure the nested network image is not displayed
            nView.setLockedValue(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE, Boolean.FALSE);
        }
        // If we were showing this as a compound node, we need to restyle
        if (groupViewType.equals(GroupViewType.COMPOUND)) {
            deActivateCompoundNode(group, view);
        }
        styleGroupNode(group, view, views);
    } else {
        // System.out.println("Got expand event for "+group);
        CyNode groupNode = group.getGroupNode();
        // Get the location of the group node before it went away
        Dimension center = ViewUtils.getLocation(network, group);
        if (center != null)
            ViewUtils.moveNodes(group, view, center);
        // If we're asked to, show the group node
        if (!groupViewType.equals(GroupViewType.NONE)) {
            CySubNetwork subnet = (CySubNetwork) network;
            // Add the node back
            subnet.addNode(group.getGroupNode());
            // Add the group nodes's edges back
            List<CyEdge> groupNodeEdges = rootNetwork.getAdjacentEdgeList(groupNode, CyEdge.Type.ANY);
            for (CyEdge edge : groupNodeEdges) {
                CyRow row = rootNetwork.getRow(edge, CyNetwork.HIDDEN_ATTRS);
                // Only add non-meta edges
                if (row != null && (!row.isSet(CyGroupImpl.ISMETA_EDGE_ATTR) || !row.get(CyGroupImpl.ISMETA_EDGE_ATTR, Boolean.class))) {
                    subnet.addEdge(edge);
                } else if (subnet.containsEdge(edge) && row != null && row.isSet(CyGroupImpl.ISMETA_EDGE_ATTR) && row.get(CyGroupImpl.ISMETA_EDGE_ATTR, Boolean.class)) {
                    // Edge is a meta edge, but is still in the network.  Remove it
                    subnet.removeEdges(Collections.singletonList(edge));
                }
            }
            if (groupViewType.equals(GroupViewType.SHOWGROUPNODE)) {
                // If this is the first time, we need to add our member
                // edges in.
                addMemberEdges(group, network);
                // Style our member edges
                styleGroupNode(group, view, views);
            }
            // Flush events so that the view hears about it
            final CyEventHelper cyEventHelper = cyGroupManager.getService(CyEventHelper.class);
            cyEventHelper.flushPayloadEvents();
            // Now, call ourselves as if we had been collapsed
            handleEvent(new GroupCollapsedEvent(group, network, true));
            if (groupViewType.equals(GroupViewType.COMPOUND) || groupViewType.equals(GroupViewType.SINGLENODE)) {
                // May be redundant, but just to make sure
                ((CyGroupImpl) group).setGroupNodeShown(network, true);
                activateCompoundNode(group, view);
            }
            cyEventHelper.flushPayloadEvents();
            view.updateView();
            return;
        }
        final List<CyNode> nodeList = group.getNodeList();
        // TODO: turn off stupid nested network thing
        for (CyNode node : nodeList) {
            if (!network.containsNode(node))
                continue;
            View<CyNode> nView = view.getNodeView(node);
            if (node.getNetworkPointer() != null && cyGroupManager.isGroup(node, network)) {
                if (!cyGroupSettings.getUseNestedNetworks(cyGroupManager.getGroup(node, network))) {
                    nView.setLockedValue(BasicVisualLexicon.NODE_NESTED_NETWORK_IMAGE_VISIBLE, Boolean.FALSE);
                }
            }
        }
        // Apply visual property to added graph elements
        ViewUtils.applyStyle(nodeList, views, cyStyleManager);
        ViewUtils.applyStyle(group.getInternalEdgeList(), views, cyStyleManager);
        ViewUtils.applyStyle(group.getExternalEdgeList(), views, cyStyleManager);
    }
    view.updateView();
}
Also used : GroupViewType(org.cytoscape.group.CyGroupSettingsManager.GroupViewType) CyEventHelper(org.cytoscape.event.CyEventHelper) CyNetwork(org.cytoscape.model.CyNetwork) Dimension(java.awt.Dimension) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CyGroup(org.cytoscape.group.CyGroup) CyGroupImpl(org.cytoscape.group.internal.CyGroupImpl) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) GroupCollapsedEvent(org.cytoscape.group.events.GroupCollapsedEvent) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 69 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class NNFParser method parse.

/**
 * Parse an entry/line in an NNF file.
 *
 * @param line
 */
public boolean parse(final String line) {
    // Split with white space chars
    parts = splitLine(line);
    length = parts.length;
    final String originalName = parts[0];
    CyNetwork network = getNetworkByName(originalName);
    if (network == null) {
        // Create network without view.  View will be created later.
        network = rootNetwork.addSubNetwork();
        final String actualName = namingUtil.getSuggestedNetworkTitle(originalName);
        network.getRow(network).set(CyNetwork.NAME, actualName);
        networksByName.put(originalName, network);
        networks.add(network);
        // Attempt to nest network within the node with the same name
        CyNode parent = getNodeByName(originalName);
        if (parent != null)
            setNestedNetwork(network, parent, network);
        // is the node exist in the overview network
        parent = getNodefromOverview(originalName);
        if (parent != null)
            setNestedNetwork(network, parent, network);
    }
    if (length == 2) {
        CyNode node;
        if (this.nMap.get(parts[1]) == null) {
            node = network.addNode();
            this.nMap.put(parts[1], this.rootNetwork.getNode(node.getSUID()));
        } else {
            node = this.nMap.get(parts[1]);
            CySubNetwork subnet = (CySubNetwork) network;
            subnet.addNode(node);
        }
        network.getRow(node).set(CyNetwork.NAME, parts[1]);
        final CyNetwork nestedNetwork = getNetworkByName(parts[1]);
        if (nestedNetwork != null) {
            setNestedNetwork(network, node, nestedNetwork);
        }
    } else if (length == 4) {
        CyNode source = null;
        // Check if the source node already existed in the network
        Collection<CyRow> matchingRows = network.getDefaultNodeTable().getMatchingRows(CyNetwork.NAME, parts[1]);
        if (!matchingRows.isEmpty()) {
            // source node already existed, get it
            CyRow row = matchingRows.iterator().next();
            Long suid = row.get("SUID", Long.class);
            if (suid != null) {
                source = network.getNode(suid);
            }
        } else {
            // source node does not existed yet, create one
            if (this.nMap.get(parts[1]) == null) {
                // Cytoscape.getCyNode(parts[1], true);
                source = network.addNode();
                CyNode newNode = this.rootNetwork.getNode(source.getSUID());
                this.nMap.put(parts[1], newNode);
            } else {
                CyNode newNode = this.nMap.get(parts[1]);
                CySubNetwork subNet = (CySubNetwork) network;
                subNet.addNode(newNode);
                source = network.getNode(newNode.getSUID());
            }
            network.getRow(source).set(CyNetwork.NAME, parts[1]);
        }
        CyNetwork nestedNetwork = networksByName.get(parts[1]);
        if (nestedNetwork != null) {
            setNestedNetwork(network, source, nestedNetwork);
        }
        CyNode target = null;
        // Check if the target already existed in the network
        Collection<CyRow> matchingRows2 = network.getDefaultNodeTable().getMatchingRows(CyNetwork.NAME, parts[3]);
        if (!matchingRows2.isEmpty()) {
            // source node already existed, get it
            CyRow row = matchingRows2.iterator().next();
            Long suid = row.get("SUID", Long.class);
            if (suid != null) {
                target = network.getNode(suid);
            }
        } else {
            if (this.nMap.get(parts[3]) == null) {
                target = network.addNode();
                CyNode newNode = this.rootNetwork.getNode(target.getSUID());
                this.nMap.put(parts[3], newNode);
            } else {
                CyNode newNode = this.nMap.get(parts[3]);
                CySubNetwork subNet = (CySubNetwork) network;
                subNet.addNode(newNode);
                target = network.getNode(newNode.getSUID());
            }
            // target = network.addNode();
            network.getRow(target).set(CyNetwork.NAME, parts[3]);
        }
        // 
        nestedNetwork = networksByName.get(parts[3]);
        if (nestedNetwork != null) {
            setNestedNetwork(network, target, nestedNetwork);
        }
        CyEdge newEdge = network.addEdge(source, target, true);
        network.getRow(newEdge).set(CyEdge.INTERACTION, parts[2]);
        network.getRow(newEdge).set(CyNetwork.NAME, parts[1] + " (" + parts[2] + ") " + parts[3]);
    } else {
        // Invalid number of columns.
        return false;
    }
    return true;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) Collection(java.util.Collection) CyNode(org.cytoscape.model.CyNode) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Example 70 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class SIFNetworkReader method readInput.

private void readInput(TaskMonitor tm) throws IOException {
    this.parentTaskMonitor = tm;
    tm.setProgress(0.0);
    String line;
    final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8").newDecoder()), 128 * 1024);
    CyRootNetwork root = getRootNetwork();
    final CySubNetwork newNetwork;
    if (root != null)
        newNetwork = root.addSubNetwork();
    else
        // Need to create new network with new root.
        newNetwork = (CySubNetwork) cyNetworkFactory.createNetwork();
    Map<Object, CyNode> nMap = getNodeMap();
    tm.setProgress(0.1);
    final String firstLine = br.readLine();
    if (firstLine.contains(TAB))
        delimiter = TAB;
    createEdge(new Interaction(firstLine.trim(), delimiter), newNetwork, nMap);
    tm.setProgress(0.15);
    tm.setStatusMessage("Processing the interactions...");
    int numInteractionsRead = 0;
    while ((line = br.readLine()) != null) {
        if (cancelled) {
            // Cancel called. Clean up the garbage.
            nMap.clear();
            br.close();
            return;
        }
        if (line.trim().length() <= 0)
            continue;
        try {
            final Interaction itr = new Interaction(line, delimiter);
            createEdge(itr, newNetwork, nMap);
        } catch (Exception e) {
            // Simply ignore invalid lines.
            continue;
        }
        if ((++numInteractionsRead % 1000) == 0)
            tm.setStatusMessage("Processed " + numInteractionsRead + " interactions so far.");
    }
    br.close();
    tm.setStatusMessage("Processed " + numInteractionsRead + " interactions in total.");
    nMap.clear();
    nMap = null;
    this.networks = new CyNetwork[] { newNetwork };
    tm.setProgress(1.0);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) CyNode(org.cytoscape.model.CyNode) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) IOException(java.io.IOException) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Aggregations

CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)88 CyNode (org.cytoscape.model.CyNode)44 CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)35 CyNetwork (org.cytoscape.model.CyNetwork)31 CyEdge (org.cytoscape.model.CyEdge)28 Test (org.junit.Test)19 CyRow (org.cytoscape.model.CyRow)15 ArrayList (java.util.ArrayList)12 CyTable (org.cytoscape.model.CyTable)11 CyNetworkView (org.cytoscape.view.model.CyNetworkView)11 HashSet (java.util.HashSet)7 CyGroup (org.cytoscape.group.CyGroup)7 HashMap (java.util.HashMap)6 CyNetworkManager (org.cytoscape.model.CyNetworkManager)6 CyApplicationManager (org.cytoscape.application.CyApplicationManager)5 CyEventHelper (org.cytoscape.event.CyEventHelper)5 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)5 Dimension (java.awt.Dimension)3 GroupCollapsedEvent (org.cytoscape.group.events.GroupCollapsedEvent)3 VisualStyle (org.cytoscape.view.vizmap.VisualStyle)3