| 1 |
""" |
| 2 |
This script illustrates the use of the relaxed clock model in TreeTime. |
| 3 |
TreeTime currently only supports a normally distributed clock model. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import print_function, division |
| 7 |
import sys |
| 8 |
import numpy as np |
| 9 |
from Bio import Phylo |
| 10 |
|
| 11 |
import matplotlib.pyplot as plt |
| 12 |
from matplotlib import cm |
| 13 |
try: |
| 14 |
import seaborn as sns |
| 15 |
sns.set_style('whitegrid') |
| 16 |
except: |
| 17 |
print ("Seaborn not found. Default style will be used for the plots") |
| 18 |
|
| 19 |
from treetime import TreeTime |
| 20 |
from treetime.utils import parse_dates |
| 21 |
import treetime.config as ttconf |
| 22 |
|
| 23 |
|
| 24 |
if __name__ == '__main__': |
| 25 |
|
| 26 |
# load data and parse dates |
| 27 |
plt.ion() |
| 28 |
base_name = '../data/h3n2_na/h3n2_na_20' |
| 29 |
dates = parse_dates(base_name+'.metadata.csv') |
| 30 |
|
| 31 |
# instantiate treetime |
| 32 |
tt_relaxed = TreeTime(gtr='Jukes-Cantor', tree = base_name+'.nwk', |
| 33 |
aln = base_name+'.fasta', verbose = 4, dates = dates) |
| 34 |
|
| 35 |
# this example uses an autocorrelated molecular clock with normal prior and parent-child coupling |
| 36 |
# the parameter slack penalizes rate deviations from the average rate |
| 37 |
# couplings penalize rate changes between parent and child nodes. |
| 38 |
tt_relaxed.run(root='best', relaxed_clock={"slack":5.0, "coupling":1.0}, max_iter=3, |
| 39 |
resolve_polytomies=True, Tc=0, time_marginal=False) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
############## |
| 44 |
# PLOTTING |
| 45 |
############## |
| 46 |
|
| 47 |
# draw trees inferred with the relaxed model |
| 48 |
fig = plt.figure() |
| 49 |
ax = plt.subplot(111) |
| 50 |
vmin, vmax = 0.5, 1.5 # color branches according to the rate deviation |
| 51 |
for n in tt_relaxed.tree.find_clades(): |
| 52 |
if n.up: |
| 53 |
n.color = [int(x*255) for x in cm.cool((min(max(vmin, n.branch_length_interpolator.gamma),vmax)-vmin)/(vmax-vmin))[:3]] |
| 54 |
else: |
| 55 |
n.color = [200,200,200] |
| 56 |
|
| 57 |
ax.set_title("relaxed clock") |
| 58 |
Phylo.draw(tt_relaxed.tree, axes=ax, show_confidence=False, label_func = lambda x:'') |
| 59 |
|
| 60 |
# Scatter branch stretch against the rate multiplier of the branch. |
| 61 |
# they are expected to have a positive correlation |
| 62 |
# 1) collect the optimal branch lenght (called mutation_length), |
| 63 |
# the timetree branch length (clock_length) and the inferred rate multiplier gamma |
| 64 |
branch_lengths = [] |
| 65 |
for n in tt_relaxed.tree.find_clades(): |
| 66 |
if n.up: |
| 67 |
branch_lengths.append((n.mutation_length, n.clock_length, n.branch_length_interpolator.gamma)) |
| 68 |
|
| 69 |
|
| 70 |
# 2) plot the difference between optimal and timetree branch length vs the rate multiplier |
| 71 |
plt.figure() |
| 72 |
branch_lengths = np.array(branch_lengths) |
| 73 |
plt.scatter(branch_lengths[:,0]-branch_lengths[:,1], branch_lengths[:,2]) |
| 74 |
plt.xlabel("stretch") |
| 75 |
plt.ylabel("rate multiplier") |
| 76 |
|
| 77 |
|