# treespace worked example: Dengue trees

#### 2018-05-14

This vignette demonstrates the use of treespace to compare a collection of trees. For this example we use trees inferred from 17 dengue virus serotype 4 sequences from Lanciotti et al. (1997). We include a sample of trees from BEAST (v1.8), as well as creating neighbour-joining (NJ) and maximum-likelihood (ML) trees.

library("treespace")
library("phangorn")
library("adegenet")

data(DengueTrees)

We load a random sample of 500 of the trees (from the second half of the posterior) produced using BEAST v1.8 with xml file 4 from Drummond and Rambaut (2007). It uses the standard GTR + Gamma + I substitution model with uncorrelated lognormal-distributed relaxed molecular clock. Each tree has 17 tips.

For convenience in our initial analysis we will take a random sample of 200 of these trees; sample sizes can be increased later.

set.seed(123)
BEASTtrees <- DengueTrees[sample(1:length(DengueTrees),200)]

data(DengueSeqs)

## Creating neighbour-joining and maximum likelihood trees:

Create a neighbour-joining (NJ) tree using the Tamura and Nei (1993) model (see ?dist.dna for more information) and root it on the outgroup "D4Thai63":

makeTree <- function(x){
tree <- nj(dist.dna(x, model = "TN93"))
tree <- root(tree, resolve.root=TRUE, outgroup="D4Thai63")
tree
}
DnjRooted <- makeTree(DengueSeqs)
# Note, there is a (small) negative branch length.
# We set this to 0 to avoid warnings from the phangorn package later:
DnjRooted$edge.length[which(DnjRooted$edge.length < 0)] <- 0
plot(DnjRooted)

We use boot.phylo to bootstrap the tree:

Dnjboots <- boot.phylo(DnjRooted, DengueSeqs, B=100,
makeTree, trees=TRUE, rooted=TRUE)
Dnjboots

and we can plot the tree again, annotating it with the bootstrap clade support values:

plot(DnjRooted)
drawSupportOnEdges(Dnjboots$BP) We create a maximum-likelihood (ML) tree and root it as before: Dfit.ini <- pml(DnjRooted, as.phyDat(DengueSeqs), k=4) Dfit <- optim.pml(Dfit.ini, optNni=TRUE, optBf=TRUE, optQ=TRUE, optGamma=TRUE, model="GTR") ## Warning: I unrooted the tree # root: DfitTreeRooted <- root(Dfit$tree, resolve.root=TRUE, outgroup="D4Thai63")

View the ML tree:

plot(DfitTreeRooted)

Create bootstrap trees:

# bootstrap supports:
DMLboots <- bootstrap.pml(Dfit, optNni=TRUE)
# root:
DMLbootsrooted <- lapply(DMLboots, function(x) root(x, resolve.root=TRUE, outgroup="D4Thai63"))
class(DMLbootsrooted) <- "multiPhylo"

Plot the ML tree again, with bootstrap support values:

plotBS(DfitTreeRooted, DMLboots, type="phylogram")

## Using treespace to compare trees

We now use the function treespace to find and plot distances between all these trees:

# collect the trees into a single object of class multiPhylo:
DengueTrees <- c(BEASTtrees, Dnjboots$trees, DMLbootsrooted, DnjRooted, DfitTreeRooted) class(DengueTrees) <- "multiPhylo" # add tree names: names(DengueTrees)[1:200] <- paste0("BEAST",1:200) names(DengueTrees)[201:300] <- paste0("NJ_boots",1:100) names(DengueTrees)[301:400] <- paste0("ML_boots",1:100) names(DengueTrees)[[401]] <- "NJ" names(DengueTrees)[[402]] <- "ML" # create vector corresponding to tree inference method: Dtype <- c(rep("BEAST",200),rep("NJboots",100),rep("MLboots",100),"NJ","ML") # use treespace to find and project the distances: Dscape <- treespace(DengueTrees, nf=5) # simple plot: plotGrovesD3(Dscape$pco, groups=Dtype)

