Exemplo n.º 1
0
        import matplotlib.pyplot as plt
        plt.ion()
        leaf_count = myTree.tree.count_terminals()
        label_func = lambda x: x.name[:20] if (leaf_count < 30 & x.is_terminal(
        )) else ''
        branch_label_func = lambda x: (
            ','.join([a + str(pos) + d for a, pos, d in x.mutations[:10]]) +
            ('...'
             if len(x.mutations) > 10 else '')) if leaf_count < 30 else ''
        plot_vs_years(
            myTree, show_confidence=False,
            label_func=label_func)  #, branch_labels=branch_label_func)
        plt.savefig(base_name + '_tree.pdf')
    else:
        # convert branch length to years (this is implicit in the above plot)
        myTree.branch_length_to_years()

    # decorate tree with inferred mutations
    outaln_name = base_name + '_ancestral.fasta'
    AlignIO.write(myTree.get_reconstructed_alignment(), outaln_name, 'fasta')
    for n in myTree.tree.find_clades():
        if n.up is None:
            continue
        if len(n.mutations):
            n.name += '_' + '_'.join(
                [a + str(pos) + d for (a, pos, d) in n.mutations])

    # write tree to file. Branch length will now be scaled such that node
    # positions correspond to sampling times.
    outtree_name = base_name + '_timetree.newick'
    Phylo.write(myTree.tree, outtree_name, 'newick')
Exemplo n.º 2
0
    # rerooting can be done along with the tree time inference
    tt.run(root="best", branch_length_mode='input')
    # if more complicated models (relaxed clocks, coalescent models) are to be used
    # or you want to resolve polytomies, treetime needs to be run for
    # several iterations, for example as
    # tt.run(root="best", resolve_polytomies=True, max_iter=2)

    # each node is now at a position that correspond to the given or inferred date
    # the units of branch length are still clock rate.
    print("clock rate: %1.5f" % tt.date2dist.clock_rate)
    fig, axs = plt.subplots(1, 2, figsize=(18, 9))
    Phylo.draw(tt.tree,
               label_func=lambda x: '',
               show_confidence=False,
               axes=axs[0])
    axs[0].set_title("Tree: units are substitutions", fontsize=18)
    # we can convert the branch length to units in years and redraw
    tt.branch_length_to_years()
    Phylo.draw(tt.tree,
               label_func=lambda x: '',
               show_confidence=False,
               axes=axs[1])
    axs[1].set_title("Tree: units are years", fontsize=18)
    axs[0].tick_params(labelsize=14)
    axs[1].tick_params(labelsize=14)
    fig.tight_layout()

    # treetime implements a convenience function to plot timetrees
    from treetime.treetime import plot_vs_years
    plot_vs_years(tt, label_func=lambda x: "", show_confidence=False)