Phylogenetic tree manipulation

Silvia Castiglione, Carmela Serio, Pasquale Raia

Index

  1. tree.merger tool
    1. tree.merger basics
    2. Input tree and data
    3. Attaching individual tips to the backbone tree
    4. Attaching clades to the backbone tree
    5. Guided examples
  2. scaleTree tool
  3. cutPhylo tool
  4. fix.poly tool

tree.merger tool

tree.merger basics

The function tree.merger is meant to merge phylogenetic information derived from different phylogenies into a single supertree. Given a backbone (backbone) and a source trees (source.tree), tree.merger drops clades from the latter to attach them on the former according to the information provided in the dataset object data. Individual tips to add can be indicated in data as well. Once the supertree is assembled, tips and nodes ages are calibrated based on user-specified values.

Input tree and data

The backbone phylogeny serves as the reference to locate where single tips or entire clades extracted from the source.tree have to be attached. The backbone is assumed to be correctly calibrated so that nodes and tips ages (including the age of the tree root) are left unchanged, unless the user specifies otherwise. The source.tree is the phylogeny where the clades to add are extracted from. For each clade attached to the backbone, the time distances between the most recent common ancestor of the clade and its descendant nodes are kept fixed, unless the ages for any of these nodes are indicated by the user. All the new tips added to the backbone, irrespective of whether they are attached as a clade or as individual tips, are placed at the maximum distance from the tree root, unless calibration ages are supplied by the user. The data object is a dataframe including information about “what” is attached, where and how. data must be made of three columns:

If different column names are supplied, tree.merger assumes they are ordered as described and eventually fails if this requirement is not met. Similarly, with duplicated bind supplied the function stops and throws an error message. A clade, either to be bound or to be the reference, must be indicated by collating the names of the two phylogenetically furthest tips belonging to it, separated by a “-”. The ‘bound’ tips/clades can be used as reference for another tip/clade to be attached. The order with which clades and tips to attach are supplied does not matter. Tips and nodes are calibrated within tree.merger by means of the function scaleTree. To this aim, named vectors of tips and nodes ages, meant as time distance from the youngest tips within the phylogeny, must be supplied. As for the data object, the nodes to be calibrated should be identified by collating the names of the two phylogenetically furthest tips it subtends to, separated by a “-”.

Attaching individual tips to the backbone tree

If only individual tips are attached the source.tree can be left unspecified. Tips set to be attached to the same reference are considered to represent a polytomy. Tips set as bind which are already on the backbone tree are removed from the latter and placed according to the reference. In the example below, tips “a1” and “a8” are set to be attached to the same reference “t6”, “t5” belonging to the backbone is indicated to be moved, and “a7” is added to the tree root thus changing the total height of the tree.

dato
bind reference poly
a1 t6 FALSE
a2 t10 FALSE
a3 t9 FALSE
a4 a2-t5 FALSE
a5 t10-t2 TRUE
a6 t2-a3 FALSE
a7 a1-t10 FALSE
t5 t7-t10 FALSE
a8 t6 FALSE
tree.merger(backbone=tree.back,data=dato,plot=FALSE)
#> Warning in tree.merger(backbone = tree.back, data = dato, plot = FALSE): t5
#> removed from the backbone tree
#> Warning in tree.merger(backbone = tree.back, data = dato, plot = FALSE): Root
#> age not indicated: the tree root arbitrarily set at 3.31

As no tip.ages are supplied to tree.merger, all the new tips are placed at the maximum distance from the tree root. Since no age for the root of the merged tree is indicated, the function places it arbitrarly and produces a warning to inform the user about its position with respect to the youngest tip on the phylogeny.

To calibrate the the ages of either tips or nodes within the merged tree, the arguments tip.ages and node.ages must be indicated.

ages.tip
#>  a7  a1  t6  a8  a6  a4  a2  a5  a3 
#> 1.0 2.0 1.7 1.5 0.8 1.5 0.3 1.2 0.2
ages.node
#>  t2-t1  a1-a8 a7-t10 
#>    2.2    2.9    3.5
tree.merger(backbone=tree.back,data=dato,tip.ages=ages.tip,node.ages = ages.node,plot=FALSE)
#> Warning in tree.merger(backbone = tree.back, data = dato, tip.ages = ages.tip, :
#> t5 removed from the backbone tree

Attaching clades to the backbone tree

When clades are attached, the nodes subtending to them on source.tree are identified as the most recent common ancestors of the tip pairs indicated in bind. If one or more tips within any of the bind clades are also set to be added as individual tips, they are removed from the clade they belong to and attached independently. In the example below, “s7” is removed from the clade subtended by the most recent common ancestor of “s1” and “s4” and attached as sister to “t3” independently.
dato.clade
bind reference poly
a1 s3 FALSE
s2-s5 t10 FALSE
s1-s4 t3-t9 FALSE
s7 t3 FALSE
a2 s2-t7 FALSE
tree.merger(backbone=tree.back,data=dato.clade,source.tree=tree.source,plot=FALSE)

Guided examples

### load the RRphylo example dataset including Cetaceans tree 
data("DataCetaceans")
DataCetaceans$treecet->treecet # phylogenetic tree

### Select two clades and some species to be removed
tips(treecet,131)->liv.Mysticetes
tips(treecet,193)->Delphininae
c("Aetiocetus_weltoni","Saghacetus_osiris",
  "Zygorhiza_kochii","Ambulocetus_natans",
  "Kentriodon_pernix","Kentriodon_schneideri","Kentriodon_obscurus")->extinct

plot(treecet,show.tip.label = FALSE,no.margin=TRUE)
nodelabels(frame="n",col="blue",font=2,node=c(131,193),text=c("living\nMysticetes","Delphininae"))
tiplabels(frame="circle",bg="red",cex=.3,text=rep("",length(c(liv.Mysticetes,Delphininae,extinct))),
          tip=which(treecet$tip.label%in%c(liv.Mysticetes,Delphininae,extinct)))


### Create the backbone and source trees
drop.tip(treecet,c(liv.Mysticetes[-which(tips(treecet,131)%in%
                                           c("Caperea_marginata","Eubalaena_australis"))],
                   Delphininae[-which(tips(treecet,193)=="Tursiops_aduncus")],extinct))->backtree
drop.tip(treecet,which(!treecet$tip.label%in%
                         c(liv.Mysticetes,Delphininae,extinct)))->sourcetree

### Create the data object
data.frame(bind=c("Balaena_mysticetus-Caperea_marginata",
                  "Aetiocetus_weltoni",
                  "Saghacetus_osiris",
                  "Zygorhiza_kochii",
                  "Ambulocetus_natans",
                  "Kentriodon_pernix",
                  "Kentriodon_schneideri",
                  "Kentriodon_obscurus",
                  "Sousa_chinensis-Delphinus_delphis",
                  "Kogia_sima",
                  "Grampus_griseus"),
           reference=c("Fucaia_buelli-Aetiocetus_weltoni",
                       "Aetiocetus_cotylalveus",
                       "Fucaia_buelli-Tursiops_truncatus",
                       "Saghacetus_osiris-Fucaia_buelli",
                       "Dalanistes_ahmedi-Fucaia_buelli",
                       "Kentriodon_schneideri",
                       "Phocoena_phocoena-Delphinus_delphis",
                       "Kentriodon_schneideri",
                       "Sotalia_fluviatilis",
                       "Kogia_breviceps",
                       "Globicephala_melas-Pseudorca_crassidens"),
           poly=c(FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE,
                  FALSE))->dato
dato
bind reference poly
Balaena_mysticetus-Caperea_marginata Fucaia_buelli-Aetiocetus_weltoni FALSE
Aetiocetus_weltoni Aetiocetus_cotylalveus FALSE
Saghacetus_osiris Fucaia_buelli-Tursiops_truncatus FALSE
Zygorhiza_kochii Saghacetus_osiris-Fucaia_buelli FALSE
Ambulocetus_natans Dalanistes_ahmedi-Fucaia_buelli FALSE
Kentriodon_pernix Kentriodon_schneideri FALSE
Kentriodon_schneideri Phocoena_phocoena-Delphinus_delphis FALSE
Kentriodon_obscurus Kentriodon_schneideri FALSE
Sousa_chinensis-Delphinus_delphis Sotalia_fluviatilis FALSE
Kogia_sima Kogia_breviceps FALSE
Grampus_griseus Globicephala_melas-Pseudorca_crassidens FALSE
### Merge the backbone and the source trees according to dat without calibrating tip and node ages
tree.merger(backbone = backtree,data=dato,source.tree = sourcetree,plot=FALSE)
#> Warning in tree.merger(backbone = backtree, data = dato, source.tree =
#> sourcetree, : Kogia_sima, Grampus_griseus removed from the backbone tree
#> Warning in tree.merger(backbone = backtree, data = dato, source.tree =
#> sourcetree, : Tursiops_aduncus, Eubalaena_australis, Caperea_marginata already
#> on the source tree: removed from the backbone tree
#> Warning in tree.merger(backbone = backtree, data = dato, source.tree =
#> sourcetree, : Root age not indicated: the tree root arbitrarily set at 45.06

