<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/phylogram-dendrograms-for-evolutionary-analysis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2018/07/12/phylogram/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Evolutionary biologists are increasingly using R for building,<br>
editing and visualizing phylogenetic trees.<br>
The reproducible code-based workflow and comprehensive array of tools<br>
available in packages such as <a rel="nofollow" href="http://ape-package.ird.fr/" target="_blank">ape</a>,<br>
<a rel="nofollow" href="https://github.com/KlausVigo/phangorn" target="_blank">phangorn</a> and<br>
<a rel="nofollow" href="http://blog.phytools.org/" target="_blank">phytools</a> make R an ideal platform for<br>
phylogenetic analysis.<br>
Yet the many different tree formats are not well integrated,<br>
as pointed out in a recent<br>
<a rel="nofollow" href="https://ropensci.org/blog/2018/05/17/treeio/" target="_blank">post</a>.</p>
<p>The standard data structure for phylogenies in R is the “phylo”<br>
object, a memory efficient, matrix-based tree representation.<br>
However, non-biologists have tended to use a tree structure<br>
called the “dendrogram”, which is a deeply nested list with<br>
node properties defined by various attributes stored at each level.<br>
While certainly not as memory efficient as the matrix-based format,<br>
dendrograms are versatile and intuitive to manipulate, and hence<br>
a large number of analytical and visualization functions exist<br>
for this object type. A good example is the<br>
<a rel="nofollow" href="https://github.com/talgalili/dendextend" target="_blank">dendextend</a> package,<br>
which features an impressive range of options for editing dendrograms<br>
and plotting publication-quality trees.</p>
<p>To better integrate the phylo and dendrogram object types,<br>
and hence increase the options available for both camps,<br>
we developed the <a rel="nofollow" href="https://github.com/ropensci/phylogram" target="_blank">phylogram</a><br>
package, which is now a part of the <a rel="nofollow" href="https://ropensci.org/" target="_blank">rOpenSci</a><br>
project.<br>
This small package features a handful of functions for tree conversion,<br>
importing and exporting trees as parenthetic text, and manipulating<br>
dendrograms for phylogenetic applications.<br>
The <code>phylogram</code> package draws heavily on <a rel="nofollow" href="http://ape-package.ird.fr/" target="_blank">ape</a>,<br>
but currently has no other non-standard dependencies.</p>

<h3>Installation</h3>
<p>To download <code>phylogram</code> from CRAN and load the package, run</p>
<pre><code>install.packages(&quot;phylogram&quot;)
library(phylogram)
</code></pre>
<p>Alternatively, to download the latest development version from <a rel="nofollow" href="https://github.com/ropensci/phylogram" target="_blank">GitHub</a>,<br>
first ensure that the <a rel="nofollow" href="https://github.com/r-lib/devtools" target="_blank">devtools</a>,<br>
<a rel="nofollow" href="https://cran.r-project.org/package=kmer" target="_blank">kmer</a>, and<br>
<a rel="nofollow" href="https://cran.r-project.org/package=dendextend" target="_blank">dendextend</a><br>
packages are installed,<br>
then run:</p>
<pre><code>devtools::install_github(&quot;ropensci/phylogram&quot;, build_vignettes = TRUE) 
library(phylogram)
</code></pre>

<h3>Tree import/export</h3>
<p>A wide variety of tree formats can be parsed as phylo objects using either the<br>
well-optimized <code>ape::read.tree</code> function<br>
(for <a rel="nofollow" href="http://evolution.genetics.washington.edu/phylip/newicktree.html" target="_blank">Newick</a><br>
strings),<br>
or the suite of specialized functions in the versatile<br>
<a rel="nofollow" href="https://ropensci.org/blog/2018/05/17/treeio/" target="_blank">treeio</a> package.<br>
To convert a phylo object to a dendrogram, the <code>phylogram</code> package includes<br>
the function <code>as.dendrogram</code>, which retains node height attributes and can handle<br>
non-ultrametric trees.</p>
<p>For single-line parsing of dendrograms from Newick text,<br>
the <code>read.dendrogram</code> function wraps <code>ape::read.tree</code><br>
and converts the resulting phylo class object to a dendrogram using <code>as.dendrogram</code>.</p>
<p>Similarly, the functions <code>write.dendrogram</code> and <code>as.phylo</code> are used to<br>
export dendrogram objects to parenthetic text and phylo objects, respectively.</p>

