get_subtrees_at_nodes: Extract subtrees descending from specific nodes.

View source: R/get_subtrees_at_nodes.R

get_subtrees_at_nodesR Documentation

Extract subtrees descending from specific nodes.

Description

Given a tree and a list of focal nodes, extract the subtrees descending from those focal nodes, with the focal nodes becoming the roots of the extracted subtrees.

Usage

get_subtrees_at_nodes(tree, nodes)

Arguments

tree

A tree of class "phylo".

nodes

Character vector or integer vector specifying the names or indices, respectively, of the focal nodes at which to extract the subtrees. If an integer vector, entries must be between 1 and tree$Nnode. If a character vector, each entry must be a valid entry in tree$node.label.

Details

The input tree need not be rooted, however "descendance" from a focal node is inferred based on the direction of edges in tree$edge. The input tree may include multi-furcations (i.e. nodes with more than 2 children) as well as mono-furcations (i.e. nodes with only one child).

Value

A list with the following elements:

subtrees

List of the same length as nodes, with each element being a new tree of class "phylo", containing the subtrees descending from the focal nodes. Each subtree will be rooted at the corresponding focal node.

new2old_tip

List of the same length as nodes, with the n-th element being an integer vector with values in 1,..,Ntips, mapping tip indices of the n-th subtree to tip indices in the original tree. In particular, tree$tip.label[new2old_tip[[n]]] will be equal to subtrees[[n]]$tip.label.

new2old_node

List of the same length as nodes, with the n-th element being an integer vector with values in 1,..,Nnodes, mapping node indices of the n-th subtree to node indices in the original tree.

For example, new2old_node[[2]][1] is the index that the 1st node of the 2nd subtree had within the original tree. In particular, tree$node.label[new2old_node[[n]]] will be equal to subtrees[[n]]$node.label (if node labels are available).

new2old_edge

List of the same length as nodes, with the n-th element being an integer vector with values in 1,..,Nedges, mapping edge indices of the n-th subtree to edge indices in the original tree. In particular, tree$edge.length[new2old_edge[[n]]] will be equal to subtrees[[n]]$edge.length (if edge lengths are available).

Author(s)

Stilianos Louca

See Also

get_subtree_at_node,

get_subtree_with_tips

Examples

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

# extract subtrees descending from random nodes
nodes = sample.int(tree$Nnode,size=10)
subtrees = get_subtrees_at_nodes(tree, nodes)$subtrees

# print summaries of extracted subtrees
for(n in length(nodes)){
  cat(sprintf("Subtree at %d-th node has %d tips\n",nodes[n],length(subtrees[[n]]$tip.label)))
}

castor documentation built on June 29, 2024, 9:08 a.m.