Graph analysis and visualization on IPython Notebook using Cytoscape / cyREST and py2cytoscape Part 1

Introduction

blue_web.png (Visualize the interprotein network of yeast with Cytoscape and cyREST)

When visualizing graph data using IPython Notebook [^ 1], it is best to use the visualization module attached to the graph analysis library as shown below. Common:

But it's also a little unsatisfactory in terms of functionality and interactivity compared to modern web-based visualization tools and desktop visualization tools. Therefore, I created cyREST to make it possible to use the analysis function of these libraries and the interactive drawing function of Cytoscape at the same time. And the py2cytoscape library. The basic idea is very simple, trying to make Cytoscape's drawing functions etc. accessible from a standard environment for data analysis such as Python and R.

What is cyREST?

cyREST is an extension [^ 2] for Cytoscape that allows you to access various Cytoscape data and functions from a RESTful API. As a simple example, if you want to access "IDs of all networks currently in Cytoscape"

http://localhost:1234/v1/networks

It is possible by making a GET request to the URL. Similarly, for resources in Cytoscape

# NETWORK_Get the network corresponding to the ID as JSON
http://localhost:1234/v1/networks/NETWORK_ID

# NETWORK_Get all the nodes in the network corresponding to the ID
http://localhost:1234/v1/networks/NETWORK_ID/nodes

#Same edge
http://localhost:1234/v1/networks/NETWORK_ID/edges

#Get a specific node in the network
http://localhost:1234/v1/networks/NETWORK_ID/nodes/NODE_ID

#Get all properties (tables) for network nodes
http://localhost:1234/v1/networks/NETWORK_ID/tables/defaultnode

#Get a list of styles
http://localhost:1234/v1/styles

#Apply the specified automatic layout algorithm to a specific network
http://localhost:1234/v1/apply/layouts/LAYOUT_NAME/TARGET_NETWORK_ID

The basic API design is resource-oriented design that maps various resources to CRUD operation, so I used various Web APIs. I think it's relatively easy for some people to understand. In short, it's a language-independent API that can be accessed from any language with an HTTP client.

What is py2cytoscape?

You can access resources in Cytoscape from any language using a common HTTP client like this, but the code that calls the raw Web API is inevitably redundant. In particular, when doing PUT / POST, there is no choice but to refer to the document each time regarding the format of how to send the data. Py2cytoscape is a collection of wrappers and utilities for Python that was created to solve this problem as much as possible.

Specifically, there are the following differences between using and not using the wrapper. Here is the code to generate the network from the Cytoscape side:

# Popular HTTP client for Python
import requests

# Built-in JSON library
import json

# Basic Setup
PORT_NUMBER = 1234
BASE = 'http://localhost:' + str(PORT_NUMBER) + '/v1/'

# Header for posting data to the server as JSON
HEADERS = {'Content-Type': 'application/json'}

# Define dictionary of empty network
empty_network = {
        'data': {
            'name': 'I\'m empty!'
        },
        'elements': {
            'nodes':[],
            'edges':[]
        }
}

res = requests.post(BASE + 'networks?collection=My%20Collection', data=json.dumps(empty_network), headers=HEADERS)
new_network_id = res.json()['networkSUID']
print('Empty network created: SUID = ' + str(new_network_id))

On the other hand, with py2cytoscape:

from py2cytoscape.data.cyrest_client import CyRestClient

cy = CyRestClient()
network = cy.network.create(name='My Network', collection='My network collection')
print('Empty network created: SUID = ' + str(network.get_id()))

In this way, the purpose of this library is to reduce redundancy and make it easier to obtain grammar support such as IDE. Besides this,

Features such as are included.

Currently, there is only a wrapper for Python, but R will be released one by one.

Actual work

From here, we will introduce an actual usage example. If you don't know what Cytoscape looks like, this article will be helpful.

Operating environment

We have confirmed the operation in the following environment.

Preparation on the Python side

I will proceed on the assumption that IPython Notebook etc. have been installed in advance. I have tested it with 3.4 / 2.7 series, but please contact me if you have any problems.

All you need to run is py2cytoscape, which can be installed with the following command

pip install py2cytoscape

If you run a notebook server on Docker, EC2, etc., be aware that the notebook server and the machine running Cytoscape are different. If you do not properly configure the IP address and port of the machine running Cytoscape, these samples will not work. __

Preparation on the Cytoscape side

The preparation here is simple, just install cyREST from App Manager. Simply bring up the App Manager dialog from the menu Apps → App Manager, select cyREST and click the Install button.

__ The default port is 1234. __Changes can be made by calling the property settings screen from Edit-Preferences-Properties ... and giving a number to rest.port.

Execution procedure

When you're ready, make sure it's working.

  1. Start Cytoscape
  2. Open your browser and open the following URL 3. http://localhost:1234/v1
  3. If the following result is returned, cyREST is working.

スクリーンショット 2015-07-04 12.17.31.png

Then create a notebook

  1. Run IPython Notebook
  2. Open a browser and create a new notebook
  3. Execute the following code
import json
from py2cytoscape.data.cyrest_client import CyRestClient

cy = CyRestClient()
print(json.dumps(cy.status(), indent=4))
{
    "apiVersion": "v1",
    "numberOfCores": 8,
    "memoryStatus": {
        "maxMemory": 28217,
        "usedMemory": 513,
        "freeMemory": 2645,
        "totalMemory": 3159
    }
}

