Differences between revisions 32 and 41 (spanning 9 versions)
Revision 32 as of 2008-05-20 17:02:03
Size: 21956
Editor: cabernet
Comment:
Revision 41 as of 2009-05-06 07:45:17
Size: 26933
Editor: csik
Comment: fix table of contents
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[TableOfContents([2])]] <<TableOfContents(2)>>
Line 17: Line 17:
#!java
Line 25: Line 26:
 }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial01.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial01.zip|Here]].

<<BR>>
Line 34: Line 36:
#!java
Line 36: Line 39:
 }}}
}}}
Line 42: Line 46:
#!java
Line 43: Line 48:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial02.tar Here].

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial02.zip|Here]].
Line 49: Line 55:
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial03.tar Here].
[[
BR]]
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial03.zip|Here]]. <<BR>>
Line 58: Line 63:
#!java
Line 78: Line 84:
 }}}
}}}
Line 82: Line 89:
#!java
Line 83: Line 91:
 }}}
}}}
Line 87: Line 96:
#!java
Line 88: Line 98:
 }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial04.tar Here].

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial04.zip|Here]].
Line 96: Line 106:
#!java
Line 98: Line 109:
 }}}
}}}
Line 102: Line 114:
#!java
Line 104: Line 117:
 }}}
}}}
Line 108: Line 122:
#!java
Line 109: Line 124:
 }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial05.tar Here].
[[
BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial05.zip|Here]]. <<BR>>
Line 118: Line 132:
#!java
Line 126: Line 141:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial06.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial06.zip|Here]].

<<BR>>
Line 132: Line 149:
#!java
Line 145: Line 163:
 }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial07.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial07.zip|Here]].

<<BR>>
Line 155: Line 173:
#!java
Line 165: Line 184:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial08.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial08.zip|Here]].

<<BR>>
Line 173: Line 194:
#!java
Line 178: Line 200:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial09.tar Here].

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial09.zip|Here]].
Line 185: Line 208:
#!java
Line 189: Line 213:
 }}}
}}}
Line 193: Line 218:
#!java
Line 195: Line 221:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial10.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial10.zip|Here]].

<<BR>>
Line 203: Line 231:
#!java
Line 205: Line 234:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial11.tar Here].

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial11.zip|Here]].
Line 212: Line 242:
#!java
Line 219: Line 250:
 }}}
}}}
Line 223: Line 255:
#!java
Line 228: Line 261:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial12.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial12.zip|Here]].

<<BR>>
Line 236: Line 271:
{{{
{{{
#!java
Line 238: Line 275:
 }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial13.tar Here].

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial13.zip|Here]].
Line 246: Line 283:
#!java
Line 248: Line 286:
 }}}
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, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial14.tar Here].

[[BR]]

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

