Attachment 'NeighborNodeSelection.java'
Download 1 import java.util.*;
2 import java.awt.event.ActionEvent;
3 import javax.swing.JOptionPane;
4
5 import giny.model.Node;
6 import giny.view.NodeView;
7
8 import cytoscape.plugin.CytoscapePlugin;
9 import cytoscape.util.CytoscapeAction;
10 import cytoscape.Cytoscape;
11 import cytoscape.CyNetwork;
12 import cytoscape.CyNode;
13 import cytoscape.CyEdge;
14 import cytoscape.view.CyNetworkView;
15 import cytoscape.data.Semantics;
16
17 /**
18 * This is a sample Cytoscape plugin using core graph and data structures.
19 * For each currently selected node in the graph view, the action method
20 * of this plugin additionally selects the neighbors of that node.
21 *
22 * Note that selection is a property of the view of the graph, while neighbors
23 * are a property of the graph itself. Thus this plugin must access both the
24 * graph and its view.
25 */
26 public class NeighborNodeSelection extends CytoscapePlugin {
27
28 /**
29 * This constructor creates an action and adds it to the Plugins menu.
30 */
31 public NeighborNodeSelection() {
32 //create a new action to respond to menu activation
33 NeighborNodeSelectionAction action = new NeighborNodeSelectionAction();
34 //set the preferred menu
35 action.setPreferredMenu("Plugins");
36 //and add it to the menus
37 Cytoscape.getDesktop().getCyMenus().addAction(action);
38 }
39
40 /**
41 * Gives a description of this plugin.
42 */
43 public String describe() {
44 StringBuffer sb = new StringBuffer();
45 sb.append("For every currently selected node in the graph view, this ");
46 sb.append("plugin additionally selects each neighbor of that node if ");
47 sb.append("the canonical names of the two nodes have the same last letter.");
48 return sb.toString();
49 }
50
51 /**
52 * This class gets attached to the menu item.
53 */
54 public class NeighborNodeSelectionAction extends CytoscapeAction {
55
56 /**
57 * The constructor sets the text that should appear on the menu item.
58 */
59 public NeighborNodeSelectionAction() {super("NeighborNodeSelection");}
60
61 /**
62 * This method is called when the user selects the menu item.
63 */
64 public void actionPerformed(ActionEvent ae) {
65 //get the network object; this contains the graph
66 CyNetwork network = Cytoscape.getCurrentNetwork();
67 //get the network view object
68 CyNetworkView view = Cytoscape.getCurrentNetworkView();
69 //can't continue if either of these is null
70 if (network == null || view == null) {return;}
71 //put up a dialog if there are no selected nodes
72 if (view.getSelectedNodes().size() == 0) {
73 JOptionPane.showMessageDialog(view.getComponent(),
74 "Please select one or more nodes.");
75 }
76
77 //a container to hold the objects we're going to select
78 Set nodeViewsToSelect = new HashSet();
79 //iterate over every node view
80 for (Iterator i = view.getSelectedNodes().iterator(); i.hasNext(); ) {
81 NodeView nView = (NodeView)i.next();
82 //first get the corresponding node in the network
83 CyNode node = (CyNode)nView.getNode();
84 // get the neighbors of that node
85 List neighbors = network.neighborsList(node);
86 // and iterate over the neighbors
87 for (Iterator ni = neighbors.iterator(); ni.hasNext(); ) {
88 CyNode neighbor = (CyNode)ni.next();
89 // get the view on this neighbor
90 NodeView neighborView = view.getNodeView(neighbor);
91 //and add that view to our container of objects to select
92 nodeViewsToSelect.add(neighborView);
93 }
94 }
95 //now go through our container and select each view
96 for (Iterator i = nodeViewsToSelect.iterator(); i.hasNext(); ) {
97 NodeView nView = (NodeView)i.next();
98 nView.setSelected(true);
99 }
100 //tell the view to redraw since we've changed the selection
101 view.redrawGraph(false, true);
102 }
103
104 /**
105 * Gets the canonical name of the given node from the network object
106 * and returns a String holding just the last letter of that name.
107 *
108 * Returns null if a valid name cannot be obtained.
109 */
110 private String getLastLetter(CyNetwork network, CyNode node) {
111 String canonicalName = (String)network.getNodeAttributeValue(node, Semantics.CANONICAL_NAME);
112 //return nothing if we can't get a valid name
113 if (canonicalName == null || canonicalName.length() == 0) {return null;}
114 //extract the last letter
115 int length = canonicalName.length();
116 String lastLetter = canonicalName.substring(length-1);
117 return lastLetter;
118 }
119 }
120 }
121
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.- [get | view] (2011-10-20 20:44:33, 2.4 KB) [[attachment:NeighborNodeSelection.jar]]
- [get | view] (2011-10-20 20:44:33, 5.0 KB) [[attachment:NeighborNodeSelection.java]]
You are not allowed to attach a file to this page.