Cytoscape and igraph

Open In Colab

by Kozo Nishida, Alexander Pico, Barry Demchak

py4cytoscape 0.0.11

This notebook will show you how to convert networks between igraph and Cytoscape.

Prerequisites

In addition to this package (py4cytoscape), you will need:

  • Cytoscape 3.8 or greater, which can be downloaded from https://cytoscape.org/download.html. Simply follow the installation instructions on screen.

  • Complete installation wizard

  • Launch Cytoscape

  • If your Cytoscape is 3.8.2 or earlier, install FileTransfer App (Follow here to do it.)

NOTE: To run this notebook, you must manually start Cytoscape first – don’t proceed until you have started Cytoscape.

Setup required only in a remote notebook environment

If you’re using a remote Jupyter Notebook environment such as Google Colab, run the cell below. (If you’re running a local Jupyter Notebook server on the desktop machine same with Cytoscape, you don’t need to do that.)

[ ]:
_PY4CYTOSCAPE = 'git+https://github.com/cytoscape/py4cytoscape@0.0.11'
import requests
exec(requests.get("https://raw.githubusercontent.com/cytoscape/jupyter-bridge/master/client/p4c_init.py").text)
IPython.display.Javascript(_PY4CYTOSCAPE_BROWSER_CLIENT_JS) # Start browser client

Note that to use the current py4cytoscape release (instead of v0.0.11), remove the _PY4CYTOSCAPE= line in the snippet above.

Sanity test to verify Cytoscape connection

By now, the connection to Cytoscape should be up and available. To verify this, try a simple operation that doesn’t alter the state of Cytoscape.

[1]:
import py4cytoscape as p4c
p4c.cytoscape_ping()
p4c.cytoscape_version_info()
You are connected to Cytoscape!
[1]:
{'apiVersion': 'v1',
 'cytoscapeVersion': '3.8.2',
 'automationAPIVersion': '1.2.0',
 'py4cytoscapeVersion': '0.0.10'}

From igraph to Cytoscape

The igraph package is a popular network tool among Python users. With py4cytoscape, you can easily translate igraph networks to Cytoscape networks!

Here is a basic igraph network construction from the Graph.DataFrame docs, https://igraph.org/python/doc/tutorial/generation.html#from-pandas-dataframe-s

[2]:
import pandas as pd
from igraph import Graph

actors = pd.DataFrame(data={'name': ["Alice", "Bob", "Cecil", "David", "Esmeralda"],
                             'age': [48,33,45,34,21],
                             'gender': ["F","M","F","M","F"]
                             })

relations = pd.DataFrame(data={'from': ["Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"],
                               'to': ["Alice", "Bob", "Alice", "Alice", "Bob", "Alice"],
                               'same_dept': [False, False, True, False, False, True],
                               'friendship': [4,5,5,2,1,1],
                               'advice': [4,5,5,4,2,3]
                               })

ig = Graph.DataFrame(relations, directed=True, vertices=actors)

You now have an igraph network, ig. In order to translate the network together with all vertex (node) and edge attributes over to Cytoscape, simply use:

[3]:
p4c.create_network_from_igraph(ig, "myIgraph")
Applying default style...
Applying preferred layout
[3]:
{'networkSUID': 51}
[4]:
p4c.notebook_export_show_image()
[4]:
../_images/tutorials_Cytoscape-and-iGraph_8_0.png

From Cytoscape to igraph

Inversely, you can use create_igraph_from_network() in py4cytoscape to retrieve vertex (node) and edge DataFrames to construct an igraph network.

[5]:
ig2 = p4c.create_igraph_from_network("myIgraph")

Compare the round-trip result for yourself…

[7]:
print(ig)
IGRAPH DN-- 5 6 --
+ attr: age (v), gender (v), name (v), advice (e), friendship (e), same_dept
  (e)
+ edges (vertex names):
Bob->Alice, Cecil->Bob, Cecil->Alice, David->Alice, David->Bob,
Esmeralda->Alice
[8]:
print(ig2)
IGRAPH DN-- 5 6 --
+ attr: age (v), gender (v), id (v), name (v), selected (v), shared name (v),
  advice (e), data.key.column (e), friendship (e), interaction (e), name (e),
  same_dept (e), selected (e), shared interaction (e), shared name (e), source
  (e), target (e)
+ edges (vertex names):
Bob->Alice, Cecil->Bob, Cecil->Alice, David->Alice, David->Bob,
Esmeralda->Alice

Note that a few additional attributes are present which are used by Cytoscape to support node/edge selection and network collections.

Also note: All networks in Cytoscape are implicitly modeled as *directed*. This means that if you start with an *undirected* network in igraph and then convert it round-trip (like described above), then you will end up with a *directed* network.

[ ]: