Locked History Actions

CyniAlgorithmTutorial

How to create a Cyni Algorithm

Introduction

In order to help app developers, we provide a Cyni Algorithm sample, which contains a "HelloWorld" implementation of a Cyni Algorithm. This sample will serve as a starting point or template for new app developers. Cyni sample code can be used by following these steps:

  1. Check out the cyni sample projects from our Github repository with this command:

git clone git://github.com/schwikowskilab/cyni-samples.git
  1. Go the cyni-algo-sample directory and compile it with mvn clean install

Next sections describe each file that needs to be created to produce a Cyni Algorithm App.

Pom File

Here are the main lines to modify in the pom.xml file. The fields bundle.symbolicName and bundle.namespace properties should be modified according to new app directory configuration. Tags groupId and artifactId also have to be modified. Finally, version tab should set to developer requirements.

<properties>
        <bundle.symbolicName>cyni-algo-sample</bundle.symbolicName>
        <bundle.namespace>fr.systemsbiology.cyniSampleAlgorithm.internal</bundle.namespace>
        <cytoscape.api.version>3.1.0</cytoscape.api.version>
        <cyni.api.version>1.0.0</cyni.api.version>
        <maven-bundle-plugin.version>2.3.4</maven-bundle-plugin.version>
        <osgi.api.version>4.2.0</osgi.api.version>
</properties>

<groupId>fr.systemsbiology.cyniSampleAlgorithm</groupId>
<artifactId>cyniSampleAlgorithm</artifactId>
<packaging>bundle</packaging>
<name>${bundle.symbolicName}</name>

<version>3.0.0-alpha1-SNAPSHOT</version>

CyActivator

In this file, any Cyni Algorithm implemented needs to be registered. These two lines do the work. First, we create the object for the new Cyni Algorithm and then we register it to make it available by the Cyni Algorithm Manager.

   1     //Define new Cyni Algorithm
   2     CyniSampleAlgorithm test = new CyniSampleAlgorithm();
   3     //Register new Cyni Algorithm
   4     registerService(bc,test,CyCyniAlgorithm.class, new Properties());

CyniSampleAlgorithm

In the constructor of this class, there are three parameters that need to be set. The computer name of the algorithm, the human name of the algorithm (the one used to be displayed) and the category of the algorithm. This parameters are passed to the parent class.

   1 public CyniSampleAlgorithm() {
   2     super("sampleAlgo.cyni","Cyni Sample Algorithm",true,CyniCategory.INDUCTION);
   3 }

The other two main functions serve to generate the task object and the context object. The context method contains a tunableSetter argument, which allows users to set the context without using a User Interface. The content of context method should be unmodified regarding the lines involving the tunableSetter.

   1 public TaskIterator createTaskIterator(Object context,CyTable table, CyNetworkFactory networkFactory, CyNetworkViewFactory networkViewFactory,CyNetworkManager networkManager,CyNetworkTableManager netTableMgr, CyRootNetworkManager rootNetMgr,CyNetworkViewManager networkViewManager, CyLayoutAlgorithmManager layoutManager, CyCyniMetricsManager metricsManager) {
   2     selectedTable = table;
   3     return new TaskIterator(new CyniSampleAlgorithmTask(getName(),(CyniSampleAlgorithmContext)context,networkFactory,networkViewFactory,networkManager,netTableMgr,rootNetMgr,networkViewManager,layoutManager,metricsManager, selectedTable));
   4         }
   5 
   6 public Object createCyniContext(CyTable table, CyCyniMetricsManager metricsManager, TunableSetter tunableSetter,Map<String, Object> mparams) {
   7     Object context;
   8     selectedTable = table;
   9     context = new CyniSampleAlgorithmContext();
  10     if(mparams != null && !mparams.isEmpty())
  11         tunableSetter.applyTunables(context, mparams);
  12     return context;
  13 }

CyniSampleAlgorithmContext

This file contains the class that will be used to define the User Interface for the parameter handling. Any input parameter that the algorithm might have must be defined in this class by using Cytoscape Tunables.
This class should extend the CyniAlgorithmContext class, which provides several useful methods as well as other methods that shouldn't be modified because this class is supposed to use Tunables. These methods are not enumerated here because it shouldn't be used. In case, Cytoscape Tunables are not flexible enough to provide you with all required tools to implement the desired User Interface. There is another tutorial that explains how to provide your own Java Swing User Interface.
Finally, if the function getValidationState is implemented, it will be used to validate the input parameters before calling the actual algorithm. If method getValidationState wants to be used to validate input parameters, the interface TunableValidator needs to be implemented in this class.

   1 @Tunable(description="Parameter 1")
   2 public int param1 = 10;
   3 
   4 @Tunable(description="Paramter 2")
   5 public Boolean param2 = false;
   6 
   7 
   8 public CyniSampleAlgorithmContext( ) {
   9         super(true);
  10 }
  11 
  12 @Override
  13 public ValidationState getValidationState(final Appendable errMsg) {
  14     if (param1 > 0 )
  15         return ValidationState.OK;
  16     else {
  17         try {
  18             errMsg.append("parameter 1 is not good");
  19         } catch (IOException e) {
  20             e.printStackTrace();
  21             return ValidationState.INVALID;
  22         }
  23         return ValidationState.INVALID;
  24     }
  25 }