<h3>Tree editing</h3>
<p>The <code>phylogram</code> package includes some new functions for manipulating<br>
trees in dendrogram format.<br>
Leaf nodes and internal branching nodes can be removed<br>
using the function <code>prune</code>, which identifies and<br>
recursively deletes nodes based on pattern<br>
matching of “label” attributes.<br>
This is slower than <code>ape::drop.tip</code>, but offers<br>
the benefits of versatile string matching using regular expressions,<br>
and the ability to remove inner nodes (and by extension all subnodes)<br>
that feature matching “label” attributes.<br>
To aid visualization, the function <code>ladder</code> rearranges<br>
the tree, sorting nodes by the number of members<br>
(analogous to <code>ape::ladderize</code>).</p>
<p>For more controlled subsetting or when creating trees from scratch<br>
(e.g. from a standard nested list), the function <code>remidpoint</code><br>
recursively corrects all “midpoint”, “members” and “leaf” attributes.<br>
Node heights can then be manipulated using either <code>reposition</code>, which<br>
scales the heights of all nodes in a tree by a given constant, or<br>
<code>as.cladogram</code>, which resets the “height” attributes of all terminal<br>
leaf nodes to zero and progressively resets the heights of the inner nodes<br>
in single incremental units.</p>
<p>As an example, a simple three-leaf dendrogram can be created from<br>
a nested list as follows:</p>
<pre><code>x &lt;- list(1, list(2, 3))
## set class, midpoint, members and leaf attributes for each node
x &lt;- remidpoint(x)
## set height attributes for each node
x &lt;- as.cladogram(x)
</code></pre>
<p>A nice feature of the dendrogram object type is that tree<br>
editing operations can be carried out recursively<br>
using fast inbuilt functions in the “apply” family such as <code>dendrapply</code><br>
and <code>lapply</code>.</p>
<p>For example, to label each leaf node of the tree alphabetically we can<br>
create a simple labeling function and apply it to the tree nodes recursively using<br>
<code>dendrapply</code>.</p>
<pre><code>set_label &lt;- function(node){
  if(is.leaf(node)) attr(node, &quot;label&quot;) &lt;- LETTERS[node]
  return(node)
}
x &lt;- dendrapply(x, set_label)
plot(x, horiz = TRUE)
</code></pre>
<p><img src="https://i2.wp.com/ropensci.org/img/blog-images/2018-07-12-phylogram/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>Applications</h3>
<p>One application motivating bi-directional conversion between phylo and<br>
dendrogram objects involves creating publication-quality ‘tanglegrams’ using<br>
the <a rel="nofollow" href="https://github.com/talgalili/dendextend" target="_blank">dendextend</a> package.<br>
For example, to see how well the fast, alignment-free <em>k</em>-mer distance<br>
from the <a rel="nofollow" href="https://cran.r-project.org/package=kmer" target="_blank">kmer</a> package<br>
performs in comparison to the standard Kimura 1980 distance measure,<br>
we can create neighbor-joining trees using each method and plot them side by side<br>
to check for incongruent nodes.</p>
<pre><code>## load woodmouse data and remove columns with ambiguities
data(woodmouse, package = &quot;ape&quot;)
woodmouse &lt;- woodmouse[, apply(woodmouse, 2, function(v) !any(v == 0xf0))]
## compute Kimura 1980 pairwise distance matrix
dist1 &lt;- ape::dist.dna(woodmouse, model = &quot;K80&quot;)
## deconstruct alignment (not strictly necessary)
woodmouse &lt;- as.list(as.data.frame(unclass(t(woodmouse))))
## compute kmer distance matrix 
dist2 &lt;- kmer::kdistance(woodmouse, k = 7) 
## build and ladderize neighbor-joining trees
phy1 &lt;- ape::nj(dist1)
phy2 &lt;- ape::nj(dist2)
phy1 &lt;- ape::ladderize(phy1)
phy2 &lt;- ape::ladderize(phy2)
## convert phylo objects to dendrograms
dnd1 &lt;- as.dendrogram(phy1)
dnd2 &lt;- as.dendrogram(phy2)
## plot the tanglegram
dndlist &lt;- dendextend::dendlist(dnd1, dnd2)
dendextend::tanglegram(dndlist, fast = TRUE, margin_inner = 5)
</code></pre>
<p><img src="https://i1.wp.com/ropensci.org/img/blog-images/2018-07-12-phylogram/unnamed-chunk-7-1.png?w=456&ssl=1" data-recalc-dims="1"></p>

<p>In this case, the trees are congruent and branch lengths are similar.<br>
However, if we reduce the <em>k</em>-mer size from 7 to 6,<br>
the accuracy of the tree reconstruction is affected, as shown by the<br>
incongruence between the original K80 tree (left) and the tree derived<br>
from the 6-mer distance matrix (right):</p>
<pre><code>## compute kmer distance matrix 
dist3 &lt;- kmer::kdistance(woodmouse, k = 6) 
phy3 &lt;- ape::nj(dist3)
phy3 &lt;- ape::ladderize(phy3)
dnd3 &lt;- as.dendrogram(phy3)
dndlist &lt;- dendextend::dendlist(dnd1, dnd3)
dendextend::tanglegram(dndlist, fast = TRUE, margin_inner = 5)
</code></pre>
<p><img src="https://i1.wp.com/ropensci.org/img/blog-images/2018-07-12-phylogram/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>

<p>Hopefully users will find the package useful for a range of other applications.<br>
Bug reports and other suggestions are welcomed, and can be directed to the<br>
<a rel="nofollow" href="http://github.com/ropensci/phylogram/issues" target="_blank">GitHub issues page</a><br>
or the <a rel="nofollow" href="https://groups.google.com/group/phylogram" target="_blank">phylogram google group</a>.<br>
Thanks to <a rel="nofollow" href="https://github.com/wcornwell" target="_blank">Will Cornwell</a> and<br>
<a rel="nofollow" href="https://github.com/BenJWard" target="_blank">Ben J. Ward</a><br>
for reviewing the code and suggesting improvements,<br>
and to <a rel="nofollow" href="https://github.com/sckott" target="_blank">Scott Chamberlain</a><br>
for handling the <a rel="nofollow" href="https://github.com/ropensci" target="_blank">rOpenSci</a><br>
onboarding process.</p>
<p>The <code>phylogram</code> package is available for download from<br>
<a rel="nofollow" href="https://github.com/ropensci/phylogram" target="_blank">GitHub</a> and<br>
<a rel="nofollow" href="https://cran.r-project.org/package=phylogram" target="_blank">CRAN</a>,<br>
and a summary of the package is published in the<br>
<a rel="nofollow" href="https://joss.theoj.org/papers/6773f7e7465b5068fc5654018c1a33b8" target="_blank">Journal of Open Source Software</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/technotes/2018/07/12/phylogram/"> rOpenSci - open tools for open science</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/goJCzjsfhWw" height="1"></a></div>