Example #1
0
            if hasattr(n, "numdate_given"):
                ofile.write("%s, %f, %f\n" %
                            (n.name, n.numdate_given, n.dist2root))
            else:
                ofile.write("%s, %f, %f\n" %
                            (n.name, d2d.numdate_from_dist2root(
                                n.dist2root), n.dist2root))
        for n in myTree.tree.get_nonterminals(order='preorder'):
            ofile.write(
                "%s, %f, %f\n" %
                (n.name, d2d.numdate_from_dist2root(n.dist2root), n.dist2root))
        print("--- wrote dates and root-to-tip distances to \n\t %s\n" %
              table_fname)

    ###########################################################################
    ### PLOT AND SAVE RESULT
    ###########################################################################
    if params.plot:
        import matplotlib.pyplot as plt
        myTree.plot_root_to_tip(label=False)
        t = np.array([np.min(dates.values()), np.max(dates.values())])
        plt.plot(t,
                 t * d2d.clock_rate + d2d.intercept,
                 label='y=%1.4f+%1.5ft, r^2=%1.2f' %
                 (d2d.intercept, d2d.clock_rate, d2d.r_val**2))
        plt.legend(loc=2)
        plt.savefig(base_name + '_root_to_tip_regression.pdf')
        print(
            "--- root-to-tip plot saved to  \n\t %s_root_to_tip_regression.pdf\n"
            % base_name)
    tt = TreeTime(gtr='Jukes-Cantor',
                  tree=base_name + '.nwk',
                  aln=base_name + '.fasta',
                  verbose=1,
                  dates=dates)

    # inititally the root if the tree is a mess:
    fig, axs = plt.subplots(1, 2, figsize=(18, 9))
    axs[0].set_title("Arbitrarily rooted tree", fontsize=18)
    axs[1].set_title("Inverse divergence-time relationship", fontsize=18)
    Phylo.draw(tt.tree,
               show_confidence=False,
               axes=axs[0],
               label_func=lambda x: x.name.split('|')[0]
               if x.is_terminal() else "")
    tt.plot_root_to_tip(ax=axs[-1])
    format_axes(fig, axs)

    # lets reroot: we now have a positve correlation of root-to-tip distance with sampling date
    tt.reroot(root="best")
    fig, axs = plt.subplots(1, 2, figsize=(18, 9))
    axs[0].set_title("Tree rerooted by treetime", fontsize=18)
    axs[1].set_title("Optimal divergence-time relationship", fontsize=18)
    Phylo.draw(tt.tree,
               show_confidence=False,
               axes=axs[0],
               label_func=lambda x: x.name.split('|')[0]
               if x.is_terminal() else "")
    tt.plot_root_to_tip(ax=axs[-1])
    format_axes(fig, axs)
Example #3
0
                     dates=dates)

    # infer an ebola time tree while rerooting and resolving polytomies
    ebola.run(root='best',
              relaxed_clock=False,
              max_iter=2,
              resolve_polytomies=True,
              Tc='skyline',
              time_marginal="assign")

    # get Skyline and 2-sigma confidence intervals
    skyline, confidence = ebola.merger_model.skyline_inferred(gen=50,
                                                              confidence=2.0)

    # scatter root to tip divergence vs sampling date
    ebola.plot_root_to_tip(add_internal=True)
    t = np.array([2014, 2016])
    plt.plot(t,
             t * ebola.date2dist.clock_rate + ebola.date2dist.intercept,
             label="y = %1.5f t%1.3f" %
             (ebola.date2dist.clock_rate, ebola.date2dist.intercept))
    plt.legend(loc=2)

    # rescale branch length to years and plot in axis 0
    from treetime.treetime import plot_vs_years
    fig, axs = plt.subplots(2,
                            1,
                            sharex=True,
                            figsize=(onecolumn_figsize[0],
                                     onecolumn_figsize[1] * 1.7))
    plot_vs_years(ebola,
Example #4
0
               axes=axs[0],
               show_confidence=False,
               label_func=lambda x: '')
    offset = myTree.tree.root.time_before_present + myTree.tree.root.branch_length
    cols = sns.color_palette()
    depth = myTree.tree.depths()
    x = np.linspace(-0.01, .2, 1000)
    for ni, node in enumerate(myTree.tree.find_clades(order="postorder")):
        axs[1].plot(offset - x,
                    node.marginal_pos_LH.prob_relative(x),
                    '-',
                    c=cols[ni % len(cols)])
        if node.up is not None:
            # add branch length distributions to tree
            x_branch = np.linspace(
                depth[node] - 2 * node.branch_length - 0.005, depth[node], 100)
            axs[0].plot(
                x_branch,
                node.ypos - 0.7 *
                node.branch_length_interpolator.prob_relative(depth[node] -
                                                              x_branch),
                '-',
                c=cols[ni % len(cols)])
    axs[1].set_yscale('log')
    axs[1].set_ylim([0.05, 1.2])
    axs[0].set_xlabel('')
    plt.tight_layout()

    # make root-to-tip plot
    myTree.plot_root_to_tip(add_internal=True, s=30)
    # include internal nodes, set symbol size to 30