### MCB112: Biological Data Analysis (Fall 2019)

# homework 07:

*the cycle of twelve*

Sand mouse cells have a circadian oscillator that depends (in part) on transcriptional regulation. The mRNA expression levels of twelve sand mouse genes are known to vary cyclically in a 24 hour period, in different phases. However, their phases relative to each other remain unknown. The phase information is critical. If the 12 phases were known, then the 12 genes could be ordered into a relative order of activation. This would make testable predictions about which gene products are most likely to be ‘upstream’ in the regulatory pathway and directly regulating transcription of which ‘downstream’ genes.

### the available data

Of course, you could do RNA-seq experiments at different time points in entrained (synchronized) sand mouse cells, but it would take time and money, and you and your lab mates are eager to leap ahead quickly to mechanism. By searching the literature and the GEO database, you’ve found that there are already a few published RNA-seq experiments in entrained sand mouse cells, at a small number of time points. You’ve collated these data into a table (which you can download from here too):

4hr | 4hr | 8hr | 8hr | 16hr | 16hr | 24hr | 24hr | |
---|---|---|---|---|---|---|---|---|

gene |
+-20 |
+-2 |
+-20 |
+-5 |
+-5 |
+-20 |
+-2 |
+-20 |

anise | 126.03 | 62.71 | 58.83 | 60.12 | 18.42 | 37.85 | 43.12 | 29.67 |

kiwi | 14.96 | 31.72 | 71.67 | 53.63 | 52.30 | 40.66 | 18.78 | 2.55 |

carrot | 18.51 | 67.09 | 42.04 | 53.43 | 32.78 | 32.27 | 66.49 | 24.99 |

grape | 19.21 | 18.00 | 37.27 | 30.94 | 71.36 | 99.78 | 27.32 | 20.85 |

tangerine | 26.77 | 31.49 | 28.86 | 22.29 | 57.75 | 103.32 | 60.55 | 42.80 |

melon | 37.44 | 41.64 | 42.79 | 26.56 | 48.16 | 35.26 | 60.70 | 72.27 |

clementine | 45.98 | 69.21 | 59.91 | 59.47 | 6.47 | 3.73 | 56.48 | 32.95 |

spinach | 9.61 | 63.58 | 93.75 | 70.85 | 39.18 | 31.04 | 38.87 | 41.49 |

beet | 42.79 | 51.03 | 24.92 | 27.29 | 39.81 | 50.76 | 65.12 | 75.09 |

huckleberry | 87.11 | 46.79 | 98.50 | 82.94 | 46.97 | 82.12 | 19.50 | 42.37 |

lentil | 8.80 | 24.44 | 12.88 | 46.04 | 71.09 | 54.28 | 21.49 | 51.04 |

cauliflower | 50.20 | 22.43 | 31.51 | 24.82 | 63.75 | 83.31 | 44.81 | 47.69 |

There are a total of 8 experiments, with two experiments each at time points at 4, 8, 16, and 24 hours, as marked in the first line of bold column headers. The numbers in the table are estimated expression levels in TPM for the 12 genes.

An example of a sand mouse gene being expressed in a circadian rhythm, with a period of 24 hours. The true average oscillation in expression level is shown with the black line. The eight available experiments have measured this gene’s expression at 4, 8, 16, and 24 hour time points. Two experiments are accurate with low variance (green dots); four experiments are inaccurate with high variance (red dots); two experiments are intermediate (yellow dots).

The catch is that the experiments have different reliabilities, as indicated in the second line of bold column headers marked +-20, +-2, and +-5. This variability is partly because different numbers of replicates were done, and partly because some experiments were more careful than others. For the purposes of the exercise, we will assume that the errors in TPM measurements are Gaussian-distributed with these known standard deviations \(\sigma\). For example, the first experiment is a 4hr time point, with normal error \(N(0,\sigma=20)\). There are two high accuracy experiments with \(\sigma = 2\) at 4hr and 24hr points; four low accuracy experiments (\(\sigma = 20\)); and two moderate accuracy experiments (\(\sigma = 5\)).

You can assume that the underlying model for an cyclic expression level \(y_t\) for a gene at time \(t\) (in hours) is

\[ y_t = b + a \sin (2 \pi \omega (t + \phi)) \]

in terms of three parameters: a baseline mean expression level \(b\) (in TPM), an oscillation amplitude \(a\) (in TPM), and a phase \(\phi\) (offset, in hours). You can treat the circadian frequency \(\omega\) as known, \(\frac{1}{24\mathrm{hr}}\). Note that the term \(2 \pi \omega (t + \phi)\) ends up in units of radians. (That’s a hint. Don’t confuse hours and radians in implementing this exercise.)

The game is to deduce the unknown phases \(\phi\), given this small amount of published RNA-seq data of variable reliability, and thereby deduce the correct relative order of the 12 genes. (If mRNA1 has a \(\phi\) +2hrs of mRNA2, that means mRNA1 starts to rise two hours before mRNA2 does, and thus is more likely to be a direct regulator of mRNA2 transcription, as opposed to the other way around.)

### Moriarty’s solution

