This document should contain all you need to get started measuring tree distances with 'TreeDist'. If you get stuck, please let me know so I can improve this documentation.
Instructions for loading phylogenetic trees into R can be found in a separate vignette. For these examples, we'll enter two simple trees by hand:
tree1 <- ape::read.tree(text = '(A, ((B, (C, (D, E))), ((F, G), (H, I))));') tree2 <- ape::read.tree(text = '(A, ((B, (C, (D, (H, I)))), ((F, G), E)));')
We can calculate distances between pairs of trees using the 'TreeDist' package.
First we'll install the package. We can either install the stable version from the CRAN repository:
install.packages('TreeDist')
or the development version, from GitHub -- which will contain the latest features but may not be as extensively tested:
devtools::install_github('ms609/TreeDist')
Then we'll load the package in to R's working environment:
library('TreeDist')
Now the package's functions are available within R. Let's proceed to calculate some tree distances.
Calculating the distance between two trees is as simple as:
distance <- TreeDistance(tree1, tree2)
The convenience function TreeDistance()
returns the variation of clustering
information between two trees, normalized
against the total information content of all splits.
If you have more than two trees to compare, you can send a list of trees
(class: list
or multiPhylo
) to the distance comparison function.
The function will then calculate the distance between each tree in the first
list and each tree in the second.
oneTree <- ape::rtree(11) twoTrees <- structure(list(one = ape::rtree(11), two = ape::rtree(11)), class = 'multiPhylo') threeTrees <- list(a = ape::rtree(11), b = ape::rtree(11), c = ape::rtree(11)) TreeDistance(oneTree, twoTrees) TreeDistance(twoTrees, threeTrees)
Generalized Robinson–Foulds metrics,
such as the variation of clustering information,
rely on matching each split within a tree with another split in the other tree.
We can view an optimal matching:
VisualizeMatching(ClusteringInfoDistance, tree1, tree2)
This shows the six splits in tree 1, and the paired splits in tree two.
Each split is labelled with a measure of its similarity, which is its
contribution to the total tree similarity score.
We can view this information in a format accessible for further examination in R with:
ClusteringInfoDistance(tree1, tree2, reportMatching = TRUE)
Here, the pairScores
attribute lists the score of each possible matching of
splits.
We can identify the splits with:
splits <- as.character(TreeTools::as.Splits(tree2)) splits
The names of the splits correspond to the number of an associated node in the original tree:
oldPar <- par(mar = rep(0, 4)) plot(tree2) ape::nodelabels() ape::nodelabels(splits, as.integer(names(splits)), adj = c(1.1, -0.2), cex = 0.8, frame = 'none')
par(oldPar)
Note that strictly, (informative) splits are associated with (internal) edges. To avoid listing the same split twice, nodes close to the root (here, 10 and 11) will not be associated with a split.
You may wish to:
Provide context for tree distances
Review available distance measures and the corresponding functions
Visualize tree landscapes using distance-based tree spaces
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.