Browse code

update ggtree with new features according to ggplot2 v2

git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@111988 bc3139a8-67e5-0310-9ffc-ced21a209358

Guangchuang Yu authored on 29/12/2015 11:53:44
Showing 33 changed files

... ...
@@ -1,20 +1,19 @@
1 1
 Package: ggtree
2 2
 Type: Package
3 3
 Title: a phylogenetic tree viewer for different types of tree annotations
4
-Version: 1.3.6
4
+Version: 1.3.7
5 5
 Author: Guangchuang Yu and Tommy Tsan-Yuk Lam
6 6
 Maintainer: Guangchuang Yu <guangchuangyu@gmail.com>
7 7
 Description: ggtree extends the ggplot2 plotting system which implemented the
8 8
     grammar of graphics. ggtree is designed for visualizing phylogenetic tree
9 9
     and different types of associated annotation data.
10 10
 Depends:
11
-    R (>= 3.1.0),
11
+    R (>= 3.2.0),
12 12
     ggplot2 (>= 2.0.0)
13 13
 Imports:
14 14
     ape,
15 15
     Biostrings,
16 16
     colorspace,
17
-    ggplot2,
18 17
     grid,
19 18
     gridExtra,
20 19
     jsonlite,
... ...
@@ -30,6 +29,7 @@ Suggests:
30 29
     phytools,
31 30
     phangorn,
32 31
     rmarkdown,
32
+    scales,
33 33
     testthat
34 34
 VignetteBuilder: knitr
35 35
 License: Artistic-2.0
... ...
@@ -24,7 +24,6 @@ export(Date2decimal)
24 24
 export(MRCA)
25 25
 export(StatHilight)
26 26
 export(add_colorbar)
27
-export(aes)
28 27
 export(annotation_image)
29 28
 export(apeBoot)
30 29
 export(as.binary)
... ...
@@ -61,7 +60,6 @@ export(getRoot)
61 60
 export(get_clade_position)
62 61
 export(get_heatmap_column_position)
63 62
 export(get_taxa_name)
64
-export(ggplotGrob)
65 63
 export(ggtree)
66 64
 export(gheatmap)
67 65
 export(gplot)
... ...
@@ -1,3 +1,12 @@
1
+CHANGES IN VERSION 1.3.7
2
+------------------------
3
+ o split the long vignette to several vignettes
4
+   + 00 ggtree <2015-12-29, Tue>
5
+   + 01 tree data import <2015-12-28, Mon>
6
+   + 02 tree visualization <2015-12-28, Mon>
7
+   + 03 tree view manipulation <2015-12-28, Mon>
8
+   + 04 tree annotation <2015-12-29, Tue>
9
+ 
1 10
 CHANGES IN VERSION 1.3.6
2 11
 ------------------------
3 12
  o MRCA function for finding Most Recent Common Ancestor among a vector of tips <2015-12-22, Tue>
... ...
@@ -11,6 +11,7 @@
11 11
 ##' @param fontsize size of text
12 12
 ##' @param angle angle of text
13 13
 ##' @param geom one of 'text' or 'label'
14
+##' @param hjust hjust
14 15
 ##' @param fill fill label background, only work with geom='label'
15 16
 ##' @param ... additional parameter
16 17
 ##' @return ggplot layers
... ...
@@ -13,12 +13,12 @@
13 13
 geom_hilight <- function(node, fill="steelblue", alpha=.5) {
14 14
                          
15 15
     
16
-    data=NULL
17
-    stat="hilight"
18
-    position="identity"
19
-    show.legend=NA
20
-    na.rm=FALSE
21
-    inherit.aes=FALSE
16
+    data = NULL
17
+    stat = "hilight"
18
+    position = "identity"
19
+    show.legend = NA
20
+    na.rm = FALSE
21
+    inherit.aes = FALSE
22 22
     
23 23
     default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch.length=~branch.length)
24 24
     mapping <- default_aes
... ...
@@ -33,15 +33,27 @@ geom_hilight <- function(node, fill="steelblue", alpha=.5) {
33 33
         show.legend=show.legend,
34 34
         inherit.aes = inherit.aes,
35 35
         params = list(node=node,
36
-            fill=fill, alpha=alpha,
37
-            na.rm = na.rm)
36
+                      fill=fill, alpha=alpha,
37
+                      na.rm = na.rm)
38 38
     )
39 39
     
40 40
 }
41 41
 
42 42
 ##' stat_hilight
43
-##' @rdname geom_hilight
43
+##'
44
+##'
45
+##' @title stat_hilight
46
+##' @param mapping aes mapping
47
+##' @param data data
44 48
 ##' @param geom geometric object
49
+##' @param position position
50
+##' @param node node number
51
+##' @param show.legend show legend
52
+##' @param inherit.aes logical
53
+##' @param fill fill color
54
+##' @param alpha transparency
55
+##' @param ... additional parameter
56
+##' @return layer
45 57
 ##' @importFrom ggplot2 layer
46 58
 ##' @export
