Debian Codegraph @

debian-tests-data/scripts/ebola.py

raw
1 '''
2 this script illustrates the use of treetime to analyze viral sequences.
3 As an example, we use Ebola virus sequences from the 2014-2015 outbreak in
4 West Africa. This example contains more than 300 sequences, it will take
5 a few minutes to run.
6 '''
7
8 from __future__ import print_function, division
9 import sys
10 import numpy as np
11 from Bio import Phylo
12
13 import matplotlib.pyplot as plt
14 from matplotlib import cm
15 try:
16 import seaborn as sns
17 sns.set_style('whitegrid')
18 except:
19 print ("Seaborn not found. Default style will be used for the plots")
20
21 from treetime import TreeTime
22 from treetime.utils import parse_dates
23 import treetime.config as ttconf
24
25 if __name__ == '__main__':
26
27
28 plt.ion()
29 base_name = '../data/ebola/ebola'
30 dates = parse_dates(base_name+'.metadata.csv')
31 # instantiate treetime
32 ebola = TreeTime(gtr='Jukes-Cantor', tree = base_name+'.nwk', precision=1,
33 aln = base_name+'.fasta', verbose = 2, dates = dates)
34
35 # infer an ebola time tree while rerooting and resolving polytomies
36 res = ebola.run(root='best', infer_gtr=True, relaxed_clock=False, max_iter=2,
37 branch_length_mode='input', n_iqd=3, resolve_polytomies=True,
38 Tc='skyline', time_marginal="assign", vary_rate=True)
39
40 if res==ttconf.ERROR:
41 sys.exit()
42
43 # scatter root to tip divergence vs sampling date
44 ebola.plot_root_to_tip(add_internal=True)
45 plt.legend(loc=2)
46
47 # rescale branch length to years and plot in axis 0
48 from treetime.treetime import plot_vs_years
49 fig, axs = plt.subplots(1,2, sharey=True, figsize=(12,8))
50 plot_vs_years(ebola, step=1, ax=axs[1], confidence=(0.05,0.95), label_func = lambda x:"")
51 axs[1].set_xlim(0, 2.5)
52 axs[1].set_title("time tree")
53
54 # assign mutation length to branch length and plot the mutation tree in axs[1]
55 axs[0].set_title("mutation tree")
56 for n in ebola.tree.find_clades():
57 n.branch_length=n.mutation_length
58 Phylo.draw(ebola.tree, label_func=lambda x:"", axes=axs[0])
59 plt.tight_layout()
60
61 # reset branch length to time (in substitution rate units)
62 for n in ebola.tree.find_clades():
63 n.branch_length=n.clock_length
64
65 # OUTPUT the GTR model
66 print(ebola.gtr)
67
68 # plot Skyline
69 skyline, confidence = ebola.merger_model.skyline_inferred(gen=50, confidence=2.0)
70 skyline_empirical = ebola.merger_model.skyline_empirical(gen=50)
71 plt.figure()
72 plt.fill_between(skyline.x, confidence[0], confidence[1], color=(0.8, 0.8, 0.8))
73 plt.plot(skyline.x, skyline.y, label='maximum likelihood skyline')
74 plt.plot(skyline_empirical.x, skyline_empirical.y, label='empirical skyline')
75 plt.yscale('log')
76 plt.legend()
77 plt.ticklabel_format(axis='x',useOffset=False)
78
79
80
81
82