def test_make_consensus_tree(self): """correctly gets parent consensus counts""" input = [['a','b','c','d','e','f','g'], ['a','b','c',None,None,'x','y'], ['h','i','j','k','l','m','n'], ['h','i','j','k','l','m','q'], ['h','i','j','k','l','m','n']] exp_str = "(((((((g)f)e)d,(((y)x)))c)b)a,((((((n,q)m)l)k)j)i)h);" obs_root, lookup = make_consensus_tree(input, check_for_rank=False) self.assertEqual(obs_root.getNewick(with_distances=False), exp_str) self.assertNotContains(None, lookup)
def test_make_consensus_tree_withtips(self): """correctly constructs the taxonomy tree with tip info""" input = [['a','b','c','d','e','f','g'], ['a','b','c',None,None,'x','y'], ['h','i','j','k','l','m','n'], ['h','i','j','k','l','m','q'], ['h','i','j','k','l','m','n']] input_ids = ['1','2','3','4','5'] exp_str = "((((((((1)g)f)e)d,((((2)y)x)))c)b)a,(((((((3,5)n,(4)q)m)l)k)j)i)h);" obs_root, lookup = make_consensus_tree(input, check_for_rank=False, tips=input_ids) self.assertEqual(obs_root.getNewick(with_distances=False), exp_str) self.assertNotContains(None, lookup)
def test_make_consensus_tree(self): """correctly gets parent consensus counts""" data = [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', None, None, 'x', 'y'], ['h', 'i', 'j', 'k', 'l', 'm', 'n'], ['h', 'i', 'j', 'k', 'l', 'm', 'q'], ['h', 'i', 'j', 'k', 'l', 'm', 'n']] exp_str = "(((((((g)f)e)d,(((y)x)))c)b)a,((((((n,q)m)l)k)j)i)h);" obs_root, lookup = make_consensus_tree(data, check_for_rank=False) self.assertEqual(str(obs_root).strip(), exp_str) self.assertNotIn(None, lookup)
def test_make_consensus_tree(self): """correctly gets parent consensus counts""" input = [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', None, None, 'x', 'y'], ['h', 'i', 'j', 'k', 'l', 'm', 'n'], ['h', 'i', 'j', 'k', 'l', 'm', 'q'], ['h', 'i', 'j', 'k', 'l', 'm', 'n']] exp_str = "(((((((g)f)e)d,(((y)x)))c)b)a,((((((n,q)m)l)k)j)i)h);" obs_root, lookup = make_consensus_tree(input, check_for_rank=False) self.assertEqual(obs_root.to_newick(with_distances=False), exp_str) self.assertNotIn(None, lookup)
def get_polyphyletic(cons): """get polyphyletic groups and a representative tip""" tips, taxonstrings = unzip(cons.items()) tree, lookup = make_consensus_tree(taxonstrings, False, tips=tips) cache_tipnames(tree) names = {} for n in tree.non_tips(): if n.name is None: continue if (n.name, n.Rank) not in names: names[(n.name, n.Rank)] = {} if n.parent is not None: names[(n.name, n.Rank)][n.parent.name] = n.tip_names[0] return names
def test_make_consensus_tree_withtips(self): """correctly constructs the taxonomy tree with tip info""" data = [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', None, None, 'x', 'y'], ['h', 'i', 'j', 'k', 'l', 'm', 'n'], ['h', 'i', 'j', 'k', 'l', 'm', 'q'], ['h', 'i', 'j', 'k', 'l', 'm', 'n']] input_ids = ['1', '2', '3', '4', '5'] exp_str = ("((((((((1)g)f)e)d,((((2)y)x)))c)b)a,(((((((3,5)n," "(4)q)m)l)k)j)i)h);") obs_root, lookup = make_consensus_tree( data, check_for_rank=False, tips=input_ids) self.assertEqual(str(obs_root).strip(), exp_str) self.assertNotIn(None, lookup)
def test_walk_consensus_tree(self): """correctly walk consensus tree""" input = [['a','b','c','d','e','f','g'], ['a','b','c',None,None,'x','y'], ['h','i','j','k','l','m','n'], ['h','i','j','k','l','m','q'], ['h','i','j','k','l','m','n']] root, lookup = make_consensus_tree(input, check_for_rank=False) exp1 = ['n','m','l'] exp2 = ['a'] exp3 = ['x','f__','o__','c'] obs1 = walk_consensus_tree(lookup, 'n', 3, reverse=False) obs2 = walk_consensus_tree(lookup, 'a', 3, reverse=False) obs3 = walk_consensus_tree(lookup, 'x', 4, reverse=False) self.assertEqual(obs1, exp1) self.assertEqual(obs2, exp2) self.assertEqual(obs3, exp3)
def test_walk_consensus_tree(self): """correctly walk consensus tree""" data = [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', None, None, 'x', 'y'], ['h', 'i', 'j', 'k', 'l', 'm', 'n'], ['h', 'i', 'j', 'k', 'l', 'm', 'q'], ['h', 'i', 'j', 'k', 'l', 'm', 'n']] _, lookup = make_consensus_tree(data, check_for_rank=False) exp1 = ['n', 'm', 'l'] exp2 = ['a'] exp3 = ['x', 'f__', 'o__', 'c'] obs1 = walk_consensus_tree(lookup, 'n', 3, reverse=False) obs2 = walk_consensus_tree(lookup, 'a', 3, reverse=False) obs3 = walk_consensus_tree(lookup, 'x', 4, reverse=False) self.assertEqual(obs1, exp1) self.assertEqual(obs2, exp2) self.assertEqual(obs3, exp3)
def generate_constrings(tree, tipname_map, verbose=False): """Assigns taxonomy to unidentified sequences in tree. Returns all sequence IDs on tree.""" counts = nlevel.collect_names_at_ranks_counts(tree) min_count = 2 nlevel.decorate_ntips(tree) nlevel.decorate_name_relative_freqs(tree, counts, min_count) nlevel.set_ranksafe(tree) nlevel.pick_names(tree) nlevel.name_node_score_fold(tree) if verbose: print "Tree score: ", nlevel.score_tree(tree) nlevel.set_preliminary_name_and_rank(tree) contree, contree_lookup = nlevel.make_consensus_tree(tipname_map.values()) nlevel.backfill_names_gap(tree, contree_lookup) nlevel.commonname_promotion(tree) nlevel.make_names_unique(tree, append_suffix=False) constrings = nlevel.pull_consensus_strings(tree) return constrings