collapse_monofurcations: Remove monofurcations from a tree.

View source: R/collapse_monofurcations.R

collapse_monofurcationsR Documentation

Remove monofurcations from a tree.

Description

Eliminate monofurcations (nodes with only a single child) from a phylogenetic tree, by connecting their incoming and outgoing edge.

Usage

collapse_monofurcations(tree, force_keep_root=TRUE, as_edge_counts=FALSE)

Arguments

tree

A rooted tree of class "phylo".

force_keep_root

Logical, indicating whether the root node should always be kept (i.e., even if it only has a single child).

as_edge_counts

Logical, indicating whether all edges should be assumed to have length 1. If TRUE, the outcome is the same as if the tree had no edges.

Details

All tips in the input tree retain their original indices, however the returned tree may include fewer nodes and edges. Edge and node indices may change.

If tree$edge.length is missing, then all edges in the input tree are assumed to have length 1.

Value

A list with the following elements:

tree

A new tree of class "phylo", containing only bifurcations (and multifurcations, if these existed in the input tree). The number of nodes in this tree, Nnodes_new, may be lower than of the input tree.

new2old_node

Integer vector of length Nnodes_new, mapping node indices in the new tree to node indices in the old tree.

Nnodes_removed

Integer. Number of nodes (monofurcations) removed from the tree.

Author(s)

Stilianos Louca

See Also

multifurcations_to_bifurcations

Examples

# generate a random tree
Ntips = 1000
tree = generate_random_tree(list(birth_rate_intercept=1), max_tips=Ntips)$tree

# prune the tree to generate random monofurcations
random_tips = sample.int(n=Ntips, size=0.5 * Ntips, replace=FALSE)
tree = get_subtree_with_tips(tree, only_tips=random_tips, collapse_monofurcations=FALSE)$subtree

# collapse monofurcations
new_tree = collapse_monofurcations(tree)$tree

# print summary of old and new tree
cat(sprintf("Old tree has %d nodes\n",tree$Nnode))
cat(sprintf("New tree has %d nodes\n",new_tree$Nnode))

castor documentation built on Aug. 18, 2023, 1:07 a.m.