Cytoscape 2.X Plugin Developer Cookbook

Introduction

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.

   1 //First, get a handler to the cytoPanel west, which is the control panel
   2 CytoPanelImp ctrlPanel = (CytoPanelImp) Cytoscape.getDesktop().getCytoPanel(SwingConstants.WEST);
   3 
   4 //Create a JPanel object (a class extends JPanel, say, MyPanel)
   5 MyPanel myPanel = new MyPanel();
   6 
   7 //Add it to the control panel.
   8 ctrlPanel.add("myPanel", myPanel);
   9 

Download a sample plugin, Here.


How to add an image icon (menu item) to the toolbar?

Create a toolbarAction

   1 ImageIcon icon = new ImageIcon(getClass().getResource("/tiger.jpg"));
   2 MyPluginToolBarAction toolbarAction = new MyPluginToolBarAction(icon, this);
   3 

The toolbarAction must extend the class cytoscape.util.CytoscapeAction and its method isInToolBar()returns true.

Add the action to Cytoscape toolbar

   1 Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) toolbarAction);
   2 

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

   1 //create a network without a view CyNetwork
   2 yNetwork = Cytoscape.createNetwork("network1", false);
   3 
   4 CyNode node0 = Cytoscape.getCyNode("rain", true);
   5 CyNode node1 = Cytoscape.getCyNode("rainbow", true);
   6 CyNode node2 = Cytoscape.getCyNode("rabbit", true);
   7 CyNode node3 = Cytoscape.getCyNode("yellow", true);
   8 
   9 cyNetwork.addNode(node0);
  10 cyNetwork.addNode(node1);
  11 cyNetwork.addNode(node2);
  12 cyNetwork.addNode(node3);
  13 
  14 CyEdge edge0 = Cytoscape.getCyEdge(node0, node1, Semantics.INTERACTION, "pp", true);
  15 CyEdge edge1 = Cytoscape.getCyEdge(node0, node2, Semantics.INTERACTION, "pp", true);
  16 CyEdge edge2 = Cytoscape.getCyEdge(node0, node3, Semantics.INTERACTION, "pp", true);
  17 
  18 cyNetwork.addEdge(edge0);
  19 cyNetwork.addEdge(edge1);
  20 cyNetwork.addEdge(edge2);
  21 

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.

   1 cyNetwork.removeNode(node1.getRootGraphIndex(), true);
   2 

The following statement will destroy a network.

   1 Cytoscape.destroyNetwork(cyNetwork);
   2 

Download a sample plugin, Here.

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.

   1 // Create a view for the network CyNetworkView
   2 cyView = Cytoscape.createNetworkView(cyNetwork, "MyNetwork");
   3 

If a node is hidden, the node and all of its incident edges will be hidden.

   1 // hide a node
   2 cyNetwork.hideNode(node1.getRootGraphIndex());
   3 

The following statement will destroy a network view.

   1 Cytoscape.destroyNetworkView(cyView);
   2 

Download a sample plugin, Here.

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.

   1 CyNetwork current_network = Cytoscape.getCurrentNetwork();
   2 Set selectedNodes = current_network.getSelectedNodes();
   3 
   4 Iterator<Node> it = selectedNodes.iterator();
   5 while (it.hasNext()) {
   6         Node aNode = (Node) it.next();
   7         System.out.println(aNode.getIdentifier());
   8 }
   9 

Download a sample plugin, Here.


How to handle events from a network (and discuss each event type)?

   1 // Handle PropertyChangeEvent
   2 public void propertyChange(PropertyChangeEvent e) {
   3 if (e.getPropertyName().equalsIgnoreCase(Cytoscape.ATTRIBUTES_CHANGED))
   4 {
   5         System.out.println("Received an event -- Cytoscape.ATTRIBUTES_CHANGED!");
   6 }
   7 if (e.getPropertyName().equalsIgnoreCase(CytoscapeDesktop.NETWORK_VIEW_FOCUSED))
   8 {
   9                                 System.out.println("Received an event -- CytoscapeDesktop.NETWORK_VIEW_FOCUSED!");
  10 }
  11 
  12 
  13 }
  14 

