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 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