Search in sources :

Example 51 with CyRootNetwork

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

the class GroupUtil method isMeta.

private boolean isMeta(CyGroup group, CyEdge edge) {
    CyRootNetwork rootNetwork = group.getRootNetwork();
    if (edge.getSource() != group.getGroupNode() && edge.getTarget() != group.getGroupNode())
        return false;
    Boolean meta = rootNetwork.getRow(edge, CyNetwork.HIDDEN_ATTRS).get(ISMETA_EDGE_ATTR, Boolean.class, Boolean.FALSE);
    return meta.booleanValue();
}
Also used : CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 52 with CyRootNetwork

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

the class GroupUtil method createGroups.

private void createGroups(final CySubNetwork net, final Set<CyNetworkView> viewSet, final Set<CyNode> collapsedChildren) {
    // Look for possible meta-nodes by inspecting the groups metadata in the network's hidden table
    final CyRootNetwork rootNet = net.getRootNetwork();
    final List<CyNode> nodes = net.getNodeList();
    final CyGroupFactory groupFactory = serviceRegistrar.getService(CyGroupFactory.class);
    // Iterate each node and check if they have network pointers
    for (final CyNode n : nodes) {
        final CyNetwork netPointer = n.getNetworkPointer();
        if (netPointer == null)
            continue;
        // Retrieve the internal nodes and edges
        final CyRow dnRow = net.getRow(n, CyNetwork.DEFAULT_ATTRS);
        final CyRow hnRow = net.getRow(n, CyNetwork.HIDDEN_ATTRS);
        final CyRow snRow = netPointer.getRow(netPointer, CyNetwork.HIDDEN_ATTRS);
        if (!dnRow.isSet(GROUP_STATE_ATTRIBUTE) && !hnRow.isSet(GROUP_STATE_ATTRIBUTE) && !hnRow.isSet(GROUP_ATTRIBUTE))
            continue;
        // Check for nested groups recursively
        createGroups((CySubNetwork) netPointer, null, collapsedChildren);
        boolean collapsed = false;
        boolean nodeShown = false;
        boolean cy2group = false;
        if (hnRow.isSet(GROUP_STATE_ATTRIBUTE)) {
            // 2.x metadata
            Integer grState = hnRow.get(GROUP_STATE_ATTRIBUTE, Integer.class);
            cy2group = true;
            if (grState.intValue() == 2)
                collapsed = true;
        } else if (dnRow.isSet(GROUP_STATE_ATTRIBUTE)) {
            // 2.x metadata
            Integer grState = dnRow.get(GROUP_STATE_ATTRIBUTE, Integer.class);
            cy2group = true;
            if (grState.intValue() == 2)
                collapsed = true;
        } else {
            List<Long> collapsedList = snRow.getList(GROUP_COLLAPSED_ATTRIBUTE, Long.class);
            if (collapsedList != null) {
                // Are we collapsed in this network?
                for (Long suid : collapsedList) {
                    if (suid.equals(net.getSUID())) {
                        collapsed = true;
                        break;
                    }
                }
            }
            List<Long> shownList = snRow.getList(GROUP_NODE_SHOWN_ATTRIBUTE, Long.class);
            if (shownList != null) {
                // Are we showing our group node in this network?
                for (Long suid : shownList) {
                    if (suid.equals(net.getSUID())) {
                        // Yup
                        nodeShown = true;
                        break;
                    }
                }
            }
        }
        // we create the group
        if (!collapsed) {
            if (!nodeShown) {
                net.removeNodes(Collections.singletonList(n));
                rootNet.restoreNode(n);
            }
            // Add our internal edges into the network (if they aren't already)
            for (CyEdge edge : netPointer.getEdgeList()) {
                if (net.containsEdge(edge))
                    continue;
                net.addEdge(edge);
            }
            // If we're a cy2 group, remember our state
            if (cy2group) {
                CyTable hnTable = rootNet.getTable(CyNode.class, CyNetwork.HIDDEN_ATTRS);
                if (hnTable.getColumn(GROUP_STATE_ATTRIBUTE) == null) {
                    hnTable.createColumn(GROUP_STATE_ATTRIBUTE, Integer.class, false);
                }
                hnTable.getRow(n.getSUID()).set(GROUP_STATE_ATTRIBUTE, 1);
            }
        }
        // Create the group
        final CyGroup group;
        // TODO: Do not create more than one instance of a CyGroup for the same group node.
        // if (groupMgr.isGroup(n, rootNet)) {
        // group = groupMgr.getGroup(n, rootNet);
        // group.addGroupToNetwork(net);
        // group.collapse(net);
        // } else {
        group = groupFactory.createGroup(net, n, true);
        // CyRow groupNodeRow = net.getRow(n, CyNetwork.HIDDEN_ATTRS);
        if (snRow.isSet(EXTERNAL_EDGE_ATTRIBUTE)) {
            List<Long> externalIDs = snRow.getList(EXTERNAL_EDGE_ATTRIBUTE, Long.class);
            List<CyEdge> externalEdges = new ArrayList<CyEdge>();
            for (Long suid : externalIDs) {
                CyEdge newEdge = rootNet.getEdge(suid);
                if (newEdge != null) {
                    // Don't add the edge if it already exists
                    if (!isMeta(group, newEdge) || !metaExists(group, newEdge)) {
                        externalEdges.add(newEdge);
                    }
                }
            }
            if (cy2group && collapsed) {
                updateMetaEdges(net, n);
            }
            group.addEdges(externalEdges);
        }
        // if we're collapsed.
        if (cy2group) {
            if (collapsed) {
                // Update the locations of child nodes
                updateChildOffsets(netPointer, net, n);
            }
            // If the group is collapsed, we need to provide the location information so we can expand it
            if (viewSet != null) {
                if (collapsed) {
                    for (CyNetworkView view : viewSet) {
                        if (view.getModel().equals(net))
                            updateGroupNodeLocation(view, n);
                        disableNestedNetworkIcon(view, n);
                    }
                }
            }
        }
        if (collapsed) {
            if (collapsedChildren.contains(n) && net.containsNode(n))
                net.removeNodes(Collections.singleton(n));
            collapsedChildren.addAll(netPointer.getNodeList());
        }
    }
    // TODO: If this is a 2.x group, clean up
    if (net.getDefaultNodeTable().getColumn(GROUP_STATE_ATTRIBUTE) != null)
        net.getDefaultNodeTable().deleteColumn(GROUP_STATE_ATTRIBUTE);
    if (net.getDefaultNodeTable().getColumn(GROUP_ISLOCAL_ATTRIBUTE) != null)
        net.getDefaultNodeTable().deleteColumn(GROUP_ISLOCAL_ATTRIBUTE);
}
Also used : ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CyGroup(org.cytoscape.group.CyGroup) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) CyGroupFactory(org.cytoscape.group.CyGroupFactory) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 53 with CyRootNetwork

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

