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);
}
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));
}
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);
}
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));
}
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();
}
Aggregations