Debian Codegraph @

debian-tests-data/scripts/rerooting_and_timetrees.py

raw
1 from __future__ import print_function, division
2 import numpy as np
3 from Bio import Phylo
4 import matplotlib.pyplot as plt
5 from matplotlib import cm
6 try:
7 import seaborn as sns
8 sns.set_style('whitegrid')
9 except:
10 print ("Seaborn not found. Default style will be used for the plots")
11
12 from treetime import TreeTime
13 from treetime.utils import parse_dates
14
15 def format_axes(fig, axs):
16 axs[0].set_axis_off()
17 axs[1].tick_params(labelsize=14)
18 axs[1].set_ylabel("root-to-tip distance", fontsize=16)
19 axs[1].set_xlabel("date", fontsize=16)
20 fig.tight_layout()
21
22
23 if __name__ == '__main__':
24
25 # load data and parse dates
26 plt.ion()
27 base_name = '../data/h3n2_na/h3n2_na_20'
28
29 dates = parse_dates(base_name+'.metadata.csv')
30 tt = TreeTime(gtr='Jukes-Cantor', tree = base_name+'.nwk',
31 aln = base_name+'.fasta', verbose = 1, dates = dates)
32
33 # inititally the root if the tree is a mess:
34 fig, axs = plt.subplots(1,2, figsize=(18,9))
35 axs[0].set_title("Arbitrarily rooted tree", fontsize=18)
36 axs[1].set_title("Inverse divergence-time relationship", fontsize=18)
37 Phylo.draw(tt.tree, show_confidence=False, axes=axs[0], label_func=lambda x:x.name.split('|')[0] if x.is_terminal() else "")
38 tt.plot_root_to_tip(ax=axs[-1])
39 format_axes(fig, axs)
40
41 # lets reroot: we now have a positve correlation of root-to-tip distance with sampling date
42 tt.reroot(root="least-squares")
43 fig, axs = plt.subplots(1,2, figsize=(18,9))
44 axs[0].set_title("Tree rerooted by treetime", fontsize=18)
45 axs[1].set_title("Optimal divergence-time relationship", fontsize=18)
46 Phylo.draw(tt.tree, show_confidence=False, axes=axs[0],
47 label_func=lambda x:x.name.split('|')[0] if x.is_terminal() else "")
48 tt.plot_root_to_tip(ax=axs[-1])
49 format_axes(fig, axs)
50
51 # rerooting can be done along with the tree time inference
52 tt.run(root="best", branch_length_mode='input', max_iter=2)
53 # if more complicated models (relaxed clocks, coalescent models) are to be used
54 # or you want to resolve polytomies, treetime needs to be run for
55 # several iterations, for example as
56 # tt.run(root="best", resolve_polytomies=True, max_iter=2)
57
58 # each node is now at a position that correspond to the given or inferred date
59 # the units of branch length are still clock rate.
60 print("clock rate: %1.5f"%tt.date2dist.clock_rate)
61 fig, axs = plt.subplots(1,2, figsize=(18,9))
62 Phylo.draw(tt.tree, label_func=lambda x:'', show_confidence=False, axes=axs[0])
63 axs[0].set_title("Tree: units are substitutions", fontsize=18)
64
65 # we can convert the branch length to units in years and redraw
66 tt.branch_length_to_years()
67 Phylo.draw(tt.tree, label_func=lambda x:'', show_confidence=False, axes=axs[1])
68 axs[1].set_title("Tree: units are years", fontsize=18)
69 axs[0].tick_params(labelsize=14)
70 axs[1].tick_params(labelsize=14)
71 fig.tight_layout()
72
73 # treetime implements a convenience function to plot timetrees
74 from treetime.treetime import plot_vs_years
75 plot_vs_years(tt, label_func=lambda x:"", show_confidence=False)
76