def alphagamma_from_t(t, prob): """ Returns a list of tuples containing each shape obtained from `Shape` sh (assuming sh has probability prob) with their associate probability under the Alpha-Gamma model. :param sh: `Shape` instance. :param prob: `function` instance. :return: `list` instance. """ n = count_leaves(t) if t.is_leaf(): return [(add_leaf_to_edge(t), lambda a, c: simplify(prob(a, c) * (1 - a) / (n - a)))] else: for i in range(len(t.children)): k = count_leaves(t.children[i]) if n > 1: p_times_m = lambda a, c, k=k: simplify( prob(a, c) * (k - a) / (n - a)) else: p_times_m = prob for ti, q in alphagamma_from_t(t.children[i], p_times_m): ts2 = list(iter_replace_tree_at(t.children, i, ti)) yield (Shape(ts2), q) yield (add_leaf_to_edge(t), lambda a, c: simplify(prob(a, c) * c / (n - a))) k = len(t.children) if k > 1: prob2 = lambda a, c, k=k: simplify( prob(a, c) * ((k - 1) * a - c) / (n - a)) else: prob2 = prob yield (add_leaf_to_node(t), prob2)
def var_depths(t): """ Given a `Shape` t, it returns the variance of its leaves' depths. :param t: `Shape` instance. :return: `float` instance. """ n = count_leaves(t) return (n - 1) / n * variance(get_leaf_depths(t))
def cherry_picking(tree, k): if tree.is_leaf(): return [tree] elif tree == Shape.CHERRY: return [Shape.LEAF] else: def go(t, i, d): # for binary eyes only if d != k and not (t.is_leaf() or t.is_cherry()): chs = [go(ch, i, d + 1) for ch in t.children] if any(ch is None for ch in chs): return None else: return PhyloTree(None, sorted(chs)) elif d != k and (t == PhyloTree( None, [PhyloTree(i), PhyloTree(i + 1)]) or t == PhyloTree( None, [PhyloTree(i - 1), PhyloTree(i)])): return None elif d == k and (t == PhyloTree( None, [PhyloTree(i), PhyloTree(i + 1)]) or t == PhyloTree( None, [PhyloTree(i - 1), PhyloTree(i)])): return PhyloTree(i) elif t == PhyloTree(i): return None else: return t n = count_leaves(tree) phyl = shape_to_phylotree(tree, gen=int) ts = [] for i in range(n): t = go(phyl, i, 1) if t: ts.append(phylotree_to_shape(t)) return unique(sorted(ts))
def normalized_sackin_index(tree): n = count_leaves(tree) return sackin_index(tree) / (sackin_index(max_sackin(n)[0]) - sackin_index(binary_max_balanced(n)))
def normalized_binary_colless_index(tree): n = count_leaves(tree) return binary_colless_index(tree) / (binary_colless_index( max_colless(n)[0]) - binary_colless_index(binary_max_balanced(n)))
def normalized_quartet_index(tree): n = count_leaves(tree) return quartet_index(tree) / quartet_index(max_quartet(n)[0])
def normalized_binary_quartet_index(tree): n = count_leaves(tree) return binary_quartet_index(tree) / binary_quartet_index( max_binary_quartet(n)[0])
def normalized_cophenetic_index(tree): n = count_leaves(tree) return cophenetic_index(tree) / (cophenetic_index(max_cophenetic(n)[0]) - cophenetic_index(min_cophenetic(n)[0]))
def normalized_binary_qcolless_index(tree): n = count_leaves(tree) return binary_qcolless_index(tree) / (binary_qcolless_index( max_qcolless(n)[0]) - binary_qcolless_index(min_qcolless(n)[0]))