Chord diagrams of protein interaction networks in Python
By: Ford Combs
Tags: Data Visualization, Python, Data Science, Biology, Programming

Humans are great at interpreting graphic displays of data. We can identify patterns in visual displays quickly and easily as compared to tabular displays. There are many reasons for this, e.g., about 30% of the cortex contains neurons dedicated to visual processing. This is why data visualization is extremely important for both understanding and communicating information. In a previous post, I described how NetworkX can be used to build informative and attractive graphic displays of protein network data. Here, I will show how the same data can be displayed as a Circos-style, chord diagram using nxviz.

Chord Diagrams

Chord diagrams are circular plots that are great for displaying relationships between objects of a set, such as nodes and connections in a network. A basic chord diagram has a ring of objects connected by chords that indicate relationships. In Python, you can create these types of plots with nxviz, a package designed for use with NetworkX graphs. If you want to learn more about NetworkX, check out my previous story. The original Circos software that helped popularize chord diagrams is written in Perl and was introduced in a paper published in 2009. The software is well-documented and there are great tutorials that show you how to build beautiful plots that display multiple layers of information. Currently, nxviz is one of the few Python libraries that can be used to create plots in this style.

Figure 3 from McGraw, Lisa A et al. “A genetic linkage map and comparative mapping of the prairie vole (Microtus ochrogaster) genome.” BMC genetics vol. 12 60. 7 Jul. 2011, doi:10.1186/1471–2156–12–60

### The required libraries and packages ###

import networkx as nx

import requests

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

# from matplotlib import cm

from nxviz.plots import CircosPlot

Gather the data

The network we are going to visualize contains protein interaction data. The following code takes a list of proteins and uses the Python requests library to collect data from the STRING database of protein interactions. You can learn more about this process here, but the end result is a data frame in which each row contains a pair of proteins and the interaction score between them.

Collect the STRING data using the requests library

Process the response object and create a simplified data frame

The next code chunk can be used to build the graph using NetworkX. The code below is the same as described previously, except for one optional modification: only high scoring interactions are used as weighted edges. This will reduce clutter in the simplified visualization. For the more advanced graph, you can keep all the edges by uncommenting the designated line below and commenting out the last two lines.

Build the graph using pairs of interacting proteins as nodes and their interaction score as the edge weight

Draw a simple plot

Now that the NetworkX graph has been created, a Circos-style plot can be generated very quickly with just three lines of code.

Image by author

The above plot shows displays important, basic information about the network. For example, it is easy to see which nodes have relatively higher or lower degree. Recall that the degree of a node is the number of edges that connect to it. For example, the node HTR1A has a high degree; it has more connections compared to other proteins in the graph. This suggests that HTR1A is involved in many different processes. MAOA, on the other hand, has a low degree; only one edge connects it to another node. This suggests that it has a more specific role.

More informative plots

The graph above is useful, but more information can be displayed in these types of plots. Nxviz allows for the nodes and edges to be colored and sized based on user-defined characteristics. The code below shows how a nodelist and edgelist can be generated from graph G and used to build a new graph g . This new graph is identical to old graph, except that it has a ["class"] attribute for each node that stores its degree.

Generate a new graph that stores each node’s degree as its “class” attribute

Draw the new graph using “class” and “weight” to display for information

Plot of the protein interaction network that includes only high scoring edges. Image by author

The above plot includes both the edge weight and node degree information. Note that the nodes are colored based on their degree. The proteins with the most interactions are pink and purple while the lower degree proteins are red. Also, note that the widths of the edges indicate the score of the interaction; the wider the edge, the higher the score.

This display can be improved further by making two alterations to the code. First, all edges can be kept by uncommenting the designated line in the create_graph_circos gist. Normally, this would result in a lot of clutter, but the widths are adjusted with one more change: the uncommenting of the alternate method in the improved_circos.py gist. This rescales the edge widths more dramatically so that high scoring interactions are much wider than low scoring interactions. Because of this dramatic change in the edge widths, it is possible to display all the edges without the graph becoming crowded and difficult to interpret.

Plot of the protein interaction network that includes all edges. Image by author

Final thoughts

The plots included in this article are effective for the visualization of protein interaction networks. The final plot does a particularly good job of displaying lots of details about the network. The node color helps indicate groups of nodes that have similar degree and the edge widths show which interactions have higher scores. This is useful in the analysis of networks, because high scoring edges likely indicate more tightly linked interactions. And, high scoring interactions linked to nodes of high degree, can indicate particularly important interactions in the network.

The proteins used in this example are related, but they were arbitrarily chosen as a proof of concept. Novel research would require more care in the creation of the protein set and in the design of the edge weights through the utilization of other scores and data. Also, improvements in chord diagram implementations in Python need to be developed. There are currently excellent options for drawing these plots in R. Stay tuned, as I hope to take a stab at a Python version soon.