Browse code

prettydoc

guangchuang yu authored on 09/12/2016 08:46:05
Showing 4 changed files

... ...
@@ -33,6 +33,7 @@ Suggests:
33 33
     EBImage,
34 34
     emojifont,
35 35
     knitr,
36
+    prettydoc,
36 37
     rmarkdown,
37 38
     scales,
38 39
     testthat
... ...
@@ -2,6 +2,7 @@
2 2
 
3 3
 S3method(as.binary,phylo)
4 4
 S3method(as.data.frame,phylo)
5
+S3method(as.data.frame,treedata)
5 6
 S3method(fortify,apeBootstrap)
6 7
 S3method(fortify,beast)
7 8
 S3method(fortify,codeml)
... ...
@@ -20,6 +21,7 @@ S3method(fortify,phylo4d)
20 21
 S3method(fortify,phyloseq)
21 22
 S3method(fortify,r8s)
22 23
 S3method(fortify,raxml)
24
+S3method(fortify,treedata)
23 25
 S3method(identify,gg)
24 26
 S3method(print,beastList)
25 27
 export("%+>%")
... ...
@@ -1,3 +1,90 @@
1
+##' @importFrom ggplot2 fortify
2
+##' @method fortify treedata
3
+##' @export
4
+fortify.treedata <- function(model, data, layout="rectangular", branch.length ="branch.length",
5
+                             ladderize=TRUE, right=FALSE, mrsd=NULL, ...) {
6
+    model <- set_branch_length(model, branch.length)
7
+    x <- reorder.phylo(get.tree(model), "postorder")
8
+    if (is.null(x$edge.length) || branch.length == "none") {
9
+        xpos <- getXcoord_no_length(x)
10
+    } else {
11
+        xpos <- getXcoord(x)
12
+    }
13
+    ypos <- getYcoord(x)
14
+    N <- Nnode(x, internal.only=FALSE)
15
+    xypos <- data.frame(node=1:N, x=xpos, y=ypos)
16
+
17
+    df <- as.data.frame(model, branch.length="branch.length") # already set by set_branch_length
18
+    idx <- is.na(df$parent)
19
+    df$parent[idx] <- df$node[idx]
20
+    rownames(df) <- df$node
21
+
22
+    res <- merge(df, xypos, by='node', all.y=TRUE)
23
+
24
+    ## add branch mid position
25
+    res <- calculate_branch_mid(res)
26
+
27
+    ## ## angle for all layout, if 'rectangular', user use coord_polar, can still use angle
28
+    res <- calculate_angle(res)
29
+    res
30
+}
31
+
32
+##' @method as.data.frame treedata
33
+##' @export
34
+## @importFrom treeio Nnode
35
+## @importFrom treeio Ntip
36
+as.data.frame.treedata <- function(x, row.names, optional, branch.length = "branch.length", ...) {
37
+    tree <- set_branch_length(x, branch.length)
38
+
39
+    ## res <- as.data.frame(tree@phylo)
40
+    res <- as.data.frame_(tree@phylo)
41
+    tree_anno <- get_tree_data(x)
42
+    if (nrow(tree_anno) > 0) {
43
+        res <- merge(res, tree_anno, by="node", all.x=TRUE)
44
+    }
45
+    return(res)
46
+}
47
+
48
+##@method as.data.frame phylo
49
+##@export
50
+as.data.frame_ <- function(x, row.names, optional, branch.length = "branch.length", ...) {
51
+    phylo <- x
52
+    ntip <- Ntip(phylo)
53
+    N <- Nnode(phylo, internal.only=FALSE)
54
+
55
+    tip.label <- phylo[["tip.label"]]
56
+    res <- as.data.frame(phylo[["edge"]])
57
+    colnames(res) <- c("parent", "node")
58
+    if (!is.null(phylo$edge.length))
59
+        res$branch.length <- phylo$edge.length
60
+
61
+    label <- rep(NA, N)
62
+    label[1:ntip] <- tip.label
63
+    if ( !is.null(phylo$node.label) ) {
64
+        label[(ntip+1):N] <- phylo$node.label
65
+    }
66
+    label.df <- data.frame(node=1:N, label=label)
67
+    res <- merge(res, label.df, by='node', all.y=TRUE)
68
+    isTip <- rep(FALSE, N)
69
+    isTip[1:ntip] <- TRUE
70
+    res$isTip <- isTip
71
+
72
+    return(res)
73
+}
74
+
75
+get_tree_data <- function(tree_object) {
76
+    tree_anno <- tree_object@data
77
+    if (has.extraInfo(tree_object)) {
78
+        if (nrow(tree_anno) > 0) {
79
+            tree_anno <- merge(tree_anno, tree_object@extraInfo, by="node")
80
+        } else {
81
+            return(tree_object@extraInfo)
82
+        }
83
+    }
84
+    return(tree_anno)
85
+}
86
+
87
+
1 88
 ##' convert tip or node label(s) to internal node number