Download a sample plugin, Here.


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.

   1 //Let user choose a color
   2 Color bkColor = JColorChooser.showDialog(Cytoscape.getDesktop(), "Please choose a background color", Color.white);
   3 
   4 // Change the background color for current view
   5 DingNetworkView  theView = (DingNetworkView) Cytoscape.getCurrentNetworkView();
   6 DingCanvas backgroundCanvas = theView.getCanvas(DGraphView.Canvas.BACKGROUND_CANVAS);
   7 backgroundCanvas.setBackground(bkColor);
   8 
   9 // Refresh the view
  10 Cytoscape.getCurrentNetworkView().updateView();
  11 

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,

   1 // Get reference to the view
   2 final CyNetworkView networkView = Cytoscape.getCurrentNetworkView();
   3 
   4 networkView.setZoom(scaleFactor);
   5 networkView.updateView();
   6 

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.

   1 String attributeName = "testAttribute";
   2 String AttributeValue = "testValue";
   3 CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes();
   4 cyNodeAttrs.setAttribute(node.getIdentifier(), attributeName, AttributeValue);
   5 

After attributes are loaded, it may be necessary to fire an event to notify other listeners that may have interest to the change.

   1 // inform others via property change event.
   2 Cytoscape.firePropertyChange(Cytoscape.ATTRIBUTES_CHANGED, null, null);
   3 

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.

   1 CyAttributes cyNodeAttrs = Cytoscape.getNodeAttributes();
   2 cyNodeAttrs.deleteAttribute(attributeName);
   3 

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,

   1 WebServiceClient<EUtilsServiceSoap> client =
   2         WebServiceClientManager.getClient("tutorial13");
   3 
   4 if (client == null) {
   5 System.out.println("Web service client tutorial13 is not found!");
   6         return;
   7 }
   8 

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.

   1 // eInfo utility returns a list of available databases
   2 EUtilsServiceSoap utils = (EUtilsServiceSoap) client.getClientStub();
   3 
   4 // call NCBI EInfo utility, "Available databases from NCBI";
   5 EInfoResult res = utils.run_eInfo(new EInfoRequest());
   6 

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

   1 WebServiceClientManager.registerClient(My_NCBIClient.getClient());
   2 

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.

   1 VisualMappingManager vmm = Cytoscape.getVisualMappingManager();
   2 Set<String> names = vmm.getCalculatorCatalog().getVisualStyleNames();
   3 

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 “Example Visual Style”.

   1       public static final String vsName = "Example Visual Style";
   2         VisualStyle createVisualStyle(CyNetwork network) {
   3 
   4                 NodeAppearanceCalculator nodeAppCalc = new NodeAppearanceCalculator();
   5                 EdgeAppearanceCalculator edgeAppCalc = new EdgeAppearanceCalculator();
   6                 GlobalAppearanceCalculator globalAppCalc = new GlobalAppearanceCalculator();
   7 
   8 
   9                 // Passthrough Mapping - set node label
  10                 //PassThroughMapping pm = new PassThroughMapping(new String(), "attr2");
  11                 PassThroughMapping pm = new PassThroughMapping(new String(), "attr2asdf");
  12                 Calculator nlc = new BasicCalculator("Example Node Label Calculator",
  13                                                      pm, VisualPropertyType.NODE_LABEL);
  14                 nodeAppCalc.setCalculator(nlc);
  15 
  16 
  17                 // Discrete Mapping - set node shapes
  18                 DiscreteMapping disMapping = new DiscreteMapping(NodeShape.RECT,
  19                                                                  ObjectMapping.NODE_MAPPING);
  20                 disMapping.setControllingAttributeName("attr1", network, false);
  21                 disMapping.putMapValue(new Integer(1), NodeShape.DIAMOND);
  22                 disMapping.putMapValue(new Integer(2), NodeShape.ELLIPSE);
  23                 disMapping.putMapValue(new Integer(3), NodeShape.TRIANGLE);
  24 
  25                 Calculator shapeCalculator = new BasicCalculator("Example Node Shape Calculator",
  26                                                                   disMapping,
  27                                                                                                                   VisualPropertyType.NODE_SHAPE);
  28                 nodeAppCalc.setCalculator(shapeCalculator);
  29 
  30 
  31                 // Continuous Mapping - set node color
  32                 ContinuousMapping continuousMapping = new ContinuousMapping(Color.WHITE,
  33                                                             ObjectMapping.NODE_MAPPING);
  34                 continuousMapping.setControllingAttributeName("attr3", network, false);
  35 
  36         Interpolator numToColor = new LinearNumberToColorInterpolator();
  37         continuousMapping.setInterpolator(numToColor);
  38 
  39                 Color underColor = Color.GRAY;
  40                 Color minColor = Color.RED;
  41                 Color midColor = Color.WHITE;
  42                 Color maxColor = Color.GREEN;
  43                 Color overColor = Color.BLUE;
  44 
  45                 // Create boundary conditions                  less than,   equals,  greater than
  46                 BoundaryRangeValues bv0 = new BoundaryRangeValues(underColor, minColor, minColor);
  47                 BoundaryRangeValues bv1 = new BoundaryRangeValues(midColor, midColor, midColor);
  48                 BoundaryRangeValues bv2 = new BoundaryRangeValues(maxColor, maxColor, overColor);
  49 
  50         // Set the attribute point values associated with the boundary values
  51                 continuousMapping.addPoint(0.0, bv0);
  52                 continuousMapping.addPoint(1.0, bv1);
  53                 continuousMapping.addPoint(2.0, bv2);
  54 
  55                 Calculator nodeColorCalculator = new BasicCalculator("Example Node Color Calc",
  56                                                                 continuousMapping,
  57                                                                                                          VisualPropertyType.NODE_FILL_COLOR);
  58                 nodeAppCalc.setCalculator(nodeColorCalculator);
  59 
  60 
  61                 // Discrete Mapping - Set edge target arrow shape
  62                 DiscreteMapping arrowMapping = new DiscreteMapping(ArrowShape.NONE,
  63                                                                    ObjectMapping.EDGE_MAPPING);
  64                 arrowMapping.setControllingAttributeName("interaction", network, false);
  65                 arrowMapping.putMapValue("pp", ArrowShape.ARROW);
  66                 arrowMapping.putMapValue("pd", ArrowShape.CIRCLE);
  67 
  68                 Calculator edgeArrowCalculator = new BasicCalculator("Example Edge Arrow Shape Calculator",
  69                                               arrowMapping, VisualPropertyType.EDGE_TGTARROW_SHAPE);
  70                 edgeAppCalc.setCalculator(edgeArrowCalculator);
  71 
  72 
  73                 // Create the visual style
  74                 VisualStyle visualStyle = new VisualStyle(vsName, nodeAppCalc, edgeAppCalc, globalAppCalc);
  75 
  76                 return visualStyle;
  77         }
  78 

