5789
Comment:
|
13135
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
= Cytoscape developer's tutorial = | = Cytoscape Plugin Developer's Tutorial = |
Line 16: | Line 16: |
(1) Get a handler to the cytoPanel west, which is the control panel {{{ |
{{{ //First, get a handler to the cytoPanel west, which is the control panel |
Line 19: | Line 19: |
}}} (2) Create a JPanel object (a class extends JPanel, say, MyPanel). {{{ |
//Create a JPanel object (a class extends JPanel, say, MyPanel) |
Line 24: | Line 22: |
}}} (3) Add it to the control panel. {{{ |
//Add it to the control panel. |
Line 29: | Line 25: |
}}} Download a sample plugin, here. |
}}} Download a sample plugin, here. [[BR]] |
Line 34: | Line 31: |
. (1) Create a toolbarAction |
Create a toolbarAction |
Line 37: | Line 35: |
MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this); }}} |
MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this); }}} |
Line 42: | Line 39: |
. (2) Add the action to Cytoscape toolbar |
Add the action to Cytoscape toolbar |
Line 45: | Line 43: |
}}} |
}}} |
Line 53: | Line 50: |
[[BR]] | |
Line 58: | Line 55: |
{{{ //create a network without a view CyNetwork |
{{{ //create a network without a view CyNetwork |
Line 62: | Line 60: |
CyNode node0 = Cytoscape.getCyNode("rain", true); CyNode node1 = Cytoscape.getCyNode("rainbow", true); CyNode node2 = Cytoscape.getCyNode("rabbit", true); |
CyNode node0 = Cytoscape.getCyNode("rain", true); CyNode node1 = Cytoscape.getCyNode("rainbow", true); CyNode node2 = Cytoscape.getCyNode("rabbit", true); |
Line 67: | Line 65: |
cyNetwork.addNode(node0); cyNetwork.addNode(node1); cyNetwork.addNode(node2); |
cyNetwork.addNode(node0); cyNetwork.addNode(node1); cyNetwork.addNode(node2); |
Line 72: | Line 70: |
CyEdge edge0 = Cytoscape.getCyEdge(node0, node1, Semantics.INTERACTION, "pp", true); CyEdge edge1 = Cytoscape.getCyEdge(node0, node2, Semantics.INTERACTION, "pp", true); CyEdge edge2 = Cytoscape.getCyEdge(node0, node3, Semantics.INTERACTION, "pp", true); cyNetwork.addEdge(edge0); cyNetwork.addEdge(edge1); |
CyEdge edge0 = Cytoscape.getCyEdge(node0, node1, Semantics.INTERACTION, "pp", true); CyEdge edge1 = Cytoscape.getCyEdge(node0, node2, Semantics.INTERACTION, "pp", true); CyEdge edge2 = Cytoscape.getCyEdge(node0, node3, Semantics.INTERACTION, "pp", true); cyNetwork.addEdge(edge0); cyNetwork.addEdge(edge1); |
Line 79: | Line 77: |
}}} | }}} |
Line 81: | Line 79: |
Line 83: | Line 82: |
}}} The following statement will destroy a network. |
}}} The following statement will destroy a network. |
Line 87: | Line 87: |
}}} | }}} |
Line 90: | Line 90: |
{{{ // Create a view for the network CyNetworkView |
{{{ // Create a view for the network CyNetworkView |
Line 93: | Line 94: |
}}} If a node is hidden, the node and all of its incident edges will be hidden. {{{ // hide a node |
}}} If a node is hidden, the node and all of its incident edges will be hidden. {{{ // hide a node |
Line 98: | Line 100: |
}}} | }}} |
Line 100: | Line 102: |
Line 102: | Line 105: |
}}} | }}} |
Line 104: | Line 107: |
|
The following snippet of code will get the list of selected nodes in current network and print out the node identifiers in console window. {{{ CyNetwork current_network = Cytoscape.getCurrentNetwork(); Set selectedNodes = current_network.getSelectedNodes(); Iterator<Node> it = selectedNodes.iterator(); while (it.hasNext()) { Node aNode = (Node) it.next(); System.out.println(aNode.getIdentifier()); } }}} Download a sample plugin, here. [[BR]] |
Line 107: | Line 122: |
|
{{{ // Handle PropertyChangeEvent public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equalsIgnoreCase(Cytoscape.ATTRIBUTES_CHANGED)) { System.out.println("Received an event -- Cytoscape.ATTRIBUTES_CHANGED!"); } if (e.getPropertyName().equalsIgnoreCase(CytoscapeDesktop.NETWORK_VIEW_FOCUSED)) { System.out.println("Received an event -- CytoscapeDesktop.NETWORK_VIEW_FOCUSED!"); } } }}} [[BR]] |
Line 110: | Line 140: |
The network view (DingNetworkView) is composed of three layers of canvases (foreground, network and background). To change the background color of a view, we first get the background canvas, and then set the color for the background. The following snippet of code will change the background color of network view, which has the focus. {{{ //Let user choose a color Color bkColor = JColorChooser.showDialog(Cytoscape.getDesktop(), "Please choose a background color", Color.white); // Change the background color for current view DingNetworkView theView = (DingNetworkView) Cytoscape.getCurrentNetworkView(); DingCanvas backgroundCanvas = theView.getCanvas(DGraphView.Canvas.BACKGROUND_CANVAS); backgroundCanvas.setBackground(bkColor); // Refresh the view Cytoscape.getCurrentNetworkView().updateView(); }}} Download a sample plugin, here. [[BR]] |
|
Line 112: | Line 157: |
To zoom a network view, all we need to know is the scale factor. Here is how, {{{ // Get reference to the view final CyNetworkView networkView = Cytoscape.getCurrentNetworkView(); networkView.setZoom(scaleFactor); networkView.updateView(); }}} Download a sample plugin, here. |
|
Line 114: | Line 169: |
Attributes are global variables. There are three types of attributes, NODE, EDGE and NETWORK. The following snippet of code shows how to add an attribute for a node – the key value will be the same as the ID of a node. {{{ String attributeName = "testAttribute"; String AttributeValue = "testValue"; CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes(); cyNodeAttrs.setAttribute(node.getIdentifier(), attributeName, AttributeValue); }}} After attributes are loaded, it may be necessary to fire an event to notify other listeners that may have interest to the change. {{{ // inform others via property change event. Cytoscape.firePropertyChange(Cytoscape.ATTRIBUTES_CHANGED, null, null); }}} Download a sample plugin, here. [[BR]] |
|
Line 116: | Line 186: |
To remove an attribute, we need to know the attribute name and its type (NODE/EDGE/NETWORK). The following two statements will remove a node attribute. {{{ CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes(); cyNodeAttrs.deleteAttribute(attributeName); }}} Download a sample plugin, here. |
|
Line 118: | Line 195: |
After a webservice is registered with the WebServiceManager, it can be used by other plugins. The following code snippet shows how to get the handler to a client by querying the WebServiceManager, {{{ WebServiceClient<EUtilsServiceSoap> client = WebServiceClientManager.getClient("tutorial13"); if (client == null) { System.out.println("Web service client tutorial13 is not found!"); return; } }}} If the manager found the webservice client and return it successfully, the client can be used. The client can be used directly by get a handler to the client stub, or by fire webservice event. The following snippet of code will get the database information from NCBI. {{{ // eInfo utility returns a list of available databases EUtilsServiceSoap utils = (EUtilsServiceSoap) client.getClientStub(); // call NCBI EInfo utility, "Available databases from NCBI"; EInfoResult res = utils.run_eInfo(new EInfoRequest()); }}} Download a sample plugin, here. [[BR]] |
|
Line 120: | Line 218: |
Cytoscape provides a WebServiceManager and a set of webservice APIs. Each webservice should be wrapped into a plugin and register to the WebServiceManager, so that other plugins can easily use the webservice by query the WebServiceManager and get access to the specific web service. We use NCBI web service “E-Utilities” as an example here. 1. Go to the NCBI webservice site, look at the WSDL http://eutils.ncbi.nlm.nih.gov/entrez/eutils/soap/eutils.wsdl, and generate stubs by following the instructions there. Put all the depended jar files used by the stubs into the lib directory of the plugin, then pack all the generated class into a jar file, say “eutils.jar”. For the jar files in the lib, we can either pack them into “eutils.jar”, or copy them into the lib directory of Cytoscape. To pack all jar into a single plugin jar is preferred, which will make it easy for the distribution of the plugin. 2. Create a webservice client class, say “My_NCBIClient”, which extends WebServiceClientImpl. The client class should implement methods, such as provide an ID and description of the client, and handle the WebserviceEvents. 3. Register the client with the WebServiceClientManager . WebServiceClientManager.registerClient(My_NCBIClient.getClient()); Download a sample plugin, here. |
|
Line 122: | Line 226: |
All the VisualStyles defined are available through the VisualMappingManager. The following two statements will get the handler of VisualMappingManager and the Set of names of all visual styles there. {{{ VisualMappingManager vmm = Cytoscape.getVisualMappingManager(); Set<String> names = vmm.getCalculatorCatalog().getVisualStyleNames(); }}} To create a VisualStyle for a network view, an easy way is to use VisualStyleBuilder. The following code snippet will create a VisualStyle with name “myVisualStyle”. After the visualStyle is created, it will appear in the comboBox of VizMapper main panel. {{{ // Create a visual style "myVisualStyle" String styleName = "myVisualStyle"; VisualStyleBuilder graphStyle = new VisualStyleBuilder(styleName, false); graphStyle.setNodeSizeLocked(false); // set some visual property for two nodes graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_WIDTH, "30"); graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_FILL_COLOR, "#E1E1E1"); graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_SHAPE, NodeShape.DIAMOND.getShapeName()); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_WIDTH, "80"); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_FILL_COLOR, "#0000E1"); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_SHAPE, NodeShape.TRIANGLE.getShapeName()); // Create the visual style graphStyle.buildStyle(); // Set the background color for this visual style GlobalAppearanceCalculator gac = Cytoscape.getVisualMappingManager().getVisualStyle().getGlobalAppearanceCalculator(); gac.setDefaultBackgroundColor(Color.red); // Refresh the view Cytoscape.getCurrentNetworkView().redrawGraph(false, true); }}} Download a sample plugin, here. [[BR]] |
|
Line 125: | Line 262: |
[[BR]] | |
Line 126: | Line 264: |
[[BR]] | |
Line 128: | Line 266: |
[[BR]] | |
Line 130: | Line 268: |
|
[[BR]] |
Line 133: | Line 270: |
[[BR]] | |
Line 135: | Line 272: |
[[BR]] | |
Line 137: | Line 274: |
[[BR]] |
Cytoscape Plugin Developer's Tutorial
This tutorial will show some code snippets about how to use Cytoscape APIs on plugin development. Each feature is included in a sample plugin, which can serve as a starting point or template for new plugin developers. The code snippets can also be used as reference for experienced developers.
Cytoscape plugin is usually packaged in a jar file and deployed to the plugins directory of Cytoscape. When Cytoscape starts up and initializes, its plugin manager will look up all the plugins in plugins directory. A plugin should have following three components.
A class, which extends CytoscapePlugin. This is the entry point to Cytoscape.
A manifest file, explicitly list the class, which extended CytoscapePlugin class
A plugin.props file, which list the information about the plugin, such as plugin name, author, release date and more. Although this file is not absolutely required, it is recommended to have one, since plugin manager will need the information to handle the plugin properly. See the page at http://www.cytoscape.org/cgi-bin/moin.cgi/Cytoscape_Plugin_Tutorial for detail about the definition of a plugin.props.
If plugin developer will share and publish their plugins, they are encouraged to submit their plugins to Cytoscape plugin website at http://cytoscape.org/plugins/index.php .
How to add a tabbed Panel to Control panel?
It takes three steps to add a tabbed panel to the control panel.
//First, get a handler to the cytoPanel west, which is the control panel CytoPanelImp ctrlPanel = (CytoPanelImp) Cytoscape.getDesktop().getCytoPanel(SwingConstants.WEST); //Create a JPanel object (a class extends JPanel, say, MyPanel) MyPanel myPanel = new MyPanel(); //Add it to the control panel. ctrlPanel.add("myPanel", myPanel);
Download a sample plugin, here.
How to add an image icon (menu item) to the toolbar?
Create a toolbarAction
ImageIcon icon = new ImageIcon(getClass().getResource("/tiger.jpg")); MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this);
The toolbarAction must extend the class cytoscape.util.CytoscapeAction and its method isInToolBar()returns true.
Add the action to Cytoscape toolbar
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) toolbarAction);
Download a sample plugin, here.
How to create a submenu?
Same as adding an image icon to the toolbar, the difference is that its method “isInMenuBar()” in CytoscapeAction should return true.
Download a sample plugin, here. BR
How to create, modify, and destroy a network, nodes, and edges?
A general procedure to create a network is, first create a bunch of CyNodes using the Cytoscape.getCyNode() method, then create edges using those nodes with Cytoscape.getCyEdge() method. Then call Cytoscape.createNetwork() with the list of nodes and edges just created.
The following snippet of code will create network with three nodes and three edges. The name of the network is “network1”.
//create a network without a view CyNetwork yNetwork = Cytoscape.createNetwork("network1", false); CyNode node0 = Cytoscape.getCyNode("rain", true); CyNode node1 = Cytoscape.getCyNode("rainbow", true); CyNode node2 = Cytoscape.getCyNode("rabbit", true); CyNode node3 = Cytoscape.getCyNode("yellow", true); cyNetwork.addNode(node0); cyNetwork.addNode(node1); cyNetwork.addNode(node2); cyNetwork.addNode(node3); CyEdge edge0 = Cytoscape.getCyEdge(node0, node1, Semantics.INTERACTION, "pp", true); CyEdge edge1 = Cytoscape.getCyEdge(node0, node2, Semantics.INTERACTION, "pp", true); CyEdge edge2 = Cytoscape.getCyEdge(node0, node3, Semantics.INTERACTION, "pp", true); cyNetwork.addEdge(edge0); cyNetwork.addEdge(edge1); cyNetwork.addEdge(edge2);
To remove a node, we use the RootGraphIndex of the Node as follows. Note that when we remove a node, we have the option to remove it from the invisible rootGrpah or just hide it from current network.
cyNetwork.removeNode(node1.getRootGraphIndex(), true);
The following statement will destroy a network.
Cytoscape.destroyNetwork(cyNetwork);
How to create, modify, and destroy a network view?
A network may or may not have a view. If we already have a network, we can create a view with the following statement.
// Create a view for the network CyNetworkView cyView = Cytoscape.createNetworkView(cyNetwork, "MyNetwork");
If a node is hidden, the node and all of its incident edges will be hidden.
// hide a node cyNetwork.hideNode(node1.getRootGraphIndex());
The following statement will destroy a network view.
Cytoscape.destroyNetworkView(cyView);
How to determine which nodes are currently selected on a network?
The following snippet of code will get the list of selected nodes in current network and print out the node identifiers in console window.
CyNetwork current_network = Cytoscape.getCurrentNetwork(); Set selectedNodes = current_network.getSelectedNodes(); Iterator<Node> it = selectedNodes.iterator(); while (it.hasNext()) { Node aNode = (Node) it.next(); System.out.println(aNode.getIdentifier()); }
Download a sample plugin, here. BR
How to handle events from a network (and discuss each event type)?
// Handle PropertyChangeEvent public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equalsIgnoreCase(Cytoscape.ATTRIBUTES_CHANGED)) { System.out.println("Received an event -- Cytoscape.ATTRIBUTES_CHANGED!"); } if (e.getPropertyName().equalsIgnoreCase(CytoscapeDesktop.NETWORK_VIEW_FOCUSED)) { System.out.println("Received an event -- CytoscapeDesktop.NETWORK_VIEW_FOCUSED!"); } }
How to change the background color of a view?
The network view (DingNetworkView) is composed of three layers of canvases (foreground, network and background). To change the background color of a view, we first get the background canvas, and then set the color for the background. The following snippet of code will change the background color of network view, which has the focus.
//Let user choose a color Color bkColor = JColorChooser.showDialog(Cytoscape.getDesktop(), "Please choose a background color", Color.white); // Change the background color for current view DingNetworkView theView = (DingNetworkView) Cytoscape.getCurrentNetworkView(); DingCanvas backgroundCanvas = theView.getCanvas(DGraphView.Canvas.BACKGROUND_CANVAS); backgroundCanvas.setBackground(bkColor); // Refresh the view Cytoscape.getCurrentNetworkView().updateView();
Download a sample plugin, here. BR
How to zoom a network view?
To zoom a network view, all we need to know is the scale factor. Here is how,
// Get reference to the view final CyNetworkView networkView = Cytoscape.getCurrentNetworkView(); networkView.setZoom(scaleFactor); networkView.updateView();
Download a sample plugin, here.
How to load attribute data?
Attributes are global variables. There are three types of attributes, NODE, EDGE and NETWORK. The following snippet of code shows how to add an attribute for a node – the key value will be the same as the ID of a node.
String attributeName = "testAttribute"; String AttributeValue = "testValue"; CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes(); cyNodeAttrs.setAttribute(node.getIdentifier(), attributeName, AttributeValue);
After attributes are loaded, it may be necessary to fire an event to notify other listeners that may have interest to the change.
// inform others via property change event. Cytoscape.firePropertyChange(Cytoscape.ATTRIBUTES_CHANGED, null, null);
Download a sample plugin, here. BR
How to remove attributes?
To remove an attribute, we need to know the attribute name and its type (NODE/EDGE/NETWORK). The following two statements will remove a node attribute.
CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes(); cyNodeAttrs.deleteAttribute(attributeName);
Download a sample plugin, here.
How to use a web service client?
After a webservice is registered with the WebServiceManager, it can be used by other plugins. The following code snippet shows how to get the handler to a client by querying the WebServiceManager,
WebServiceClient<EUtilsServiceSoap> client = WebServiceClientManager.getClient("tutorial13"); if (client == null) { System.out.println("Web service client tutorial13 is not found!"); return; }
If the manager found the webservice client and return it successfully, the client can be used. The client can be used directly by get a handler to the client stub, or by fire webservice event. The following snippet of code will get the database information from NCBI.
// eInfo utility returns a list of available databases EUtilsServiceSoap utils = (EUtilsServiceSoap) client.getClientStub(); // call NCBI EInfo utility, "Available databases from NCBI"; EInfoResult res = utils.run_eInfo(new EInfoRequest());
Download a sample plugin, here. BR
How to write a web service client?
Cytoscape provides a WebServiceManager and a set of webservice APIs. Each webservice should be wrapped into a plugin and register to the WebServiceManager, so that other plugins can easily use the webservice by query the WebServiceManager and get access to the specific web service.
We use NCBI web service “E-Utilities” as an example here. 1. Go to the NCBI webservice site, look at the WSDL http://eutils.ncbi.nlm.nih.gov/entrez/eutils/soap/eutils.wsdl, and generate stubs by following the instructions there. Put all the depended jar files used by the stubs into the lib directory of the plugin, then pack all the generated class into a jar file, say “eutils.jar”. For the jar files in the lib, we can either pack them into “eutils.jar”, or copy them into the lib directory of Cytoscape. To pack all jar into a single plugin jar is preferred, which will make it easy for the distribution of the plugin. 2. Create a webservice client class, say “My_NCBIClient”, which extends WebServiceClientImpl. The client class should implement methods, such as provide an ID and description of the client, and handle the WebserviceEvents. 3. Register the client with the WebServiceClientManager
WebServiceClientManager.registerClient(My_NCBIClient.getClient());
Download a sample plugin, here.
How to use the VizMapper programmatically?
All the VisualStyles defined are available through the VisualMappingManager. The following two statements will get the handler of VisualMappingManager and the Set of names of all visual styles there.
VisualMappingManager vmm = Cytoscape.getVisualMappingManager(); Set<String> names = vmm.getCalculatorCatalog().getVisualStyleNames();
To create a VisualStyle for a network view, an easy way is to use VisualStyleBuilder. The following code snippet will create a VisualStyle with name “myVisualStyle”. After the visualStyle is created, it will appear in the comboBox of VizMapper main panel.
// Create a visual style "myVisualStyle" String styleName = "myVisualStyle"; VisualStyleBuilder graphStyle = new VisualStyleBuilder(styleName, false); graphStyle.setNodeSizeLocked(false); // set some visual property for two nodes graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_WIDTH, "30"); graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_FILL_COLOR, "#E1E1E1"); graphStyle.addProperty(node1.getIdentifier(), VisualPropertyType.NODE_SHAPE, NodeShape.DIAMOND.getShapeName()); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_WIDTH, "80"); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_FILL_COLOR, "#0000E1"); graphStyle.addProperty(node2.getIdentifier(), VisualPropertyType.NODE_SHAPE, NodeShape.TRIANGLE.getShapeName()); // Create the visual style graphStyle.buildStyle(); // Set the background color for this visual style GlobalAppearanceCalculator gac = Cytoscape.getVisualMappingManager().getVisualStyle().getGlobalAppearanceCalculator(); gac.setDefaultBackgroundColor(Color.red); // Refresh the view Cytoscape.getCurrentNetworkView().redrawGraph(false, true);
Download a sample plugin, here. BR