add.ages.phylo: Adding taxa and/or node ages to a phylogeny

View source: R/add_ages_phylo.R

add.ages.phyloR Documentation

Adding taxa and/or node ages to a phylogeny

Description

This function transforms a phylo object to add to it taxa ages at the nodes and/or at the terminal tips

Usage

add.ages.phylo(tree, tip.ages = NULL, node.ages = NULL, plot = TRUE)

Arguments

tree

A phylogenetic tree.

tip.ages

Optional. A numeric vector with ages of the terminal tips. Must be of same length than the number of tree tips. By default, values are considered to be in the order of the tree tips; this is bypassed if the vector is named with the tip labels.

node.ages

Optional. A numeric vector with ages of the internal nodes. By default, values are considered to be in the order of the tree nodes; this is bypassed if the tree has node labels and if this vector is named with these node labels.

plot

Optional. Turned to TRUE by default, meaning that the phylogeny is plotted at the end of the execution of the function. Turn to FALSE if not desired.

Examples

require(ape)
require(phytools)
### Define all tip and node ages
# Create an ultrametric tree
tree<-rtree(20)
tree<-tree[-which(names(tree)=="edge.length")]
class(tree)<-"phylo"
# Create random tip ages
tip.ages<-runif(Ntip(tree),0,5)
# Create random node ages, sorting them so that the more basal a node is, the oldest it is (and therefore avoid negative branch lengths)
node.ages<-sort(runif(Nnode(tree),6,10),decreasing = TRUE)
# Add tip and node ages to the tree
newtree<-add.ages.phylo(tree,tip.ages,node.ages)

### Change some tip and node ages
# Create a tree
set.seed(1)
tree<-rtree(20)
par(mfrow=c(1,3),mar=c(1,0,0,0),mgp=c(0,0,0))
plot(tree)
axisPhylo()
# Modify some tips
tip.ages<-(max(nodeHeights(tree))-nodeHeights(tree)[tree$edge[,2]<=Ntip(tree),2])
set.seed(3)
tips_to_replace<-sample(c(1:Ntip(tree)),runif(1,1,Ntip(tree)),replace=FALSE)
tip.ages[tips_to_replace]<-runif(length(tips_to_replace),min((max(nodeHeights(tree))-nodeHeights(tree))[tree$edge[,2]<=Ntip(tree),2]),min((max(nodeHeights(tree))-nodeHeights(tree))[tree$edge[,2]>Ntip(tree),2]))
newTree<-add.ages.phylo(tree,tip.ages)
tiplabels(rep("here",length(tips_to_replace)),tips_to_replace)
# Modify some nodes
node.ages<-c(max(nodeHeights(tree)),(max(nodeHeights(tree))-nodeHeights(tree)[tree$edge[,2]>Ntip(tree),2]))
node.ages[c(2,8,18)]<-c(4,2.1,1.05)
newTree<-add.ages.phylo(tree,node.ages=node.ages)
nodelabels(rep("here",3),node=c(2,8,18)+Ntip(tree))


jacobmaugoust/ULT documentation built on May 16, 2023, 1:29 p.m.