Search in sources :

Example 36 with CyGroup

use of org.cytoscape.group.CyGroup in project cytoscape-impl by cytoscape.

the class AbstractNetworkFromSelectionTask method run.

@Override
public void run(TaskMonitor tm) {
    if (parentNetwork == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Source network must be specified.");
        return;
    }
    tm.setProgress(0.0);
    final Collection<CyNetworkView> views = networkViewManager.getNetworkViews(parentNetwork);
    CyNetworkView sourceView = null;
    if (views.size() != 0)
        sourceView = views.iterator().next();
    tm.setProgress(0.1);
    // Get the selected nodes
    final Set<CyNode> nodes = getNodes(parentNetwork);
    tm.setProgress(0.2);
    if (// return;
    nodes.size() <= 0)
        throw new IllegalArgumentException("No nodes are selected.");
    // create subnetwork and add selected nodes and appropriate edges
    newNet = rootNetMgr.getRootNetwork(parentNetwork).addSubNetwork();
    // We need to cpy the columns to local tables, since copying them to default table will duplicate the virtual columns.
    addColumns(parentNetwork.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
    addColumns(parentNetwork.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS));
    addColumns(parentNetwork.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));
    tm.setProgress(0.3);
    for (final CyNode node : nodes) {
        newNet.addNode(node);
        cloneRow(parentNetwork.getRow(node), newNet.getRow(node));
        // Set rows and edges to not selected state to avoid conflicts with table browser
        newNet.getRow(node).set(CyNetwork.SELECTED, false);
        if (groupMgr.isGroup(node, parentNetwork)) {
            CyGroup group = groupMgr.getGroup(node, parentNetwork);
            GroupUtils.addGroupToNetwork(group, parentNetwork, newNet);
        }
    }
    tm.setProgress(0.4);
    for (final CyEdge edge : getEdges(parentNetwork)) {
        newNet.addEdge(edge);
        cloneRow(parentNetwork.getRow(edge), newNet.getRow(edge));
        // Set rows and edges to not selected state to avoid conflicts with table browser
        newNet.getRow(edge).set(CyNetwork.SELECTED, false);
    }
    tm.setProgress(0.5);
    newNet.getRow(newNet).set(CyNetwork.NAME, getNetworkName());
    DataUtils.saveParentNetworkSUID(newNet, parentNetwork.getSUID());
    networkManager.addNetwork(newNet, false);
    tm.setProgress(0.6);
    // create the view in a separate task
    final Set<CyNetwork> networks = new HashSet<CyNetwork>();
    networks.add(newNet);
    // Pick a CyNetworkViewFactory that is appropriate for the sourceView
    CyNetworkViewFactory sourceViewFactory = viewFactory;
    if (sourceView != null) {
        NetworkViewRenderer networkViewRenderer = appMgr.getNetworkViewRenderer(sourceView.getRendererId());
        if (networkViewRenderer != null) {
            sourceViewFactory = networkViewRenderer.getNetworkViewFactory();
        }
    }
    final CreateNetworkViewTask createViewTask = new CreateNetworkViewTask(undoSupport, networks, sourceViewFactory, networkViewManager, networkManager, null, eventHelper, vmMgr, renderingEngineMgr, appMgr, sourceView, serviceRegistrar);
    insertTasksAfterCurrentTask(createViewTask);
    /*
		insertTasksAfterCurrentTask(createViewTask, new AbstractTask() {
			@Override
			@SuppressWarnings("unchecked")
			public void run(final TaskMonitor tm) throws Exception {
				// Select the new view
				tm.setProgress(0.0);
				final List<CyNetworkView> createdViews = (List<CyNetworkView>) createViewTask.getResults(List.class);
				
				if (!createdViews.isEmpty()) {
					CyNetworkView nv = createdViews.get(createdViews.size() - 1);

					if (nv != null) {
						insertTasksAfterCurrentTask(new RegisterNetworkTask(nv, null, networkManager, vmMgr, appMgr, networkViewManager));
						return;
					}
				}
				insertTasksAfterCurrentTask(new RegisterNetworkTask(newNet, networkManager, vmMgr, appMgr, networkViewManager));
				
				tm.setProgress(1.0);
			}
		});
		*/
    tm.setProgress(1.0);
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) CyEdge(org.cytoscape.model.CyEdge) CyGroup(org.cytoscape.group.CyGroup) CyNetworkViewFactory(org.cytoscape.view.model.CyNetworkViewFactory) CreateNetworkViewTask(org.cytoscape.task.internal.view.CreateNetworkViewTask) CyNode(org.cytoscape.model.CyNode) CyNetworkView(org.cytoscape.view.model.CyNetworkView) HashSet(java.util.HashSet) NetworkViewRenderer(org.cytoscape.application.NetworkViewRenderer)