Moriarty, the brilliant postdoc in the lab who’s a bit full of himself, says he’s seen this problem before – it’s **harmonic regression**. He’s familiar with a clever and widely used trick for it. The equation above for \(y_t\) is nonlinear in \(\phi\) (because of the \(\sin\)), but Moriarty remembers his angle addition formulas from trigonometry, one of which is:

\[ \sin(\alpha + \beta) = \sin \alpha \, \cos \beta + \sin \beta \, \cos \alpha \]

If we apply that to the modeling equation for \(y_t\) we can get a linear regression function with three parameters:

\[ y_t = p_0 + p_1 \sin( 2 \pi \omega t) + p_2 \cos(2 \pi \omega t) \]

where those three parameters are:

\[ \begin{eqnarray*} p_0 & = & b \\ p_1 & = & a \cos(2 \pi \omega \phi) \\ p_2 & = & a \sin(2 \pi \omega \phi) \\ \end{eqnarray*} \]

The function is nonlinear in \(t\), but it’s linear in the *parameters*, which is what matters for solving for \(\phi\). Moriarty says, once we’re in this form, we can just solve for the parameters with ordinary least squares. Given a least squares fit for the parameters, \(\frac{p_2}{p_1} = \tan(2 \pi \omega \phi)\) and we can use \(\arctan\) to solve for \(\hat{\phi}\); then given \(\hat{\phi}\), we can get \(\hat{a}\) from either \(p_1\) or \(p_2\).

Moriarty’s script, which you can download here, takes the data table as input, and does an ordinary least squares fit for each gene’s expression data to obtain best fit \(\hat{b}\), \(\hat{a}\), and \(\hat{\phi}\). Ordering the results by \(\hat{\phi}\), Moriarty deduces that the pathway order appears to be:

gene | b (tpm) | a (tpm) | phase \(\phi\) (hr) |
---|---|---|---|

spinach | 46.97 | 25.12 | 20.97 |

huckleberry | 63.29 | 34.52 | 20.00 |

kiwi | 38.26 | 31.60 | 18.78 |

grape | 49.31 | 35.35 | 14.63 |

lentil | 41.75 | 22.33 | 13.32 |

cauliflower | 51.18 | 22.99 | 12.20 |

tangerine | 53.34 | 30.44 | 12.02 |

beet | 46.81 | 25.43 | 7.82 |

melon | 45.29 | 18.41 | 7.74 |

carrot | 40.55 | 6.68 | 1.34 |

clementine | 34.78 | 29.27 | 0.85 |

anise | 47.97 | 29.64 | 0.22 |

But even as Moriarty goes around the lab crowing about his solution, designing incisive new experiments based on his brilliant deduction of the order (or so he says), you’re not quite convinced. The uneven reliability of the eight experiments bothers you. You’re not sure the implicit assumptions of least squares fitting have been met.

### 1. solve by maximum likelihood

Given the RNA-seq data, use maximum likelihood to solve for the unknown phases \(\phi\).

For each gene independently, you’ll need to calculate a log likelihood \(\log P(\mathrm{data} \mid \theta)\), where the observed data are the expression levels in the 8 experiments, and the parameters include the unknown parameters \(b\), \(a\), and \(\phi\) you want to find, plus the known parameters \(\sigma\) for each experiment.

Then you want to identify ML estimates \(\hat{a}\), \(\hat{b}\), and \(\hat{\phi}\) for each gene, by identifying the parameters that maximize the log likelihood.

You could discretize (as we’ve done before) and enumerate a grid search over a range of parameter values, but with three parameters in play, that’d get to be tedious. Instead, this week it’s time to learn a new trick: solve this one using multidimensional optimization using SciPy’s `optimize.minimize()`

function.

The interface to calling any general library’s optimization routine (SciPy or otherwise) is always finicky. You give it your objective function (here, your log likelihood calculation) in a general form, so SciPy can call it blindly and doesn’t need to worry about what anything means. All it needs to do is start from an initial guess at a parameter vector \(\bar{p}\) (provided by you) and move \(\bar{p}\) around in parameter space, calling the objective function you provided every time it moves to a new point, until it finds a point \(\bar{p}\) such that your objective function \(f(\bar{p})\) reaches an optimum. There’s a ton of art in making optimizers work properly, but since that’s been done for you in SciPy, essentially all the art (from your point of view) is in figuring out the interface to `optimize.minimize()`

. Documentation for it is here, and see the hints at the end for an example.

### 2. compare solutions

Moriarty is incensed that you have a different solution. He offers to bet you on who’s right. Compute the total log likelihood (summed over all 12 genes) for Moriarty’s solution and yours. Which is more likely, and by how much?

### 3. plot the fits

For each of the 12 genes, plot the observed data and the two fits (so you can show Moriarty how he went wrong).

(You can use something like a 3x4 multipanel figure in `matplotlib`

.)

### turning in your work

Email a Jupyter notebook page (a `.ipynb`

file) to mcb112.teachingfellow@gmail.com. Please name your file `<LastName><FirstName>_MCB112_<psetnumber>.ipynb`

; for example, mine would be `EddySean_MCB112_07.ipynb.`

### hints

- Here is a heavily commented example of using
`scipy.optimize.minimize`

on a simple maximum likelihood optimization.