Exemple #1
0
    def test_birth_wait_time(self):
        """ensure reconstructed birth wait time has right distribution"""

        T = 1.2
        birth = 2.0
        death = 1.2

        times = [birthdeath.sample_birth_wait_time(1, T, birth, death)
                 for i in xrange(10000)]

        hx, hy = util.distrib(times, 30)

        cond = 1.0 - birthdeath.prob_no_birth(1, T, birth, death)
        x = list(frange(0, T, T/40.0))        
        y = [birthdeath.birth_wait_time(t, 1, T, birth, death) / cond
             for t in x]

        print sum(y) * T / 40.0

        prep_dir("test/output/sim-wait")
        rplot_start("test/output/sim-wait/wait_time.pdf")
        rplot("plot", hx, hy, t="l", ylim=[0, max(hy)])
        rp.lines(x, y, col="red")
        rplot_end(False)
Exemple #2
0
    def test_branch_prior_simple2(self):
        """Test branch prior 2"""

        tree = treelib.parse_newick("((a1:2, a2:3):.4, b1:2);")
        stree = treelib.parse_newick("(A:2, B:2);")

        gene2species = lambda x: x[0].upper()

        params = {
            "A": (1.0, 1.0),
            "B": (3.0, 3.0),
            1: (1.0, 1.0),
            "baserate": (11.0, 10.0)
        }
        birth = .01
        death = .02
        pretime = 1.0
        nsamples = 100

        recon = phylo.reconcile(tree, stree, gene2species)
        events = phylo.label_events(tree, recon)
        #pd(mapdict(recon, key=lambda x: x.name, val=lambda x: x.name))
        #pd(mapdict(events, key=lambda x: x.name))

        p = spidir.branch_prior(tree,
                                stree,
                                recon,
                                events,
                                params,
                                birth,
                                death,
                                nsamples=nsamples,
                                approx=False)

        tot = 0.0

        gstart = 0.01
        gend = 3.0
        step = (gend - gstart) / 20.0
        s2 = step / 2.0
        gs = list(frange(gstart + s2, gend + s2, step))
        for g in gs:
            pg = invgammaPdf(g, params["baserate"])

            pa = 0.0

            for i in range(nsamples):

                t = birthdeath.sample_birth_wait_time(1, stree.nodes["A"].dist,
                                                      birth, death)
                #print t

                t2 = stree.nodes["A"].dist - t

                pa1 = gammaPdf(tree.nodes["a1"].dist,
                               [params["A"][0], params["A"][1] / (g * t2)])

                pa2 = gammaPdf(tree.nodes["a2"].dist,
                               [params["A"][0], params["A"][1] / (g * t2)])

                pb = spidir.gammaSumPdf(
                    tree.nodes["b1"].dist + tree.nodes[2].dist, 2,
                    [params["B"][0], params["A"][0]], [
                        params["B"][1] /
                        (g * stree.nodes["B"].dist), params["A"][1] / (g * t)
                    ], .001)

                if "nan" not in map(str, [pa1, pa2, pb]):
                    pa += pa1 * pa2 * pb / nsamples

            tot += pg * pa * step
        #tot /= len(gs)

        print "unfold", (tree.nodes["b1"].dist + tree.nodes[2].dist,
                         [params["B"][0], params["A"][0]], [
                             params["B"][1] / (g * stree.nodes["B"].dist),
                             params["A"][1] / (g * t)
                         ])

        print "C", p
        print "P", log(tot)
Exemple #3
0
    def test_branch_prior_simple2(self):
        """Test branch prior 2"""

        tree = treelib.parse_newick("((a1:2, a2:3):.4, b1:2);")
        stree = treelib.parse_newick("(A:2, B:2);")

        gene2species = lambda x: x[0].upper()

        params = {"A": (1.0, 1.0), "B": (3.0, 3.0), 1: (1.0, 1.0), "baserate": (11.0, 10.0)}
        birth = 0.01
        death = 0.02
        pretime = 1.0
        nsamples = 100

        recon = phylo.reconcile(tree, stree, gene2species)
        events = phylo.label_events(tree, recon)
        # pd(mapdict(recon, key=lambda x: x.name, val=lambda x: x.name))
        # pd(mapdict(events, key=lambda x: x.name))

        p = spidir.branch_prior(tree, stree, recon, events, params, birth, death, nsamples=nsamples, approx=False)

        tot = 0.0

        gstart = 0.01
        gend = 3.0
        step = (gend - gstart) / 20.0
        s2 = step / 2.0
        gs = list(frange(gstart + s2, gend + s2, step))
        for g in gs:
            pg = invgammaPdf(g, params["baserate"])

            pa = 0.0

            for i in range(nsamples):

                t = birthdeath.sample_birth_wait_time(1, stree.nodes["A"].dist, birth, death)
                # print t

                t2 = stree.nodes["A"].dist - t

                pa1 = gammaPdf(tree.nodes["a1"].dist, [params["A"][0], params["A"][1] / (g * t2)])

                pa2 = gammaPdf(tree.nodes["a2"].dist, [params["A"][0], params["A"][1] / (g * t2)])

                pb = spidir.gammaSumPdf(
                    tree.nodes["b1"].dist + tree.nodes[2].dist,
                    2,
                    [params["B"][0], params["A"][0]],
                    [params["B"][1] / (g * stree.nodes["B"].dist), params["A"][1] / (g * t)],
                    0.001,
                )

                if "nan" not in map(str, [pa1, pa2, pb]):
                    pa += pa1 * pa2 * pb / nsamples

            tot += pg * pa * step
        # tot /= len(gs)

        print "unfold", (
            tree.nodes["b1"].dist + tree.nodes[2].dist,
            [params["B"][0], params["A"][0]],
            [params["B"][1] / (g * stree.nodes["B"].dist), params["A"][1] / (g * t)],
        )

        print "C", p
        print "P", log(tot)