Tutorial:Creating a Simple Cytoscape 3 App

From OpenTutorials

Revision as of 19:20, 9 April 2012 by JasonMontojo (Talk | contribs)
Jump to: navigation, search


Slideshow Creating a Simple Cytoscape 3 App (about 15 minutes)
Example source code (2 Java source files)
Preliminary version of Cytoscape 3 API jar (1 JAR file)

Tutorial Curators Johannes Ruscheinski, Jason Montojo


Contents


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

First you need to create a new subdirectory for your app project. Then you need to download the Cytoscape 3 API jar file.

Main App Class

Next we create the skeleton code which is the same for all simple apps and looks as follows:

import org.cytoscape.app.AbstractCyApp;
import org.cytoscape.app.CyAppAdapter;


public class HideSingletonNodesApp extends AbstractCyApp {
	public HideSingletonNodesApp(CyAppAdapter 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.AbstractCyApp;
import org.cytoscape.app.CyAppAdapter;


public class HideSingletonNodesApp extends AbstractCyApp {
    public HideSingletonNodesApp(CyAppAdapter 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 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 Cytoscape3-api.jar *.java

This results in two .class files.

A simple app is a Java jar file with a "Cytoscape-App" entry in its META-INF/MANIFEST.MF file. This entry specifies the 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!):

Cytoscape-App: HideSingletonNodesApp

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 File->Import->App... from the menu. 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.

Personal tools
Create a book