CyniSampleAlgorithmTask

This file contains the actual implementation of the Cyni Algorithm. In the constructor, the context and the selected table are passed. In the context, there is the information of each input parameter for the algorithm.
Any implementation related to the Cyni Algorithm needs to be developed in the doCyniTask method.

   1 public CyniSampleAlgorithmTask(final String name, final CyniSampleAlgorithmContext context, CyNetworkFactory networkFactory, CyNetworkViewFactory networkViewFactory,CyNetworkManager networkManager,CyNetworkTableManager netTableMgr, CyRootNetworkManager rootNetMgr,CyNetworkViewManager networkViewManager,CyLayoutAlgorithmManager layoutManager, CyCyniMetricsManager metricsManager, CyTable selectedTable)
   2 {
   3     super(name, context, networkFactory, networkViewFactory, networkManager, networkViewManager, netTableMgr, rootNetMgr);
   4     param1 = context.param1;
   5     this.mytable = selectedTable;
   6     this.param2 = context.param2;
   7 
   8 }
   9 
  10 /**
  11  *  Perform actualtask.
  12 */
  13 @Override
  14 final protected void doCyniTask(final TaskMonitor taskMonitor) {
  15 
  16     Double progress = 0.0d;
  17     CyNetwork networkSelected = null;
  18     String networkName;
  19     CyLayoutAlgorithm layout;
  20     CyNetworkView newNetworkView ;
  21 
  22     taskMonitor.setStatusMessage("Algorithm running ...");
  23     taskMonitor.setProgress(progress);
  24 
  25     .......................
  26 
  27     taskMonitor.setProgress(1.0d);
  28 }

The core of the method doCyniTask contains the main steps to create the new network. As shown in the code below, first a new network is created and then the selected table is checked to see if it is associated to a Cytoscape network. After that, if needed, a Cyni Table can be created to help better working with Cytoscape Table Data.
Finally, the new created network needs to be displayed and a layout is applied to the network. Several functions have been developed to help to perform all these tasks.

   1 //Create new network
   2 CyNetwork newNetwork = netFactory.createNetwork();
   3                 
   4                 
   5 //Check if a network is associated to the selected table
   6 networkSelected = getNetworkAssociatedToTable(mytable);
   7                 
   8 // Create the CyniTable
   9 CyniTable data = new CyniTable(mytable,attributeArray.toArray(new String[0]), false, false, selectedOnly);
  10                 
  11                 
  12 //Set the name of the network, another name could be chosen
  13 networkName = "Induction " + newNetwork.getSUID();
  14 if (newNetwork != null && networkName != null) {
  15     CyRow netRow = newNetwork.getRow(newNetwork);
  16     netRow.set(CyNetwork.NAME, networkName);
  17 }
  18                 
  19                 
  20 /*****************************************************/
  21 //
  22 // Add the different nodes and edges according to the table data
  23 //
  24 //
  25 /*****************************************************/
  26                 
  27 //Display the new network
  28 if (!cancelled)
  29 {
  30     newNetworkView = displayNewNetwork(newNetwork, false);
  31     taskMonitor.setProgress(1.0d);
  32     layout = layoutManager.getDefaultLayout();
  33     Object context = layout.getDefaultLayoutContext();
  34     insertTasksAfterCurrentTask(layout.createTaskIterator(newNetworkView, context,     CyLayoutAlgorithm.ALL_NODE_VIEWS,"");
  35 }

Finally, there are two actions that will be performed very often when working with tables and networks and it is important to know how to do it.

How to get data associated to a node or edge

There are two possibilities to perform this action.

1. First, get the table and then use getRow method.

   1 CyTable table = network.getTable(CyNode.class,CyNetwork.DEFAULT_ATTRS);
   2 CyRow nodeRow = table.getRow(node.getSUID());

2. You can also go straight into getting the row without having to get the table by using the getRow method in the CyNetwork class.

   1 CyRow nodeRow = cyNetwork.getRow(node,CyNetwork.DEFAULT_ATTRS);

How to get a node or an edge associated to a row in a table data

You need to get the SUID value for that row and then use it as input paramter in the getNode method.

   1 CyNode node = network.getNode(row.get(CyIdentifiable.SUID, Long.class));