The following snippet of code shows how to use the above createVisualStyle(network) method. After the visualStyle is created, it will appear in the comboBox of VizMapper main panel.

   1                 // get the network and view
   2                 CyNetwork network = Cytoscape.getCurrentNetwork();
   3                 CyNetworkView networkView = Cytoscape.getCurrentNetworkView();
   4 
   5                 // get the VisualMappingManager and CalculatorCatalog
   6                 VisualMappingManager manager = Cytoscape.getVisualMappingManager();
   7                 CalculatorCatalog catalog = manager.getCalculatorCatalog();
   8 
   9                 // check to see if a visual style with this name already exists
  10                 VisualStyle vs = catalog.getVisualStyle(vsName);
  11                 if (vs == null) {
  12                         // if not, create it and add it to the catalog
  13                         vs = createVisualStyle(network);
  14                         catalog.addVisualStyle(vs);
  15                 }
  16 
  17                 networkView.setVisualStyle(vs.getName()); // not strictly necessary
  18 
  19                 // actually apply the visual style
  20                 manager.setVisualStyle(vs);
  21                 networkView.redrawGraph(true,true);
  22 

Download a sample plugin, Here.


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

To map node degree to node color, first we need to define a calculator with continuous mapping for the visual attribute NODE_FILL_COLOR. Then set the calculator to the NodeAppearanceCalculator of the visual style to be applied.

The following code snippet will set the calculator for a visualStyle.

   1 // Create a node color calculator for "Degree" attribute
   2 Calculator nodeColorCalculator = createCalculator(); // see below
   3 
   4 // Set the calculator to the visualStyle
   5 vs.getNodeAppearanceCalculator().setCalculator(nodeColorCalculator);
   6 

