示例#1
0
    def _test_survivor_distrib(self):
        """ensure proper number of survivors"""

        T = 1.2
        birth = 2.0
        death = 1.0
        ntrees = 10000

        survivors = []     # number of survivors in a tree

        for i in xrange(ntrees):
            tree = birthdeath.sample_birth_death_tree(T, birth, death)
            if len(tree.leaves()) == 1:
                if tree.leaves()[0].dist >= T:
                    survivors.append(1)
                else:
                    survivors.append(0)
            else:
                survivors.append(len(tree.leaves()))

        doomprob = birthdeath.prob_birth_death(1, 0, T, birth, death)
        
        h = [(1.0 - doomprob) * x/float(ntrees)
             for x in util.hist_int(survivors)]
        h[0] = doomprob
        h2 = [birthdeath.prob_birth_death(1, x, T, birth, death)
              for x in range(0, 10)]

        diff = util.vsub(h[:10], h2[:10])
        
        #print h[:10]
        #print h2[:10]
        self.assert_(max(map(abs, diff)) < .01)
示例#2
0
    def _test_expected_births(self):
        """ensure proper number of births are occurring"""

        T = 1.5
        birth = 2.0
        death = 0.0
        ntrees = 1000

        birth_counts = []  # number of births along a random path

        for i in xrange(ntrees):
            tree = birthdeath.sample_birth_death_tree(T, birth, death)
            #treelib.drawTree(tree)

            node = tree.root
            i = 0
            while not node.isLeaf():
                node = node.children[random.randint(0, 1)]
                i += 1
            birth_counts.append(i)

        fequal(mean(birth_counts), birth * T, .05)
示例#3
0
    def test_birth_death_single_sim(self):
        """test the single branch prior"""
        
        duprate = 2.0
        lossrate = .5
        ntrees = 1000
        tabsize = 100
        T = 1.0

        tops = []
        survivors = []
        lookup = {}

        # define species tree
        stree = treelib.parse_newick("(A:1);")
        def gene2species(gene):
            return gene[:1].upper()

        # simulate gene trees
        util.tic("simulating %d trees" % ntrees)
        for i in xrange(ntrees):
            tree, doom = birthdeath.sample_birth_death_tree(
                T, duprate, lossrate)

            if tree.root in doom:
                tops.append("()")
                survivors.append(0)
            else:
                rename_leaves(tree, stree, lambda x: "A")
                tops.append(phylo.hash_tree(tree, gene2species))
                survivors.append(len(tree.leaves()))
            lookup[tops[-1]] = tree
        util.toc()

        # setup test output
        outdir = "test/output/birthdeath_sim_simple"
        prep_dir(outdir)

        # histogram of topologies and survivors (# leaves)
        hist_tops = histtab(tops)
        hist_num = histtab(survivors)

        # compute survivor prior
        probs = []
        for row in hist_num:
            ngenes = row["item"]
            probs.append(birthDeathCount(ngenes, T, duprate, lossrate))

        # compute topologie priors
        probs_tops = []
        for row in hist_tops:
            tree = lookup[row["item"]]

            if tree.root.is_leaf():
                p = log(birthdeath.prob_birth_death1(
                    0, T, duprate, lossrate))
            else:
                nhist = numTopologyHistories(tree.root)
                s = len(tree.leaves())
                thist = factorial(s) * factorial(s-1) / 2**(s-1)
                r = numRedunantTopology(tree.root, gene2species,
                                        all_leaves=True)
                p = log(r * nhist / thist * birthdeath.prob_birth_death1(
                    s, T, duprate, lossrate))
            
            probs_tops.append(exp(p))

        self.calc_fit(outdir + "/sim_prior_ngenes", hist_num, probs)
        self.calc_fit(outdir + "/sim_prior_top", hist_tops, probs_tops)