Example 37 with CyGroup

use of org.cytoscape.group.CyGroup in project cytoscape-impl by cytoscape.

the class AddToGroupTask method run.

@Override
public void run(TaskMonitor tm) throws Exception {
    net = nodesAndEdges.getNetwork();
    if (groupName == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Group must be specified");
        return;
    }
    CyGroup grp = getGroup(groupName);
    if (grp == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Can't find group '" + groupName + "' in network: " + net.toString());
        return;
    }
    List<CyEdge> edgeList = nodesAndEdges.getEdgeList(false);
    List<CyNode> nodeList = nodesAndEdges.getNodeList(false);
    if (edgeList == null && nodeList == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Nothing to add");
        return;
    }
    int edges = 0;
    if (edgeList != null)
        edges = edgeList.size();
    int nodes = 0;
    if (nodeList != null)
        nodes = nodeList.size();
    tm.showMessage(TaskMonitor.Level.INFO, "Adding " + nodes + " nodes and " + edges + " edges to group " + getGroupDesc(grp));
    if (edgeList != null && edgeList.size() > 0)
        grp.addEdges(edgeList);
    if (nodeList != null && nodeList.size() > 0)
        grp.addNodes(nodeList);
    tm.showMessage(TaskMonitor.Level.INFO, "Added " + nodes + " nodes and " + edges + " edges to group " + getGroupDesc(grp));
}
Also used : CyGroup(org.cytoscape.group.CyGroup) CyNode(org.cytoscape.model.CyNode) CyEdge(org.cytoscape.model.CyEdge)

Example 38 with CyGroup

use of org.cytoscape.group.CyGroup in project cytoscape-impl by cytoscape.

the class CollapseGroupTask method run.

public void run(TaskMonitor tm) throws Exception {
    if (network != null)
        net = network;
    if (groups == null && groupList == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "List of groups must be specified");
        return;
    }
    if (groups == null)
        groups = getGroupList(tm, groupList);
    if (groups == null) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Can't find group " + groupList);
        return;
    }
    tm.setProgress(0.0);
    int collapsed = 0;
    for (CyGroup group : groups) {
        if (collapse) {
            if (group.isInNetwork(net) && !group.isCollapsed(net)) {
                collapsed++;
                group.collapse(net);
            }
        } else {
            if (group.isInNetwork(net) && group.isCollapsed(net)) {
                collapsed++;
                group.expand(net);
            }
        }
        tm.setProgress(1.0d / groups.size());
    }
    if (collapse)
        tm.showMessage(TaskMonitor.Level.INFO, "Collapsed " + collapsed + " groups");
    else
        tm.showMessage(TaskMonitor.Level.INFO, "Expanded " + collapsed + " groups");
    tm.setProgress(1.0d);
}
Also used : CyGroup(org.cytoscape.group.CyGroup)

Example 39 with CyGroup

use of org.cytoscape.group.CyGroup in project cytoscape-impl by cytoscape.

the class UnGroupNodesTaskFactoryImpl method createTaskIterator.

public TaskIterator createTaskIterator(View<CyNode> nodeView, CyNetworkView netView) {
    List<CyNode> nodeList = new ArrayList<CyNode>();
    CyNetwork net = netView.getModel();
    nodeList.add(nodeView.getModel());
    nodeList.addAll(CyTableUtil.getNodesInState(net, CyNetwork.SELECTED, true));
    Set<CyGroup> groups = getGroups(net, nodeList);
    return new TaskIterator(new UnGroupNodesTask(undoSupport, net, factory, groups, mgr, netView));
}
Also used : CyGroup(org.cytoscape.group.CyGroup) TaskIterator(org.cytoscape.work.TaskIterator) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyNode(org.cytoscape.model.CyNode)

Example 40 with CyGroup

use of org.cytoscape.group.CyGroup in project cytoscape-impl by cytoscape.

the class CyGroupImpl method collapse.

/**
 * @see org.cytoscape.group.CyGroup#collapse()
 */