The function plotGrovesD3 produces interactive d3 plots which enable zooming, moving, tooltip text and legend hovering. We now refine the plot with colour-blind friendly colours (selected using ColorBrewer2), bigger points, varying symbols and point opacity to demonstrate the NJ and ML trees, informative legend title and smaller legend width:

Dcols <- c("#1b9e77","#d95f02","#7570b3")
Dmethod <- c(rep("BEAST",200),rep("NJ",100),rep("ML",100),"NJ","ML")
Dbootstraps <- c(rep("replicates",400),"NJ","ML")
Dhighlight <- c(rep(1,400),2,2)
plotGrovesD3(Dscape$pco, groups=Dmethod, colors=Dcols, col_lab="Tree type", size_var=Dhighlight, size_range = c(100,500), size_lab="", symbol_var=Dbootstraps, symbol_lab="", point_opacity=c(rep(0.4,400),1,1), legend_width=80) We can also add tree labels to the plot. Where these overlap, the user can use “drag and drop” to move them around for better visibility. plotGrovesD3(Dscape$pco,
groups=Dmethod,
treeNames = names(DengueTrees), # add the tree names as labels
colors=Dcols,
col_lab="Tree type",
size_var=Dhighlight,
size_range = c(100,500),
size_lab="",
symbol_var=Dbootstraps,
symbol_lab="",
point_opacity=c(rep(0.4,400),1,1),
legend_width=80)

Alternatively, where labels are too cluttered, it may be preferable not to plot them but to make the tree names available as tooltip text instead:

plotGrovesD3(Dscape$pco, groups=Dmethod, tooltip_text = names(DengueTrees), # add the tree names as tooltip text colors=Dcols, col_lab="Tree type", size_var=Dhighlight, size_range = c(100,500), size_lab="", symbol_var=Dbootstraps, symbol_lab="", point_opacity=c(rep(0.4,400),1,1), legend_width=80) The scree plot is available as part of the treespace output: barplot(Dscape$pco$eig, col="navy") We can also view the plot in 3D: library(rgl) Dcols3D <- c(rep(Dcols[[1]],200),rep(Dcols[[2]],100),rep(Dcols[[3]],100),Dcols[[2]],Dcols[[3]]) rgl::plot3d(Dscape$pco$li[,1],Dscape$pco$li[,2],Dscape$pco$li[,3], type="s", size=c(rep(1.5,400),3,3), col=Dcols3D, xlab="", ylab="", zlab="") ## treespace analysis From these plots we can see that treespace has identified variation in the trees according to the Kendall Colijn metric ($$\lambda=0$$, ignoring branch lengths). The NJ and ML bootstrap trees have broadly similar topologies but are different from any of the BEAST trees. We can check whether any bootstrap trees have the same topology as either the NJ or ML tree, as follows: # trees with the same topology as the NJ tree: which(as.matrix(Dscape$D)["NJ",]==0)
## NJ_boots91         NJ
##        291        401
# trees with the same topology as the ML tree:
which(as.matrix(Dscape\$D)["ML",]==0)
##  ML_boots1  ML_boots6 ML_boots26 ML_boots40 ML_boots43 ML_boots48
##        301        306        326        340        343        348
## ML_boots51 ML_boots62 ML_boots65 ML_boots69 ML_boots70 ML_boots76
##        351        362        365        369        370        376
## ML_boots79         ML
##        379        402

This shows that the NJ tree has the same topology as one NJ bootstrap tree and one ML bootstrap tree. The ML tree has the same topology as 15 ML bootstrap trees, but no NJ bootstrap trees.

We can compare pairs of trees using the plotTreeDiff function to see exactly where their differences arise. Tips with identical ancestry in the two trees are coloured grey, whereas tips with differing ancestry are coloured peach-red, with the colour darkening according to the number of ancestral differences found at each tip. Since we are comparing the trees topologically (ignoring branch lengths, for the moment), we plot with constant branch lengths for clarity.

# comparing NJ and ML:
plotTreeDiff(DnjRooted,DfitTreeRooted, use.edge.length=FALSE)