The following code snippet shows how to create a calculator using Degree as controlling attribute.

   1 VisualPropertyType type = VisualPropertyType.NODE_FILL_COLOR;
   2 final Object defaultObj = type.getDefault(Cytoscape.getVisualMappingManager().getVisualStyle());
   3 
   4 ContinuousMapping cm = new ContinuousMapping(defaultObj, ObjectMapping.NODE_MAPPING);
   5 // Set controlling Attribute
   6 cm.setControllingAttributeName("Degree", Cytoscape.getCurrentNetwork(), false);
   7 
   8 Interpolator numToColor = new LinearNumberToColorInterpolator();
   9 cm.setInterpolator(numToColor);
  10 
  11 Color underColor = Color.GRAY;
  12 Color minColor = Color.RED;
  13 Color midColor = Color.WHITE;
  14 Color maxColor = Color.GREEN;
  15 Color overColor = Color.BLUE;
  16 
  17 BoundaryRangeValues bv0 = new BoundaryRangeValues(underColor, minColor, minColor);
  18 BoundaryRangeValues bv1 = new BoundaryRangeValues(midColor, midColor, midColor);
  19 BoundaryRangeValues bv2 = new BoundaryRangeValues(maxColor, maxColor, overColor);
  20 
  21 // Set the attribute point values associated with the boundary values
  22 // The points p1, p2, p3 are the values between (min~max) of the degree
  23 cm.addPoint(p1, bv0); cm.addPoint(p2, bv1); cm.addPoint(p3, bv2);
  24 
  25 // Create a calculator
  26 BasicCalculator myCalculator = new BasicCalculator("My degree calcualtor", cm, VisualPropertyType.NODE_FILL_COLOR);
  27 

Download a sample plugin, Here.


How to load a visual properties file?

Cytoscape supported import/export of visual properties file. They can be found at File --> Import --> Vizmap Property File and File--> Export --> Vizmap Property File. To load a vizmap property file programmatically, we can reuse most of the code in the Cytoscape core class “cytoscape.actions.ImportVizmapAction”.

Cytoscape already had build-in class to handle the import of Vzmap property file, all we need is to fire a VIZMAP_LOADED event as following

   1 Cytoscape.firePropertyChange(Cytoscape.VIZMAP_LOADED, null, file.getAbsolutePath());
   2 

After the event is fired, a dialog box will prompt the Import of Vizmap Property File.

Download a sample plugin, Here.

How to write a layout algorithm?

All Cytoscape layout algorithms are grouped together and accessible though the layout menu. To add a new layout algorithm, the new layout class should implement the interface CyLayoutAlgorithm, or extend the AbstractLayout class. The new algorithm should then register with the CyLayout manager. In this way, a new menu item, name defined in the new layout algorithm, will be available under Layout menu.

The following line shows how to register a layout algorithm with the layout manager,

   1 CyLayouts.addLayout(new MyLayout(), "My Layouts");
   2 

One of the methods that a CyLayoutAlgorithm must implement is a getSettings() method, which returns an object of LayoutProperties that winds up under the Settings menu. Layout parameters may be supplied with the LayoutProperties. For example, the following statement defines a parameter in the LayoutProperties – name, description, data type and value.

   1 layoutProperties.add(new Tunable("groupcount", "Number of random groups ",
   2 Tunable.INTEGER, new Integer(2)));
   3 

Download a sample plugin, Here.


How to write a Group Viewer?

A class of CyGroupViewer must implement the CyGroupViewer interface and it should register with the CyGroupManager.

   1 MyGroupViewer myView = new MyGroupViewer();
   2 // Register with CyGroup
   3 CyGroupManager.registerGroupViewer(myView);
   4 

Download a sample plugin, Here.


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

To add a component to content menu of a node view, first we need create a listener class, which implements the interface NodeContextMenuListener, then register this listener through the network view.

Here is what the listener class looks like,

   1 class MyNodeContextMenuListener implements NodeContextMenuListener {
   2         public void addNodeContextMenuItems(NodeView nodeView, JPopupMenu menu)
   3         {
   4                 JMenuItem myMenuItem = new JMenuItem("MyNodeMenuItem");
   5 
   6                 myMenuItem.addActionListener(new MyNodeAction(nodeView));
   7                 if (menu == null) {
   8                         menu = new JPopupMenu();
   9                 }
  10                 menu.add(myMenuItem);
  11         }
  12 }
  13 