the class GroupUtil method metaExists.

private boolean metaExists(CyGroup group, CyEdge edge) {
    CyRootNetwork rootNetwork = group.getRootNetwork();
    CyNode source;
    CyNode target;
    if (edge.getSource() == group.getGroupNode()) {
        source = group.getGroupNode();
        target = edge.getTarget();
    } else {
        source = edge.getSource();
        target = group.getGroupNode();
    }
    List<CyEdge> list = rootNetwork.getConnectingEdgeList(source, target, CyEdge.Type.DIRECTED);
    for (CyEdge e : list) {
        if (e != edge && isMeta(group, e))
            // Already got a meta-edge for this
            return true;
    }
    return false;
}
Also used : CyNode(org.cytoscape.model.CyNode) CyEdge(org.cytoscape.model.CyEdge) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 54 with CyRootNetwork

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

the class ReadDataManager method createEdge.

protected CyEdge createEdge(final CyNode source, final CyNode target, Object id, final String label, final boolean directed, CyNetwork net) {
    CyEdge edge = null;
    if (id == null)
        id = label;
    if (net instanceof CySubNetwork && this.getParentNetwork() != null) {
        // Do not create the element again if the network is a sub-network and the edge already exists!
        edge = cache.getEdge(id);
        if (edge != null)
            ((CySubNetwork) net).addEdge(edge);
    }
    if (edge == null) {
        // OK, create it
        // But first get the actual source/target instances from the current network,
        // because both node instances have to belong to the same root or sub-network.
        CyNode actualSrc = net.getNode(source.getSUID());
        CyNode actualTgt = net.getNode(target.getSUID());
        // For 2.x groups
        List<Long> extEdgeIds = null;
        if ((getDocumentVersion() < 3.0 || !isSessionFormat()) && (actualSrc == null || actualTgt == null)) {
            // The nodes might have been added to other sub-networks, but not to the current one.
            // If that is the case, the root network should have both nodes,
            // so let's just add the edge to the root.
            final CyRootNetwork rootNet = getRootNetwork();
            if (actualSrc == null)
                actualSrc = rootNet.getNode(source.getSUID());
            if (actualTgt == null)
                actualTgt = rootNet.getNode(target.getSUID());
            // Does the current subnetwork belong to a 2.x group?
            if (getDocumentVersion() < 3.0 && !compoundNodeStack.isEmpty() && networkStack.size() > 1) {
                // Get the current compound node
                final CyNode grNode = compoundNodeStack.peek();
                // Get the network of that node (the current one is its network pointer)
                final CyNetwork parentNet = cache.getNetwork(networkStack.elementAt(networkStack.size() - 2));
                // Check for the group's metadata attribute
                final CyRow gnhRow = parentNet.getRow(grNode, CyNetwork.HIDDEN_ATTRS);
                if (gnhRow.isSet(GROUP_STATE_ATTRIBUTE)) {
                    // It's a group!
                    // Get the network pointer's hidden row
                    final CyRow nphRow = net.getRow(net, CyNetwork.HIDDEN_ATTRS);
                    // Add extra metadata for external edges, so that the information is not lost
                    if (nphRow.getTable().getColumn(EXTERNAL_EDGE_ATTRIBUTE) == null) {
                        nphRow.getTable().createListColumn(EXTERNAL_EDGE_ATTRIBUTE, Long.class, false);
                        // These are already the new SUIDs. Let's tell the SUIDUpdater to ignore this column,
                        // in order to prevent it from replacing the correct list by an empty one.
                        suidUpdater.ignoreColumn(nphRow.getTable(), EXTERNAL_EDGE_ATTRIBUTE);
                    }
                    extEdgeIds = nphRow.getList(EXTERNAL_EDGE_ATTRIBUTE, Long.class);
                    if (extEdgeIds == null) {
                        nphRow.set(EXTERNAL_EDGE_ATTRIBUTE, new ArrayList<Long>());
                        extEdgeIds = nphRow.getList(EXTERNAL_EDGE_ATTRIBUTE, Long.class);
                    }
                }
            }
            net = rootNet;
        }
        edge = net.addEdge(actualSrc, actualTgt, directed);
        if (extEdgeIds != null)
            extEdgeIds.add(edge.getSUID());
        mapSUIDs(id, edge.getSUID());
    }
    // Add to internal cache:
    cache.cache(id, edge);
    return edge;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) CyNode(org.cytoscape.model.CyNode) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 55 with CyRootNetwork

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