{{{
#!java
      public static final String vsName = "Example Visual Style";
        VisualStyle createVisualStyle(CyNetwork network) {

                NodeAppearanceCalculator nodeAppCalc = new NodeAppearanceCalculator();
                EdgeAppearanceCalculator edgeAppCalc = new EdgeAppearanceCalculator();
                GlobalAppearanceCalculator globalAppCalc = new GlobalAppearanceCalculator();


                // Passthrough Mapping - set node label
                //PassThroughMapping pm = new PassThroughMapping(new String(), "attr2");
                PassThroughMapping pm = new PassThroughMapping(new String(), "attr2asdf");
                Calculator nlc = new BasicCalculator("Example Node Label Calculator",
                                                     pm, VisualPropertyType.NODE_LABEL);
                nodeAppCalc.setCalculator(nlc);


                // Discrete Mapping - set node shapes
                DiscreteMapping disMapping = new DiscreteMapping(NodeShape.RECT,
                                                                 ObjectMapping.NODE_MAPPING);
                disMapping.setControllingAttributeName("attr1", network, false);
                disMapping.putMapValue(new Integer(1), NodeShape.DIAMOND);
                disMapping.putMapValue(new Integer(2), NodeShape.ELLIPSE);
                disMapping.putMapValue(new Integer(3), NodeShape.TRIANGLE);

                Calculator shapeCalculator = new BasicCalculator("Example Node Shape Calculator",
                                                                  disMapping,
                                                                                                                  VisualPropertyType.NODE_SHAPE);
                nodeAppCalc.setCalculator(shapeCalculator);


                // Continuous Mapping - set node color
                ContinuousMapping continuousMapping = new ContinuousMapping(Color.WHITE,
                                                            ObjectMapping.NODE_MAPPING);
                continuousMapping.setControllingAttributeName("attr3", network, false);

        Interpolator numToColor = new LinearNumberToColorInterpolator();
        continuousMapping.setInterpolator(numToColor);

                Color underColor = Color.GRAY;
                Color minColor = Color.RED;
                Color midColor = Color.WHITE;
                Color maxColor = Color.GREEN;
                Color overColor = Color.BLUE;

                // Create boundary conditions less than, equals, greater than
                BoundaryRangeValues bv0 = new BoundaryRangeValues(underColor, minColor, minColor);
                BoundaryRangeValues bv1 = new BoundaryRangeValues(midColor, midColor, midColor);
                BoundaryRangeValues bv2 = new BoundaryRangeValues(maxColor, maxColor, overColor);

        // Set the attribute point values associated with the boundary values
                continuousMapping.addPoint(0.0, bv0);
                continuousMapping.addPoint(1.0, bv1);
                continuousMapping.addPoint(2.0, bv2);

                Calculator nodeColorCalculator = new BasicCalculator("Example Node Color Calc",
                                                                continuousMapping,
                                                                                                         VisualPropertyType.NODE_FILL_COLOR);
                nodeAppCalc.setCalculator(nodeColorCalculator);


                // Discrete Mapping - Set edge target arrow shape
                DiscreteMapping arrowMapping = new DiscreteMapping(ArrowShape.NONE,
                                                                   ObjectMapping.EDGE_MAPPING);
                arrowMapping.setControllingAttributeName("interaction", network, false);
                arrowMapping.putMapValue("pp", ArrowShape.ARROW);
                arrowMapping.putMapValue("pd", ArrowShape.CIRCLE);

                Calculator edgeArrowCalculator = new BasicCalculator("Example Edge Arrow Shape Calculator",
                                              arrowMapping, VisualPropertyType.EDGE_TGTARROW_SHAPE);
                edgeAppCalc.setCalculator(edgeArrowCalculator);


                // Create the visual style
                VisualStyle visualStyle = new VisualStyle(vsName, nodeAppCalc, edgeAppCalc, globalAppCalc);

                return visualStyle;
        }


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

{{{
#!java
                // get the network and view
                CyNetwork network = Cytoscape.getCurrentNetwork();
                CyNetworkView networkView = Cytoscape.getCurrentNetworkView();

                // get the VisualMappingManager and CalculatorCatalog
                VisualMappingManager manager = Cytoscape.getVisualMappingManager();
                CalculatorCatalog catalog = manager.getCalculatorCatalog();

                // check to see if a visual style with this name already exists
                VisualStyle vs = catalog.getVisualStyle(vsName);
                if (vs == null) {
                        // if not, create it and add it to the catalog
                        vs = createVisualStyle(network);
                        catalog.addVisualStyle(vs);
                }

                networkView.setVisualStyle(vs.getName()); // not strictly necessary

                // actually apply the visual style
                manager.setVisualStyle(vs);
                networkView.redrawGraph(true,true);

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial14.zip|Here]].

<<BR>>
Line 282: Line 407:
{{{
{{{
#!java
Line 286: Line 413:
// Set the calculator to the visualStyle    // Set the calculator to the visualStyle
Line 288: Line 415:
 }}}   
}}}
Line 291: Line 418:
{{{
{{{
#!java
Line 294: Line 423:
   
Line 301: Line 430:
   
Line 307: Line 436:
   
Line 311: Line 440:
   
Line 315: Line 444:
   
Line 317: Line 446:
BasicCalculator myCalculator = new BasicCalculator("My degree calcualtor", cm, VisualPropertyType.NODE_FILL_COLOR);     }}}

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial15.tar Here].

[[
BR]]
BasicCalculator myCalculator = new BasicCalculator("My degree calcualtor", cm, VisualPropertyType.NODE_FILL_COLOR);

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial15.zip|Here]].

<<
BR>>
Line 325: Line 454:
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 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”.
Line 328: Line 457:
{{{
{{{
#!java
Line 330: Line 461:
 }}}
}}}
Line 333: Line 465:

Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial16.tar Here].
[[
BR]]
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial16.zip|Here]]. <<BR>>
Line 338: Line 468:
Line 342: Line 471:
{{{
{{{
#!java
Line 344: Line 475:
 }}}
}}}
Line 346: Line 478:
{{{
{{{
#!java
Line 349: Line 483:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial17.tar Here].


[[BR]]
== How to customize node graphics? ==



Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial18.tar Here
].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial17.zip|Here]].

<<BR>>
Line 363: Line 490:



Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsf/scooter/sampleGroupViewer Here].

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

{{{
#!java
MyGroupViewer myView = new MyGroupViewer();
// Register with CyGroup
CyGroupManager.registerGroupViewer(myView);

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsf/scooter/sampleGroupViewer|Here]].

<<BR>>
Line 370: Line 504:
Line 374: Line 507:
{{{
{{{
#!java
Line 376: Line 511:
 public void addNodeContextMenuItems(NodeView nodeView, JPopupMenu menu)   {    JMenuItem myMenuItem = new JMenuItem("MyNodeMenuItem");
       myMenuItem.addActionListener(new MyNodeAction(nodeView));        if (menu == null) {
   menu = new JPopupMenu();
  }
  
menu.add(myMenuItem);   }
}
 }}}
        public void addNodeContextMenuItems(NodeView nodeView, JPopupMenu menu)
        
{
                
JMenuItem myMenuItem = new JMenuItem("MyNodeMenuItem");

                
myMenuItem.addActionListener(new MyNodeAction(nodeView));
                
if (menu == null) {
                        menu = new JPopupMenu();
                }
                
menu.add(myMenuItem);
        }
}

}}}
Line 391: Line 527:
{{{
{{{
#!java
Line 393: Line 531:
Cytoscape.getCurrentNetworkView().addNodeContextMenuListener(l); 
 }}}
Cytoscape.getCurrentNetworkView().addNodeContextMenuListener(l);

}}}
Line 398: Line 537:
{{{
import browser.AttributeBrowserPlugin; 

{{{
#!java

import browser.AttributeBrowserPlugin;
Line 405: Line 546:
 }}}
}}}
Line 408: Line 550:
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial20.tar Here].

[[BR]]
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial20.zip|Here]].

<<BR>>
Line 412: Line 555:
Line 414: Line 556:
{{{
{{{
#!java
Line 422: Line 566:
 File global_prop_file = CytoscapeInit.getConfigFile("tutorial21.props");
}
 }}}
        File global_prop_file = CytoscapeInit.getConfigFile("tutorial21.props");
}

}}}
Line 426: Line 571:
{{{
{{{
#!java
Line 429: Line 576:
 }}}
Download a sample plugin, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial21.tar Here].

[[BR]]

}}}
Download a sample plugin, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial21.zip|Here]].

<<BR>>
Line 434: Line 583:

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, [http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial22.tar Here].
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, [[http://chianti.ucsd.edu/svn/csplugins/trunk/ucsd/pwang/wikizip/tutorial22.zip|Here]].

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.

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

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