{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 2: Network motifs (DL 13.11.)\n",
"======================================\n",
"\n",
"Your name:\n",
"\n",
"Name of your pair:\n",
"\n",
"In this exercise we will use the networkx python library that gives us access to some network related algorithms and data structures."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Counting motifs in a network (2 points)\n",
"------------------------------------------\n",
"\n",
"In this exercise set we will investigate two network motifs: Feed Forward Loop ($A\\rightarrow B, B \\rightarrow C, A \\rightarrow C$) and 3-cycle ($A\\rightarrow B, B \\rightarrow C, C \\rightarrow A$). The code below defines these as networkx graphs and shows two ways of creating a graph."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import networkx as nx\n",
"\n",
"# Define FFL by giving a list of edges\n",
"ffl = nx.DiGraph([('A','B'), ('B','C'), ('A','C')])\n",
"# Define 3-cycle by creating an empty graph and adding vertices and edges one by one\n",
"cycle = nx.DiGraph()\n",
"cycle.add_node('A')\n",
"cycle.add_node('B')\n",
"cycle.add_node('C')\n",
"cycle.add_edge('A','B')\n",
"cycle.add_edge('B','C')\n",
"cycle.add_edge('C','A')\n",
"\n",
"# Print the edge lists\n",
"print(ffl.edges())\n",
"print(cycle.edges())"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The code below defines a network G. Write a function that counts the occurrences of the motifs FFL and 3-cycle. Use the frequency concept $\\cal{F}_1$. Use the function to count the occurrences of FFL and 3-cycle in G.\n",
"\n",
"_Hint:_ Enumerate all paths of length two, i.e. ($A\\rightarrow B, B \\rightarrow C$), and check for the existence of the third edge of the motif. The DiGraph in networkx has methods successors and has_edge that you might find useful. Pay attention to the symmetries in the 3-cycle motif and make sure you report each occurrence of the motif only once.\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"G = nx.DiGraph([(1,3),(2,1),(2,3),(3,4),(4,2),(4,5),(4,6),(5,6)])"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Motifs in the E. coli regulatory network (2 points)\n",
"------------------------------------------------------\n",
"\n",
"a) In the previous exercise set (Question 2) you wrote code that reads the E. coli regulatory network. Modify that code to read the network into a networkx graph.\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import urllib.request # library handling URLs\n",
"\n",
"network = nx.DiGraph()\n",
"\n",
"# E. coli network file\n",
"networkfile=\"http://regulondb.ccg.unam.mx/menu/download/datasets/files/network_tf_gene.txt\"\n",
"# Open the file. This will work exactly like a regular file.\n",
"data = urllib.request.urlopen(networkfile)\n",
"for line in data:\n",
" # load the data\n",
" line = line.decode('utf-8')\n",
" \n",
"print(network.edges())"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"b) Using the function from the previous exercise, count the occurrences of FFL and 3-cycle in the E. coli reulatory network.\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Statistical significance of motifs (3 points)\n",
"------------------------------------------------\n",
"\n",
"a) The lecture slides give a formula for the expected value and variance of the number of motif occurrences in ER networks. Using the formula compute these values for the two motifs, FFL and 3-cycle. Compare the number of occurrences of these motifs in the E. coli regulatory network to the expected number of occurrences in ER graphs by calculating the Z-score. Is the number of occurrences of the motifs statistically significant?\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_Write your answer to the questions here._"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"b) The networkx library includes the function erdos_renyi_graph for generating ER graphs. Use it to generate an ensemble of 100 random directed graphs with $n$ vertices and edge probability equal to $m/n^2$ where $m$ is the number edges and $n$ the number of vertices in the E. coli regulatory network. For each graph in the ensemble compute the number of occurrences for the two motifs, FFL and 3-cycle. Calculate the average number of occurrences for the motifs and standard deviation of the number of occurrences. Do these coincide with the theoretical results? For each of the motifs plot the number of occurrences as a histogram.\n",
"\n",
"Note: The networkx function does not add self loops to the ER graph. If you want to be perfect, you can add those yourself with the given probability. However, this will not affect the results of our analysis of the occurrence of these motifs because the motifs do not have self loops.\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_Write your answer to the questions here._"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Enumerating motifs (3 points)\n",
"--------------------------------\n",
"\n",
"Let us consider directed graphs allowing self loops (an edge from vertex v to vertex v) but disallowing multiple edges (edges with the same direction between the same pair of vertices). In this exercise we will enumerate all possible motifs of a given size.\n",
"\n",
"The two motifs with a single vertex are a graph containing only a single vertex and a graph containing a single vertex and a self loop on that vertex. Write code that starts with these two single vertex motifs and expands them first to all possible two vertex motifs and then to all possible three vertex motifs. Remember that a motif has to be connected and your collection of motifs should not contain motifs that are isomorphic to each other. How many motifs with two vertices exist? How about motifs with three vertices? Can you compute the the number of motifs with four vertices?\n",
"\n",
"_Hint:_ The networkx library has a function is_isomorphic which checks if two graphs are isomorphic to each other.\n",
"\n",
"**Your solution:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The two motifs with one vertex\n",
"G1 = nx.DiGraph()\n",
"G1.add_node(1)\n",
"motifs1 = [G1, nx.DiGraph([(1,1)])]\n",
"\n",
"# Add code to generate motifs with 2 and 3 vertices"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}