The method addNodeContentMenuItems() takes two arguments, NodeView and !JPopupMenu. The !JPopupMenu will be the one show up when user right-click on the NodeView on canvas. The sample code above will add a new menu item “MyNodeMenuItem” in the popupMenu. The MyNodeAction class, which implements ActionListener, will be the class performs the action after the menu item is clicked. The NodeView parameter to the listener will provide the reference to the Node via NodeView.getNode() method and take whatever action desired.

The following two lines will register the listener with the current view,

   1 MyNodeContextMenuListener l = new MyNodeContextMenuListener();
   2 Cytoscape.getCurrentNetworkView().addNodeContextMenuListener(l);
   3 

The sample works for NodeView, case for the EdgeView is very similar.

Attribute browser is a Cytoscape core plugin, it also provides interface to add new menu item to the conextMenu (since Cytoscape 2.6).

   1 import browser.AttributeBrowserPlugin;
   2 
   3 
   4 JMenuItem menuItem = new JMenuItem("MyMenuItem_browser");
   5 menuItem.addActionListener(actionListener);
   6 AttributeBrowserPlugin.addMenuItem(browser.DataObjectType.NODES, menuItem);
   7 

Note that there are three browser panels for NODE, EDGE and NETWORK, we should explicitly specify which browser a menuItem will be added. In the sample code above, a menu item is added to contextMenu of the node browser.

Download a sample plugin, Here.


How to save/restore plugin states?

All Cytoscape plugin extends CytoscapePlugin class, which implements PropertychangeListener. In order to save/restore plugin properties (globally), the plugin need to overwrite the method “onCytoscapeExit()” for property saving and some code in plugin constructor for restore.

   1 /**
   2 * Save global state to "tutorial21.props"
   3 */
   4 public void onCytoscapeExit() {}
   5 
   6 // restore state
   7 public void restoreInitState() {
   8         File global_prop_file = CytoscapeInit.getConfigFile("tutorial21.props");
   9 }
  10 

If plugin need to save states in Session, overwrite the following two methods.

   1 public void saveSessionStateFiles(List<File> pFileList) {}
   2 public void restoreSessionState(List<File> pStateFileList) {}
   3 

Download a sample plugin, Here.


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

If a task will take a long time to finish, it is better to use Cytoscape task manager to run it in a separate thread and show its progress to the user. With the Task manager, it is possible to cancel the task, and the window won’t freeze.

Download a sample plugin, Here.


How to add new attribute functions via a Cytoscape plug-in

Here we will go through all the steps necessary to create a new built-in function IXOR(). The complete example code and can be downloaded from here tutorial-23.zip.

The easiest part is writing the actual plug-in class, which look similar to this:

   1 package cytoscape.tutorial23;
   2 
   3 
   4 import cytoscape.plugin.CytoscapePlugin;
   5 
   6 import org.cytoscape.equations.EqnParser;
   7 import org.cytoscape.equations.Parser;
   8 
   9 
  10 public class Tutorial23 extends CytoscapePlugin {
  11         public Tutorial23() {
  12                 final EqnParser theParser = Parser.getParser();
  13                 theParser.registerFunction(new IXor());
  14         }
  15 }
  16 

Here we register a new built-in function called IXor. You can also register multiple built-in functions if you prefer.

The next part is creating one class each for every new built-in. Each such class has to implement the org.cytoscape.equations.Function interface usually via org.cytoscape.equations.AbstractFunction. The easiest way to get started is to peruse the existing built-ins in equations Cytoscape core library and look for a function with a similar or identical argument list. If you can't find one it may still be instructive to read through the implementation of a couple of existing functions.

First you have to create the constructor where you describe the arguments that your function will take:

public IXor() {
        super(new ArgDescriptor[] {
                        new ArgDescriptor(ArgType.INT, "arg1", "A quantity that can be converted to an integer."),
                        new ArgDescriptor(ArgType.INT, "arg2", "A quantity that can be converted to an integer."),
                });
}

The most trivial to implement methods are getName(), and getFunctionSummary().

   1 /**                                                                                                                                                                                                                                                  
   2  *  Used to parse the function string.  This name is treated in a case-insensitive manner!                                                                                                                                                           
   3  *  @return the name by which you must call the function when used in an attribute equation.                                                                                                                                                        
   4  */
   5 public String getName() { return "IXOR"; }
   6 
   7 /**                                                                                                                                                                                                                                                  
   8   *  Used to provide help for users.                                                                                                                                                                                                                  
   9   *  @return a description of what this function does                                                                                                                                                                                                
  10   */
  11 public String getFunctionSummary() { return "Returns an integer value that is the exclusive-or of 2 other integer values."; }
  12 

