Tutorial:Creating a Simple Cytoscape 3 App

From OpenTutorials
Jump to: navigation, search

The simple app format has been deprecated - all apps for 3.4 and future versions should be developed as OSGi (bundle) apps.

Example source code (2 Java source files)


Cytoscape is an open source software tool for integrating, visualizing, and analyzing data in the context of networks. This tutorial will cover:

  1. Writing the code for a functional simple app for Cytoscape 3
  2. Generation of the app jar file
  3. Deploying of the final app into Cytoscape 3

Introduction

We will demonstrate how to create a simple (vs. a bundle) app for Cytoscape 3 by implementing a fully functional app. This app will add a menu item under the Select menu that when invoked will hide all unconnected (singleton) nodes in the current network view.

Prerequisites

To start off, you will need to have Cytoscape (version 3 or later) installed as well as a copy of the Java Development Kit (JDK) version 1.6 or later. Additionally, you should download the Cytoscape Swing App API JAR - this is available for Cytoscape 3.1 here. Remember the path you saved this file to, as you will need it later.

After downloading and installing Cytoscape and the Swing App API JAR, create a directory for your sample app's source code.

Main App Class

Next we create the skeleton code which is the same for all simple apps and looks as follows. Note that in this sample Java class, we do not put our code in a package, so all our classses will be in a single directory.

import org.cytoscape.app.swing.AbstractCySwingApp;
import org.cytoscape.app.swing.CySwingAppAdapter;

public class HideSingletonNodesApp extends AbstractCySwingApp 
{
	public HideSingletonNodesApp(CySwingAppAdapter adapter)
	{
		super(adapter);
        // Add any customization here!
	}
}

We save this code in a file called HideSingletonNodesApp.java.

Menu Action Class

Next we add support for a Cytoscape menu item. The skeleton for that code looks like this:

import java.awt.event.ActionEvent;
import org.cytoscape.application.swing.AbstractCyAction;
import org.cytoscape.app.CyAppAdapter;


public class MenuAction extends AbstractCyAction {
    private final CyAppAdapter adapter;

    public MenuAction(CyAppAdapter adapter) {
        super("Hide unconnected nodes",
            adapter.getCyApplicationManager(),
            "network",
            adapter.getCyNetworkViewManager());
        this.adapter = adapter;
        setPreferredMenu("Select");
    }

    public void actionPerformed(ActionEvent e) {
        // Add your  code here!
    }
}

We save this new code in a file called MenuAction.java.

Main App Class (final)

We add the line adapter.getCySwingApplication().addAction(new MenuAction(adapter)); in HideSingletonNodesApp.java which leads to this final version:

import org.cytoscape.app.swing.AbstractCySwingApp;
import org.cytoscape.app.swing.CySwingAppAdapter;

public class HideSingletonNodesApp extends AbstractCySwingApp 
{
	public HideSingletonNodesApp(CySwingAppAdapter adapter)
	{
		super(adapter);
		adapter.getCySwingApplication()
                    .addAction(new MenuAction(adapter));
	}
}

Menu Action Class (final)

Finally we add the code that actually hides the singleton nodes to "MenuAction.java" in the actionPerformed() method. We also add several new import declarations for the APIs we need to get this final version:

import java.awt.event.ActionEvent;

import org.cytoscape.app.CyAppAdapter;
import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.swing.AbstractCyAction;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;


public class MenuAction extends AbstractCyAction {
    private final CyAppAdapter adapter;

    public MenuAction(CyAppAdapter adapter) {
        super("Hide unconnected nodes",
            adapter.getCyApplicationManager(),
            "network",
            adapter.getCyNetworkViewManager());
        this.adapter = adapter;
        setPreferredMenu("Select");
    }
 
    public void actionPerformed(ActionEvent e) {
        final CyApplicationManager manager = adapter.getCyApplicationManager();
        final CyNetworkView networkView = manager.getCurrentNetworkView();
        final CyNetwork network = networkView.getModel();
        
        for (CyNode node : network.getNodeList()) {
            if (network.getNeighborList(node, CyEdge.Type.ANY).isEmpty())
                networkView.getNodeView(node).setVisualProperty(
                    BasicVisualLexicon.NODE_VISIBLE, false);
        }

        networkView.updateView();
    }
}

Generation of the App Jar File

Now we compile both classes with:

javac -cp ".:/path/2/cytoscape-swing-app-api-3.x.x.jar" *.java

Substitute the path to the Cytoscape Swing App API JAR you downloaded above. This results in two .class files, which can be bundled into a JAR file to be used with Cytoscape.

NOTE: if you have the Java 7 JDK installed, the resulting classes will only work on Cytoscape when using Java 7 or above. If you want to compile the app for Java 6, you can either use the Java 6 JDK or explicitly specify 1.6 as the source and target VM as follows:

javac -source 1.6 -target 1.6 -cp ".:/path/2/cytoscape-swing-app-api-3.x.x.jar" *.java

Note that you may receive a warning stating "bootstrap class path not set in conjunction with -source 1.6" (or something similar). This is just warning you that you are targeting Java 1.6 without using a 1.6 Java library. The only real consequence is that the compiler would not catch the use of 1.7-only classes (that won't work on 1.6) - if you are worried about this, you can specify the use of a Java 1.6 library using the -Xbootclasspath option. The location of the relevant library depends on where the Java 6 JVM is installed - it can be found in that directory under jre/lib/rt.jar (except on Mac, where this file can be found under /System/Library/Frameworks/JavaVM.framework/Classes/classes.jar).

A simple app is a Java jar file with a META-INF/MANIFEST.MF file. The "Cytoscape-App" entry specifies the path and name of the class that is derived from AbstractCyApp and is the main entry point for the app. In order to get this into our app jar file we create a file which we name app-manifest with the following contents (Make sure that the one line in app-manifest ends in a line end character/newline!):

Manifest-Version: 1.0
Cytoscape-API-Compatibility: 3.0
Cytoscape-App: HideSingletonNodesApp
Cytoscape-App-Name: HideSingletonNodesApp
Cytoscape-App-Version: 0.1

Finally we build the app jar file with this command:

jar cfm HideSingletonNodesApp.jar app-manifest *.class

This results in the app file HideSingletonNodesApp.jar.

Deploying of the Final App into Cytoscape

The resulting jar file is the working app and can now be tested in Cytoscape 3 by selecting Apps->App Manager from the menu. On the dialog, click the button Install from File... . We then select HideSingletonNodesApp.jar and we can now test the app by loading or generating a network with some isolated nodes and then selecting Select->Hide unconnected nodes from the Cytoscape menu.