2 89
 ##'
3 90
 ##'
... ...
@@ -8,9 +8,11 @@ author: "\\
8 8
 date: "`r Sys.Date()`"
9 9
 bibliography: ggtree.bib
10 10
 csl: nature.csl
11
-output: 
12
-  html_document:
11
+output:
12
+  prettydoc::html_pretty:
13 13
     toc: true
14
+    theme: cayman
15
+    highlight: github
14 16
   pdf_document:
15 17
     toc: true
16 18
 vignette: >
... ...
@@ -44,38 +46,43 @@ The `gheatmap` function is designed to visualize phylogenetic tree with heatmap
44 46
 
45 47
 In the following example, we visualized a tree of H3 influenza viruses with their associated genotype.
46 48
 
47
-```{r fig.width=20, fig.height=16, fig.align="center"}
49
+```{r fig.width=8, fig.height=6, fig.align="center", warning=FALSE, message=FALSE}
48 50
 beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
49 51
 beast_tree <- read.beast(beast_file)
50 52
 
51 53
 genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
52 54
 genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F)
53
-p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_treescale(x=2008, y=1)
54
-p <- p + geom_tiplab(size=3)
55
-gheatmap(p, genotype, offset = 2, width=0.5)
55
+colnames(genotype) <- sub("\\.$", "", colnames(genotype))
56
+p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_treescale(x=2008, y=1, offset=2)
57
+p <- p + geom_tiplab(size=2)
58
+gheatmap(p, genotype, offset = 5, width=0.5, font.size=3, colnames_angle=-45, hjust=0) +
59
+    scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"))
56 60
 ```
57 61
 
58 62
 The _width_ parameter is to control the width of the heatmap. It supports another parameter _offset_ for controlling the distance between the tree and the heatmap, for instance to allocate space for tip labels.
59 63
 
60 64
 
61
-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. 
65
+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.
62 66
 
63 67
 <!-- User can also use `gplot` and tweak the positions of two plot to align properly. -->
64 68
 
65
-```{r fig.width=20, fig.height=16, fig.align="center", warning=FALSE}
66
-p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_tiplab(size=3, align=TRUE) + theme_tree2()
69
+
70
+
71
+```{r fig.width=8, fig.height=6, fig.align="center", warning=FALSE}
72
+p <- ggtree(beast_tree, mrsd="2013-01-01") + geom_tiplab(size=2, align=TRUE, linesize=.5) + theme_tree2()
67 73
 pp <- (p + scale_y_continuous(expand=c(0, 0.3))) %>%
68
-    gheatmap(genotype, offset=4, width=0.5, colnames=FALSE) %>%
74
+    gheatmap(genotype, offset=8, width=0.6, colnames=FALSE) %>%
69 75
         scale_x_ggtree()
70 76
 pp + theme(legend.position="right")
71 77
 ```
72 78
 
79
+
73 80
 # Visualize tree with multiple sequence alignment
74 81
 
75 82
 With `msaplot` function, user can visualize multiple sequence alignment with phylogenetic tree, as demonstrated below:
76 83
 ```{r fig.width=8, fig.height=12, fig.align='center'}
77 84
 fasta <- system.file("examples/FluA_H3_AA.fas", package="ggtree")
78
-msaplot(ggtree(beast_tree), fasta) 
85
+msaplot(ggtree(beast_tree), fasta)
79 86
 ```
80 87
 
81 88
 A specific slice of the alignment can also be displayed by specific _window_ parameter.