IP address and port

When actually analyzing data, IPython Notebook is often in the cloud such as AWS, and Cytoscape and the browser are local. In this case, you can specify the location of the machine where Cytoscape is running by passing the address and port to CyRestClient.

cy = CyRestClient(ip='127.0.0.1', port=1234)

Now everything is ready. From here, I would like to introduce a very simple example of actually using Cytoscape as an external drawing engine for IPython Notebook.

Hello (Graph) World Here is a very simple example. If possible, prepare the environment and try running the notebook.

Random network creation with NetworkX

It doesn't matter what graph data you use, but here we will use NetworkX to create a scale-free network.

import networkx as nx
g = nx.scale_free_graph(500) #500 node network

Calculation of various statistics

Calculate the Degree and Betweenness of this graph.

deg = nx.degree(g)
btw = nx.betweenness_centrality(g)
nx.set_node_attributes(g, 'degree', deg)
nx.set_node_attributes(g, 'betweenness', btw)

Send to Cytoscape

It is possible to send a NetworkX graph object directly to Cytoscape with a single command. At this time, notes, edges, and network attributes can also be sent at the same time.

g_cy = cy.network.create_from_networkx(g)

Apply automatic layout algorithm

Here, the Kamada-Kawai algorithm is used to calculate and draw the layout on the Cytoscape side.

cy.layout.apply(name='kamada-kawai', network=g_cy)

At this point, you can see that some drawing has been done.

スクリーンショット 2015-07-04 22.53.38.png

Apply another Visual Style

In Cytoscape, the definition of various mappings from properties to visual elements is called Visual Style. Here, we will switch from the presets to something called Directed.

directed = cy.style.create('Directed')
cy.style.apply(directed, network=g_cy)

It's a little confusing, but by calling Style with create, a reference to that Style is created.

Bundle edges to make them easier to see

This is [Scale Free Network](https://en.wikipedia.org/wiki/%E8%A4%87%E9%9B%91%E3%83%8D%E3%83%83%E3%83% 88% E3% 83% AF% E3% 83% BC% E3% 82% AF # .E3.82.B9.E3.82.B1.E3.83.BC.E3.83.AB.E3.83.95.E3 .83.AA.E3.83.BC.E6.80.A7), so connections are concentrated on some nodes. To make this easier to see, we use dynamic simulation to bundle the edges (Edge Bundling).

cy.edgebundling.apply(g_cy)

Embed in notebook as image

Using the image output function built into Cytoscape, it is possible to embed a network as an image file or output it to a file. The current version supports PNG, SVG and PDF.

# PNG
network_png = g_cy.get_png()
from IPython.display import Image
Image(network_png)

# SVG
network_svg = g_cy.get_svg()
from IPython.display import SVG
SVG(network_svg)

#Save PDF to file
network_pdf = g_cy.get_pdf()
f = open('scale_free_500.pdf', 'wb')
f.write(network_pdf)
f.close()

スクリーンショット 2015-07-04 23.53.20.png

Embed as an interactive object in JavaScript

The py2cytoscape library has a built-in HTML widget that uses Cytoscape.js [^ 3], so if you use this, you can use JavaScript to create a Canvas with JavaScript. Network drawing using is possible.

スクリーンショット 2015-07-05 0.20.05.png

import py2cytoscape.cytoscapejs as renderer

view = g_cy.get_first_view()
# style_for_widget = cy.style.get(my_yeast_style.get_name(), data_format='cytoscapejs')
renderer.render(view, 'Directed', background='radial-gradient(#FFFFFF 15%, #DDDDDD 105%)')

Currently, some features, including Edge Bundling, have not yet been implemented and will not be fully reproduced, but we will gradually support them as well.

Manually edit this result further

Up to this point, we have drawn with code, but it is also easy to interactively view and modify the results from the GUI. This is a difficult part of a library that only generates still images. For example, fine-tuning the position of a node or zooming in on details.

net2.png

The above example is a simple edit that just changes the background color and maps Betweenness to the size of the node, but you can write code for reproducible visualization and intuitive from the GUI. You can enjoy both merits of playing with the visualization result while looking directly at the edit.

Summary

This time, I just introduced the basic functions in a hurry, but in order to perform actual data analysis, it is convenient to generate using style code and use the results of various graph analysis libraries. From the next time onward, I would like to introduce how to use these Cytoscape + Python uniquely.


[^ 1]: Currently rebranding as Jupyter Notebook [^ 2]: Called App. Available from the Cytoscape App Store (http://apps.cytoscape.org/). [^ 3]: A JavaScript library specializing in network drawing and analysis. The Cytoscape development team develops and maintains it.

Recommended Posts

Graph analysis and visualization on IPython Notebook using Cytoscape / cyREST and py2cytoscape Part 1
Visualize network data using Cytoscape from IPython Notebook Part 1
Try using Pillow on iPython (Part 1)
Try using Pillow on iPython (Part 2)
Try using Pillow on iPython (Part 3)
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 1
Launch and use IPython notebook on the network
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 2
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Displaying strings on IPython Notebook
Run IPython Notebook on Docker
Graph drawing with IPython Notebook
Install Anaconda on Mac and upload Jupyter (IPython) notebook to Anaconda Cloud