def test_generation_no_stop(self): """ Tests an assert is raised if no stopping criteria are given. """ with self.assertRaises(ValueError): ngesh.gen_tree(1.0, 0.5)
def test_generation_seed_bio_label(self): """ Test equality of trees generated with the same seed, bio label. """ # Enumerating label t1 = ngesh.gen_tree(1.0, 0.5, max_time=3.0, labels="bio", seed=1234) t2 = ngesh.gen_tree(1.0, 0.5, max_time=3.0, labels="bio", seed=1234) assert t1.write() == t2.write()
def test_generation_seed_no_label(self): """ Test equality of trees generated with the same seed, no label. """ # No label t1 = ngesh.gen_tree(1.0, 0.5, max_time=3.0, labels=None, seed=1234) t2 = ngesh.gen_tree(1.0, 0.5, max_time=3.0, labels=None, seed=1234) assert t1.write() == t2.write()
def tree_test(): CONCEPTS = 10 # generate a fixed tree for the tests tree = ngesh.gen_tree(1.0, 0.5, max_time=1.0, labels="human", seed=13) # TODO: need to add seed to `add_characters` # TODO: need to sort the taxa in output in `tree2nexus` tree = ngesh.add_characters(tree, CONCEPTS, 3.0, 0.5, seed=13) # collect all taxa and their characters, provided the characters exist # TODO: reuse code? already in tree2nexus data = {leaf.name: leaf.chars for leaf in tree.get_leaves()} # get the number of words to generate -- we generate all those that # will be used at the beginning, following their path as if they # always existed # TODO: generate based on phonology of current available ones? num_words = max([max(chars) for chars in data.values()]) + 1 words = abzu.kiss.random_words(num_words, param={}) print(tree) print(ngesh.tree2nexus(tree)) print(data) print(words, len(words)) for node in tree.iter_descendants(): print([node.name], node.dist) print(dir(node))
def test_generation_max_time(self): """ Tests tree generation with maximum_time stop criterion. """ tree = ngesh.gen_tree(1.0, 0.5, max_time=0.1, seed="myseed") assert (tree.write() == "((L1:0.0437074,L2:0.0437074)1:0.0503426,L3:0.09405);")
def test_generation_yule_model(self): """ Tests tree generation in a birth-only model. """ tree = ngesh.gen_tree(1.0, 0.0, max_time=1.0, seed="myseed") assert ( tree.write() == "((((L01:0.0210924,L02:0.0210924)1:0.114516,((L03:0.108258,L04:0.108258)1:0.0135044,(L05:0.110611,L06:0.110611)1:0.0111507)1:0.0138468)1:0.0157386,(L07:0.124442,L08:0.124442)1:0.0269054)1:0.798963,((L09:0.096708,L10:0.096708)1:0.655743,L11:0.752451)1:0.197859);" )
def test_generation_min_leaves(self): """ Tests tree generation with minimum leaf number stop criterion. """ tree = ngesh.gen_tree(1.0, 0.5, min_leaves=5, seed="myseed") assert ( tree.write() == "(L1:0.532642,((L2:0.0290176,L3:0.0290176)1:0.411228,((L4:0.0111507,L5:0.0111507)1:0.00297779,L6:0.0141285)1:0.426117)1:0.131906);" )
def test_generation_polytomy(self): """ Tests tree generation with hard politomy. """ tree = ngesh.gen_tree(1.0, 0.5, min_leaves=25, lam=2.5, seed="myseed") assert ( tree.write() == "((L01:0.278359,L02:0.267157,((L03:0.329973,L04:0.329973,L05:0.329973)1:0.00737038,L06:0.337343,L07:0.00637183,(L08:0.154122,(L09:0.286005,L10:0.286005,L11:0.286005,L12:0.108018,L13:0.010044,L14:0.286005,L15:0.03119)1:0.0378078,L16:0.323813)1:0.0135302,L17:0.337343)1:0.279476,((L18:0.127314,L19:0.127314,L20:0.127314,((L21:0.0391456,L22:0.0391456,L23:0.0391456,L24:0.0391456)1:0.026045,L25:0.0651906,L26:0.0651906)1:0.0621232,L27:0.127314)1:0.217102,L28:0.344416,L29:0.344416,L30:0.344416)1:0.272403)1:0.131906,L31:0.748725,L32:0.748725);" )
def test_generation_pruning(self): """ Tests tree generation with pruning in a birth-death model. """ tree = ngesh.gen_tree(1.0, 0.5, max_time=5.0, prune=True, seed="myseed") assert ( tree.write() == "(L01:2.08229,((((L02:0.836492,(L03:0.481244,L04:0.481244)1:0.266392)1:0.256375,L05:1.09287)1:0.325198,(((L06:0.0445452,L07:0.0445452)1:0.264622,L08:0.309167)1:0.323491,L09:0.632659)1:0.297593)1:1.46117,(((L10:0.925725,(L11:0.743874,L12:0.18682)1:0.0634664)1:0.304216,L13:0.490981)1:0.0129382,L14:0.740564)1:0.385008)1:0.692202);" )
def test_generation_labelling(self): """ Tests tree generation with all the label models. """ e_tree = ngesh.gen_tree(1.0, 0.5, max_time=0.5, labels="enum", seed="myseed") h_tree = ngesh.gen_tree(1.0, 0.5, max_time=0.5, labels="human", seed="myseed") b_tree = ngesh.gen_tree(1.0, 0.5, max_time=0.5, labels="bio", seed="myseed") assert ( e_tree.write() == "(L1:0.449746,((L2:0.0968117,L3:0.0968117)1:0.330426,L4:0.26938)1:0.0225083);" ) assert ( h_tree.write() == "(Hifvepo:0.449746,((Bibeu:0.0968117,Pelbe:0.0968117)1:0.330426,Fuzegpu:0.26938)1:0.0225083);" ) assert ( b_tree.write() == "(Sbibeus neartas:0.449746,((Spelbes rempucis:0.0968117,Spuzegpus spicus:0.0968117)1:0.330426,Wipepo uales:0.26938)1:0.0225083);" ) # Assert error with self.assertRaises(ValueError): ngesh.gen_tree(1.0, 0.5, max_time=0.5, labels="XXX")
def new_tree(args): """ Generates and returns a new tree. This function is a simple wrapper on the main `gen_tree()` and `add_characters()` functions. It is intented for command-line usage, but it can be used in code for quick prototyping. Parameters ---------- args : Namespace A namespace with the parameters for tree generation. Returns ------- tree: ete3 object The randomly generated tree. """ # Generate the random tree tree = ngesh.gen_tree( args.birth, args.death, min_leaves=args.min_leaves, max_time=args.max_time, labels=args.labels, lam=args.lam, seed=args.seed, ) # Add characters if requested if args.num_chars: tree = ngesh.add_characters( tree, args.num_chars, args.k_mut, args.th_mut, k_hgt=args.k_hgt, th_hgt=args.th_hgt, e=args.e_mut, seed=args.seed, ) # Simulate bad sampling if requested if args.sampling: ngesh.simulate_bad_sampling(tree, args.sampling, seed=args.seed) return tree