def test_birth_death_single2_sim(self): """test the single branch prior""" duprate = 2.0 lossrate = .5 T = 1.0 stree = treelib.parse_newick("(A:1,B:1);") def gene2species(gene): return gene[:1].upper() s = stree.leaves()[0] b = birthDeathCount(1, T, duprate, lossrate) # 1 tree = treelib.parse_newick("(a,b);") recon = phylo.reconcile(tree, stree, gene2species) p = birthDeathCount(1, T, duprate, lossrate) * b p2 = exp(calcBirthDeathPrior(tree, stree, recon, duprate, lossrate)) p2 *= numRedunantTopology(tree.root, gene2species) print p, p2 fequal(p, p2) # 2 tree = treelib.parse_newick("((a,a),b);") recon = phylo.reconcile(tree, stree, gene2species) p = birthDeathCount(2, T, duprate, lossrate) * b p2 = exp(calcBirthDeathPrior(tree, stree, recon, duprate, lossrate)) p2 *= numRedunantTopology(tree.root, gene2species) print p, p2 fequal(p, p2) # 3 tree = treelib.parse_newick("(((a,a),a),b);") recon = phylo.reconcile(tree, stree, gene2species) p = birthDeathCount(3, T, duprate, lossrate) * b p2 = exp(calcBirthDeathPrior(tree, stree, recon, duprate, lossrate)) p2 *= numRedunantTopology(tree.root, gene2species) print p, p2 fequal(p, p2) # 4 tree = treelib.parse_newick("(((a,a),(a,a)),b);") recon = phylo.reconcile(tree, stree, gene2species) p = birthDeathCount(4, T, duprate, lossrate) * b / 3.0 p2 = exp(calcBirthDeathPrior(tree, stree, recon, duprate, lossrate)) p2 *= numRedunantTopology(tree.root, gene2species) print p, p2 fequal(p, p2)
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)