get_all_distances_to_root {castor} | R Documentation |
Given a rooted phylogenetic tree, calculate the phylogenetic distance (cumulative branch length) of the root to each tip and node.
get_all_distances_to_root(tree, as_edge_count=FALSE)
tree |
A rooted tree of class "phylo". The root is assumed to be the unique node with no incoming edge. |
as_edge_count |
Logical, specifying whether distances should be counted in number of edges, rather than cumulative edge length. This is the same as if all edges had length 1. |
If tree$edge.length
is missing, then every edge in the tree is assumed to be of length 1. The tree may include multi-furcations as well as mono-furcations (i.e. nodes with only one child).
The asymptotic average time complexity of this function is O(Nedges), where Nedges is the number of edges in the tree.
A numeric vector of size Ntips+Nnodes, with the i-th element being the distance (cumulative branch length) of the i-th tip or node to the root. Tips are indexed 1,..,Ntips and nodes are indexed (Ntips+1),..,(Ntips+Nnodes).
Stilianos Louca
# generate a random tree Ntips = 1000 tree = generate_random_tree(list(birth_rate_intercept=1, death_rate_intercept=0.5), max_tips=Ntips)$tree # calculate distances to root all_distances = get_all_distances_to_root(tree) # extract distances of nodes to root node_distances = all_distances[(Ntips+1):(Ntips+tree$Nnode)] # plot histogram of distances (across all nodes) hist(node_distances, xlab="distance to root", ylab="# nodes", prob=FALSE);