### Set tips and nodes calibration ages
c(Aetiocetus_weltoni=28.0,
  Saghacetus_osiris=33.9,
  Zygorhiza_kochii=34.0,
  Ambulocetus_natans=40.4,
  Kentriodon_pernix=15.9,
  Kentriodon_schneideri=11.61,
  Kentriodon_obscurus=13.65)->tipages
c("Ambulocetus_natans-Fucaia_buelli"=52.6,
  "Balaena_mysticetus-Caperea_marginata"=21.5)->nodeages

### Merge the backbone and the source trees and calibrate tips and nodes ages
tree.merger(backbone = backtree,data=dato,source.tree = sourcetree,
            tip.ages=tipages,node.ages=nodeages,plot=FALSE)
#> Warning in tree.merger(backbone = backtree, data = dato, source.tree =
#> sourcetree, : Kogia_sima, Grampus_griseus removed from the backbone tree
#> Warning in tree.merger(backbone = backtree, data = dato, source.tree =
#> sourcetree, : Tursiops_aduncus, Eubalaena_australis, Caperea_marginata already
#> on the source tree: removed from the backbone tree

scaleTree tool

The function scaleTree is a useful tool to deal with phylogenetic age calibration written around Gene Hunt’s scalePhylo function (https://naturalhistory.si.edu/staff/gene-hunt). It rescales branches and leaves of the tree according to species and/or nodes calibration ages (meant as distance from the youngest tip within the tree).

If only species ages are supplied (argument tip.ages), the function changes leaves length, leaving node ages and internal branch lengths unaltered. When node ages are supplied (argument node.ages), the function shifts nodes position along their own branches while keeping other nodes and species positions unchanged.

sp.ages
#>    t9   t73   t11   t43   t78   t46   t52   t26 
#> 1.250 1.205 0.000 2.430 3.150 1.050 0.000 1.550
scaleTree(tree,tip.ages=sp.ages)->treeS1
nod.ages
#>   98  152  123   85  118  127  164  143 
#> 10.7  0.7  1.2 12.6  5.1  5.8 18.8 12.8
scaleTree(tree,node.ages=nod.ages)->treeS2

It may happen that species and/or node ages to be calibrated are older than the age of their ancestors. In such cases, after moving the species (node) to its target age, the function reassembles the phylogeny above it by assigning the same branch length (set through the argument min.branch) to the all the branches along the species (node) path, so that the tree is well-conformed and ancestor-descendants relationships remain unchanged. In this way changes to the original tree topology only pertain to the path along the “calibrated” species.

c(sp.ages,nod.ages)
#>   t1   96 
#> 20.5 15.6
scaleTree(tree,tip.ages = sp.ages,node.ages = nod.ages,min.branch = 1)->treeS

Guided examples

# load the RRphylo example dataset including Felids tree
data("DataFelids")
DataFelids$treefel->tree

# get species and nodes ages 
# (meant as distance from the youngest species, that is the Recent in this case)
max(nodeHeights(tree))->H
H-dist.nodes(tree)[(Ntip(tree)+1),(Ntip(tree)+1):(Ntip(tree)+Nnode(tree))]->age.nodes
H-diag(vcv(tree))->age.tips

# apply Pagel's lambda transformation to change node ages only 
geiger::rescale(tree,"lambda",0.8)->tree1

# apply scaleTree to the transformed phylogeny, by setting
# the original ages at nodes as node.ages
scaleTree(tree1,node.ages=age.nodes)->treeS1

# change leaf length of 10 sampled species
tree->tree2
set.seed(14)
sample(tree2$tip.label,10)->sam.sp
age.tips[sam.sp]->age.sam
age.sam[which(age.sam>0.1)]<-age.sam[which(age.sam>0.1)]-1.5
age.sam[which(age.sam<0.1)]<-age.sam[which(age.sam<0.1)]+0.2
tree2$edge.length[match(match(sam.sp,tree$tip.label),tree$edge[,2])]<-age.sam

# apply scaleTree to the transformed phylogeny, by setting
# the original ages at sampled tips as tip.ages
scaleTree(tree2,tip.ages=age.tips[sam.sp])->treeS2

# apply Pagel's kappa transformation to change both species and node ages, 
# including the age at the tree root
geiger::rescale(tree,"kappa",0.5)->tree3

# apply scaleTree to the transformed phylogeny, by setting
# the original ages at nodes as node.ages
scaleTree(tree1,tip.ages = age.tips,node.ages=age.nodes)->treeS3

cutPhylo tool

The function cutPhylo is meant to cut the phylogentic tree to remove all the tips and nodes younger than a reference (user-specified) age, which can also coincide with a specific node. When an entire clade is cut, the user can choose (by the argument keep.lineage) to keep its branch length as a tip of the new tree, or remove it completely.

cutPhylo(tree,age=5,keep.lineage = TRUE)
cutPhylo(tree,age=5,keep.lineage = FALSE)

cutPhylo(tree,node=129,keep.lineage = TRUE)
cutPhylo(tree,node=129,keep.lineage = FALSE)

fix.poly tool

The function fix.poly randomly resolves polytomies either at specified nodes or througout the tree (Castiglione et al. 2020). This latter feature works like ape’s multi2di. However, contrary to the latter, polytomies are resolved to non-zero length branches, to provide credible partition of the evolutionary time among the nodes descending from the dichotomized node. This could be useful to gain realistic evolutionary rate estimates at applying RRphylo. Under the type = collapse specification the user is expected to indicate which node/s must be transformed into a multichotomus clade.

 ### load the RRphylo example dataset including Cetaceans tree 
 data("DataCetaceans")
 DataCetaceans$treecet->treecet

 ### Resolve all the polytomies within Cetaceans phylogeny
 fix.poly(treecet,type="resolve")->treecet.fixed
 
 ## Set branch colors
 unlist(sapply(names(which(table(treecet$edge[,1])>2)),function(x) 
   c(x,getDescendants(treecet,as.numeric(x)))))->tocolo
 unlist(sapply(names(which(table(treecet$edge[,1])>2)),function(x) 
   c(getMRCA(treecet.fixed,tips(treecet,x)),
     getDescendants(treecet.fixed,as.numeric(getMRCA(treecet.fixed,tips(treecet,x)))))))->tocolo2
 colo<-rep("gray60",nrow(treecet$edge))
 names(colo)<-treecet$edge[,2]
 colo2<-rep("gray60",nrow(treecet.fixed$edge))
 names(colo2)<-treecet.fixed$edge[,2]
 colo[match(tocolo,names(colo))]<-"red"
 colo2[match(tocolo2,names(colo2))]<-"red"
 
 par(mfrow=c(1,2))
 plot(treecet,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo,edge.width=1.3)
 plot(treecet.fixed,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo2,edge.width=1.3)


 ### Resolve the polytomies pertaining the genus Kentriodon
 fix.poly(treecet,type="resolve",node=221)->treecet.fixed2
 
 ## Set branch colors
 c(221,getDescendants(treecet,as.numeric(221)))->tocolo
 c(getMRCA(treecet.fixed2,tips(treecet,221)),
   getDescendants(treecet.fixed2,as.numeric(getMRCA(treecet.fixed2,tips(treecet,221)))))->tocolo2
 colo<-rep("gray60",nrow(treecet$edge))
 names(colo)<-treecet$edge[,2]
 colo2<-rep("gray60",nrow(treecet.fixed2$edge))
 names(colo2)<-treecet.fixed2$edge[,2]
 colo[match(tocolo,names(colo))]<-"red"
 colo2[match(tocolo2,names(colo2))]<-"red"
 
 
 par(mfrow=c(1,2))
 plot(treecet,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo,edge.width=1.3)
 plot(treecet.fixed2,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo2,edge.width=1.3)


 ### Collapse Delphinidae into a polytomous clade
 fix.poly(treecet,type="collapse",node=179)->treecet.collapsed
 
 # Set branch colors
 c(179,getDescendants(treecet,as.numeric(179)))->tocolo
 c(getMRCA(treecet.collapsed,tips(treecet,179)),
   getDescendants(treecet.collapsed,as.numeric(getMRCA(treecet.collapsed,tips(treecet,179)))))->tocolo2
 colo<-rep("gray60",nrow(treecet$edge))
 names(colo)<-treecet$edge[,2]
 colo2<-rep("gray60",nrow(treecet.collapsed$edge))
 names(colo2)<-treecet.collapsed$edge[,2]
 colo[match(tocolo,names(colo))]<-"red"
 colo2[match(tocolo2,names(colo2))]<-"red"
 
 par(mfrow=c(1,2))
 plot(treecet,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo,edge.width=1.3)
 plot(treecet.collapsed,no.margin=TRUE,show.tip.label=FALSE,edge.color = colo2,edge.width=1.3)

References

Castiglione, S., Serio, C., Piccolo, M., Mondanaro, A., Melchionna, M., Di Febbraro, M., Sansalone, G., Wroe, S.,& Raia, P. (2020). The influence of domestication, insularity and sociality on the tempo and mode of brain size evolution in mammals. Biological Journal of the Linnean Society, in press.