def _ilr_with_tree(X, tree): # Import ilr_transform from gneiss from gneiss.composition import ilr_transform # Check tree type tree_type = _infer_tree_type(tree) # Check leaves components = set(X.columns) leaves_in_tree = _get_leaves(tree=tree, tree_type=tree_type) assert components <= leaves_in_tree, "Not all components (X.columns) are represented in tree" # Prune tree if components < leaves_in_tree: tree = tree.copy() n_leaves_before_pruning = len(leaves_in_tree) tree = _prune_tree(tree=tree, tree_type=tree_type, leaves=components) n_leaves_after_pruning = len( _get_leaves(tree=tree, tree_type=tree_type)) n_pruned = n_leaves_before_pruning - n_leaves_after_pruning if verbose: print("Pruned {} attributes to match components (X.columns)". format(n_pruned), file=sys.stderr) # Polytomy if check_polytomy: _check_polytomy(tree=tree, tree_type=tree_type) # ETE if tree_type == "ete": tree = _ete_to_skbio(tree=tree) return ilr_transform(table=X, tree=tree)
def ilr_phylogenetic( table: pd.DataFrame, tree: skbio.TreeNode, pseudocount: float = 0.5) -> (pd.DataFrame, skbio.TreeNode): t = tree.copy() t.bifurcate() t = rename_internal_nodes(t) return ilr_transform(add_pseudocount(table, pseudocount), t), t
def ilr_hierarchical(table: pd.DataFrame, tree: skbio.TreeNode, pseudocount: float = 0.5) -> pd.DataFrame: return ilr_transform(add_pseudocount(table, pseudocount), tree)