<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/"></div></div><div><a data-url="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/" data-counturl="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/" data-count="horizontal" data-text="align genomic features with phylogenetic tree" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2016/11/align-genomic-features-with-phylogenetic-tree"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://www.biostars.org/p/218591/">A question</a> on biostars asking how to generate the following figure:</p>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/ggtree_ggbio/218591.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This can be quite easy to implement in <a target="_blank" rel="nofollow" href="https://guangchuangyu.github.io/">ggtree</a>, I can write a <code>geom</code> layer to layout the alignment. As <a target="_blank" rel="nofollow" href="https://bioconductor.org/packages/ggbio/">ggbio</a> already provides many <code>geom</code> for genomic data and I don’t want to re-invent the wheel, I decided to try <code>ggtree+ggbio</code>. This is also the beauty of <code>R</code> that packages complete each others.</p>
<p>Here is a sample data presented in <code>ggbio</code> vignette:</p>
<pre><code>library(ggbio)
library(biovizBase) 
library(Homo.sapiens)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
txdb &lt;- TxDb.Hsapiens.UCSC.hg19.knownGene

data(genesymbol, package = &quot;biovizBase&quot;)
wh &lt;- genesymbol[c(&quot;BRCA1&quot;, &quot;NBR1&quot;)]
wh &lt;- range(wh, ignore.strand = TRUE)

gr.txdb &lt;- crunch(txdb, which = wh)
## change column to  model 
colnames(values(gr.txdb))[4] &lt;- &quot;model&quot;
grl &lt;- split(gr.txdb, gr.txdb$tx_id)
set.seed(2016-10-25)
names(grl) &lt;- sample(LETTERS, size = length(grl), replace = TRUE)
</code></pre>
<pre><code>> print(grl)
GRangesList object of length 32:
$J 
GRanges object with 7 ranges and 4 metadata columns:
      seqnames               ranges strand |    tx_id    tx_name
         &lt;Rle>            &lt;IRanges>  &lt;Rle> | &lt;factor>   &lt;factor>
  [1]    chr17 [41277600, 41277787]      + |    61241 uc002idf.3
  [2]    chr17 [41283225, 41283287]      + |    61241 uc002idf.3
  [3]    chr17 [41284973, 41285154]      + |    61241 uc002idf.3
  [4]    chr17 [41290674, 41292342]      + |    61241 uc002idf.3
  [5]    chr17 [41277788, 41283224]      * |    61241 uc002idf.3
  [6]    chr17 [41283288, 41284972]      * |    61241 uc002idf.3
  [7]    chr17 [41285155, 41290673]      * |    61241 uc002idf.3
       gene_id    model
      &lt;factor> &lt;factor>
  [1]    10230     exon
  [2]    10230     exon
  [3]    10230     exon
  [4]    10230     exon
  [5]    10230      gap
  [6]    10230      gap
  [7]    10230      gap

$U 
GRanges object with 3 ranges and 4 metadata columns:
      seqnames               ranges strand | tx_id    tx_name
  [1]    chr17 [41277600, 41277787]      + | 61242 uc010czb.2
  [2]    chr17 [41290674, 41292342]      + | 61242 uc010czb.2
  [3]    chr17 [41277788, 41290673]      * | 61242 uc010czb.2
      gene_id model
  [1]   10230  exon
  [2]   10230  exon
  [3]   10230   gap

$D 
GRanges object with 9 ranges and 4 metadata columns:
      seqnames               ranges strand | tx_id    tx_name
  [1]    chr17 [41277600, 41277787]      + | 61243 uc002idg.3
  [2]    chr17 [41283225, 41283287]      + | 61243 uc002idg.3
  [3]    chr17 [41290674, 41290939]      + | 61243 uc002idg.3
  [4]    chr17 [41291833, 41292300]      + | 61243 uc002idg.3
  [5]    chr17 [41296745, 41297125]      + | 61243 uc002idg.3
  [6]    chr17 [41277788, 41283224]      * | 61243 uc002idg.3
  [7]    chr17 [41283288, 41290673]      * | 61243 uc002idg.3
  [8]    chr17 [41290940, 41291832]      * | 61243 uc002idg.3
  [9]    chr17 [41292301, 41296744]      * | 61243 uc002idg.3
      gene_id model
  [1]   10230  exon
  [2]   10230  exon
  [3]   10230  exon
  [4]   10230  exon
  [5]   10230  exon
  [6]   10230   gap
  [7]   10230   gap
  [8]   10230   gap
  [9]   10230   gap