47 59
 stat_hilight <- function(mapping=NULL, data=NULL, geom="rect",
... ...
@@ -4,7 +4,6 @@
4 4
 ##' @title ggtree
5 5
 ##' @param tr phylo object
6 6
 ##' @param mapping aes mapping
7
-##' @param showDistance add distance legend, logical
8 7
 ##' @param layout one of 'rectangular', 'slanted', 'fan'/'circular', 'radial' or 'unrooted'
9 8
 ##' @param mrsd most recent sampling date
10 9
 ##' @param as.Date logical whether using Date class in time tree
... ...
@@ -461,15 +460,6 @@ add_colorbar <- function(p, color, x=NULL, ymin=NULL, ymax=NULL, font.size=4) {
461 460
 }
462 461
 
463 462
 
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473 463
 ##' get taxa name of a selected node
474 464
 ##'
475 465
 ##' 
476 466
deleted file mode 100644
... ...
@@ -1,43 +0,0 @@
1
-##' @rdname groupOTU-methods
2
-##' @exportMethod groupOTU
3
-setMethod("groupOTU", signature(object="phylo"),
4
-          function(object, focus, group_name="group") {
5
-              groupOTU.phylo(object, focus, group_name)
6
-          })
7
-
8
-##' @rdname groupOTU-methods
9
-##' @exportMethod groupOTU
10
-##' @param tree which tree selected
11
-setMethod("groupOTU", signature(object="r8s"),
12
-          function(object, focus, group_name="group", tree="TREE") {
13
-              groupOTU_(get.tree(object)[[tree]], focus, group_name)
14
-          }
15
-          )
16
-
17
-
18
-##' @rdname groupOTU-methods
19
-##' @exportMethod groupOTU
20
-setMethod("groupOTU", signature(object="codeml"),
21
-          function(object, focus, group_name="group") {
22
-              groupOTU_(object, focus, group_name)
23
-          }
24
-          )
25
-
26
-
27
-##' @rdname groupOTU-methods
28
-##' @exportMethod groupOTU
29
-setMethod("groupOTU", signature(object="codeml_mlc"),
30
-          function(object, focus, group_name="group") {
31
-              groupOTU_(object, focus, group_name)
32
-          }
33
-          )
34
-
35
-
36
-##' @rdname groupOTU-methods
37
-##' @exportMethod groupOTU
38
-setMethod("groupOTU", signature(object="paml_rst"),
39
-          function(object, focus, group_name="group") {
40
-              groupOTU_(object, focus, group_name)
41
-          }
42
-          )
43
-
... ...
@@ -103,7 +103,7 @@ merge_tree <- function(obj1, obj2) {
103 103
     info2$parent <- node_map.df[info2$parent, 2]
104 104
 
105 105
     cn <- colnames(info2)
106
-    i <- match(c("x", "y", "isTip", "label", "branch", "branch.length"), cn)
106
+    i <- match(c("x", "y", "isTip", "label", "branch", "branch.length", "angle"), cn)
107 107
     i <- i[!is.na(i)]
108 108
     info2 <- info2[, -i]
109 109
 
... ...
@@ -105,14 +105,16 @@ rm.singleton.newick <- function(nwk, outfile = NULL) {
105 105
 ##' @method fortify beast
106 106
 ##' @export
107 107
 fortify.beast <- function(model, data,
108
-                          layout    = "rectangular",
109
-                          yscale    = "none",
110
-                          ladderize = TRUE,
111
-                          right     =FALSE,
112
-                          ndigits   = NULL,
108
+                          layout        = "rectangular",
109
+                          yscale        = "none",
110
+                          ladderize     = TRUE,
111
+                          right         = FALSE,
112
+                          branch.length = "branch.length",
113
+                          ndigits       = NULL,
113 114
                           mrsd = NULL, ...) {
114 115
 
115
-    phylo <- get.tree(model)
116
+    phylo <- set_branch_length(model, branch.length)
117
+
116 118
     df    <- fortify(phylo, layout=layout,
117 119
                      ladderize=ladderize, right=right, mrsd = mrsd, ...)
118 120
     
... ...
@@ -258,11 +260,9 @@ fortify.codeml <- function(model, data,
258 260
         phylo <- get.tree(model@rst)
259 261
     } else {
260 262
         if (length == "mlc.branch.length") {
261
-            length = "branch.length"
263
+            length <- "branch.length"
262 264
         }
263
-        phylo <- fortify.codeml_mlc_(model@mlc, data, layout,
264
-                                     ladderize, right,
265
-                                     branch.length = length, ...)
265
+        phylo <- set_branch_length(model@mlc, length)
266 266
     }
267 267
     
268 268
     df <- fortify(phylo, data, layout, ladderize, right,
... ...
@@ -271,7 +271,7 @@ fortify.codeml <- function(model, data,
271 271
     res <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
272 272
     df <- merge_phylo_anno.paml_rst(res, model@rst)
273 273
     df <- scaleY(phylo, df, yscale, layout, ...)
274
-
274
+    
275 275
     append_extraInfo(df, model)
276 276
 }
277 277
 
... ...
@@ -287,11 +287,11 @@ fortify.codeml_mlc <- function(model, data,
287 287
                                ndigits       = NULL,
288 288
                                mrsd          = NULL,
289 289
                                ...) {
290
+
291
+    phylo <- set_branch_length(model, branch.length)
290 292
         
291
-    phylo <- fortify.codeml_mlc_(model, data, layout,
292
-                                 ladderize, right,
293
-                                 branch.length, mrsd=mrsd, ...)
294
-    df <- fortify(phylo, data, layout, ladderize, right, branch.length=branch.length, ...)
293
+    df <- fortify(phylo, data, layout, ladderize, right,
294
+                  branch.length=branch.length, mrsd=mrsd, ...)
295 295
     
296 296
     dNdS <- model@dNdS
297 297
 
... ...
@@ -325,26 +325,10 @@ fortify.codeml_mlc_ <- function(model, data,
325 325
                                 right         = FALSE,
326 326
                                 branch.length = "branch.length",
327 327
                                 ...) {
328
-    dNdS <- model@dNdS
329
-    length <- match.arg(branch.length, c("none", "branch.length",
330
-                                         colnames(dNdS)[-c(1,2)]))
331
-    phylo <- get.tree(model)
332 328
 
333
-    if (! length %in%  c("branch.length", "none")) {
334
-        edge <- as.data.frame(phylo$edge)
335
-        colnames(edge) <- c("parent", "node")
336
-        
337
-        dd <- merge(edge, dNdS,
338
-                    by.x  = c("node", "parent"),
339
-                    by.y  = c("node", "parent"),
340
-                    all.x = TRUE)
341
-        dd <- dd[match(edge$node, dd$node),]
342
-        phylo$edge.length <- dd[, length]
343
-    }
344
-
345
-    return(phylo)
346 329
 }
347 330
 
331
+
348 332
     
349 333
 ##' @method fortify paml_rst
350 334
 ##' @export
... ...
@@ -1,3 +1,29 @@
1
+
2
+##' @rdname groupClade-methods
3
+##' @exportMethod groupClade
4
+setMethod("groupClade", signature(object="phylo"),
5
+          function(object, node, group_name="group") {
6
+              groupClade.phylo(object, node, group_name)
7
+          })
8
+
9
+groupClade.phylo <- function(object, node, group_name) {
10
+    if (length(node) == 1) {
11
+        clade <- extract.clade(object, node)
12
+        tips <- clade$tip.label
13
+    } else {
14
+        tips <- lapply(node, function(x) {
15
+            clade <- extract.clade(object, x)
16
+            clade$tip.label
17
+        })
18
+    }
19
+    
20
+    groupOTU.phylo(object, tips, group_name)
21
+}
22
+
23
+
24
+
25
+
26
+
1 27
 ##' group selected clade
2 28
 ##'
3 29
 ##' 
... ...
@@ -1,3 +1,26 @@
1
+##' group OTU
2
+##'
3
+##' 
4
+##' @title groupOTU.phylo
5
+##' @param phy tree object
6
+##' @param focus tip list
7
+##' @param group_name name of the group
8
+##' @return phylo object
9
+##' @author ygc
10
+groupOTU.phylo <- function(phy, focus, group_name="group") {
11
+    attr(phy, group_name) <- NULL
12
+    if ( is(focus, "list") ) {
13
+        for (i in 1:length(focus)) {
14
+            phy <- gfocus(phy, focus[[i]], group_name)
15
+        } 
16
+    } else {
17
+        phy <- gfocus(phy, focus, group_name)
18
+    }
19
+    attr(phy, group_name) <- factor(attr(phy, group_name))
20
+    return(phy)
21
+}
22
+
23
+
1 24
 ##' group tree based on selected OTU, will traceback to MRCA
2 25
 ##'
3 26
 ##' 
... ...
@@ -8,6 +31,7 @@ setMethod("groupOTU", signature(object="phylo"),
8 31
               groupOTU.phylo(object, focus, group_name)
9 32
           })
10 33
 
34
+
11 35
 ##' @rdname groupOTU-methods
12 36
 ##' @exportMethod groupOTU
13 37
 setMethod("groupOTU", signature(object="apeBootstrap"),
... ...
@@ -39,49 +39,4 @@ setMethod("scale_color", signature(object="phylo"),
39 39
 
40 40
 
41 41
 
42
-##' group OTU
43
-##'
44
-##' 
45
-##' @title groupOTU.phylo
46
-##' @param phy tree object
47
-##' @param focus tip list
48
-##' @param group_name name of the group
49
-##' @return phylo object
50
-##' @author ygc
51
-groupOTU.phylo <- function(phy, focus, group_name="group") {
52
-    attr(phy, group_name) <- NULL
53
-    if ( is(focus, "list") ) {
54
-        for (i in 1:length(focus)) {
55
-            phy <- gfocus(phy, focus[[i]], group_name)
56
-        } 
57
-    } else {
58
-        phy <- gfocus(phy, focus, group_name)
59
-    }
60
-    attr(phy, group_name) <- factor(attr(phy, group_name))
61
-    return(phy)
62
-}
63
-
64
-##' @rdname groupClade-methods
65
-##' @exportMethod groupClade
66
-setMethod("groupClade", signature(object="phylo"),
67
-          function(object, node, group_name="group") {
68
-              groupClade.phylo(object, node, group_name)
69
-          })
70
-
71
-groupClade.phylo <- function(object, node, group_name) {
72
-    if (length(node) == 1) {
73
-        clade <- extract.clade(object, node)
74
-        tips <- clade$tip.label
75
-    } else {
76
-        tips <- lapply(node, function(x) {
77
-            clade <- extract.clade(object, x)
78
-            clade$tip.label
79
-        })
80
-    }
81
-    
82
-    groupOTU.phylo(object, tips, group_name)
83
-}
84
-
85
-
86
-
87 42
 
... ...
@@ -869,3 +869,44 @@ calculate_branch_mid <- function(res) {
869 869
     res$branch[is.na(res$branch)] <- 0
870 870
     return(res)
871 871
 }
872
+
873
+
874
+set_branch_length <- function(tree_object, branch.length) {
875
+    phylo <- get.tree(tree_object)
876
+    
877
+    if (branch.length %in%  c("branch.length", "none")) {
878
+        return(phylo)
879
+    }
880
+
881
+
882
+    ## if (is(tree_object, "codeml")) {
883
+    ##     tree_anno <- tree_object@mlc@dNdS
884
+    ## } else
885
+    
886
+    if (is(tree_object, "codeml_mlc")) {
887
+        tree_anno <- tree_object@dNdS
888
+    } else if (is(tree_object, "beast")) {
889
+        tree_anno <- tree_object@stats
890
+    }
891
+    
892
+    length <- match.arg(branch.length, c("none", "branch.length",
893
+                                         colnames(tree_anno)[-c(1,2)]))
894
+
895
+  
896
+    edge <- as.data.frame(phylo$edge)
897
+    colnames(edge) <- c("parent", "node")
898
+    
899
+    dd <- merge(edge, tree_anno,
900
+                by.x  = "node",
901
+                by.y  = "node",
902
+                all.x = TRUE)
903
+    dd <- dd[match(edge$node, dd$node),]
904
+    len <- unlist(dd[, length])
905
+    len <- as.numeric(len)
906
+    len[is.na(len)] <- 0
907
+    
908
+    phylo$edge.length <- len
909
+
910
+    return(phylo)
911
+}
912
+
872 913
deleted file mode 100644
... ...
@@ -1,27 +0,0 @@
1
-% Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/ggplot2.R
3
-\name{aes}
4
-\alias{aes}
5
-\title{creates a lists of unevaluated expressions}
6
-\source{
7
-This is just the imported function
8
-from the ggplot2 package. The documentation you should
9
-read for the aes function can be found here: \link[ggplot2]{aes}
10
-}
11
-\usage{
12
-aes(x, y, ...)
13
-}
14
-\arguments{
15
-\item{x}{name values}
16
-
17
-\item{y}{name values}
18
-
19
-\item{...}{additional name values}
20
-}
21
-\description{
22
-creates a lists of unevaluated expressions
23
-}
24
-\seealso{
25
-\link[ggplot2]{aes}
26
-}
27
-
... ...
@@ -27,6 +27,8 @@ geom_cladelabel(node, label, offset = 0, offset.text = 0, align = FALSE,
27 27
 
28 28
 \item{geom}{one of 'text' or 'label'}
29 29
 
30
+\item{hjust}{hjust}
31
+
30 32
 \item{fill}{fill label background, only work with geom='label'}
31 33
 
32 34
 \item{...}{additional parameter}
... ...
@@ -2,14 +2,9 @@
2 2
 % Please edit documentation in R/geom_hilight.R
3 3
 \name{geom_hilight}
4 4
 \alias{geom_hilight}
5
-\alias{stat_hilight}
6 5
 \title{geom_hilight}
7 6
 \usage{
8 7
 geom_hilight(node, fill = "steelblue", alpha = 0.5)
9
-
10
-stat_hilight(mapping = NULL, data = NULL, geom = "rect",
11
-  position = "identity", node, show.legend = NA, inherit.aes = FALSE,
12
-  fill, alpha, ...)
13 8
 }
14 9
 \arguments{
15 10
 \item{node}{selected node to hilight}
... ...
@@ -17,16 +12,12 @@ stat_hilight(mapping = NULL, data = NULL, geom = "rect",
17 12
 \item{fill}{color fill}
18 13
 
19 14
 \item{alpha}{alpha (transparency)}
20
-
21
-\item{geom}{geometric object}
22 15
 }
23 16
 \value{
24 17
 ggplot2
25 18
 }
26 19
 \description{
27 20
 layer of hilight clade with rectangle
28
-
29
-stat_hilight
30 21
 }
31 22
 \author{
32 23
 Guangchuang Yu
33 24
deleted file mode 100644
... ...
@@ -1,23 +0,0 @@
1
-% Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/ggplot2.R
3
-\name{ggplotGrob}
4
-\alias{ggplotGrob}
5
-\title{generate a ggplot2 plot grob}
6
-\source{
7
-This is just the imported function
8
-from the ggplot2 package. The documentation you should
9
-read for the ggplotGrob function can be found here: \link[ggplot2]{ggplotGrob}
10
-}
11
-\usage{
12
-ggplotGrob(x)
13
-}
14
-\arguments{
15
-\item{x}{ggplot2 object}
16
-}
17
-\description{
18
-generate a ggplot2 plot grob
19
-}
20
-\seealso{
21
-\link[ggplot2]{ggplotGrob}
22
-}
23
-
... ...
@@ -8,8 +8,8 @@
8 8
 \title{visualizing phylogenetic tree and heterogenous associated data based on grammar of graphics
9 9
 \code{ggtree} provides functions for visualizing phylogenetic tree and its associated data in R.}
10 10
 \usage{
11
-ggtree(tr, mapping = NULL, showDistance = FALSE, layout = "rectangular",
12
-  mrsd = NULL, as.Date = FALSE, yscale = "none", yscale_mapping = NULL,
11
+ggtree(tr, mapping = NULL, layout = "rectangular", mrsd = NULL,
12
+  as.Date = FALSE, yscale = "none", yscale_mapping = NULL,
13 13
   ladderize = TRUE, right = FALSE, branch.length = "branch.length",
14 14
   ndigits = NULL, ...)
15 15
 }
... ...
@@ -18,8 +18,6 @@ ggtree(tr, mapping = NULL, showDistance = FALSE, layout = "rectangular",
18 18
 
19 19
 \item{mapping}{aes mapping}
20 20
 
21
-\item{showDistance}{add distance legend, logical}
22
-
23 21
 \item{layout}{one of 'rectangular', 'slanted', 'fan'/'circular', 'radial' or 'unrooted'}
24 22
 
25 23
 \item{mrsd}{most recent sampling date}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/AllGenerics.R, R/RAxML.R, R/ape.R, R/beast.R, R/codeml.R, R/codeml_mlc.R, R/hyphy.R, R/jplace.R, R/method-groupClade.R, R/paml_rst.R, R/phangorn.R, R/phylo.R, R/r8s.R
2
+% Please edit documentation in R/AllGenerics.R, R/RAxML.R, R/ape.R, R/beast.R, R/codeml.R, R/codeml_mlc.R, R/hyphy.R, R/jplace.R, R/method-groupClade.R, R/paml_rst.R, R/phangorn.R, R/r8s.R
3 3
 \docType{methods}
4 4
 \name{groupClade}
5 5
 \alias{groupClade}
... ...
@@ -35,6 +35,8 @@ groupClade(object, node, group_name = "group", ...)
35 35
 
36 36
 \S4method{groupClade}{jplace}(object, node, group_name = "group")
37 37
 
38
+\S4method{groupClade}{phylo}(object, node, group_name = "group")
39
+
38 40
 \S4method{groupClade}{nhx}(object, node, group_name = "group")
39 41
 
40 42
 \S4method{groupClade}{ggplot}(object, node, group_name)
... ...
@@ -45,8 +47,6 @@ groupClade(object, node, group_name = "group", ...)
45 47
 
46 48
 \S4method{groupClade}{phangorn}(object, node, group_name = "group")
47 49
 
48
-\S4method{groupClade}{phylo}(object, node, group_name = "group")
49
-
50 50
 \S4method{groupClade}{r8s}(object, node, group_name = "group",
51 51
   tree = "TREE")
52 52
 }
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/AllGenerics.R, R/RAxML.R, R/groupOTU.R, R/hyphy.R, R/jplace.R, R/method-groupOTU.R
2
+% Please edit documentation in R/AllGenerics.R, R/RAxML.R, R/hyphy.R, R/jplace.R, R/method-groupOTU.R
3 3
 \docType{methods}
4 4
 \name{groupOTU}
5 5
 \alias{groupOTU}
... ...
@@ -23,16 +23,6 @@ groupOTU(object, focus, group_name = "group", ...)
23 23
 
24 24
 \S4method{groupOTU}{raxml}(object, focus, group_name = "group")
25 25
 
26
-\S4method{groupOTU}{phylo}(object, focus, group_name = "group")
27
-
28
-\S4method{groupOTU}{r8s}(object, focus, group_name = "group", tree = "TREE")
29
-
30
-\S4method{groupOTU}{codeml}(object, focus, group_name = "group")
31
-
32
-\S4method{groupOTU}{codeml_mlc}(object, focus, group_name = "group")
33
-
34
-\S4method{groupOTU}{paml_rst}(object, focus, group_name = "group")
35
-
36 26
 \S4method{groupOTU}{hyphy}(object, focus, group_name = "group")
37 27
 
38 28
 \S4method{groupOTU}{jplace}(object, focus, group_name = "group")
... ...
@@ -69,8 +59,6 @@ codeml_mlc, codeml, jplace, beast, hyphy}
69 59
 
70 60
 \item{...}{additional parameter}
71 61
 
72
-\item{tree}{which tree selected}
73
-
74 62
 \item{tree}{which tree selected}
75 63
 }
76 64
 \value{
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/phylo.R
2
+% Please edit documentation in R/method-groupOTU.R
3 3
 \name{groupOTU.phylo}
4 4
 \alias{groupOTU.phylo}
5 5
 \title{groupOTU.phylo}
... ...
@@ -6,9 +6,6 @@
6 6
 \title{Class "r8s"
7 7
 This class stores output info from r8s}
8 8
 \description{
9
-Class "r8s"
10
-This class stores output info from r8s
11
-
12 9
 Class "r8s"
13 10
 This class stores output info from r8s
14 11
 }
... ...
@@ -23,21 +20,9 @@ This class stores output info from r8s
23 20
 
24 21
 \item{\code{phylo}}{multiPhylo, time tree, rate tree and absolute substitution tree}
25 22
 
26
-\item{\code{extraInfo}}{extra information}
27
-
28
-\item{\code{file}}{input file}
29
-
30
-\item{\code{fields}}{available feature}
31
-
32
-\item{\code{treetext}}{tree text}
33
-
34
-\item{\code{phylo}}{multiPhylo, time tree, rate tree and absolute substitution tree}
35
-
36 23
 \item{\code{extraInfo}}{extra information}
37 24
 }}
38 25
 \author{
39
-Guangchuang Yu \url{http://ygc.name}
40
-
41 26
 Guangchuang Yu \url{http://ygc.name}
42 27
 }
43 28
 \keyword{classes}
44 29
new file mode 100644
... ...
@@ -0,0 +1,38 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/geom_hilight.R
3
+\name{stat_hilight}
4
+\alias{stat_hilight}
5
+\title{stat_hilight}
6
+\usage{
7
+stat_hilight(mapping = NULL, data = NULL, geom = "rect",
8
+  position = "identity", node, show.legend = NA, inherit.aes = FALSE,
9
+  fill, alpha, ...)
10
+}
11
+\arguments{
12
+\item{mapping}{aes mapping}
13
+
14
+\item{data}{data}
15
+
16
+\item{geom}{geometric object}
17
+
18
+\item{position}{position}
19
+
20
+\item{node}{node number}
21
+
22
+\item{show.legend}{show legend}
23
+
24
+\item{inherit.aes}{logical}
25
+
26
+\item{fill}{fill color}
27
+
28
+\item{alpha}{transparency}
29
+
30
+\item{...}{additional parameter}
31
+}
32
+\value{
33
+layer
34
+}
35
+\description{
36
+stat_hilight
37
+}
38
+
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/ggtree.R
2
+% Please edit documentation in R/theme.R
3 3
 \name{theme_transparent}
4 4
 \alias{theme_transparent}
5 5
 \title{theme_transparent}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/ggtree.R
2
+% Please edit documentation in R/theme.R
3 3
 \name{theme_tree}
4 4
 \alias{theme_tree}
5 5
 \title{theme_tree}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/ggtree.R
2
+% Please edit documentation in R/theme.R
3 3
 \name{theme_tree2}
4 4
 \alias{theme_tree2}
5 5
 \title{theme_tree2}
... ...
@@ -17,7 +17,7 @@ test_that("convert polytomy to binary tree", {
17 17
     expect_equal(is.binary.tree(bt), TRUE)
18 18
 })
19 19
 
20
-df <- ggtree:::fortify.phylo(rtree(10))
20
+df <- fortify(rtree(10))
21 21
 child <- ggtree:::getChild.df(df, 11)
22 22
 test_that("root node should not be included in its ancestor node list", {
23 23
     expect_equal(11 %in% child, FALSE)
24 24
Binary files a/vignettes/figures/ggtree_objects.png and b/vignettes/figures/ggtree_objects.png differ
25 25
new file mode 100644
... ...
@@ -0,0 +1,139 @@
1
+---
2
+title: "ggtree: a phylogenetic tree viewer for different types of tree annotations"
3
+author: "\\
4
+
5
+	Guangchuang Yu (<guangchuangyu@gmail.com>) and Tommy Tsan-Yuk Lam (<ttylam@hku.hk>)\\
6
+
7
+        School of Public Health, The University of Hong Kong"
8
+date: "`r Sys.Date()`"
9
+bibliography: ggtree.bib
10
+csl: nature.csl
11
+output: 
12
+  BiocStyle::html_document:
13
+    toc: true
14
+  BiocStyle::pdf_document:
15
+    toc: true
16
+vignette: >
17
+  %\VignetteIndexEntry{00 ggtree introduction}
18
+  %\VignetteEngine{knitr::rmarkdown}
19
+  %\usepackage[utf8]{inputenc}
20
+---
21
+
22
+```{r style, echo=FALSE, results="asis", message=FALSE}
23
+BiocStyle::markdown()
24
+knitr::opts_chunk$set(tidy = FALSE,
25
+		   message = FALSE)
26
+```
27
+
28
+
29
+```{r echo=FALSE, results="hide", message=FALSE}
30
+library("colorspace")
31
+library("Biostrings")
32
+library("ape")
33
+library("ggplot2")
34
+library("gridExtra")
35
+library("ggtree")
36
+library("phangorn")
37
+```
38
+
39
+
40
+
41
+> You can't even begin to understand biology, you can't understand life, unless you understand what it's all there for, how it arose - and that means evolution.
42
+> --- Richard Dawkins
43
+
44
+
45
+# Citation
46
+If you use `r Biocpkg("ggtree")` in published research, please cite:
47
+
48
+```
49
+G Yu, D Smith, H Zhu, Y Guan, TTY Lam,
50
+ggtree: an R package for visualization and annotation of phylogenetic tree with different types of meta-data.
51
+revised.
52
+```
53
+
54
+# Introduction
55
+
56
+This project arose from our needs to annotate nucleotide substitutions in the phylogenetic tree, and we found that there is no tree visualization software can do this easily. Existing tree viewers are designed for displaying phylogenetic tree, but not annotating it. Although some tree viewers can displaying bootstrap values in the tree, it is hard/impossible to display other information in the tree. Our first solution for displaying nucleotide substituitions in the tree is to add this information in the node/tip names and use traditional tree viewer to show it. We displayed the information in the tree successfully, but we believe this indirect approach is inefficient.
57
+
58
+In the old day, phylogenetic tree is often small. At that time, as we almost didn't have a need to annotate a tree; displaying the evolution relationships is mostly enough. Nowadays, we can obtain a lot of data from different experiments, and we want to associate our data, for instance antigenic change, with the evolution relationship. Visualizing these associations in the phylogenetic tree can help us to identify evolution patterns. We believe we need a next generation tree viewer that should be programmable and extensible. It can view a phylogenetic tree easily as we did with classical software and support adding annotation data in a layer above the tree. This is the objective of developing the `r Githubpkg("GuangchuangYu/ggtree")`. Common tasks of annotating a phylogenetic tree should be easy and complicated tasks can be possible to achieve by adding multiple layers of annotation.
59
+
60
+The `r Githubpkg("GuangchuangYu/ggtree")` is designed by extending the `r CRANpkg("ggplot2")`[@wickham_ggplot2_2009] package. It is based on the grammar of graphics and takes all the good parts of `r CRANpkg("ggplot2")`. There are other R packages that implement tree viewer using `r CRANpkg("ggplot2")`, including `r CRANpkg("OutbreakTools")`, `r Biocpkg("phyloseq")`[@mcmurdie_phyloseq_2013] and `r Githubpkg("gjuggler/ggphylo")`; they mostly create complex tree view functions for their specific needs. Internally, these packages interpret a phylogenetic as a collection of `lines`, which makes it hard to annotate diverse user input that are related to node (taxa). The `r Githubpkg("GuangchuangYu/ggtree")` is different to them by interpreting a tree as a collection of `taxa` and allowing general flexibilities of annotating phylogenetic tree with diverse types of user inputs. 
61
+
62
+
63
+# Getting data into `R`
64
+
65
+Most of the tree viewer software (including `R` packages) focus on `Newick` and `Nexus` file format, while there are file formats from different evolution analysis software that contain supporting evidences within the file that are ready for annotating a phylogenetic tree.
66
+The `r Githubpkg("GuangchuangYu/ggtree")` package define several parser functions and `S4` classes to store statistical evidences inferred by commonly used software packages. It supports several file format, including:
67
+
68
++ Newick (via `ape`)
69
++ Nexus (via `ape`)
70
++ New Hampshire eXtended format (NHX)
71
++ Jplace
72
+
73
+and software output from:
74
+
75
++ [BEAST](http://beast2.org/)[@bouckaert_beast_2014] 
76
++ [EPA](http://sco.h-its.org/exelixis/web/software/epa/index.html)[@berger_EPA_2011]
77
++ [HYPHY](http://hyphy.org/w/index.php/Main_Page)[@pond_hyphy_2005]
78
++ [PAML](http://abacus.gene.ucl.ac.uk/software/paml.html)[@yang_paml_2007]
79
++ [PHYLDOG](http://pbil.univ-lyon1.fr/software/phyldog/)[@boussau_genome-scale_2013]
80
++ [pplacer](http://matsen.fhcrc.org/pplacer/)[@matsen_pplacer_2010]
81
++ [r8s](http://loco.biosci.arizona.edu/r8s/)[@marazzi_locating_2012]
82
++ [RAxML](http://sco.h-its.org/exelixis/web/software/raxml/)[@stamatakis_raxml_2014]
83
++ [RevBayes](http://revbayes.github.io/intro.html)[@hohna_probabilistic_2014]
84
+
85
+
86
+Parsing data from a number of molecular evolution software is not only for visualization in `ggtree`, but also bring these data to `R` users for further analysis (e.g. summarization, visualization, comparision, test, _etc_).
87
+
88
+For more details, please refer to [Tree Data Import](treeImport.html) vignette.
89
+
90
+
91
+# Tree Visualization and Annotation
92
+
93
+Tree Visualization in `ggtree` is easy, with one line of command `ggtree(tree_object)`. It supports several layout, including `rectangular`, `slanted` and `circular` for `Phylogram` and `Cladogram`, `unrooted` layout, time-scaled and two dimentional phylogenies. [Tree Visualization](treeVisualization.html) vignette describe these feature in details.
94
+
95
+We implement several functions to manipulate a phylogenetic tree.
96
+
97
++ taxa can be clustered together using `groupClade` or `groupOTU` functions
98
++ clades can be collapsed via `collapse` function
99
++ collapsed clade can be expanded by using `expand` function
100
++ clade can be re-scale to zoom in or zoom out by `scaleClade` function
101
++ selected clade can be rotated by 180 degree using `rotate` function
102
++ position of two selected clades (should share a same parent) can be exchanged by `flip` function
103
+
104
+Details and examples can be found in [Tree Manipulation](treeManipulation.html) vignette.
105
+
106
+
107
+Most of the phylogenetic trees are scaled by evolutionary distance (substitution/site), in `ggtree` a phylogenetic tree can be re-scaled by any numerical variable inferred by evolutionary analysis (e.g. species divergence time, *dN/dS*, _etc_). Numerical and category variable can be used to color a phylogenetic tree.
108
+
109
+The `ggtree` package provides several layers to annotate a phylogenetic tree, including `geom_treescale` for adding a legend of tree scale, `geom_hilight` for highlighting selected clades and `geom_cladelabel` for labelling selected clades.
110
+
111
+It supports annotating phylogenetic trees with analyses by R packages and other commonly used evolutionary software. User's specific annotation (e.g. experimental data) can be integrated to annotate phylogenetic tree. `ggtree` provides `write.jplace` function to combine Newick tree file and user's own data to a single `jplace` file that can be parsed and the data can be used to annotate the tree directly in `ggtree`.
112
+
113
+`ggtree` integrates `phylopic` database and silhouette images of organisms can be downloaded and used to annotate phylogenetic directly. `ggtree` also supports using local images to annotate a phylogenetic tree.
114
+
115
+Visualizing an annotated phylogenetic tree with numerical matrix (e.g. genotype table) and multiple sequence alignment is also supported in `ggtree`. Examples of annotating phylogenetic trees can be found in the [Tree Annotation](treeAnnotation.html) vignette.
116
+
117
+
118
+# Vignette Entry
119
+
120
++ [Tree Data Import](treeImport.html)
121
++ [Tree Visualization](treeVisualization.html)
122
++ [Tree Manipulation](treeManipulation.html)
123
++ [Tree Annotation](treeAnnotation.html)
124
+
125
+
126
+# Bugs/Feature requests
127
+
128
+If you have any, [let me know](https://github.com/GuangchuangYu/ggtree/issues). Thx!
129
+
130
+
131
+# Session info
132
+
133
+Here is the output of `sessionInfo()` on the system on which this document was compiled:
134
+```{r echo=FALSE}
135
+sessionInfo()
136
+```
137
+
138
+
139
+# References
0 140
new file mode 100644
... ...
@@ -0,0 +1,331 @@
1
+---
2
+title: "Tree Annotation"
3
+author: "\\
4
+
5
+	Guangchuang Yu (<guangchuangyu@gmail.com>) and Tommy Tsan-Yuk Lam (<ttylam@hku.hk>)\\
6
+
7
+        School of Public Health, The University of Hong Kong"
8
+date: "`r Sys.Date()`"
9
+bibliography: ggtree.bib
10
+csl: nature.csl
11
+output: 
12
+  BiocStyle::html_document:
13
+    toc: true
14
+  BiocStyle::pdf_document:
15
+    toc: true
16
+vignette: >
17
+  %\VignetteIndexEntry{04 Tree Annotation}
18
+  %\VignetteEngine{knitr::rmarkdown}
19
+  %\VignetteDepends{phangorn}
20
+  %\VignetteDepends{gridExtra}
21
+  %\usepackage[utf8]{inputenc}
22
+---
23
+
24
+```{r style, echo=FALSE, results="asis", message=FALSE}
25
+BiocStyle::markdown()
26
+knitr::opts_chunk$set(tidy = FALSE,
27
+		   message = FALSE)
28
+```
29
+
30
+
31
+```{r echo=FALSE, results="hide", message=FALSE}
32
+library("ape")
33
+library("ggplot2")
34
+library("ggtree")
35
+library("gridExtra")
36
+```
37
+
38
+
39
+# Rescale tree
40
+
41
+Most of the phylogenetic trees are scaled by evolutionary distance (substitution/site), in `ggtree`, we can re-scale a phylogenetic tree by any numerical variable inferred by evolutionary analysis (e.g. *dN/dS*).
42
+
43
+
44
+```{r fig.width=10, fig.height=5}
45
+beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
46
+beast_tree <- read.beast(beast_file)
47
+beast_tree
48
+p1 <- ggtree(beast_tree, mrsd='2013-01-01') + theme_tree2() +
49
+    ggtitle("Divergence time")
50
+p2 <- ggtree(beast_tree, branch.length = 'rate') + theme_tree2() +
51
+    ggtitle("Substitution rate")
52
+require(gridExtra)
53
+grid.arrange(p1, p2, ncol=2)
54
+```
55
+
56
+```{r fig.width=10, fig.height=5}
57
+mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="ggtree")
58
+mlc_tree <- read.codeml_mlc(mlcfile)
59
+p1 <- ggtree(mlc_tree) + theme_tree2() +
60
+    ggtitle("nucleotide substitutions per codon")
61
+p2 <- ggtree(mlc_tree, branch.length='dN_vs_dS') + theme_tree2() +
62
+    ggtitle("dN/dS tree")
63
+grid.arrange(p1, p2, ncol=2)
64
+```
65
+
66
+# Zoom on a portion of tree
67
+
68
+`r Githubpkg("GuangchuangYu/ggtree")` provides _`gzoom`_ function that similar to _`zoom`_ function provided in `r CRANpkg("ape")`. This function plots simultaneously a whole phylogenetic tree and a portion of it. It aims at exploring very large trees.
69
+
70
+```{r fig.width=18, fig.height=10, fig.align="center"}
71
+library("ape")
72
+data(chiroptera)
73
+library("ggtree")
74
+gzoom(chiroptera, grep("Plecotus", chiroptera$tip.label))
75
+```
76
+
77
+# Color tree
78
+
79
+In `r Githubpkg("GuangchuangYu/ggtree")`, coloring phylogenetic tree is easy, by using `aes(color=VAR)` to map the color of tree based on a specific variable (numeric and category are both supported).
80
+
81
+```{r fig.width=5, fig.height=5}
82
+ggtree(beast_tree, aes(color=rate)) +
83
+    scale_color_continuous(low='darkgreen', high='red') +
84
+    theme(legend.position="right")
85
+```
86
+
87
+User can use any feature (if available), including clade posterior and *dN/dS* _etc._, to scale the color of the tree.
88
+
89
+## Annotate clades
90
+`r Githubpkg("GuangchuangYu/ggtree")` implements _`geom_cladelabel`_ layer to annotate a selected clade with a bar indicating that clade with a corresponding label.
91
+
92
+The _`geom_cladelabel`_ layer accepts a selected internal node number. To get the internal node number, please refer to [Tree Manipulation](treeManipulation.html#internal-node-number) vignette.
93
+
94
+
95
+```{r}
96
+set.seed(2015-12-21)
97
+tree = rtree(30)
98
+p <- ggtree(tree) + xlim(NA, 6)
99
+
100
+p+geom_cladelabel(node=45, label="test label") +
101
+    geom_cladelabel(node=34, label="another clade") 
102
+```
103
+
104
+We can set the parameter, `align = TRUE`, to align the clade label, and use the parameter, `offset`, to adjust the position.
105
+
106
+```{r}
107
+p+geom_cladelabel(node=45, label="test label", align=TRUE, offset=.5) +
108
+    geom_cladelabel(node=34, label="another clade", align=TRUE, offset=.5)
109
+```
110
+
111
+We can change the color of the clade label via the parameter `color`.
112
+
113
+```{r}
114
+p+geom_cladelabel(node=45, label="test label", align=T, color='red') +
115
+    geom_cladelabel(node=34, label="another clade", align=T, color='blue')
116
+```
117
+
118
+We can change the `angle` of the clade label text and the relative position from text to bar via the parameter `offset.text`.
119
+
120
+```{r}
121
+p+geom_cladelabel(node=45, label="test label", align=T, angle=270, hjust='center', offset.text=.5) +
122
+    geom_cladelabel(node=34, label="another clade", align=T, angle=45) 
123
+```
124
+
125
+We can change the size of the bar and text via the parameters `barsize` and `fontsize` respectively.
126
+
127
+```{r}
128
+p+geom_cladelabel(node=45, label="test label", align=T, angle=270, hjust='center', offset.text=.5, barsize=1.5) +
129
+    geom_cladelabel(node=34, label="another clade", align=T, angle=45, fontsize=8) 
130
+```
131
+
132
+We can also use `geom_label` to label the text.
133
+
134
+```{r}
135
+p+ geom_cladelabel(node=34, label="another clade", align=T, geom='label', fill='lightblue') 
136
+```
137
+
138
+# Highlight clades
139
+
140
+`r Githubpkg("GuangchuangYu/ggtree")` implements _`geom_hilight`_ layer, that an internal node number and add a layer of rectangle to highlight the selected clade.
141
+
142
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
143
+nwk <- system.file("extdata", "sample.nwk", package="ggtree")
144
+tree <- read.tree(nwk)
145
+ggtree(tree) + geom_hilight(node=21, fill="steelblue", alpha=.6) +
146
+    geom_hilight(node=17, fill="darkgreen", alpha=.6)
147
+```
148
+
149
+
150
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
151
+ggtree(tree, layout="circular") + geom_hilight(node=21, fill="steelblue", alpha=.6) +
152
+    geom_hilight(node=23, fill="darkgreen", alpha=.6)
153
+```
154
+
155
+Another way to highlight selected clades is setting the clades with different colors and/or line types as demonstrated in [Tree Manipulation](treeManipulation.html#groupclade) vignette.
156
+
157
+
158
+# Tree annotation with analysis of R packages
159
+
160
+## annotating tree with ape bootstraping analysis
161
+
162
+```{r results='hide', message=FALSE}
163
+library(ape)
164
+data(woodmouse)
165
+d <- dist.dna(woodmouse)
166
+tr <- nj(d)
167
+bp <- boot.phylo(tr, woodmouse, function(xx) nj(dist.dna(xx)))
168
+```
169
+
170
+```{r fig.width=6, fig.height=6, warning=FALSE, fig.align="center"}
171
+tree <- apeBoot(tr, bp)
172
+ggtree(tree) + geom_label(aes(label=bootstrap)) + geom_tiplab()
173
+```
174
+
175
+## annotating tree with phangorn output
176
+
177
+```{r results='hide', message=FALSE}
178
+library(phangorn)
179
+treefile <- system.file("extdata", "pa.nwk", package="ggtree")
180
+tre <- read.tree(treefile)
181
+tipseqfile <- system.file("extdata", "pa.fas", package="ggtree")
182
+tipseq <- read.phyDat(tipseqfile,format="fasta")
183
+fit <- pml(tre, tipseq, k=4)
184
+fit <- optim.pml(fit, optNni=FALSE, optBf=T, optQ=T,
185
+                 optInv=T, optGamma=T, optEdge=TRUE,
186
+                 optRooted=FALSE, model = "GTR")
187
+```
188
+
189
+```{r fig.width=12, fig.height=10, width=60, warning=FALSE, fig.align="center"}
190
+phangorn <- phyPML(fit, type="ml")
191
+ggtree(phangorn) + geom_text(aes(x=branch, label=AA_subs, vjust=-.5))
192
+```
193
+
194
+# Tree annotation with output from evolution software
195
+
196
+In `ggtree`, we implemented several parser functions to parse output from commonly used software package in evolutionary biology, including:
197
+
198
++ [BEAST](http://beast2.org/)[@bouckaert_beast_2014] 
199
++ [EPA](http://sco.h-its.org/exelixis/web/software/epa/index.html)[@berger_EPA_2011]
200
++ [HYPHY](http://hyphy.org/w/index.php/Main_Page)[@pond_hyphy_2005]
201
++ [PAML](http://abacus.gene.ucl.ac.uk/software/paml.html)[@yang_paml_2007]
202
++ [PHYLDOG](http://pbil.univ-lyon1.fr/software/phyldog/)[@boussau_genome-scale_2013]
203
++ [pplacer](http://matsen.fhcrc.org/pplacer/)[@matsen_pplacer_2010]
204
++ [r8s](http://loco.biosci.arizona.edu/r8s/)[@marazzi_locating_2012]
205
++ [RAxML](http://sco.h-its.org/exelixis/web/software/raxml/)[@stamatakis_raxml_2014]
206
++ [RevBayes](http://revbayes.github.io/intro.html)[@hohna_probabilistic_2014]
207
+
208
+Evolutionary evidences inferred by these software packages can be used for further analysis in `R` and annotate phylogenetic tree directly in `ggtree`. For more details, please refer to the [Tree Data Import](treeImport.html) vignette.
209
+
210
+
211
+# Tree annotation with user specific annotation
212
+
213
+## the `%<+%` operator
214
+
215
+We provides several functions to parse and store information from common software output.
216
+
217
+Here, we would like to demonstrate how to inject user specific annotation data in a tree.
218
+
219
+Suppose we have the following data that associated with the tree and would like to attach the data in the tree.
220
+
221
+```{r}
222
+nwk <- system.file("extdata", "sample.nwk", package="ggtree")
223
+tree <- read.tree(nwk)
224
+p <- ggtree(tree)
225
+
226
+dd <- data.frame(taxa  = LETTERS[1:13], 
227
+      		 place = c(rep("GZ", 5), rep("HK", 3), rep("CZ", 4), NA),
228
+                 value = round(abs(rnorm(13, mean=70, sd=10)), digits=1))
229
+## you don't need to order the data
230
+## data was reshuffled just for demonstration
231
+dd <- dd[sample(1:13, 13), ]
232
+row.names(dd) <- NULL
233
+```
234
+```{r eval=FALSE}
235
+print(dd)
236
+```
237
+
238
+```{r echo=FALSE, results='asis'}
239
+knitr::kable(dd)
240
+```
241
+
242
+We can imaging that the _`place`_ column is the place we isolated the species and _`value`_ column stored numerical values for example bootstrap values.
243
+
244
+We have shown using the operator, _`%<%`_, to update a tree view with a new tree. Here, we will introduce another operator, _`%<+%`_, that attaches annotation data to a tree view. The only requirement of the input data is that its first column should be matched with the node/tip labels of the tree.
245
+
246
+After attaching the annotation data to the tree by _`%<+%`_, all the columns in the data are visible to _`ggplot2`_. As an example, here we attach the above annotation data to the tree view, _`p`_, and add a layer that showing the tip labels and colored them by the isolation site stored in _`place`_ column.
247
+
248
+```{r fig.width=6, fig.height=5, warning=FALSE, fig.align="center"}
249
+p <- p %<+% dd + geom_text(aes(color=place, label=label), hjust=-0.5) + 
250
+       geom_tippoint(aes(size=value, shape=place, color=place), alpha=0.25)
251
+p+theme(legend.position="right")
252
+```
253
+
254
+Once the data was attached, it is always attached. So we can add another layer to display the isolation sites easily.
255
+```{r fig.width=6, fig.height=5, warning=FALSE, fig.align="center"}
256
+p <- p + geom_text(aes(color=place, label=place), hjust=1, vjust=-0.4, size=3)
257
+print(p)
258
+```
259
+
260
+And another layer showing numerical values:
261
+```{r fig.width=6, fig.height=5, warning=FALSE, fig.align="center"}
262
+p <- p + geom_text(aes(color=place, label=value), hjust=1, vjust=1.4, size=3)
263
+print(p)
264
+```
265
+
266
+## `jplace` file format
267
+
268
+In `ggtree`, we provide `write.jplace` function to store user's own data and associated newick tree to a single `jplace` file, which can be parsed directly in `ggtree` and user's data can be used to annotate the tree directly. For more detail, please refer to the [Tree Data Import](treeImport.html#jplace-file-format) vignette.
269
+
270
+
271
+# Tree annotation with Phylopic
272
+
273
+
274
+[PhyloPic](http://phylopic.org/) is a database that stores reusable silhouette images of organisms. `r Githubpkg("GuangchuangYu/ggtree")` supports downloading images from [PhyloPic](http://phylopic.org/) and annotating phylogenetic tree with the downloaded images.
275
+
276
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE, eval=FALSE}
277
+pp <- ggtree(tree) %>% phylopic("79ad5f09-cf21-4c89-8e7d-0c82a00ce728", color="steelblue", alpha = .3)
278
+print(pp)
279
+```
280
+![](../inst/extdata/phylopic1.png)
281
+
282
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE, eval=FALSE}
283
+pp %>% phylopic("67382184-5135-4faa-8e98-eadff02c3e8a", color="#86B875", alpha=.8, node=4) %>%
284
+     phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", color="darkcyan", alpha=.8, node=17, width=.2)
285
+```
286
+![](../inst/extdata/phylopic2.png)
287
+
288
+
289
+Annotate phylogenetic tree with local images is also supported, please refer to the [blog post](http://guangchuangyu.github.io/2015/08/ggtree-annotate-phylogenetic-tree-with-local-images/).
290
+
291
+
292
+# Visualize tree with associated matrix
293
+
294
+At first we implemented `gplot` function to visualize tree with heatmap but it has [an issue](https://github.com/GuangchuangYu/ggtree/issues/3) that it can't always guarantee the heatmap aligning to the tree properly, since the line up is between two figures and it's currently not supported internally by ggplot2. I have implemented another function `gheatmap` that can do the line up properly by creating a new layer above the tree.
295
+
296
+
297
+In the following example, we visualized a tree of H3 influenza viruses with their associated genotype.
298
+
299
+```{r fig.width=20, fig.height=16, fig.align="center"}
300
+beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
301
+beast_tree <- read.beast(beast_file)
302
+
303
+genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
304
+genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F)
305
+p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_treescale(x=2008, y=1)
306
+p <- p + geom_tiplab(size=3)
307
+gheatmap(p, genotype, offset = 2, width=0.5)
308
+```
309
+
310
+The _width_ parameter is to control the width of the heatmap. It supports another parameter _offset_ for controling the distance between the tree and the heatmap, for instance left space for tip labels.
311
+
312
+
313
+For time scaled tree, as in this example, it's more often to use x axis by using `theme_tree2`. But with this solution, the heatmap is just another layer and will change the `x` axis. To overcome this issue, we implemented `scale_x_ggtree` to set the x axis more reasonable. User can also use `gplot` and tweak the positions of two plot to align properly.
314
+
315
+```{r fig.width=20, fig.height=16, fig.align="center"}
316
+p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_tiplab(size=3, align=TRUE) + theme_tree2()
317
+pp <- (p + scale_y_continuous(expand=c(0, 0.3))) %>%
318
+    gheatmap(genotype, offset=4, width=0.5, colnames=FALSE) %>%
319
+        scale_x_ggtree()
320
+pp + theme(legend.position="right")
321
+```
322
+
323
+# visualize tree with multiple sequence alignment
324
+
325
+With `msaplot` function, user can visualizes multiple sequence alignment with phylogenetic tree, as demonstrated below:
326
+```{r fig.width=8, fig.height=12, fig.align='center'}
327
+fasta <- system.file("examples/FluA_H3_AA.fas", package="ggtree")
328
+msaplot(ggtree(beast_tree), fasta) 
329
+```
330
+
331
+A specific slice of the alignment can also be displayed by specific _window_ parameter.
... ...
@@ -16,6 +16,7 @@ output:
16 16
 vignette: >
17 17
   %\VignetteIndexEntry{01 Tree Data Import}
18 18
   %\VignetteEngine{knitr::rmarkdown}
19
+  %\VignetteDepends{scales}
19 20
   %\usepackage[utf8]{inputenc}
20 21
 ---
21 22
 
... ...
@@ -229,6 +230,48 @@ head(fortify(ml))
229 230
 All the features in both files are available for annotation. For example, we can annotate _`dN/dS`_ with the tree in _`rstfile`_ and amino acid substitutions with the tree in _`mlcfile`_.
230 231
 
231 232
 
233
+## Parsing HYPHY output
234
+
235
+```{r warning=FALSE}
236
+nwk <- system.file("extdata/HYPHY", "labelledtree.tree", package="ggtree")
237
+ancseq <- system.file("extdata/HYPHY", "ancseq.nex", package="ggtree")
238
+tipfas <- system.file("extdata", "pa.fas", package="ggtree")
239
+hy <- read.hyphy(nwk, ancseq, tipfas)
240
+hy
241
+```
242
+
243
+```{r fig.width=12, fig.height=10, width=60, warning=FALSE, fig.align="center"}
244
+ggtree(hy) + geom_text(aes(x=branch, label=AA_subs), vjust=-.5)
245
+```
246
+
247
+## Parsing r8s output
248
+
249
+[r8s](http://loco.biosci.arizona.edu/r8s/) output contains 3 trees, namely `TREE`, `RATO` and `PHYLO` for time tree, rate tree and absolute substitution tree respectively.
250
+
251
+
252
+```{r fig.width=4, fig.height=6, width=60, warning=FALSE, fig.align="center")
253
+r8s <- read.r8s(system.file("extdata/r8s", "H3_r8s_output.log", package="ggtree"))
254
+ggtree(r8s, branch.length="TREE", mrsd="2014-01-01") + theme_tree2()
255
+```
256
+
257
+
258
+`branch.length` should be one of `TREE`, `RATO` or `PHYLO` for selecting the corresponding tree.
259
+
260
+User can also view 3 trees simultaneously.
261
+
262
+
263
+```{r fig.width=16, fig.height=10, width=60, warning=FALSE, fig.align="center")
264
+ggtree(get.tree(r8s), aes(color=.id)) + facet_wrap(~.id, scales="free_x")
265
+```
266
+
267
+## Parsing output of RAxML bootstraping analysis
268
+
269
+```{r fig.width=12, fig.height=10, width=60, warning=FALSE, fig.align="center"}
270
+raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="ggtree")
271
+raxml <- read.raxml(raxml_file)
272
+ggtree(raxml) + geom_label(aes(label=bootstrap, fill=bootstrap)) + geom_tiplab() +
273
+    scale_fill_continuous(low='darkgreen', high='red') + theme_tree2(legend.position='right')
274
+```
232 275
 
233 276
 
234 277
 ## Parsing NHX tree
... ...
@@ -245,6 +288,104 @@ ggtree(nhx) + geom_tiplab() + geom_point(aes(color=S), size=5, alpha=.5) +
245 288
 ```
246 289
 
247 290
 
291
+## Parsing EPA and pplacer output
292
+
293
+[EPA](http://sco.h-its.org/exelixis/web/software/epa/index.html)[@berger_EPA_2011] and [PPLACER](http://matsen.fhcrc.org/pplacer/)[@matsen_pplacer_2010] have common output file format, `jplace`. 
294
+
295
+```{r}
296
+jpf <- system.file("extdata/sample.jplace",  package="ggtree")
297
+jp <- read.jplace(jpf)
298
+print(jp)
299
+```
300
+
301
+In `r Githubpkg("GuangchuangYu/ggtree")`, we provide _`get.placements`_ method to access the placement.
302
+
303
+```{r}
304
+## get only best hit
305
+get.placements(jp, by="best")
306
+## get all placement
307
+get.placements(jp, by="all")
308
+```
309
+
310
+This is only a tiny sample file. In reality, [EPA](http://sco.h-its.org/exelixis/web/software/epa/index.html) and [PPLACER](http://matsen.fhcrc.org/pplacer/) may place thousands of short reads on a reference tree.
311
+
312
+
313
+We may, for example, count the number of placement and annotate this information in the tree. 
314
+
315
+
316
+# merging tree objects
317
+
318
+
319
+In ggtree, tree objects can be merged and evidences inferred from different phylogenetic analyses can be combined or compared and visualized.
320
+
321
+User can use the command like:
322
+```r
323
+tree <- merge_tree(tree_object_1, tree_object_2)
324
+## it's chainable, and serveral tree objects can be merged into one tree object
325
+tree <- merge_tree(tree_object_1, tree_object_2) %>% merge_tree(tree_object_3) %>% merge_tree(tree_object_4)
326
+```
327
+
328
+Here we use a tree analyzed by BEAST and CodeML and merge them into one.
329
+
330
+```{r}
331
+beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
332
+beast_tree <- read.beast(beast_file)
333
+
334
+rst_file <- system.file("examples/rst", package="ggtree")
335
+mlc_file <- system.file("examples/mlc", package="ggtree")
336
+codeml_tree <- read.codeml(rst_file, mlc_file)
337
+
338
+merged_tree <- merge_tree(beast_tree, codeml_tree)
339
+
340
+merged_tree
341
+head(fortify(merged_tree))
342
+```
343
+
344
+After merging, all evidences inferred from different tools can be used to annotate the tree simultaneously. In this example, we used 'dN/dS' inferred by CodeML to color the tree and annotate the tree with 'posterior' inferred by BEAST.
345
+
346
+```{r fig.width=20, fig.height=26, warning=FALSE}
347
+ggtree(merged_tree, aes(color=dN), mrsd="2013-01-01", ndigits = 3) +
348
+    geom_text(aes(label=posterior), vjust=.1, hjust=-.1, size=5, color="black") + 
349
+    scale_color_continuous(name='dN/dS', limits=c(0, 1.5),
350
+                           oob=scales::squish, low="darkgreen", high="red")+
351
+    theme_tree2(legend.position="right")
352
+```	
353
+
354
+
355
+# `jplace` file format
356
+The _`jplace`_ file format was defined by Masten[@matsen_format_2012] for phylogenetic placements. We employed this file format to store phylogenetic tree and user specific annotation data. Suppose we have a tree, and the associated data as shown below:
357
+
358
+```{r}
359
+tree <- system.file("extdata", "pa.nwk", package="ggtree")
360
+data <- read.csv(system.file("extdata", "pa_subs.csv", package="ggtree"), stringsAsFactor=FALSE)
361
+print(tree)
362
+head(data)
363
+```
364
+
365
+The _`data`_ contains amino acid substitutions from parent node to child node and GC contents of each node. We can annotate the tree as demonstrated in [User specific annotation](treeAnnotation.html#user-specific-annotation) session of [Tree Annotation](treeAnnotation.html) vignette.
366
+
367
+
368
+`r Githubpkg("GuangchuangYu/ggtree")` provides a function, _`write.jplace`_, to combine a tree and an associated data and store them to a single _`jplace`_ file.
369
+```{r}
370
+outfile <- tempfile()
371
+write.jplace(tree, data, outfile)
372
+```
373
+
374
+Then _`read.jplace`_ function was designed to read the _`jplace`_ file and store the information to a _`jplace`_ object.
375
+```{r}
376
+jp <- read.jplace(outfile)
377
+print(jp)
378
+```
379
+
380
+Now we know the _`jp`_ object stored the tree and the associated amino acid substitution and GC content information, we can view the tree and display the associated annotation data on it directly by _`ggtree`_.
381
+
382
+```{r fig.width=12, fig.height=12, warning=FALSE, fig.align="center"}
383
+ggtree(jp) + 
384
+    geom_text(aes(x=branch, label=subs), color="purple", vjust=-1, size=3) + 
385
+    geom_text(aes(label=gc), color="steelblue", hjust=-.6, size=3) +
386
+    geom_tiplab()
387
+```
388
+
248 389
 
249 390
 
250 391
 # References
... ...
@@ -16,6 +16,7 @@ output:
16 16
 vignette: >
17 17
   %\VignetteIndexEntry{03 Tree Manipulation}
18 18
   %\VignetteEngine{knitr::rmarkdown}
19
+  %\VignetteDepends{gridExtra}
19 20
   %\usepackage[utf8]{inputenc}
20 21
 ---
21 22
 
... ...
@@ -30,18 +31,37 @@ knitr::opts_chunk$set(tidy = FALSE,
30 31
 library("ape")
31 32
 library("ggplot2")
32 33
 library("ggtree")
34
+library("gridExtra")
33 35
 ```
34 36
 
37
+# Internal node number
35 38
 
39
+Some of the functions works with clade and accepts a parameter of internal node number. To get the internal node number, user can use `geom_text2` to display it:
40
+
41
+```{r}
42
+nwk <- system.file("extdata", "sample.nwk", package="ggtree")
43
+tree <- read.tree(nwk)
44
+ggtree(tree) + geom_text2(aes(subset=!isTip, label=node), hjust=-.3) + geom_tiplab()
45
+```