{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 3: Gene regulatory network inference (DL 27 November)\n",
"\n",
"Lecturer: Antti Honkela\n",
"\n",
"Your name:\n",
"\n",
"Name of your pair:\n",
"\n",
"In this exercise you will experiment with methods for inferring regulatory interactions.\n",
"\n",
"If you have any questions, please post them to the Moodle discussion area!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 1: Regulatory interaction inference with Gaussian Markov random field (2 pts)\n",
"\n",
"In this task, you will generate a sparse Erd\u0151s\u2013R\u00e9nyi random graph, form the corresponding Gaussian Markov random field and study its covariance.\n",
"\n",
"1. Generate an undirected ER random graph for 12 genes using p=0.4. Construct a corresponding Gaussian Markov random field by forming the precision matrix which has non-zero elements corresponding to the edges of the graph. Make sure the precision matrix is positive definite by making the diagonal elements positive and sufficiently large compared to the other elements of the matrix.\n",
"2. Compute the correlation matrix among the 12 genes from the precision matrix. Visualise the matrix. Can you spot the true connections?\n",
"3. Try setting a threshold for absolute values of the correlation matrix elements to distinguish between correct and false connections. Use the Hamming distance between the true and estimated connectivity matrices as a measure of accuracy. How small a distance can you achieve?\n",
"\n",
"Hints:\n",
"\n",
"* You can check if the precision matrix is positive definite using `scipy.linalg.eig()`. The first argument contains the eigenvalues of the matrix which should all be positive. Eigenvalues of a symmetric matrix are always real. If you get complex eigenvalues, make sure the matrix is symmetric. (Precision and covariance matrices of Gaussian distributions are always symmetric.)\n",
"* You can force a matrix $\\mathbf{A}$ to be positive definite by adding a diagonal term. If eigenvalues of $\\mathbf{A}$ are $(\\lambda_1, \\dots, \\lambda_n)$, eigenvalues of $\\mathbf{A} + \\alpha \\mathbf{I}$ are $(\\lambda_1+\\alpha, \\dots, \\lambda_n+\\alpha)$.\n",
"* `plt.spy()` function can be useful for exploring the sparsity pattern of matrices.\n",
"* Hamming distance of the binary connectivity matrices is the number of positions in which their values are different."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"# Import some useful modules\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.random as npr\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Answers to questions:\n",
"\n",
"1. -\n",
"2. \n",
"3. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 2: Inferring interactions from data (3 pts)\n",
"\n",
"In this task, you will simulate observations from the Gaussian Markov random field model constructed in Task 1 and attempt to infer the network structure using those observations.\n",
"\n",
"1. Simulate three multivariate normal observation data sets of sizes [25, 50, 100] with 0 mean using the covariance from Task 1.\n",
"2. Compute and visualise the correlation matrix among the 12 genes simulated. How small Hamming distance can you obtain with the different data sets using a suitable threshold?\n",
"3. Compute and visualise the inverse of the correlation matrix. How small Hamming distance can you now obtain with the different data sets using a suitable threshold?\n",
"4. Learn the structure using graphical lasso (`sklearn.covariance.graph_lasso()`). Try different values of the regularisation parameter. How good results in terms of the Hamming distance can you now obtain?\n",
"\n",
"Hints:\n",
"\n",
"* See hints for Task 1 about positive definiteness of the covariance matrix.\n",
"* Graphical lasso is implemented in scikit-learn. You can install the package using the command \"`pip3 install sklearn --user`\" on the command line. If this does not work, more instructions are at: http://scikit-learn.org/"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 3: Inferring interactions from data II (2 pts)\n",
"\n",
"In this task, you will simulate observations using a linear generative model:\n",
"$$ \\mathbf{y_i} = \\mathbf{A} \\mathbf{x_i} + \\epsilon_i. $$\n",
"Here $\\mathbf{x_i}$ are TF expression levels (assumed to be random, following a Gaussian distribution), $\\mathbf{A}$ is the connectivity matrix from TFs to target genes, $\\mathbf{y_i}$ are the target gene expression levels and $\\epsilon_i$ is Gaussian observation noise. Based on these data, you will attempt to infer the network structure.\n",
"\n",
"1. Generate a sparse Erd\u0151s\u2013R\u00e9nyi random connectivity graph from two TFs to 10 target genes and sample the random connection weigths having this sparsity pattern.\n",
"2. Simulate 3 data sets following the above connectivity graph and model with 2 TFs and 10 target genes, having [25, 50, 100] samples. You can simulate $\\mathbf{x_i}$ from $\\mathcal{N}(0, 1)$ and $\\epsilon_i$ from $\\mathcal{N}(0, 0.1^2)$.\n",
"3. Form a data set by concatenating the TFs and targets. Compute and visualise the correlation matrix among the 12 genes. How small Hamming distance between the true network and the estimated network can you obtain?\n",
"4. Compute and visualise the inverse of the correlation matrix. How well can you now find the true connections in terms of the Hamming distance?\n",
"\n",
"Hints:\n",
"\n",
"* When computing covariances, it can be useful to check the shape of the result that you are computing it the right way. You can then transpose the data (`X` -> `X.T`) if necessary."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 4: Inferring interactions from dynamic simulations (3 pts)\n",
"\n",
"In this task, you will simulate observations using the following dynamical model:\n",
"$$ \\frac{d \\mathbf{x}(t)}{dt} = \\sigma(\\mathbf{A} \\mathbf{x}) - \\delta \\mathbf{x} + \\gamma, $$\n",
"where $\\mathbf{x}$ are the gene expression levels, $\\mathbf{A}$ is the connectivity matrix (connecting only TFs to TFs and TFs to targets), $\\sigma(x) = 1/(1+e^{-x})$ is a sigmoid function, $\\delta$ are the mRNA degradation rates and $\\gamma$ are constant external inputs (affecting only TFs).\n",
"\n",
"1. Implement a function for simulating from the above system. You can use `scipy.integrate.odeint()` to numerically solve the differential equation given a suitable non-negative initial state.\n",
"2. Simulate the system with a constant non-negative external input and $\\delta = 1$ and visualise the results.\n",
"3. Simulate the system with number of non-negative random external inputs until it reaches steady state. What can you see?\n",
"4. Compute the correlations and inverse covariance for the combined steady-state measurements from [10, 20, 40] simulations. How well can you recover the true connections?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Background on Task 4\n",
"\n",
"The use of sigmoid function in the dynamics as in this task is not very common. It has clear advantages over plain linear model (saturation of transcription rate to a fixed maximum, possibly easier stability guarantees, possibility to model repression), but the presented formulation still cannot realistically model combinatorial regulation, and it makes model inference much more difficult than linear differential equations."
]
}
],
"metadata": {}
}
]
}