the class CyNetworkManagerImpl method destroyNetwork.

@Override
public void destroyNetwork(final CyNetwork network) {
    if (network == null)
        throw new NullPointerException("Network is null");
    final Long networkId = network.getSUID();
    synchronized (lock) {
        if (!networkMap.containsKey(networkId))
            throw new IllegalArgumentException("network is not recognized by this NetworkManager");
    }
    final CyEventHelper cyEventHelper = serviceRegistrar.getService(CyEventHelper.class);
    // let everyone know!
    cyEventHelper.fireEvent(new NetworkAboutToBeDestroyedEvent(CyNetworkManagerImpl.this, network));
    synchronized (lock) {
        // check again within the lock in case something has changed
        if (!networkMap.containsKey(networkId))
            throw new IllegalArgumentException("network is not recognized by this NetworkManager");
        for (CyNode n : network.getNodeList()) network.getRow(n).set(CyNetwork.SELECTED, false);
        for (CyEdge e : network.getEdgeList()) network.getRow(e).set(CyNetwork.SELECTED, false);
        networkMap.remove(networkId);
    }
    if (network instanceof CySubNetwork) {
        final CySubNetwork subNetwork = (CySubNetwork) network;
        final CyRootNetwork rootNetwork = subNetwork.getRootNetwork();
        final CySubNetwork baseNetwork = rootNetwork.getBaseNetwork();
        if (!subNetwork.equals(baseNetwork) || rootNetwork.getSubNetworkList().size() > 1) {
            rootNetwork.removeSubNetwork(subNetwork);
            network.dispose();
        }
        if (!hasRegisteredNetworks(rootNetwork))
            rootNetwork.dispose();
    } else {
        network.dispose();
    }
    // let everyone know that some network is gone
    cyEventHelper.fireEvent(new NetworkDestroyedEvent(CyNetworkManagerImpl.this));
}
Also used : NetworkDestroyedEvent(org.cytoscape.model.events.NetworkDestroyedEvent) CyEventHelper(org.cytoscape.event.CyEventHelper) NetworkAboutToBeDestroyedEvent(org.cytoscape.model.events.NetworkAboutToBeDestroyedEvent) CyNode(org.cytoscape.model.CyNode) CyEdge(org.cytoscape.model.CyEdge) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Aggregations

CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)86 CyNetwork (org.cytoscape.model.CyNetwork)39 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)38 CyNode (org.cytoscape.model.CyNode)29 CyEdge (org.cytoscape.model.CyEdge)15 CyRow (org.cytoscape.model.CyRow)15 CyRootNetworkManager (org.cytoscape.model.subnetwork.CyRootNetworkManager)15 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 CyTable (org.cytoscape.model.CyTable)11 CyNetworkView (org.cytoscape.view.model.CyNetworkView)11 CyGroup (org.cytoscape.group.CyGroup)7 HashSet (java.util.HashSet)6 Dimension (java.awt.Dimension)4 IOException (java.io.IOException)4 CyNetworkManager (org.cytoscape.model.CyNetworkManager)4 Image (java.awt.Image)3 BufferedImage (java.awt.image.BufferedImage)3 CyApplicationManager (org.cytoscape.application.CyApplicationManager)3 CyEventHelper (org.cytoscape.event.CyEventHelper)3