Exemplo n.º 1
0
    def test_internal_branch(self):

        duprate = .4
        lossrate = .01
        stree = treelib.parse_newick("((A:.05,B:.01):1,C:.01);")
        def gene2species(gene):
            return gene[:1].upper()
        tree = treelib.parse_newick("((((A1,B3),(A2,B2)),(A3,B1)),C1)")
        recon = phylo.reconcile(tree, stree, gene2species)
        #p = exp(calcBirthDeathPrior(tree, stree, recon, duprate, lossrate,
        #                            maxdoom))
        p = exp(c_calcBirthDeathPrior(tree, stree, recon,
                                      duprate, lossrate))

        print p, 0.0012 * 3
Exemplo n.º 2
0
    def do_test_birth_death_gene_sim(self, stree, gene2species,
                                     duprate, lossrate,
                                     ntrees=10000, tabsize=30):
        """Perform a birth death gene tree simulation test"""

        doomtable = calcDoomTable(stree, duprate, lossrate)
        
        tops = []
        lookup = {}


        def rename_tree(tree, gene2species):
            if len(tree.nodes) == 0:
                return
            spcounts = util.hist_dict(map(gene2species, tree.leaf_names()))
            names = {}
            for sp, c in spcounts.items():
                names[sp] = range(1, c+1)
                random.shuffle(names[sp])

            for node in tree.leaves():
                sp = gene2species(node.name)
                tree.rename(node.name, sp + "." + str(names[sp].pop()))
            

        util.tic("simulating %d trees" % ntrees)
        for i in xrange(ntrees):
            tree, recon, events = birthdeath.sample_birth_death_gene_tree(
                stree, duprate, lossrate, 
                removeloss=True)
            phylo.add_implied_spec_nodes(tree, stree, recon, events)

            if len(tree.nodes) == 1 and recon[tree.root] == stree.root:
                tops.append("()")
                lookup["()"] = (None, None, None)
            else:
                rename_tree(tree, gene2species)

                tops.append(phylo.hash_tree(tree))
                lookup[tops[-1]] = (tree, recon, events)
        util.toc()
        
        hist = histtab(tops)

        probs = []
        for row in hist:
            tree, recon, events = lookup[row["item"]]

            if tree is None:
                probs.append(exp(doomtable[-1]))
            else:                
                p = c_calcBirthDeathPrior(tree, stree, recon,
                                          duprate, lossrate,
                                          events=events)
                p2 = calcBirthDeathPrior(tree, stree, recon,
                                         duprate, lossrate,
                                         events=events)

                fequal(p, p2)
                probs.append(exp(p))

        return hist, probs