| 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 treetime import TreeAnc |
| 6 |
import treetime |
| 7 |
|
| 8 |
if __name__ == '__main__': |
| 9 |
plt.ion() |
| 10 |
|
| 11 |
# path to data |
| 12 |
base_name = '../data/h3n2_na/h3n2_na_200' |
| 13 |
|
| 14 |
# instantiate treetime |
| 15 |
myTree = TreeAnc(gtr='Jukes-Cantor', tree=base_name+".nwk", aln=base_name+'.fasta', verbose = 0) |
| 16 |
|
| 17 |
# infer a GTR model and ML ancestral sequences, either marginally or jointly most likely |
| 18 |
myTree.infer_ancestral_sequences(infer_gtr=True, marginal=True) |
| 19 |
|
| 20 |
# lets examine the properties of a node in the tree after ancestral inference |
| 21 |
node = myTree.tree.get_nonterminals()[7] |
| 22 |
# each node now has an inferred sequence |
| 23 |
if treetime.version<"0.7": |
| 24 |
print("\nthe inferred sequences is an array of characters:", node.sequence) |
| 25 |
else: |
| 26 |
print("\nthe inferred sequences is an array of characters:", myTree.sequence(node, as_string=False)) |
| 27 |
|
| 28 |
# in addition, each node of the tree now has an mutation object attached |
| 29 |
# note that the mutation numbering starts at 0 rather than 1 |
| 30 |
print("\nmutations on the branch leading to node %s:"%node.name, node.mutations) |
| 31 |
|
| 32 |
# we can readily verify these mutations by checking the inferred sequences |
| 33 |
if node.mutations: |
| 34 |
mut = node.mutations[0] |
| 35 |
if treetime.version<"0.7": |
| 36 |
print("\nmutation %s%d%s corresponds to"%mut, |
| 37 |
"parent state: %s, child state %s\n\n"%(node.up.sequence[mut[1]], node.sequence[mut[1]])) |
| 38 |
else: |
| 39 |
print("\nmutation %s%d%s corresponds to"%mut, |
| 40 |
"parent state: %s, child state %s\n\n"%(myTree.sequence(node.up)[mut[1]], myTree.sequence(node)[mut[1]])) |
| 41 |
|
| 42 |
|
| 43 |
# plot the tree and label branches by mutation |
| 44 |
fig, ax = plt.subplots(1,1,figsize=(18,13)) |
| 45 |
ax.set_title("branches annotated by inferred mutations", fontsize=18) |
| 46 |
ax.set_axis_off() |
| 47 |
Phylo.draw(myTree.tree, label_func = lambda x:"", axes=ax, |
| 48 |
branch_labels=lambda x:", ".join(map(lambda m: "%s%d%s"%m, x.mutations[:3])) |
| 49 |
+('...' if len(x.mutations)>3 else "")) |
| 50 |
|
| 51 |
# finally, we print the inferred GTR model |
| 52 |
print(myTree.gtr) |