Evolutionary biologists are increasingly using R for building,
editing and visualizing phylogenetic trees.
The reproducible code-based workflow and comprehensive array of tools
available in packages such as ape,
phangorn and
phytools make R an ideal platform for
phylogenetic analysis.
Yet the many different tree formats are not well integrated,
as pointed out in a recent
post.
The standard data structure for phylogenies in R is the “phylo”
object, a memory efficient, matrix-based tree representation.
However, non-biologists have tended to use a tree structure
called the “dendrogram”, which is a deeply nested list with
node properties defined by various attributes stored at each level.
While certainly not as memory efficient as the matrix-based format,
dendrograms are versatile and intuitive to manipulate, and hence
a large number of analytical and visualization functions exist
for this object type. A good example is the
dendextend package,
which features an impressive range of options for editing dendrograms
and plotting publication-quality trees.
To better integrate the phylo and dendrogram object types,
and hence increase the options available for both camps,
we developed the phylogram
package, which is now a part of the rOpenSci
project.
This small package features a handful of functions for tree conversion,
importing and exporting trees as parenthetic text, and manipulating
dendrograms for phylogenetic applications.
The phylogram package draws heavily on ape,
but currently has no other non-standard dependencies.
Installation
To download phylogram from CRAN and load the package, run
install.packages(&quot;phylogram&quot;)
library(phylogram)
Alternatively, to download the latest development version from GitHub,
first ensure that the devtools,
kmer, and
dendextend
packages are installed,
then run:
devtools::install_github(&quot;ropensci/phylogram&quot;, build_vignettes = TRUE) library(phylogram)
Tree import/export
A wide variety of tree formats can be parsed as phylo objects using either the
well-optimized ape::read.tree function
(for Newick
strings),
or the suite of specialized functions in the versatile
treeio package.
To convert a phylo object to a dendrogram, the phylogram package includes
the function as.dendrogram, which retains node height attributes and can handle
non-ultrametric trees.
For single-line parsing of dendrograms from Newick text,
the read.dendrogram function wraps ape::read.tree
and converts the resulting phylo class object to a dendrogram using as.dendrogram.
Similarly, the functions write.dendrogram and as.phylo are used to
export dendrogram objects to parenthetic text and phylo objects, respectively.
Tree editing
The phylogram package includes some new functions for manipulating
trees in dendrogram format.
Leaf nodes and internal branching nodes can be removed
using the function prune, which identifies and
recursively deletes nodes based on pattern
matching of “label” attributes.
This is slower than ape::drop.tip, but offers
the benefits of versatile string matching using regular expressions,
and the ability to remove inner nodes (and by extension all subnodes)
that feature matching “label” attributes.
To aid visualization, the function ladder rearranges
the tree, sorting nodes by the number of members
(analogous to ape::ladderize).
For more controlled subsetting or when creating trees from scratch
(e.g. from a standard nested list), the function remidpoint
recursively corrects all “midpoint”, “members” and “leaf” attributes.
Node heights can then be manipulated using either reposition, which
scales the heights of all nodes in a tree by a given constant, or
as.cladogram, which resets the “height” attributes of all terminal
leaf nodes to zero and progressively resets the heights of the inner nodes
in single incremental units.
As an example, a simple three-leaf dendrogram can be created from
a nested list as follows:
x