R: Extract a subtree descending from a specific node.

get_subtree_at_node {castor} | R Documentation

## Extract a subtree descending from a specific node.

### Description

Given a tree and a focal node, extract the subtree descending from the focal node and place the focal node as the root of the extracted subtree.

### Usage

    get_subtree_at_node(tree, node)

### Arguments

`tree` | A tree of class "phylo".
`node` | Character or integer specifying the name or index, respectively, of the focal node at which to extract the subtree. If an integer, it must be between 1 and `tree$Nnode`. If a character, it must be a valid entry in `tree$node.label`.

### Details

The input tree need not be rooted, however "descendance" from the 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:

`subtree` | A new tree of class "phylo", containing the subtree descending from the focal node. This tree will be rooted, with the new root being the focal node.
`new2old_tip` | Integer vector of length Ntips_kept (=number of tips in the extracted subtree) with values in 1,..,Ntips, mapping tip indices of the subtree to tip indices in the original tree. In particular, `tree$tip.label[new2old_tip]` will be equal to `subtree$tip.label`.
`new2old_node` | Integer vector of length Nnodes_kept (=number of nodes in the extracted subtree) with values in 1,..,Nnodes, mapping node indices of the subtree to node indices in the original tree. For example, `new2old_node[1]` is the index that the first node of the subtree had within the original tree. In particular, `tree$node.label[new2old_node]` will be equal to `subtree$node.label` (if node labels are available).
`new2old_edge` | Integer vector of length Nedges_kept (=number of edges in the extracted subtree), with values in 1,..,Nedges, mapping edge indices of the subtree to edge indices in the original tree. In particular, `tree$edge.length[new2old_edge]` will be equal to `subtree$edge.length` (if edge lengths are available).

### Author(s)

Stilianos Louca

### See Also

`get_subtree_with_tips`

### Examples

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

    # extract subtree descending from a random node
    node = sample.int(tree$Nnode,size=1)
    subtree = get_subtree_at_node(tree, node)$subtree

    # print summary of subtree
    cat(sprintf("Subtree at %d-th node has %d tips\n",node,length(subtree$tip.label)))

[Package castor version 1.7.0 Index]