forked from YuLab-SMU/treedata-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA-app-tools.Rmd
243 lines (159 loc) · 9.13 KB
/
A-app-tools.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Related tools {#related-tools}
## MicrobiotaProcess: Convert taxonomy table to a `treedata` object {#MicrobiotaProcess-taxonomy}
Taxonomy (genus, family, ...) data are widely used in microbiome or ecology. Hierarchical taxonomies are tree-like structure that organize items into subcategories and can be converted to a tree object (see also the [phylog object](#phylog)). The `r Biocpkg("MicrobiotaProcess")` supports converting a `taxonomyTable` object, defined in the `r Biocpkg("phyloseq")` package, to a `treedata` object, and the taxonomic hierarchical relationship can be visualized using `r Biocpkg("ggtree")`. When there are taxonomy names that are [confused and missing](https://github.com/YuLab-SMU/MicrobiotaProcess/issues/14), the `as.treedata()` method for `taxonomyTable` object will complete their upper level taxonomic information automatically.
(ref:TaxaToTreescap) Convert a `taxonomyTable` object to a `treedata` object.
(ref:TaxaToTreecap) **Convert a `taxonomyTable` object to a `treedata` object.**
```{r TaxaTree, fig.widht=8, fig.height=8, fig.process=svg2png, fig.cap="(ref:TaxaToTreecap)", fig.scap="ref:TaxaToTreescap"}
library(MicrobiotaProcess)
library(ggtree)
data(kostic2012crc)
taxa <- phyloseq::tax_table(kostic2012crc)
tree <- as.treedata(taxa)
ggtree(tree, layout="circular", size=0.2) +
geom_tiplab(size=1)
```
## rtol: An R interface to Open Tree API {#rtol}
`r CRANpkg("rtol")` [@michonneau_rotl:_2016] is an R package to interact with the Open Tree of Life data APIs.
(ref:rotlSubtreescap) Get an induced subtree from the big Open Tree.
(ref:rotlSubtreecap) **Get an induced subtree from the big Open Tree.**
```{r rotlSubtree, fig.width=8, fig.height=6, fig.cap="(ref:rotlSubtreecap)", fig.scap="(ref:rotlSubtreescap)", out.width='100%'}
## example from: https://github.com/ropensci/rotl
library(rotl)
apes <- c("Pongo", "Pan", "Gorilla", "Hoolock", "Homo")
(resolved_names <- tnrs_match_names(apes))
tr <- tol_induced_subtree(ott_ids = ott_id(resolved_names))
ggtree(tr) + geom_tiplab() + xlim(NA, 5)
```
## Convert ggtree to plotly object {#plotly}
One way to make quick interactive phylogenetic tree is using `r Biocpkg("ggtree")` with the `r CRANpkg("plotly")` package. The [ggplotly()](https://plotly-r.com/improving-ggplotly.html) is able to convert `ggtree` object to a `plotly` object. Note that the `r Biocpkg("ggtree")` package also supports interactive manipulation of phylogenetic tree via the [identify()](#identify) method.
```r
# example from https://twitter.com/drandersgs/status/965996335882059776
# LOAD LIBS ---------------------------------------------------------------
library(ape)
library(ggtree)
library(plotly)
# CREATE A TREE -------------------------------------------------------------
n_samples <- 20
n_grp <- 4
tree <- ape::rtree(n = n_samples)
# CREATE SOME METADATA ----------------------------------------------------
id <- tree$tip.label
set.seed(42)
grp <- sample(LETTERS[1:n_grp], size = n_samples, replace = T)
dat <- tibble::tibble(id = id,
grp = grp)
# PLOT THE TREE -----------------------------------------------------------
p1 <- ggtree(tree)
metat <- p1$data %>%
dplyr::inner_join(dat, c('label' = 'id'))
p2 <- p1 +
geom_point(data = metat,
aes(x = x,
y = y,
colour = grp,
label = id))
plotly::ggplotly(p2)
```
(ref:ggtreeplotlyscap) Interactive phylogenetic tree by combining ggtree with plotly.
(ref:ggtreeplotlycap) **Interactive phylogenetic tree by combining ggtree with plotly.**
```{r ggtreeplotly, fig.cap="(ref:ggtreeplotlycap)", fig.scap="(ref:ggtreeplotlyscap)", echo=FALSE, out.width='100%'}
knitr::include_graphics("img/ggtree-plotly.gif")
```
## Comic (xkcd-like) phylogenetic tree {#commicR}
```{r ggsvg, fig.show='hide'}
library(htmltools)
library(XML)
library(gridSVG)
library(ggplot2)
library(ggtree)
library(comicR)
p <- ggtree(rtree(30), layout="circular") +
geom_tiplab(aes(label=label), color="purple")
print(p)
svg <- grid.export(name="", res=100)$svg
```
(ref:comicRscap) Remove image background.
(ref:comicRcap) **Remove image background.** Plotting silhouette images on phylogenetic tree without (A) and with (B) background remove.
```{r comicR}
tagList(
tags$div(
id = "ggtree_comic",
tags$style("#ggtree_comic text {font-family:Chalkduster;}"),
HTML(saveXML(svg)),
comicR("#ggtree_comic", ff=5)
)
) # %>% html_print
```
## Print ASCII-art rooted tree {#ascii-tree}
```{r asciiTree, comment=NA}
library(data.tree)
tree <- rtree(10)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
```
It is neat to print ASCII-art of phylogeny. Sometimes we don't want to plot the tree, but just take a glance at the tree structure without leaving the focus from R console. However, it is not a good idea to print the whole tree as ASCII text if the tree is large. Sometimes we just want to look at a specific portion of the tree and their immediate relatives. At this scenario, we can use `treeio::tree_subset()` function (see [session 2.4](subsetting-tree-with-data)) to extract selected portion of a tree. Then we can print ASCII-art of the tree subset to explore evolutionary relationship of interested species in R console.
`r Biocpkg("ggtree")` supports parsing tip labels as emoji to create [phylomoji](#phylomoji). With the `r CRANpkg("data.tree")` and `r CRANpkg("emojifont")` packages, we can also print phylomoji as ASCII text.
```{r asciiTreeEmoji, comment=NA}
library(emojifont)
tt <- '((snail,mushroom),(((sunflower,evergreen_tree),leaves),green_salad));'
tree <- read.tree(text = tt)
tree$tip.label <- emoji(tree$tip.label)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
```
Another way to print ASCII-art pf phylogeny is to use the `ascii()` device defined in [devout](https://github.com/coolbutuseless/devout) package. Here is an example:
```{r devout-ascii, comment=NA}
library(devout)
ascii(width=80)
ggtree(rtree(5))
invisible(dev.off())
```
## Zoom in selected portion {#facet-zoom}
(ref:facetZoomscap) Zoom in selected clade.
(ref:facetZoomcap) **Zoom in selected clade.**
```{r facetZoom, fig.width=6, fig.height=4, fig.cap="(ref:facetZoomcap)", fig.scap="(ref:facetZoomscap)"}
set.seed(2019-08-05)
x <- rtree(30)
nn <- tidytree::offspring(x, 43, self_include=TRUE)
ggtree(x) + ggforce::facet_zoom(xy = node %in% nn)
```
## Tips for using `ggtree` with `ggimage` {#ggimage-tips}
`r Biocpkg("ggtree")` supports annotating tree with silhouette images via the `r CRANpkg("ggimage")` package. `r CRANpkg("ggimage")` provides grammar of graphic syntax to work with image files. It allows processing images on the fly via the `image_fun` parameter, which accepts a function to process `magick-image` object. The `r CRANpkg("magick")` package provides several functions and these functions can be combined to perform a particular task.
### Example 1: Remove background of images {#ggimage-rm-image-bg}
(ref:ggimagebgscap) Remove image background.
(ref:ggimagebgcap) **Remove image background.** Plotting silhouette images on phylogenetic tree without (A) and with (B) background remove.
```{r ggimagebg, fig.width=8, fig.height=4, fig.cap="(ref:ggimagebgcap)", fig.scap="(ref:ggimagebgscap)", out.width='100%'}
set.seed(1982)
x <- rtree(5)
p <- ggtree(x) + theme_grey()
p1 <- p + geom_nodelab(image="img/frogs/frog.jpg", geom="image", size=.12) +
ggtitle("original image")
p2 <- p + geom_nodelab(image="img/frogs/frog.jpg", geom="image", size=.12,
image_fun= function(.) magick::image_transparent(., "white")) +
ggtitle("image with background removed")
plot_grid(p1, p2, ncol=2)
```
### Example 2: Plot tree on a background image {#ggimage-bgimage}
The `geom_bgimage` add a layer of the image and put the layer to the bottom of the layer stack. It is a normal layer and doesn't change the structure of the output `ggtree` object. Users can add annotation layers as without the background image layer.
(ref:bgimagescap) Use image file as tree background.
(ref:bgimagecap) **Use image file as tree background.**
```{r bgimage, fig.width=6, fig.height=4, fig.process=svg2png, fig.cap="(ref:bgimagecap)", fig.scap="(ref:bgimagescap)"}
ggtree(rtree(20), size=1.5, color="white") +
geom_bgimage('img/blackboard.jpg') +
geom_tiplab(color="white", size=5, family='xkcd')
```
## Run ggtree in Jupyter notebook
If you have [Jupyter notebook](https://jupyter.org/) installed on your system, you can install [IRkernel](https://irkernel.github.io/) with the following command in R:
```r
install.packages("IRkernel")
IRkernel::installspec()
```
Then you can use ggtree and other R packages in Jupyter notebook. Here is a screenshot of recreating Figure \@ref(fig:phylomoji1) in Jupyter notebook.
(ref:jupyterscap) ggtree in Jupyter notebook.
(ref:jupytercap) **ggtree in Jupyter notebook.** Running ggtree in Jupyter notebook via R kernel.
```{r jupyter, echo=F, fig.cap="(ref:jupytercap)", fig.scap="(ref:jupyterscap)", out.width='100%'}
## htmltools::includeHTML("img/ggtree_jupyter.html")
knitr::include_graphics("img/Screenshot_2019-06-24_ggtree-jupyter.png")
```