The next method is still simple but already touches on the one area that is somewhat complicated in attribute functions: data types and data type conversions!

   1 public Class getReturnType() { return Long.class; }
   2 

Our example function is supposed to return an integer value. Integers in equation functions are represented as instances of class java.lang.Long. Therefore it is imperative not to return Integer.class! The user of the function can be blissfully unaware of this distinction. Just remember to always substitute Long for Integer and you should be fine.

The next method evaluateFunction() is the one that gets called when an expression is being evaluated. No argument type-checking is needed here because the compiler already took care of that for us. But, we need to handle all possible valid argument types and counts. (N.B., functions may be overloaded and/or variadic.) In this example the arguments can be any combination of Long and Double.

   1 public Object evaluateFunction(final Object[] args) {
   2         long arg1;
   3         try {
   4                 arg1 = FunctionUtil.getArgAsLong(args[0]);
   5         } catch (final Exception e) {
   6                 throw new IllegalArgumentException("IXOR: can't convert the 1st argument to an integer!");
   7         }
   8 
   9         long arg2;
  10         try {
  11                 arg2 = FunctionUtil.getArgAsLong(args[0]);
  12         } catch (final Exception e) {
  13                 throw new IllegalArgumentException("IXOR: can't convert the 2nd argument to an integer!");
  14         }
  15 
  16         final long result = arg1 ^ arg2;
  17         return (Long)result;
  18 }
  19 

See Also

This tutorial on how to write attribute functions may also be helpful.

Final Comments

If you find functions in Exceltm that provide similar functionality to what you plan to implement, please try to match Exceltm's functionality as closely as possible. Many users are familiar with either Exceltm or another spreadsheet program that implements Exceltm-compatible functions and by following its lead you may greatly reduce the cognitive overhead you place on your users.

Also, if you believe that the functionality you request may well be generally useful and not a one-off for your own organisation, you may consider contacting the Cytoscape development team at UCSD and suggest to them to implement it for you.

How to add plug-in specific help to the Cytoscape main help system

This tutorial tutorial24.zip provides a step-by-step recipe on adding additional help topics from within a plug-in to the Cytoscape help.

Start with unzipping the attached example and copying the help subdirectory tree into your own project. Next copy the help target of the example's build.xml file to your own ant file or implement equivalent functionality in your own build system. Make sure not to forget to actually invoke the help target. In the main entry point of your plug-in, add the following function:

        ...
import cytoscape.view.CyHelpBroker;
import javax.help.HelpSet;
import java.net.URL;
        ...
        /**                                                                                                                                         
         *  Hook plugin help into the Cytoscape main help system:                                                                                   
         */
        private void addHelp() {
                final String HELP_SET_NAME = "/help/jhelpset";
                final ClassLoader classLoader = Tutorial24.class.getClassLoader();
                URL helpSetURL;
                try {
                        helpSetURL = HelpSet.findHelpSet(classLoader, HELP_SET_NAME);
                        final HelpSet newHelpSet = new HelpSet(classLoader, helpSetURL);
                        if (!CyHelpBroker.addHelpSet(newHelpSet))
                                System.err.println("Tutorial24: Failed to add help set!");
                } catch (final Exception e) {
                        System.err.println("Tutorial24: Could not find help set: \"" + HELP_SET_NAME + "!");
                }
        }

Invoke this function somewhere in the constructor for the class that extends CytoscapePlugin. Finally, edit the files in the help directory. Most likely you would want to edit the files named jhelpmap.jhm and Topic.html. You may consider removing both, SubTopic.html and the reference to it in jhelpmap.jhm. You can also add additional topics, if you want. The effect this will have is to add a new topic at the very bottom of the Cytoscape help index. You may have a help button in your plug-in, in which case you would probably want to add code similar to the following:

       ...
import cytoscape.view.CyHelpBroker;
       ...
       CyHelpBroker.getHelpBroker().enableHelpOnButton(helpButton, "Topic", null);
       ...

That should be all that is needed to dynamically add your own help topic(s)!

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