Learn R Programming

TreeTools (version 1.12.0)

ListAncestors: List ancestors

Description

ListAncestors() reports all ancestors of a given node.

Usage

ListAncestors(parent, child, node = NULL)

AllAncestors(parent, child)

Value

If node = NULL, ListAncestors() returns a list. Each entry i contains a vector containing, in order, the nodes encountered when traversing the tree from node i to the root node. The last entry of each member of the list is therefore the root node, with the exception of the entry for the root node itself, which is a zero-length integer.

If node is an integer, ListAncestors() returns a vector of the numbers of the nodes ancestral to the given node, including the root node.

Arguments

parent

Integer vector corresponding to the first column of the edge matrix of a tree of class phylo, i.e. tree[["edge"]][, 1]

child

Integer vector corresponding to the second column of the edge matrix of a tree of class phylo, i.e. tree[["edge"]][, 2].

node

Integer giving the index of the node or tip whose ancestors are required, or NULL to return ancestors of all nodes.

Functions

  • AllAncestors(): Alias for ListAncestors(node = NULL).

Details

Note that if node = NULL, the tree's edges must be listed such that each internal node (except the root) is listed as a child before it is listed as a parent, i.e. its index in child is less than its index in parent. This will be true of trees listed in Preorder.

See Also

Implemented less efficiently in phangorn:::Ancestors, on which this code is based.

Other tree navigation: AncestorEdge(), CladeSizes(), DescendantEdges(), EdgeAncestry(), EdgeDistances(), MRCA(), MatchEdges(), NDescendants(), NodeDepth(), NodeNumbers(), NodeOrder(), RootNode()

Other tree navigation: AncestorEdge(), CladeSizes(), DescendantEdges(), EdgeAncestry(), EdgeDistances(), MRCA(), MatchEdges(), NDescendants(), NodeDepth(), NodeNumbers(), NodeOrder(), RootNode()

Examples

Run this code
tree <- PectinateTree(5)
edge <- tree[["edge"]]

# Identify desired node with:
plot(tree)
nodelabels()
tiplabels()

# Ancestors of specific nodes:
ListAncestors(edge[, 1], edge[, 2], 4L)
ListAncestors(edge[, 1], edge[, 2], 8L)

# Ancestors of each node, if tree numbering system is uncertain:
lapply(seq_len(max(edge)), ListAncestors,
       parent = edge[, 1], child = edge[, 2])

# Ancestors of each node, if tree is in preorder:
ListAncestors(edge[, 1], edge[, 2])

# Alias:
AllAncestors(edge[, 1], edge[, 2])

Run the code above in your browser using DataLab