Differences between revisions 7 and 18 (spanning 11 versions)
Revision 7 as of 2008-05-13 20:24:23
Size: 5707
Editor: cabernet
Comment:
Revision 18 as of 2008-05-14 18:33:25
Size: 13012
Editor: cabernet
Comment:
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
{{{CytoPanelImp ctrlPanel = (CytoPanelImp) Cytoscape.getDesktop().getCytoPanel(SwingConstants.WEST);
}}}

 2. Create a JPanel object (a class extends JPanel, say, MyPanel).
{{{ MyPanel myPanel = new MyPanel();
}}}

 3. Add it to the control panel.
{{{ctrlPanel.add("myPanel", myPanel);
}}}

Download a sample plugin, here.
{{{
//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.

[[BR]]
Line 31: Line 31:
 . (1) Create a toolbarAction Create a toolbarAction
Line 34: Line 35:
MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this); 
}}}
MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this);
}}}
Line 39: Line 39:
 . (2) Add the action to Cytoscape toolbar Add the action to Cytoscape toolbar
Line 43: Line 44:
Line 56: Line 56:
//create a network without a view CyNetwork cyNetwork = 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);
{{{
//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);
}}}
Line 66: Line 80:
{{{
Line 67: Line 82:

The following statement will destroy a network. Cytoscape.destroyNetwork(cyNetwork);
}}}
The following statement will destroy a network.

{{{
Cytoscape.destroyNetwork(cyNetwork);
}}}
Line 73: Line 91:
// 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());
{{{
// 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());
}}}
Line 79: Line 103:
{{{
Line 80: Line 105:
}}}
Line 82: 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.
Line 85: 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!");
}


}
}}}
Line 88: Line 138:
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.
Line 90: Line 155:
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 92: Line 167:
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.
Line 94: Line 184:
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 96: Line 193:
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.
Line 98: Line 216:
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 100: Line 224:
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.
Line 102: Line 259:
Line 104: Line 260:
Line 106: Line 261:
Line 108: Line 262:

Line 111: Line 263:
Line 113: Line 264:
Line 115: Line 265:

TableOfContents([2])

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.

  1. A class, which extends CytoscapePlugin. This is the entry point to Cytoscape.

  2. A manifest file, explicitly list the class, which extended CytoscapePlugin class

  3. 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.

BR

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.

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.

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.

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.

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.

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

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.

How to apply a continuous color gradient to nodes according to their degree?

How to load a visual properties file?

How to write a layout algorithm?

How to customize node graphics?

How to write a Group Viewer?

How to add components to the node view, edge view, and attribute browser context menus?

How to save/restore plugin states?

How to use Cytoscape task monitor to show the progress of my job?

plugin_developer_tutorial (last edited 2011-09-06 18:52:37 by merlot)

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux