View source: R/find_root_of_monophyletic_tips.R
find_root_of_monophyletic_tips | R Documentation |
Given a tree (rooted or unrooted) and a specific set of target tips, this function finds the tip or node that, if turned into root, would make a set of target tips a monophyletic group that either descends from a single child of the new root (if as_MRCA==FALSE
) or whose MRCA is the new root (if as_MRCA==TRUE
).
find_root_of_monophyletic_tips(tree, monophyletic_tips, as_MRCA=TRUE, is_rooted=FALSE)
tree |
A tree object of class "phylo". Can be unrooted or rooted. |
monophyletic_tips |
Character or integer vector, specifying the names or indices, respectively, of the target tips that should be turned monophyletic. If an integer vector, its elements must be between 1 and Ntips. If a character vector, its elements must be elements in |
as_MRCA |
Logical, specifying whether the new root should become the MRCA of the target tips. If |
is_rooted |
Logical, specifying whether the input tree can be assumed to be rooted. If you are sure that the input tree is rooted, set this to |
The input tree may include an arbitrary number of incoming and outgoing edges per node (but only one edge per tip), and the direction of these edges can be arbitrary. Of course, the undirected graph defined by all edges must still be a valid tree (i.e. a connected acyclic graph). This function does not change the tree, it just determines which tip or node should be made root for the target tips to be a monophyletic group. If the target tips do not form a monophyletic group regardless of root placement (this is typical if the tips are simply chosen randomly), this function returns NA
.
The asymptotic time complexity of this function is O(Nedges).
A single integer between 1 and (Ntips+Nnodes), specifying the index of the tip or node that, if made root, would make the target tips monophyletic. If this was not possible, NA
is returned.
Stilianos Louca
find_root
# generate a random tree
Ntips = 100
tree = generate_random_tree(list(birth_rate_intercept=1),max_tips=Ntips)$tree
# pick a random node and find all descending tips
MRCA = sample.int(tree$Nnode,size=1)
monophyletic_tips = get_subtree_at_node(tree, MRCA)$new2old_tip
# change root of tree (change edge directions)
tree = root_at_node(tree, new_root_node=10, update_indices=FALSE)
# determine root that would make target tips monophyletic
new_root = find_root_of_monophyletic_tips(tree, monophyletic_tips, as_MRCA=TRUE, is_rooted=FALSE)
# compare expectation with result
cat(sprintf("MRCA = %d, new root node=%d\n",MRCA,new_root-Ntips))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.