Visualizing protein interaction networks in Python | by Ford Combs | Towards Data Science

Protein interaction data is incredibly important. It describes the interplay between the biomolecules encoded by genes. It allows us to understand the complexities of cellular function and even predict potential therapeutics. There are many databases that contain protein interaction data, but STRING is one of the best. It contains 3,123,056,667 total interactions at the moment of this writing. These interactions involve over 20,000,000 proteins in over 5,000 organisms, combining to form. In this article, I’m going to show you how to download protein interaction data from the STRING API and how to create graphs and visualizations of that data using NetworkX.
### The required libraries and packages ###import networkx as nximport requestsimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib import cm
Our data set will contain pairwise interactions for a handful of proteins that are involved in serotonin pathways. There are a number of ways these interactions can be found in STRING. We could go to the STRING website with a list of proteins and use the multiple proteins search, but here we want to do everything using Python. The requests library allows us to easily use HTTP requests to gather the data. The STRING documentation gives details about using the REST API and has lots of examples, but the basic call for gathering network information for multiple proteins looks like this:
https://string-db.org/api/[output-format]/network?identifiers=[your_identifiers]&[optional_parameters]
The details about output-format, identifiers, and optional parameters can all be found in the documentation. We will use the tsv (tab separated values) output format, a list of protein identifiers separated by %0d’s, and one optional parameter: species. The code to convert the list of proteins into the correct URL is shown below.
The list of proteins was selected from a larger set of human proteins related to serotonin. One thing to note is the &species=9606 optional parameter. 9606 is the species number for humans. You can select different species by using the organism search. Next, the data can be converted to a pandas data frame which will be used to create the network.
NetworkX includes several classes of graph. In this case, we will build an undirected, weighted graph. The graph is undirected because the interaction between protein A and protein B is the same as the interaction between protein B and protein A. The graph is weighted because the edges will have weights based on the interaction score.
The interactions data frame has one row for each interaction that contains the two interacting proteins and the score of the interaction. The following code shows how that data can be used to build the graph with NetworkX. Each node represents a protein, each edge represents an interaction between two proteins, and each edge is weighted by the score.
Here is some basic information about the graph using nx.info(G) :
Name: Protein Interaction GraphType: GraphNumber of nodes: 20Number of edges: 128Average degree:  12.8000
The graph contains 20 nodes (proteins) with 128 edges (interactions). The degree of a node is the number of edges connected to that node. In this graph, the average degree is 12.8. We can create a simple visualization of this graph using the following code. Note the use of nx.spring_layout, which is one of NetworkX’s node positioning algorithms.
The simple visualization shown above can be enhanced to include more information about the graph. I already mentioned degree, which is the number of edges connected to a node. Another measure we can include is betweenness centrality. Each pair of nodes in the graph is connected by one shortest path. In an unweighted graph, the shortest path consists of the smallest set of edges that connect two nodes. In a weighted graph, like we have here, the shortest path consists of the set of edges with the smallest sum of weights. The betweenness centrality of a given node is the measure of the number of shortest paths that pass through that node out of all of the shortest paths. The following code uses the degree and betweenness centrality of each node to determine its color and size and uses the edge weights to determine the color and size of the edges.
We can use these color mappings to create an enhanced visualization with this code:
There is a lot more information displayed in this graph. The color range goes from dark purple to bright yellow. The yellower the node, the higher the degree. The larger the node, the greater the betweenness centrality. The yellower and wider the edge, the greater the interaction score.
One final useful visualization is the minimum spanning tree, which is the subset of edges that connect all the nodes with the lowest possible weight:
Research in the field of data visualization has repeatedly shown that humans are particularly good at finding patterns in visual data. It takes less time and less brain power to understand a graph that to gather the same information from a table. Visualizations like those described here are great tools for examining and demonstrating any kind of network data. I hope you enjoyed this tutorial and gained some useful techniques that you can employ on your own projects!