@Override
public void collapse(CyNetwork net) {
    CySubNetwork subnet;
    CyNetwork groupNet;
    synchronized (lock) {
        // printGroup();
        if (isCollapsed(net))
            // Already collapsed
            return;
        if (!networkSet.contains(net))
            // We're not in that network
            return;
        subnet = (CySubNetwork) net;
        groupNet = groupNode.getNetworkPointer();
        // First collapse any children that are groups
        for (CyNode node : getNodeList()) {
            // not in our network, it might have been collapsed by another group
            if (mgr.isGroup(node, net)) {
                final CyGroup gn = mgr.getGroup(node, net);
                if (!gn.isCollapsed(net)) {
                    // Yes, collapse it
                    gn.collapse(net);
                }
            }
        }
    }
    // Now collapse ourselves
    cyEventHelper.fireEvent(new GroupAboutToCollapseEvent(CyGroupImpl.this, net, true));
    collapsing = true;
    // Since we're going to hide our nodes and edges, we probably shouldn't
    // force these to redraw while we're collapsing.  It causes a significant
    // performance hit if we do.
    cyEventHelper.silenceEventSource(net.getDefaultNodeTable());
    cyEventHelper.silenceEventSource(net.getDefaultEdgeTable());
    synchronized (lock) {
        // Deselect all of the nodes
        for (CyNode node : getNodeList()) {
            if (net.containsNode(node))
                net.getRow(node).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
        // Deselect all of our internal edges
        for (CyEdge edge : getInternalEdgeList()) {
            if (net.containsEdge(edge))
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
        // Deselect all of our member edges
        for (CyEdge edge : memberEdges) {
            if (net.containsEdge(edge))
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
        }
    }
    // Make sure to restore any of our existing metaEdges
    for (CyEdge e : metaEdges.values()) {
        rootNetwork.restoreEdge(e);
    }
    List<CyNode> nodes;
    synchronized (lock) {
        // Deselect all of our external edges, and check for any possible
        // collapsed partner groups
        ListIterator<CyEdge> iterator = (new ArrayList<CyEdge>(externalEdges)).listIterator();
        while (iterator.hasNext()) {
            CyEdge edge = iterator.next();
            if (net.containsEdge(edge)) {
                net.getRow(edge).set(CyNetwork.SELECTED, Boolean.FALSE);
            } else if (!externalEdgeProcessed.contains(edge)) {
                // See if a partner node got collapsed
                CyNode node = getPartner(edge);
                if (!net.containsNode(node) && !mgr.getGroupsForNode(node).isEmpty()) {
                    for (CyGroup group : mgr.getGroupsForNode(node)) {
                        if (!group.equals(this)) {
                            // Create our meta-edge
                            CyEdge metaEdge = null;
                            metaEdge = createMetaEdge(edge, group.getGroupNode(), groupNode);
                            addMetaEdge(edge, metaEdge);
                            // Get the reciprocal edges
                            addPartnerMetaEdges(net, edge, group, metaEdge);
                            // Get the external edges and make them part of our external
                            ListIterator<CyEdge> edgeIterator = (new ArrayList<CyEdge>(group.getExternalEdgeList())).listIterator();
                            while (edgeIterator.hasNext()) {
                                CyEdge partnerEdge = edgeIterator.next();
                                CyEdge partnerMetaEdge = null;
                                if (groupNet.containsNode(partnerEdge.getSource())) {
                                    partnerMetaEdge = createMetaEdge(partnerEdge, partnerEdge.getTarget(), groupNode);
                                } else if (groupNet.containsNode(partnerEdge.getTarget())) {
                                    partnerMetaEdge = createMetaEdge(partnerEdge, partnerEdge.getSource(), groupNode);
                                }
                                if (partnerMetaEdge != null) {
                                    ((CyGroupImpl) group).addExternalEdge(partnerMetaEdge);
                                }
                            }
                        }
                    }
                }
                externalEdgeProcessed.add(edge);
            }
        }
        cyEventHelper.unsilenceEventSource(net.getDefaultNodeTable());
        cyEventHelper.unsilenceEventSource(net.getDefaultEdgeTable());
        // Only collapse nodes that are actually in our
        // network.  This checks for nodes that are in
        // multiple groups.
        nodes = new ArrayList<CyNode>();
        for (CyNode node : getNodeList()) {
            if (net.containsNode(node)) {
                nodes.add(node);
            } else {
                // This node may be in multiple groups, ours and at least one
                // other, or it has been removed.  We need to add a meta-edge
                // to that node in case we expand it, and potentially remove
                // a meta-edge that might not apply
                List<CyGroup> otherGroups = mgr.getGroupsForNode(node);
                // Make sure someone didn't remove it
                if (otherGroups == null || otherGroups.size() == 0)
                    continue;
                for (CyGroup group : otherGroups) {
                    if (!((CyGroupImpl) group).equals(this)) {
                        // Find any internal edges that point to this node
                        for (CyEdge edge : groupNet.getAdjacentEdgeList(node, CyEdge.Type.ANY)) {
                            // If this node has an internal edge, add a meta-edge to it
                            CyEdge metaEdge = createMetaEdge(edge, node, groupNode);
                            ((CyGroupImpl) group).addExternalEdge(metaEdge);
                        // TODO: Need to deal with unnecessary meta-edge
                        }
                    }
                }
            }
        }
        // System.out.println(nodes.size()+" nodes to collapse: "+nodes);
        collapsedNodes.put(net.getSUID(), nodes);
    }
    // We flush our events to renderers can process any edge
    // creation events before we start removing things...
    cyEventHelper.flushPayloadEvents();
    // Remove all of the nodes from the target network:
    // But first, Save their locked visual properties values...
    final CyNetworkViewManager netViewMgr = mgr.getService(CyNetworkViewManager.class);
    final Collection<CyNetworkView> netViewList = netViewMgr.getNetworkViews(subnet);
    for (CyNode n : nodes) {
        lvpMgr.saveLockedValues(n, netViewList);
        saveLocalAttributes(net, n);
        for (CyEdge e : subnet.getAdjacentEdgeList(n, CyEdge.Type.ANY)) {
            lvpMgr.saveLockedValues(e, netViewList);
            saveLocalAttributes(net, e);
        }
    }
    // causes external edges to be removed as well
    subnet.removeNodes(nodes);
    // Make sure the external edges don't get auto-deleted
    for (CyEdge edge : externalEdges) {
        subnet.getRootNetwork().restoreEdge(edge);
    }
    final Set<CyIdentifiable> addedElements = new HashSet<CyIdentifiable>();
    synchronized (lock) {
        if (net.containsNode(groupNode)) {
            // System.out.println("Network contains group node!");
            nodeShownSet.add(net.getSUID());
        }
        // memberEdges.size()+" and nodeShownSet = "+nodeShownSet.contains(net.getSUID()));
        if (memberEdges.size() == 0 && !nodeShownSet.contains(net.getSUID())) {
            subnet.addNode(groupNode);
            addedElements.add(groupNode);
            for (CyEdge e : groupNodeEdges) {
                // I have no idea why this would be necessary, but it is....
                if (subnet.containsEdge(e)) {
                    // Save edge's locked visual properties values before they are removed
                    lvpMgr.saveLockedValues(e, netViewList);
                    saveLocalAttributes(net, e);
                    subnet.removeEdges(Collections.singletonList(e));
                    // But don't loose track of it...
                    rootNetwork.restoreEdge(e);
                }
                if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                    subnet.addEdge(e);
                    addedElements.add(e);
                }
            }
        }
        // Add the meta-edges
        for (CyEdge e : getMetaEdgeList()) {
            // The same meta-edge might represent multiple edges
            if (subnet.containsEdge(e)) {
                continue;
            }
            if (subnet.containsNode(e.getSource()) && subnet.containsNode(e.getTarget())) {
                subnet.addEdge(e);
                addedElements.add(e);
                nameMetaEdge(e, e.getSource(), e.getTarget());
                copyEdgeName(subnet, e);
            }
        }
        collapseSet.add(net.getSUID());
        // Update attributes
        updateCountAttributes(net);
    }
    // Make sure the view "hears" about all of the changes...
    cyEventHelper.flushPayloadEvents();
    // Restore locked visual property values of added nodes/edges
    lvpMgr.setLockedValues(netViewList, addedElements);
    restoreLocalAttributes(net, addedElements);
    // OK, all done
    collapsing = false;
    cyEventHelper.fireEvent(new GroupCollapsedEvent(CyGroupImpl.this, net, true));
// System.out.println("collapsed "+this.toString()+" in net "+net.toString()+": isCollapsed = "+isCollapsed(net));
// printGroup();
}
Also used : CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) ListIterator(java.util.ListIterator) CyEdge(org.cytoscape.model.CyEdge) CyGroup(org.cytoscape.group.CyGroup) 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)

Aggregations

CyGroup (org.cytoscape.group.CyGroup)60 CyNode (org.cytoscape.model.CyNode)33 CyNetwork (org.cytoscape.model.CyNetwork)26 ArrayList (java.util.ArrayList)22 CyEdge (org.cytoscape.model.CyEdge)12 CyNetworkView (org.cytoscape.view.model.CyNetworkView)10 HashSet (java.util.HashSet)8 CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)7 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)7 List (java.util.List)6 GroupViewType (org.cytoscape.group.CyGroupSettingsManager.GroupViewType)6 CyGroupManager (org.cytoscape.group.CyGroupManager)5 CyRow (org.cytoscape.model.CyRow)5 TaskIterator (org.cytoscape.work.TaskIterator)5 CyGroupImpl (org.cytoscape.group.internal.CyGroupImpl)4 Dimension (java.awt.Dimension)3 LinkedHashSet (java.util.LinkedHashSet)3 CyEventHelper (org.cytoscape.event.CyEventHelper)3 GroupCollapsedEvent (org.cytoscape.group.events.GroupCollapsedEvent)3 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)2