11444
Comment:
|
16207
|
Deletions are marked like this. | Additions are marked like this. |
Line 119: | Line 119: |
{{{ #!java }}} |
Cytoscape provides a service 'CyNetworkFactory' for network creation. To create a new network, all we should do is to get a reference to the service and use this service to create new network. With the new network, new nodes and edges can be created with the API of CyNetwork. {{{ #!java // To get a reference of CyNetworkFactory at CyActivator class of the App CyNetworkFactory cyNetworkFactoryServiceRef = getService(bc,CyNetworkFactory.class); ... // To create a new network CyNetwork myNet = cnf.createNetwork(); ... // Add two nodes to the network CyNode node1 = myNet.addNode(); CyNode node2 = myNet.addNode(); // set name for new nodes myNet.getDefaultNodeTable().getRow(node1.getSUID()).set("name", "Node1"); myNet.getDefaultNodeTable().getRow(node2.getSUID()).set("name", "Node2"); // Add an edge myNet.addEdge(node1, node2, true); }}} To destroy, we need a reference to Network manager {{{ #!java // To get a reference of CyNetworkManager at CyActivator class of the App CyNetworkManager netMgr = getService(bc,CyNetworkManager.class); // To destroy a network with NetworkManager netMgr.destroyNetwork(myNet); }}} |
Line 126: | Line 157: |
{{{ #!java |
Cytoscape provides a service 'CyNetworkViewFactory' for network view creation. To create a new network view, all we should do is to get a reference to the service and use this service to create new network view. {{{ #!java // To get a reference of CyNetworkViewFactory at CyActivator class of the App CyNetworkViewFactory cyNetworkViewFactoryServiceRef = getService(bc,CyNetworkViewFactory.class); ... // To create a new networkView for myNet CyNetworkView myView = cnvf.createNetworkView(myNet); ... }}} To destroy a network view, we need a reference of NetworkViewManager, a service provided by Cytoscape. {{{ #!java // get a reference of CyNetworkViewManager at CyActivator class CyNetworkViewManager cyNetworkViewManagerServiceRef = getService(bc,CyNetworkViewManager.class); ... // To destroy a network view through NetworkViewManager networkViewManager.destroyNetworkView(myView); |
Line 133: | Line 189: |
{{{ #!java |
We can use the class CyTableUtil to get the list of selected nodes in a network {{{ #!java //Get the selected nodes List<CyNode> nodes = CyTableUtil.getNodesInState(myNetwork,"selected",true); |
Line 139: | Line 197: |
{{{ #!java |
To handle Cytoscape events, a class must implements Cytosape listener interface and register the listener. {{{ #!java // Define a class, which implements a listener interface public class MyListenerClass implements NetworkAddedListener { .... public void handleEvent(NetworkAddedEvent e){ // do something here } } // Register the listener in the CyActivator class registerService(bc,myListenerClass, NetworkAddedListener.class, new Properties()); |
Line 145: | Line 214: |
{{{ #!java |
Network background color is one of the visual property of the visual style. {{{ #!java // Set the background of current view to RED view.setVisualProperty(BasicVisualLexicon.NETWORK_BACKGROUND_PAINT, Color.red); view.updateView(); |
Line 283: | Line 355: |
1. Get the service 'LoadVisualStyles' {{{ #!java }}} 2. Define a task to load the vizMap property file {{{ #!java }}} 3. Apply the newly loaded visual style to a network view {{{ #!java }}} |
Cytoscape provide a service 'LoadVizmapFileTaskFactory' for loading visual styles definded in a property file. {{{ #!java // get a reference to Cytoscape service -- LoadVizmapFileTaskFactory LoadVizmapFileTaskFactory loadVizmapFileTaskFactory = getService(bc,LoadVizmapFileTaskFactory.class); }}} {{{ #!java // Use the service to load visual style, 'f' is the File object to hold the visual properties Set<VisualStyle> vsSet = loadVizmapFileTaskFactory.loadStyles(f); }}} |
Line 305: | Line 370: |
{{{ #!java |
First define a layout class, which implements CyLayoutAlgorithm interface or extends AbstractLayoutAlgorithm class. Then register the layout class as a service. {{{ #!java // Define a layout class public class MyLayout extends AbstractLayoutAlgorithm { ... } // Define a layout task class public class MyLayoutTask extends AbstractLayoutTask { ... //Perform actual layout task final protected void doLayout(final TaskMonitor taskMonitor) { ... } ... } // Register the layout class as a service in CyActivator class Properties myLayoutProps = new Properties(); myLayoutProps.setProperty("preferredMenu","My Layouts"); registerService(bc,myLayout,CyLayoutAlgorithm.class, myLayoutProps); |
Line 332: | Line 422: |
{{{ #!java |
Get a Cytoscape provide DialogTaskManager and execute the task through the taskManager. {{{ #!java // Get a Cytoscape service 'DialogTaskManager' in CyActivator class DialogTaskManager dialogTaskManager = getService(bc, DialogTaskManager.class); // Define a task and set the progress in the run() method public class MyTask extends AbstractTask { ... public void run(final TaskMonitor taskMonitor) { // Give the task a title. taskMonitor.setTitle("My task"); ... taskMonitor.setProgress(0.1); // do something here ... taskMonitor.setProgress(1.0); } // Execute the task through the TaskManager DialogTaskManager.execute(myTaskFactory); |
Contents
- About the cookbook
- How to use the cookbook?
-
The list of app examples in the cookbook
- Introduction
- How to add a tabbed Panel to Control panel?
- How to add an image icon (menu item) to the toolbar?
- How to create a submenu?
- How to create, modify, and destroy a network, nodes, and edges?
- How to create, modify, and destroy a network view?
- How to determine which nodes are currently selected on a network?
- How to handle events from a network (and discuss each event type)?
- How to change the background color of a view?
- How to zoom a network view?
- How to load attribute data?
- How to remove attributes?
- How to use a web service client?
- How to write a web service client?
- How to use the VizMapper programmatically?
- 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 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 the Cytoscape task monitor to show the progress of my job?
- How to add new attribute functions via a Cytoscape plug-in?
- How to add plug-in specific help to the Cytoscape main help system?
- How to add NetworkViewTaskFactories to the right click or double click menus on network view?
- How to save/restore app states?
- Trouble shooting
- Recommendations
- App Porting Hints
- Questions, suggestions
About the cookbook
To help app developers to develop their own app, the Cytoscape developer team developed the cookbook, a collection of small apps. The cookbook could be used to
- get your feet wet
- find a template project to extend
- find out how to do something in Cytoscape.
How to use the cookbook?
SVN repository: http://chianti.ucsd.edu/svn/core3/support/trunk/samples/
Steps:
- Check out sample project from SVN repository with the above URL
- Compile with “mvn clean install”
- Copy the jar in the project target directory to Cytoscape installation: bundles/apps
- Start Cytoscape 3.0
The list of app examples in the cookbook
The list of sample apps is basically the same as in the cookbook for Cytoscape 2.X. They are mostly Cytoscape Bundle apps, but also include a few Simple apps. Some of them are,
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.
A class, which extends CytoscapePlugin. This is the entry point to Cytoscape.
A manifest file, explicitly list the class, which extended CytoscapePlugin class
A plugin.props file, which list the information about the plugin, such as plugin name, author, release date and more. Although this file is not absolutely required, it is recommended to have one, since plugin manager will need the information to handle the plugin properly. See the page at http://www.cytoscape.org/cgi-bin/moin.cgi/Cytoscape_Plugin_Tutorial for detail about the definition of a plugin.props.
If plugin developer will share and publish their plugins, they are encouraged to submit their plugins to Cytoscape plugin website at http://cytoscape.org/plugins/index.php .
How to add a tabbed Panel to Control panel?
It takes three steps to add a tabbed panel to the control panel.
1 //1. Define a CytoPanel class
2 public class MyCytoPanel extends JPanel implements CytoPanelComponent {
3
4 ...
5 @Override
6 public CytoPanelName getCytoPanelName() {
7 return CytoPanelName.WEST;
8 }
9 ...
10 }
11
12 // 2. Create an instance of MyCytoPanel
13 MyCytoPanel myPanel = new MyPanel();
14
15 //3. Register myPanel as a service
16 registerService(bc,myCytoPanel,CytoPanelComponent.class, new Properties());
17
Download a sample plugin, here.
How to add an image icon (menu item) to the toolbar?
1 // Define a CyAction class
2 public class AddImageIconAction extends AbstractCyAction {
3
4 public AddImageIconAction(CySwingApplication desktopApp){
5 ...
6 ImageIcon icon = new ImageIcon(getClass().getResource("/images/tiger.jpg"));
7
8 putValue(LARGE_ICON_KEY, icon);
9 ...
10 }
11
12 public boolean isInToolBar() {
13 return true;
14 }
15 ...
16 }
17
1 // Register the CyAction as service
2 AddImageIconAction addImageIconAction = new AddImageIconAction(cytoscapeDesktopService);
3 registerService(bc,addImageIconAction,CyAction.class, new Properties());
4
How to create a submenu?
1 // Define a CyAction class
2 public class Sample04 extends AbstractCyAction {
3 ...
4 public Sample04(CySwingApplication desktopApp){
5 // Add a sub-menu item -- Apps->Sample04->sample04
6 super("sample04...");
7 setPreferredMenu("Apps.Sample04");
8 //Specify the menuGravity value to put the menuItem in the desired place
9 setMenuGravity(2.0f);
10 ...
11 }
12
1 // Register the CyAction as service
2 Sample04 Sample04Action = new Sample04(cytoscapeDesktopService);
3 registerService(bc,Sample04Action,CyAction.class, new Properties());
4
How to create, modify, and destroy a network, nodes, and edges?
Cytoscape provides a service 'CyNetworkFactory' for network creation. To create a new network, all we should do is to get a reference to the service and use this service to create new network. With the new network, new nodes and edges can be created with the API of CyNetwork.
1 // To get a reference of CyNetworkFactory at CyActivator class of the App
2 CyNetworkFactory cyNetworkFactoryServiceRef = getService(bc,CyNetworkFactory.class);
3
4 ...
5
6 // To create a new network
7 CyNetwork myNet = cnf.createNetwork();
8
9 ...
10
11 // Add two nodes to the network
12 CyNode node1 = myNet.addNode();
13 CyNode node2 = myNet.addNode();
14
15 // set name for new nodes
16 myNet.getDefaultNodeTable().getRow(node1.getSUID()).set("name", "Node1");
17 myNet.getDefaultNodeTable().getRow(node2.getSUID()).set("name", "Node2");
18
19 // Add an edge
20 myNet.addEdge(node1, node2, true);
21
To destroy, we need a reference to Network manager
1 // To get a reference of CyNetworkManager at CyActivator class of the App
2 CyNetworkManager netMgr = getService(bc,CyNetworkManager.class);
3
4 // To destroy a network with NetworkManager
5 netMgr.destroyNetwork(myNet);
6
How to create, modify, and destroy a network view?
Cytoscape provides a service 'CyNetworkViewFactory' for network view creation. To create a new network view, all we should do is to get a reference to the service and use this service to create new network view.
1 // To get a reference of CyNetworkViewFactory at CyActivator class of the App
2 CyNetworkViewFactory cyNetworkViewFactoryServiceRef = getService(bc,CyNetworkViewFactory.class);
3
4 ...
5
6 // To create a new networkView for myNet
7 CyNetworkView myView = cnvf.createNetworkView(myNet);
8
9 ...
10
To destroy a network view, we need a reference of NetworkViewManager, a service provided by Cytoscape.
1 // get a reference of CyNetworkViewManager at CyActivator class
2 CyNetworkViewManager cyNetworkViewManagerServiceRef = getService(bc,CyNetworkViewManager.class);
3
4 ...
5
6 // To destroy a network view through NetworkViewManager
7 networkViewManager.destroyNetworkView(myView);
8
How to determine which nodes are currently selected on a network?
We can use the class CyTableUtil to get the list of selected nodes in a network
1 //Get the selected nodes
2 List<CyNode> nodes = CyTableUtil.getNodesInState(myNetwork,"selected",true);
3
How to handle events from a network (and discuss each event type)?
To handle Cytoscape events, a class must implements Cytosape listener interface and register the listener.
1 // Define a class, which implements a listener interface
2 public class MyListenerClass implements NetworkAddedListener {
3 ....
4 public void handleEvent(NetworkAddedEvent e){
5 // do something here
6 }
7 }
8
9
10 // Register the listener in the CyActivator class
11 registerService(bc,myListenerClass, NetworkAddedListener.class, new Properties());
12
How to change the background color of a view?
Network background color is one of the visual property of the visual style.
1 // Set the background of current view to RED
2 view.setVisualProperty(BasicVisualLexicon.NETWORK_BACKGROUND_PAINT, Color.red);
3 view.updateView();
4
How to zoom a network view?
1 // Define a task
2 public class ZoomTask extends AbstractNetworkViewTask {
3 ...
4 ZoomTask(CyNetworkView v) {
5 super(v);
6 ...
7 }
8
9 public void run(TaskMonitor tm) {
10 ...
11 // Get the scale and adjust it
12 double newScale = view.getVisualProperty(NETWORK_SCALE_FACTOR).doubleValue() * scale;
13 view.setVisualProperty(NETWORK_SCALE_FACTOR, newScale);
14
15 view.updateView();
16 }
17 }
18 ...
19 }
20
1 // Define taskFactory and pass in the view as parameter to the task
2 public class Sample10TaskFactory extends AbstractNetworkViewTaskFactory {
3 ....
4 public TaskIterator createTaskIterator() {
5 ...
6 return new TaskIterator(new ZoomTask(this.view));
7 ....
8 }
9 }
10
1 // Register the task factory as service
2 registerService(bc,sample10TaskFactory,TaskFactory.class, sample10TaskFactoryProps);
3
How to load attribute data?
There are two step to load attribute to a table. (1) Create a global table and populate it. (2) Map the global table to specific table based on a key attribute, i.e. create virtual column for the table.
1 // Define a task
2 public class CreateTableTask extends AbstractTask {
3 ....
4 @Override
5 public void run(TaskMonitor tm) throws IOException {
6 // Step 1: create a new table
7 CyTable table = tableFactory.createTable("MyAttrTable " + Integer.toString(numImports++),
8 "name", String.class, true, true);
9
10 // create a column for the table
11 String attributeNmae = "MyAttributeName";
12 table.createColumn(attributeNmae, Integer.class, false);
13
14 // Step 2: populate the table with some data
15 String[] keys = {"YLL021W","YBR170C","YLR249W"}; //map to the the "name" column
16 CyRow row = table.getRow(keys[0]);
17 row.set(attributeNmae, new Integer(2));
18
19 row = table.getRow(keys[1]);
20 row.set(attributeNmae, new Integer(3));
21
22 row = table.getRow(keys[2]);
23 row.set(attributeNmae, new Integer(4));
24
25 // We are loading node attribute
26 Class<? extends CyTableEntry> type = CyNode.class;
27
28 // Step 3: pass the new table to MapNetworkAttrTask
29 super.insertTasksAfterCurrentTask( new MapNetworkAttrTask(type,table,netMgr,appMgr,rootNetworkManager) );
30 }
31 ....
32 }
33
How to remove attributes?
1. get the CyTable through the network
1 // case for Node table
2 CyTable nodeTable = network.getDefaultNodeTable();
3
2. Find the column and delete it
1 if(nodeTable.getColumn(columnName)!= null){
2 nodeTable.deleteColumn(columnName);
3 }
4
How to use a web service client?
1
2
How to write a web service client?
1
2
How to use the VizMapper programmatically?
1
2
How to apply a continuous color gradient to nodes according to their degree?
1
2
How to load a visual properties file?
Cytoscape provide a service 'LoadVizmapFileTaskFactory' for loading visual styles definded in a property file.
1 // get a reference to Cytoscape service -- LoadVizmapFileTaskFactory
2 LoadVizmapFileTaskFactory loadVizmapFileTaskFactory = getService(bc,LoadVizmapFileTaskFactory.class);
3
1 // Use the service to load visual style, 'f' is the File object to hold the visual properties
2 Set<VisualStyle> vsSet = loadVizmapFileTaskFactory.loadStyles(f);
3
How to write a layout algorithm?
First define a layout class, which implements CyLayoutAlgorithm interface or extends AbstractLayoutAlgorithm class. Then register the layout class as a service.
1 // Define a layout class
2 public class MyLayout extends AbstractLayoutAlgorithm {
3 ...
4 }
5
6
7 // Define a layout task class
8 public class MyLayoutTask extends AbstractLayoutTask {
9 ...
10
11 //Perform actual layout task
12 final protected void doLayout(final TaskMonitor taskMonitor) {
13 ...
14 }
15 ...
16 }
17
18
19 // Register the layout class as a service in CyActivator class
20 Properties myLayoutProps = new Properties();
21 myLayoutProps.setProperty("preferredMenu","My Layouts");
22 registerService(bc,myLayout,CyLayoutAlgorithm.class, myLayoutProps);
23
How to write a Group Viewer?
1
2
How to add components to the node view, edge view, and attribute browser context menus?
1
2
How to save/restore plugin states?
1
2
How to use the Cytoscape task monitor to show the progress of my job?
Get a Cytoscape provide DialogTaskManager and execute the task through the taskManager.
1 // Get a Cytoscape service 'DialogTaskManager' in CyActivator class
2 DialogTaskManager dialogTaskManager = getService(bc, DialogTaskManager.class);
3
4
5 // Define a task and set the progress in the run() method
6 public class MyTask extends AbstractTask {
7 ...
8 public void run(final TaskMonitor taskMonitor) {
9 // Give the task a title.
10 taskMonitor.setTitle("My task");
11 ...
12 taskMonitor.setProgress(0.1);
13
14 // do something here
15
16 ...
17 taskMonitor.setProgress(1.0);
18 }
19
20
21 // Execute the task through the TaskManager
22 DialogTaskManager.execute(myTaskFactory);
23
How to add new attribute functions via a Cytoscape plug-in?
1
2
How to add plug-in specific help to the Cytoscape main help system?
1
2
How to add NetworkViewTaskFactories to the right click or double click menus on network view?
First the customized TaskFactory must implement NetworkViewTaskFactory interface or extend AbstractNetworkViewTaskFactory. Secondly, the service property "preferredAction" should be set to be "OPEN" for double click, or "NEW" for right click menu.
1 MyNetworkViewTaskFactory myNetworkViewTaskFactory = new MyNetworkViewTaskFactory(applicationManagerManagerServiceRef);
2
3 // Add double click menu to the network view
4 Properties myNetworkViewTaskFactoryProps = new Properties();
5 myNetworkViewTaskFactoryProps.setProperty("preferredAction","OPEN");
6 myNetworkViewTaskFactoryProps.setProperty("title","my title");
7
8 // Register the service
9 registerService(bc,myNetworkViewTaskFactory,NetworkViewTaskFactory.class, myNetworkViewTaskFactoryProps);
10
This also applies to add menu item to the double click / right click of nodeView or edgeView.
1 // To add a right click menu item on node view, set "preferredAction" to "NEW"
2 MyNodeViewTaskFactory myNodeViewTaskFactory = new MyNodeViewTaskFactory();
3
4 // Add double click menu item to the node view
5 Properties myNodeViewTaskFactoryProps = new Properties();
6 myNodeViewTaskFactoryProps.setProperty("preferredAction","NEW");
7 myNodeViewTaskFactoryProps.setProperty("title","my node action");
8
9 // Register the service
10 registerService(bc,myNodeViewTaskFactory,NodeViewTaskFactory.class, myNodeViewTaskFactoryProps);
11
How to save/restore app states?
1
2
Trouble shooting
If you get compile error,
Check the version number of parent POM, the latest is at Cytoscape repository
If this is a dependency problem, check the version number of depended bundle at Cytoscape repository
Recommendations
- If you’re a beginner you probably want to use the Simple app type. see example02a, example03a
- If you want to port as quickly as possible, again, go with the Simple app type.
- If you want to publish an API you must use the Bundle app type
- If you experience version conflicts or anticipate future version conflicts, again you must use the Bundle app type.
- If you are in doubt, you should probably use the Simple app type. You can always port it to the Bundle app type later should that become necessary. Both styles are supported and will be until (at least) version 4.0.
App Porting Hints
How to
- get current network --- see sample app 5
- get attributes --- see sample app 11
- add a menu item --- see sample app 3
Questions, suggestions
Please send e-mail to cytoscape help desk or discussion group