...
&lt;29 more elements>
-------
seqinfo: 1 sequence from hg19 genome
</code></pre>
<p>We can visualize the alignment simply using:</p>
<pre><code>ggplot() + geom_alignment(grl, alpha=.5)
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/ggtree_ggbio/ggbio.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The input data for <code>geom_alignment</code> is a <code>GRangesList</code> object, while <a target="_blank" rel="nofollow" href="https://guangchuangyu.github.io/2016/10/facet_plot-a-general-solution-to-associate-data-with-phylogenetic-tree/">facet_plot</a> defined in <code>ggtree</code> expect the input data as a <code>data.frame</code>. I extend the <code>facet_plot</code> to work with <code>geom_alignment</code>. In doing this, I find a bug of the <code>geom_alignment</code> function and send a <a target="_blank" rel="nofollow" href="https://github.com/tengfei/ggbio/issues/83#issuecomment-256284645">patch</a> to Michael. My patch was incorporated in <code>ggbio 1.23.2</code>.</p>
<p>With the updates of both <code>ggtree</code> and <code>ggbio</code>, we can use <code>facet_plot</code> to align alignment with phylogenetic tree.</p>
<p>Suppose we have the following tree:</p>
<pre><code>library(ggtree)
n &lt;- names(grl) %>% unique %>% length
set.seed(2016-10-25)
tr = rtree(n)
set.seed(2016-10-25)
tr$tip.label = sample(unique(names(grl)), n)

p &lt;- ggtree(tr) + geom_tiplab()
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/ggtree_ggbio/random_tree.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It is quite easy to use <code>facet_plot</code> to visualize the above alignment with this tree:</p>
<pre><code>facet_plot(p, 'alignment', grl, geom_alignment, inherit.aes=FALSE, mapping=aes())
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/Bioconductor/ggtree/ggtree_ggbio/ggtree_ggbio.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Beware of <code>mapping=aes()</code> is required as <code>ggbio</code> can’t accept <code>mapping=NULL</code>.</p>
<p>PS: I only test <code>geom_alignment</code> which works with <code>GRanges</code> and <code>GRangesList</code>. Other <code>geoms</code> defined in <code>ggbio</code> may not be supported. If you find <code>facet_plot</code> fail to work with other <code>geoms</code>, please open an <a target="_blank" rel="nofollow" href="https://github.com/GuangchuangYu/ggtree/issues">issue</a> in github. Feature requests are welcome.</p>
<h2>Citation</h2>
<p><strong>G Yu</strong>, DK Smith, H Zhu, Y Guan, TTY Lam<sup>*</sup>. <a target="_blank" rel="nofollow" href="http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12628/abstract">ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data</a>. <strong><em>Methods in Ecology and Evolution</em></strong>. <a target="_blank" rel="nofollow" href="http://dx.doi.org/10.1111/2041-210X.12628"><code>doi:10.1111/2041-210X.12628</code></a>.</p>
<p></p>
<div data-badge-popover="right" data-doi="10.1111/2041-210X.12628" data-badge-type="donut"></div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/"></div></div><div><a data-url="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/" data-counturl="https://www.r-bloggers.com/align-genomic-features-with-phylogenetic-tree/" data-count="vertical" data-text="align genomic features with phylogenetic tree" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://guangchuangyu.github.io/2016/11/align-genomic-features-with-phylogenetic-tree"> R on Guangchuang YU</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/JNJQciwzovg"></a></div>