brj1 authored on 06/12/2016 18:37:15
Showing 23 changed files

... ...
@@ -84,6 +84,7 @@ export(groupClade)
84 84
 export(groupOTU)
85 85
 export(gzoom)
86 86
 export(inset)
87
+export(is.ggtree)
87 88
 export(mask)
88 89
 export(merge_tree)
89 90
 export(msaplot)
... ...
@@ -1,5 +1,7 @@
1 1
 CHANGES IN VERSION 1.7.4
2 2
 ------------------------
3
+ o is.ggtree function to test whether object is produced by ggtree <2016-12-06, Tue>
4
+ o now branch.length can set to feature available in phylo4d@data and yscale is supported for phylo4d object <2016-12-06, Tue>
3 5
  o bug fixed of rm.singleton.newick, remove singleton parent instead of singleton <2016-12-01, Thu>
4 6
  o reorder phylo to postorder before ladderrize <2016-11-28, Mon>
5 7
  o allow yscale to use data stored in phylo4d object <2016-11-24, Thu>
... ...
@@ -1,6 +1,6 @@
1 1
 ##' drawing phylogenetic tree from phylo object
2 2
 ##'
3
-##' 
3
+##'
4 4
 ##' @title ggtree
5 5
 ##' @param tr phylo object
6 6
 ##' @param mapping aes mapping
... ...
@@ -43,13 +43,13 @@ ggtree <- function(tr,
43 43
                    branch.length  = "branch.length",
44 44
                    ndigits        = NULL,
45 45
                    ...) {
46
-    
46
+
47 47
     layout %<>% match.arg(c("rectangular", "slanted", "fan", "circular", "radial", "unrooted"))
48 48
 
49 49
     if (is(tr, "r8s") && branch.length == "branch.length") {
50 50
         branch.length = "TREE"
51 51
     }
52
-    
52
+
53 53
     if(yscale != "none") {
54 54
         ## for 2d tree
55 55
         layout <- "slanted"
... ...
@@ -76,20 +76,22 @@ ggtree <- function(tr,
76 76
     } else {
77 77
         multiPhylo <- FALSE
78 78
     }
79
-    
79
+
80 80
     p <- p + geom_tree(layout=layout, multiPhylo=multiPhylo, ...)
81 81
 
82 82
 
83 83
     p <- p + theme_tree()
84
-    
84
+
85 85
     if (layout == "circular" || layout == "radial") {
86 86
         p <- layout_circular(p)
87 87
         ## refer to: https://github.com/GuangchuangYu/ggtree/issues/6
88 88
         ## and also have some space for tree scale (legend)
89
-        p <- p + ylim(0, NA) 
89
+        p <- p + ylim(0, NA)
90 90
     } else if (layout == "fan") {
91 91
         p <- layout_fan(p, open.angle)
92 92
     }
93 93
 
94
+    class(p) <- c("ggtree", class(p))
95
+
94 96
     return(p)
95 97
 }
... ...
@@ -394,8 +394,11 @@ fortify.phylo4 <- function(model, data, layout="rectangular", yscale="none",
394 394
 ##' @method fortify phylo4d
395 395
 ##' @export
396 396
 fortify.phylo4d <- function(model, data, layout="rectangular", yscale="none",
397
-                            ladderize=TRUE, right=FALSE, mrsd=NULL, ...) {
398
-    res <- fortify.phylo4(model, data, layout, yscale="none", ladderize, right, mrsd, ...) # not apply yscale at this moment
397
+                            ladderize=TRUE, right=FALSE, branch.length="branch.length",
398
+                            mrsd=NULL, ...) {
399
+    phylo <- set_branch_length(model, branch.length)
400
+    res <- fortify(phylo, data, layout, branch.length=branch.length,
401
+                   ladderize, right, mrsd, ...)
399 402
     tdata <- model@data[match(res$node, rownames(model@data)), , drop=FALSE]
400 403
     df <- cbind(res, tdata)
401 404
     scaleY(as.phylo.phylo4(model), df, yscale, layout, ...)
... ...
@@ -685,9 +688,3 @@ fortify.phyloseq <- function(model, data, layout="rectangular",
685 688
 ##     ggplot(df) + geom_tree()
686 689
 
687 690
 ## }
688
-
689
-
690
-calculate_angle <- function(data) {
691
-    data$angle <- 360/(diff(range(data$y)) + 1) * data$y
692
-    return(data)
693
-}
... ...
@@ -65,7 +65,7 @@ setMethod("get.tree", signature(object="phylo"),
65 65
           function(object, ...) {
66 66
               return(object)
67 67
           })
68
-         
68
+        
69 69
 ##' @rdname get.tree-methods
70 70
 ##' @exportMethod get.tree
71 71
 setMethod("get.tree", signature(object="data.frame"),
... ...
@@ -85,4 +85,5 @@ setMethod("get.tree", signature(object="data.frame"),
85 85
               phylo$Nnode <- sum(!object[, "isTip"])
86 86
               class(phylo) <- "phylo"
87 87
               return(phylo)
88
-          })
89 88
\ No newline at end of file
89
+          })
90
+
... ...
@@ -15,19 +15,11 @@ setMethod("groupClade", signature(object="codeml"),
15 15
 
16 16
 ##' @rdname groupClade-methods
17 17
 ##' @exportMethod groupClade
18
-setMethod("groupClade", signature(object="gg"),
18
+setMethod("groupClade", signature(object="ggtree"),
19 19
           function(object, node, group_name) {
20
-              groupClade.ggplot(object, node, group_name)
20
+              groupClade.ggtree(object, node, group_name)
21 21
           })
22 22
 
23
-##' @rdname groupClade-methods
24
-##' @exportMethod groupClade
25
-setMethod("groupClade", signature(object="ggplot"),
26
-          function(object, node, group_name) {
27
-              groupClade.ggplot(object, node, group_name)
28
-          })
29
-
30
-
31 23
 ##' @rdname groupClade-methods
32 24
 ##' @exportMethod groupClade
33 25
 setMethod("groupClade", signature(object="jplace"),
... ...
@@ -38,7 +30,7 @@ setMethod("groupClade", signature(object="jplace"),
38 30
 
39 31
 ##' group selected clade
40 32
 ##'
41
-##' 
33
+##'
42 34
 ##' @rdname groupClade-methods
43 35
 ##' @exportMethod groupClade
44 36
 setMethod("groupClade", signature(object="nhx"),
... ...
@@ -73,7 +65,7 @@ groupClade.phylo <- function(object, node, group_name) {
73 65
             clade$tip.label
74 66
         })
75 67
     }
76
-    
68
+
77 69
     groupOTU.phylo(object, tips, group_name)
78 70
 }
79 71
 
... ...
@@ -88,7 +80,7 @@ groupClade_ <- function(object, node, group_name) {
88 80
 }
89 81
 
90 82
 
91
-groupClade.ggplot <- function(object, nodes, group_name) {
83
+groupClade.ggtree <- function(object, nodes, group_name) {
92 84
     df <- object$data
93 85
     df[, group_name] <- 0
94 86
     for (node in nodes) {
... ...
@@ -32,18 +32,12 @@ setMethod("groupOTU", signature(object="codeml_mlc"),
32 32
           }
33 33
           )
34 34
 
35
-##' @rdname groupOTU-methods
36
-##' @exportMethod groupOTU
37
-setMethod("groupOTU", signature(object="gg"),
38
-          function(object, focus, group_name, ...) {
39
-              groupOTU.ggplot(object, focus, group_name, ...)
40
-          })
41 35
 
42 36
 ##' @rdname groupOTU-methods
43 37
 ##' @exportMethod groupOTU
44
-setMethod("groupOTU", signature(object="ggplot"),
38
+setMethod("groupOTU", signature(object="ggtree"),
45 39
           function(object, focus, group_name="group", ...) {
46
-              groupOTU.ggplot(object, focus, group_name, ...)
40
+              groupOTU.ggtree(object, focus, group_name, ...)
47 41
           })
48 42
 
49 43
 
... ...
@@ -185,7 +179,7 @@ groupOTU_ <- function(object, focus, group_name, ...) {
185 179
 }
186 180
 
187 181
 
188
-groupOTU.ggplot <- function(object, focus, group_name, ...) {
182
+groupOTU.ggtree <- function(object, focus, group_name, ...) {
189 183
     df <- object$data
190 184
     df[, group_name] <- 0
191 185
     object$data <- groupOTU.df(df, focus, group_name, ...)
... ...
@@ -38,7 +38,7 @@ gzoom.phylo <- function(phy, focus, subtree=FALSE, widths=c(.3, .7)) {
38 38
     invisible(list(p1=p1, p2=p2))
39 39
 }
40 40
 
41
-gzoom.ggplot <- function(tree_view, focus, widths=c(.3, .7), xmax_adjust=0) {
41
+gzoom.ggtree <- function(tree_view, focus, widths=c(.3, .7), xmax_adjust=0) {
42 42
     node <- MRCA(tree_view, focus)
43 43
     cpos <- get_clade_position(tree_view, node)
44 44
     p2 <- with(cpos, tree_view+
... ...
@@ -51,9 +51,9 @@ gzoom.ggplot <- function(tree_view, focus, widths=c(.3, .7), xmax_adjust=0) {
51 51
 ##' @rdname gzoom-methods
52 52
 ##' @exportMethod gzoom
53 53
 ##' @param xmax_adjust adjust xmax (xlim[2])
54
-setMethod("gzoom", signature(object="gg"),
54
+setMethod("gzoom", signature(object="ggtree"),
55 55
           function(object, focus, widths=c(.3, .7), xmax_adjust=0) {
56
-              gzoom.ggplot(object, focus, widths, xmax_adjust)
56
+              gzoom.ggtree(object, focus, widths, xmax_adjust)
57 57
           })
58 58
 
59 59
 
60 60
similarity index 95%
61 61
rename from R/tree-utilities.R
62 62
rename to R/tidytree.R
... ...
@@ -1,6 +1,6 @@
1 1
 ##' convert tip or node label(s) to internal node number
2 2
 ##'
3
-##' 
3
+##'
4 4
 ##' @title nodeid
5 5
 ##' @param x tree object or graphic object return by ggtree
6 6
 ##' @param label tip or node label(s)
... ...
@@ -8,9 +8,9 @@
8 8
 ##' @export
9 9
 ##' @author Guangchuang Yu
10 10
 nodeid <- function(x, label) {
11
-    if (is(x, "gg")) 
11
+    if (is(x, "gg"))
12 12
         return(nodeid.gg(x, label))
13
-    
13
+
14 14
     nodeid.tree(x, label)
15 15
 }
16 16
 
... ...
@@ -40,14 +40,14 @@ reroot_node_mapping <- function(tree, tree2) {
40 40
         ip <- getParent(tree, k)
41 41
         if (node_map[ip, "visited"])
42 42
             next
43
-        
43
+
44 44
         cc <- getChild(tree, ip)
45 45
         node2 <- node_map[cc,2]
46 46
         if (anyNA(node2)) {
47 47
             node <- c(node, k)
48 48
             next
49 49
         }
50
-        
50
+
51 51
         to <- unique(sapply(node2, getParent, tr=tree2))
52 52
         to <- to[! to %in% node_map[,2]]
53 53
         node_map[ip, 2] <- to
... ...
@@ -75,7 +75,7 @@ layout.unrooted <- function(tree) {
75 75
     df[root, "start"] <- 0
76 76
     df[root, "end"]   <- 2
77 77
     df[root, "angle"] <- 0
78
-    
78
+
79 79
     nb.sp <- sapply(1:N, function(i) length(get.offspring.tip(tree, i)))
80 80
 
81 81
     nodes <- getNodes_by_postorder(tree)
... ...
@@ -83,22 +83,22 @@ layout.unrooted <- function(tree) {
83 83
     for(curNode in nodes) {
84 84
         curNtip <- nb.sp[curNode]
85 85
         children <- getChild(tree, curNode)
86
-        
86
+
87 87
         start <- df[curNode, "start"]
88 88
         end <- df[curNode, "end"]
89
-        
89
+
90 90
         if (length(children) == 0) {
91 91
             ## is a tip
92 92
             next
93 93
         }
94
-        
94
+
95 95
         for (i in seq_along(children)) {
96 96
             child <- children[i]
97 97
             ntip.child <- nb.sp[child]
98
-            
98
+
99 99
             alpha <- (end - start) * ntip.child/curNtip
100 100
             beta <- start + alpha / 2
101
-            
101
+
102 102
             length.child <- df[child, "length"]
103 103
             df[child, "x"] <- df[curNode, "x"] + cospi(beta) * length.child
104 104
             df[child, "y"] <- df[curNode, "y"] + sinpi(beta) * length.child
... ...
@@ -107,9 +107,9 @@ layout.unrooted <- function(tree) {
107 107
             df[child, "end"] <- start + alpha
108 108
             start <- start + alpha
109 109
         }
110
-        
110
+
111 111
     }
112
-    
112
+
113 113
     return(df)
114 114
 }
115 115
 
... ...
@@ -118,7 +118,7 @@ getParent.df <- function(df, node) {
118 118
     res <- df$parent[i]
119 119
     if (res == node) {
120 120
         ## root node
121
-        return(0) 
121
+        return(0)
122 122
     }
123 123
     return(res)
124 124
 }
... ...
@@ -165,10 +165,10 @@ get.offspring.df <- function(df, node) {
165 165
     return(sp)
166 166
 }
167 167
 
168
-    
168
+
169 169
 ##' extract offspring tips
170 170
 ##'
171
-##' 
171
+##'
172 172
 ##' @title get.offspring.tip
173 173
 ##' @param tr tree
174 174
 ##' @param node node
... ...
@@ -188,17 +188,17 @@ get.offspring.tip <- function(tr, node) {
188 188
 
189 189
 ##' calculate total number of nodes
190 190
 ##'
191
-##' 
191
+##'
192 192
 ##' @title getNodeNum
193 193
 ##' @param tr phylo object
194
-##' @return number 
194
+##' @return number
195 195
 ##' @author Guangchuang Yu
196 196
 ##' @export
197 197
 getNodeNum <- function(tr) {
198 198
     Ntip <- length(tr[["tip.label"]])
199 199
     Nnode <- tr[["Nnode"]]
200 200
     ## total nodes
201
-    N <- Ntip + Nnode 
201
+    N <- Ntip + Nnode
202 202
     return(N)
203 203
 }
204 204
 
... ...
@@ -233,7 +233,7 @@ getSibling <- function(tr, node) {
233 233
     if (node == root) {
234 234
         return(NA)
235 235
     }
236
-    
236
+
237 237
     parent <- getParent(tr, node)
238 238
     child <- getChild(tr, parent)
239 239
     sib <- child[child != node]
... ...
@@ -272,9 +272,9 @@ getNodeName <- function(tr) {
272 272
     return(nodeName)
273 273
 }
274 274
 
275
-##' get the root number 
275
+##' get the root number
276
+##'
276 277
 ##'
277
-##' 
278 278
 ##' @title getRoot
279 279
 ##' @param tr phylo object
280 280
 ##' @return root number
... ...
@@ -286,7 +286,7 @@ getRoot <- function(tr) {
286 286
     ## 2nd col is child,
287 287
     if (!is.null(attr(tr, "order")) && attr(tr, "order") == "postorder")
288 288
         return(edge[nrow(edge), 1])
289
-    
289
+
290 290
     parent <- unique(edge[,1])
291 291
     child <- unique(edge[,2])
292 292
     ## the node that has no parent should be the root
... ...
@@ -306,7 +306,7 @@ get.trunk <- function(tr) {
306 306
 
307 307
 ##' path from start node to end node
308 308
 ##'
309
-##' 
309
+##'
310 310
 ##' @title get.path
311 311
 ##' @param phylo phylo object
312 312
 ##' @param from start node
... ...
@@ -333,7 +333,7 @@ get.path_length <- function(phylo, from, to, weight=NULL) {
333 333
     if (is.null(weight)) {
334 334
         return(length(path)-1)
335 335
     }
336
-    
336
+
337 337
     df <- fortify(phylo)
338 338
     if ( ! (weight %in% colnames(df))) {
339 339
         stop("weight should be one of numerical attributes of the tree...")
... ...
@@ -350,7 +350,7 @@ get.path_length <- function(phylo, from, to, weight=NULL) {
350 350
         ee <- get_edge_index(df, path[i], path[i+1])
351 351
         res <- res + df[ee, weight]
352 352
     }
353
-    
353
+
354 354
     return(res)
355 355
 }
356 356
 
... ...
@@ -362,7 +362,7 @@ getNodes_by_postorder <- function(tree) {
362 362
 getXcoord2 <- function(x, root, parent, child, len, start=0, rev=FALSE) {
363 363
     x[root] <- start
364 364
     x[-root] <- NA  ## only root is set to start, by default 0
365
-        
365
+
366 366
     currentNode <- root
367 367
     direction <- 1
368 368
     if (rev == TRUE) {
... ...
@@ -395,12 +395,12 @@ getXcoord_no_length <- function(tr) {
395 395
     cl <- split(child, parent)
396 396
     child_list <- list()
397 397
     child_list[as.numeric(names(cl))] <- cl
398
-    
398
+
399 399
     while(anyNA(x)) {
400 400
         idx <- match(currentNode, child)
401 401
         pNode <- parent[idx]
402 402
         ## child number table
403
-        p1 <- table(parent[parent %in% pNode]) 
403
+        p1 <- table(parent[parent %in% pNode])
404 404
         p2 <- table(pNode)
405 405
         np <- names(p2)
406 406
         i <- p1[np] == p2
... ...
@@ -419,8 +419,8 @@ getXcoord_no_length <- function(tr) {
419 419
         currentNode <- unique(c(currentNode, newNode))
420 420
 
421 421
     }
422
-    x <- x - min(x) 
423
-    return(x)    
422
+    x <- x - min(x)
423
+    return(x)
424 424
 }
425 425
 
426 426
 
... ...
@@ -439,7 +439,7 @@ getXcoord <- function(tr) {
439 439
 }
440 440
 
441 441
 getXYcoord_slanted <- function(tr) {
442
-    
442
+
443 443
     edge <- tr$edge
444 444
     parent <- edge[,1]
445 445
     child <- edge[,2]
... ...
@@ -470,7 +470,7 @@ getYcoord <- function(tr, step=1) {
470 470
     cl <- split(child, parent)
471 471
     child_list <- list()
472 472
     child_list[as.numeric(names(cl))] <- cl
473
-    
473
+
474 474
     y <- numeric(N)
475 475
     tip.idx <- child[child <= Ntip]
476 476
     y[tip.idx] <- 1:Ntip * step
... ...
@@ -486,19 +486,19 @@ getYcoord <- function(tr, step=1) {
486 486
         ## idx <- sapply(pNode, function(i) all(child[parent == i] %in% currentNode))
487 487
         idx <- sapply(pNode, function(i) all(child_list[[i]] %in% currentNode))
488 488
         newNode <- pNode[idx]
489
-        
489
+
490 490
         y[newNode] <- sapply(newNode, function(i) {
491 491
             mean(y[child_list[[i]]], na.rm=TRUE)
492
-            ##child[parent == i] %>% y[.] %>% mean(na.rm=TRUE)           
492
+            ##child[parent == i] %>% y[.] %>% mean(na.rm=TRUE)
493 493
         })
494
-        
494
+
495 495
         currentNode <- c(currentNode[!currentNode %in% unlist(child_list[newNode])], newNode)
496 496
         ## currentNode <- c(currentNode[!currentNode %in% child[parent %in% newNode]], newNode)
497 497
         ## parent %in% newNode %>% child[.] %>%
498 498
         ##     `%in%`(currentNode, .) %>% `!` %>%
499 499
         ##         currentNode[.] %>% c(., newNode)
500 500
     }
501
-    
501
+
502 502
     return(y)
503 503
 }
504 504
 
... ...
@@ -507,7 +507,7 @@ getYcoord_scale <- function(tr, df, yscale) {
507 507
 
508 508
     N <- getNodeNum(tr)
509 509
     y <- numeric(N)
510
-    
510
+
511 511
     root <- getRoot(tr)
512 512
     y[root] <- 0
513 513
     y[-root] <- NA
... ...
@@ -539,7 +539,7 @@ getYcoord_scale <- function(tr, df, yscale) {
539 539
 
540 540
 getYcoord_scale2 <- function(tr, df, yscale) {
541 541
     root <- getRoot(tr)
542
-    
542
+
543 543
     pathLength <- sapply(1:length(tr$tip.label), function(i) {
544 544
         get.path_length(tr, i, root, yscale)
545 545
     })
... ...
@@ -566,7 +566,7 @@ getYcoord_scale2 <- function(tr, df, yscale) {
566 566
         } else {
567 567
             ordered_tip <- c(ordered_tip[1:ii],sib)
568 568
         }
569
-        
569
+
570 570
         ii <- ii + nn + 1
571 571
     }
572 572
 
... ...
@@ -576,20 +576,20 @@ getYcoord_scale2 <- function(tr, df, yscale) {
576 576
 
577 577
     N <- getNodeNum(tr)
578 578
     y <- numeric(N)
579
-    
579
+
580 580
     y[root] <- 0
581 581
     y[-root] <- NA
582 582
 
583 583
     ## yy <- df[, yscale]
584 584
     ## yy[is.na(yy)] <- 0
585
-    
585
+
586 586
     for (i in 2:length(long_branch)) {
587 587
         y[long_branch[i]] <- y[long_branch[i-1]] + df[long_branch[i], yscale]
588 588
     }
589
-    
589
+
590 590
     parent <- df[, "parent"]
591 591
     child <- df[, "node"]
592
-    
592
+
593 593
     currentNodes <- root
594 594
     while(anyNA(y)) {
595 595
         newNodes <- c()
... ...
@@ -618,14 +618,14 @@ getYcoord_scale2 <- function(tr, df, yscale) {
618 618
 getYcoord_scale_numeric <- function(tr, df, yscale, ...) {
619 619
     df <- .assign_parent_status(tr, df, yscale)
620 620
     df <- .assign_child_status(tr, df, yscale)
621
-    
621
+
622 622
     y <- df[, yscale]
623 623
 
624 624
     if (anyNA(y)) {
625 625
         warning("NA found in y scale mapping, all were setting to 0")
626 626
         y[is.na(y)] <- 0
627 627
     }
628
-    
628
+
629 629
     return(y)
630 630
 }
631 631
 
... ...
@@ -655,7 +655,7 @@ getYcoord_scale_numeric <- function(tr, df, yscale, ...) {
655 655
     if (!is.null(yscale_mapping)) {
656 656
         yy <- yscale_mapping[yy]
657 657
     }
658
-    
658
+
659 659
     na.idx <- which(is.na(yy))
660 660
     if (length(na.idx) > 0) {
661 661
         tree <- get.tree(tr)
... ...
@@ -680,7 +680,7 @@ getYcoord_scale_numeric <- function(tr, df, yscale, ...) {
680 680
 getYcoord_scale_category <- function(tr, df, yscale, yscale_mapping=NULL, ...) {
681 681
     if (is.null(yscale_mapping)) {
682 682
         stop("yscale is category variable, user should provide yscale_mapping,
683
-             which is a named vector, to convert yscale to numberical values...") 
683
+             which is a named vector, to convert yscale to numberical values...")
684 684
     }
685 685
     if (! is(yscale_mapping, "numeric") ||
686 686
         is.null(names(yscale_mapping))) {
... ...
@@ -694,11 +694,11 @@ getYcoord_scale_category <- function(tr, df, yscale, yscale_mapping=NULL, ...) {
694 694
             df[ii, yscale] <- df[ii, "node"]
695 695
         }
696 696
     }
697
-    
697
+
698 698
     ## assign to parent status is more prefer...
699 699
     df <- .assign_parent_status(tr, df, yscale)
700 700
     df <- .assign_child_status(tr, df, yscale, yscale_mapping)
701
-    
701
+
702 702
     y <- df[, yscale]
703 703
 
704 704
     if (anyNA(y)) {
... ...
@@ -733,37 +733,43 @@ calculate_branch_mid <- function(res) {
733 733
 
734 734
 
735 735
 set_branch_length <- function(tree_object, branch.length) {
736
-    phylo <- get.tree(tree_object)
737
-    
736
+    if (is(tree_object, "phylo4d")) {
737
+        phylo <- as.phylo.phylo4(tree_object)
738
+        d <- tree_object@data
739
+        tree_anno <- data.frame(node=rownames(d), d)
740
+    } else {
741
+        phylo <- get.tree(tree_object)
742
+    }
743
+
738 744
     if (branch.length %in%  c("branch.length", "none")) {
739 745
         return(phylo)
740 746
     }
741 747
 
742
-
743 748
     ## if (is(tree_object, "codeml")) {
744 749
     ##     tree_anno <- tree_object@mlc@dNdS
745 750
     ## } else
746
-    
751
+
747 752
     if (is(tree_object, "codeml_mlc")) {
748 753
         tree_anno <- tree_object@dNdS
749 754
     } else if (is(tree_object, "beast")) {
750 755
         tree_anno <- tree_object@stats
751 756
     }
757
+
752 758
     if (has.extraInfo(tree_object)) {
753 759
         tree_anno <- merge(tree_anno, tree_object@extraInfo, by.x="node", by.y="node")
754 760
     }
755 761
     cn <- colnames(tree_anno)
756 762
     cn <- cn[!cn %in% c('node', 'parent')]
757
-    
763
+
758 764
     length <- match.arg(branch.length, cn)
759 765
 
760 766
     if (all(is.na(as.numeric(tree_anno[, length])))) {
761 767
         stop("branch.length should be numerical attributes...")
762 768
     }
763
-    
769
+
764 770
     edge <- as.data.frame(phylo$edge)
765 771
     colnames(edge) <- c("parent", "node")
766
-    
772
+
767 773
     dd <- merge(edge, tree_anno,
768 774
                 by.x  = "node",
769 775
                 by.y  = "node",
... ...
@@ -772,7 +778,7 @@ set_branch_length <- function(tree_object, branch.length) {
772 778
     len <- unlist(dd[, length])
773 779
     len <- as.numeric(len)
774 780
     len[is.na(len)] <- 0
775
-    
781
+
776 782
     phylo$edge.length <- len
777 783
 
778 784
     return(phylo)
... ...
@@ -785,7 +791,7 @@ re_assign_ycoord_df <- function(df, currentNode) {
785 791
         idx <- sapply(pNode, function(i) with(df, all(node[parent == i & parent != node] %in% currentNode)))
786 792
         newNode <- pNode[idx]
787 793
         ## newNode <- newNode[is.na(df[match(newNode, df$node), "y"])]
788
-        
794
+
789 795
         df[match(newNode, df$node), "y"] <- sapply(newNode, function(i) {
790 796
             with(df, mean(y[parent == i], na.rm = TRUE))
791 797
         })
... ...
@@ -795,3 +801,20 @@ re_assign_ycoord_df <- function(df, currentNode) {
795 801
     return(df)
796 802
 }
797 803
 
804
+
805
+##' test whether input object is produced by ggtree function
806
+##'
807
+##'
808
+##' @title is.ggtree
809
+##' @param x object
810
+##' @return TRUE or FALSE
811
+##' @export
812
+##' @author guangchuang yu
813
+is.ggtree <- function(x) inherits(x, 'ggtree')
814
+
815
+
816
+
817
+calculate_angle <- function(data) {
818
+    data$angle <- 360/(diff(range(data$y)) + 1) * data$y
819
+    return(data)
820
+}
798 821
similarity index 74%
799 822
rename from R/NHX.R
800 823
rename to R/treeio.R
... ...
@@ -1,3 +1,13 @@
1
+filename <- function(file) {
2
+    ## textConnection(text_string) will work just like a file
3
+    ## in this case, just set the filename as ""
4
+    file_name <- ""
5
+    if (is.character(file)) {
6
+        file_name <- file
7
+    }
8
+    return(file_name)
9
+}
10
+
1 11
 ##' read nhx tree file
2 12
 ##'
3 13
 ##'
... ...
@@ -103,3 +113,47 @@ setMethod("get.fields", signature(object="nhx"),
103 113
               get.fields.tree(object)
104 114
           }
105 115
           )
116
+
117
+
118
+Ntip <- function(tree) {
119
+    phylo <- get.tree(tree)
120
+    length(phylo$tip.label)
121
+}
122
+
123
+Nnode <- function(tree, internal.only=TRUE) {
124
+    phylo <- get.tree(tree)
125
+    if (internal.only)
126
+        return(phylo$Nnode)
127
+
128
+    Ntip(phylo) + phylo$Nnode
129
+}
130
+
131
+
132
+has.extraInfo <- function(object) {
133
+    if (!is.tree(object)) {
134
+        return(FALSE)
135
+    }
136
+
137
+    if (! .hasSlot(object, "extraInfo")) {
138
+        return(FALSE)
139
+    }
140
+
141
+    extraInfo <- object@extraInfo
142
+
143
+    if (nrow(extraInfo) > 0) {
144
+        return(TRUE)
145
+    }
146
+
147
+    return(FALSE)
148
+}
149
+
150
+##' @importFrom methods .hasSlot is missingArg new slot slot<-
151
+has.slot <- function(object, slotName) {
152
+    if (!isS4(object)) {
153
+        return(FALSE)
154
+    }
155
+    .hasSlot(object, slotName)
156
+    ## slot <- tryCatch(slot(object, slotName), error=function(e) NULL)
157
+    ## ! is.null(slot)
158
+}
159
+
... ...
@@ -1,26 +1,6 @@
1
-Ntip <- function(tree) {
2
-    phylo <- get.tree(tree)
3
-    length(phylo$tip.label)
4
-}
5
-
6
-Nnode <- function(tree, internal.only=TRUE) {
7
-    phylo <- get.tree(tree)
8
-    if (internal.only)
9
-        return(phylo$Nnode)
10 1
 
11
-    Ntip(phylo) + phylo$Nnode
12
-}
13 2
 
14 3
 
15
-filename <- function(file) {
16
-    ## textConnection(text_string) will work just like a file
17
-    ## in this case, just set the filename as ""
18
-    file_name <- ""
19
-    if (is.character(file)) {
20
-        file_name <- file
21
-    }
22
-    return(file_name)
23
-}
24 4
 
25 5
 
26 6
 ##' @importFrom ggplot2 last_plot
... ...
@@ -32,15 +12,6 @@ get_tree_view <- function(tree_view) {
32 12
 }
33 13
 
34 14
 
35
-##' @importFrom methods .hasSlot is missingArg new slot slot<-
36
-has.slot <- function(object, slotName) {
37
-    if (!isS4(object)) {
38
-        return(FALSE)
39
-    }
40
-    .hasSlot(object, slotName)
41
-    ## slot <- tryCatch(slot(object, slotName), error=function(e) NULL)
42
-    ## ! is.null(slot)
43
-}
44 15
 
45 16
 has.field <- function(tree_object, field) {
46 17
     if ( ! field %in% get.fields(tree_object) ) {
... ...
@@ -80,24 +51,6 @@ has.field <- function(tree_object, field) {
80 51
     return(res)
81 52
 }
82 53
 
83
-has.extraInfo <- function(object) {
84
-    if (!is.tree(object)) {
85
-        return(FALSE)
86
-    }
87
-
88
-    if (! .hasSlot(object, "extraInfo")) {
89
-        return(FALSE)
90
-    }
91
-
92
-    extraInfo <- object@extraInfo
93
-
94
-    if (nrow(extraInfo) > 0) {
95
-        return(TRUE)
96
-    }
97
-
98
-    return(FALSE)
99
-}
100
-
101 54
 append_extraInfo <- function(df, object) {
102 55
     if (has.extraInfo(object)) {
103 56
         info <- object@extraInfo
... ...
@@ -4,7 +4,7 @@ ggtree: an R package for visualization and annotation of phylogenetic trees with
4 4
 
5 5
 [![releaseVersion](https://img.shields.io/badge/release%20version-1.6.4-green.svg?style=flat)](https://bioconductor.org/packages/ggtree) [![develVersion](https://img.shields.io/badge/devel%20version-1.7.4-green.svg?style=flat)](https://github.com/GuangchuangYu/ggtree) [![Bioc](http://www.bioconductor.org/shields/years-in-bioc/ggtree.svg)](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#since) [![total](https://img.shields.io/badge/downloads-16878/total-blue.svg?style=flat)](https://bioconductor.org/packages/stats/bioc/ggtree) [![month](https://img.shields.io/badge/downloads-1621/month-blue.svg?style=flat)](https://bioconductor.org/packages/stats/bioc/ggtree) <img src="logo.png" align="right" />
6 6
 
7
-[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) [![codecov](https://codecov.io/gh/GuangchuangYu/ggtree/branch/master/graph/badge.svg)](https://codecov.io/gh/GuangchuangYu/ggtree) [![Last-changedate](https://img.shields.io/badge/last%20change-2016--12--01-green.svg)](https://github.com/GuangchuangYu/ggtree/commits/master) [![GitHub forks](https://img.shields.io/github/forks/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/network) [![GitHub stars](https://img.shields.io/github/stars/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/stargazers) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://awesome-r.com/#awesome-r-graphic-displays)
7
+[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) [![codecov](https://codecov.io/gh/GuangchuangYu/ggtree/branch/master/graph/badge.svg)](https://codecov.io/gh/GuangchuangYu/ggtree) [![Last-changedate](https://img.shields.io/badge/last%20change-2016--12--06-green.svg)](https://github.com/GuangchuangYu/ggtree/commits/master) [![GitHub forks](https://img.shields.io/github/forks/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/network) [![GitHub stars](https://img.shields.io/github/stars/GuangchuangYu/ggtree.svg)](https://github.com/GuangchuangYu/ggtree/stargazers) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://awesome-r.com/#awesome-r-graphic-displays)
8 8
 
9 9
 [![platform](http://www.bioconductor.org/shields/availability/devel/ggtree.svg)](https://www.bioconductor.org/packages/devel/bioc/html/ggtree.html#archives) [![Build Status](http://www.bioconductor.org/shields/build/devel/bioc/ggtree.svg)](https://bioconductor.org/checkResults/devel/bioc-LATEST/ggtree/) [![Linux/Mac Travis Build Status](https://img.shields.io/travis/GuangchuangYu/ggtree/master.svg?label=Mac%20OSX%20%26%20Linux)](https://travis-ci.org/GuangchuangYu/ggtree) [![AppVeyor Build Status](https://img.shields.io/appveyor/ci/Guangchuangyu/ggtree/master.svg?label=Windows)](https://ci.appveyor.com/project/GuangchuangYu/ggtree) [![install with bioconda](https://img.shields.io/badge/install%20with-bioconda-green.svg?style=flat)](http://bioconda.github.io/recipes/bioconductor-ggtree/README.html)
10 10
 
... ...
@@ -18,7 +18,7 @@ Please cite the following article when using `ggtree`:
18 18
 
19 19
 **G Yu**, DK Smith, H Zhu, Y Guan, TTY Lam<sup>\*</sup>. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. ***Methods in Ecology and Evolution***. *accepted*
20 20
 
21
-[![doi](https://img.shields.io/badge/doi-10.1111/2041--210X.12628-green.svg?style=flat)](http://dx.doi.org/10.1111/2041-210X.12628) [![citation](https://img.shields.io/badge/cited%20by-1-green.svg?style=flat)](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627) [![Altmetric](https://img.shields.io/badge/Altmetric-274-green.svg?style=flat)](https://www.altmetric.com/details/10533079)
21
+[![doi](https://img.shields.io/badge/doi-10.1111/2041--210X.12628-green.svg?style=flat)](http://dx.doi.org/10.1111/2041-210X.12628) [![citation](https://img.shields.io/badge/cited%20by-1-green.svg?style=flat)](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627) [![Altmetric](https://img.shields.io/badge/Altmetric-284-green.svg?style=flat)](https://www.altmetric.com/details/10533079)
22 22
 
23 23
 ------------------------------------------------------------------------
24 24
 
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/AllGenerics.R, R/NHX.R, R/RAxML.R, R/ape.R, R/beast.R, R/codeml.R, R/codeml_mlc.R, R/hyphy.R, R/jplace.R, R/paml_rst.R, R/phangorn.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/paml_rst.R, R/phangorn.R, R/r8s.R, R/treeio.R
3 3
 \docType{methods}
4 4
 \name{get.fields}
5 5
 \alias{get.fields}
... ...
@@ -18,8 +18,6 @@
18 18
 \usage{
19 19
 get.fields(object, ...)
20 20
 
21
-\S4method{get.fields}{nhx}(object, ...)
22
-
23 21
 \S4method{get.fields}{raxml}(object, ...)
24 22
 
25 23
 \S4method{get.fields}{apeBootstrap}(object, ...)
... ...
@@ -39,6 +37,8 @@ get.fields(object, ...)
39 37
 \S4method{get.fields}{phangorn}(object, ...)
40 38
 
41 39
 \S4method{get.fields}{r8s}(object, ...)
40
+
41
+\S4method{get.fields}{nhx}(object, ...)
42 42
 }
43 43
 \arguments{
44 44
 \item{object}{one of \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/tree-utilities.R
2
+% Please edit documentation in R/tidytree.R
3 3
 \name{get.offspring.tip}
4 4
 \alias{get.offspring.tip}
5 5
 \title{get.offspring.tip}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/tree-utilities.R
2
+% Please edit documentation in R/tidytree.R
3 3
 \name{get.path}
4 4
 \alias{get.path}
5 5
 \title{get.path}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/tree-utilities.R
2
+% Please edit documentation in R/tidytree.R
3 3
 \name{getNodeNum}
4 4
 \alias{getNodeNum}
5 5
 \title{getNodeNum}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/tree-utilities.R
2
+% Please edit documentation in R/tidytree.R
3 3
 \name{getRoot}
4 4
 \alias{getRoot}
5 5
 \title{getRoot}
... ...
@@ -7,8 +7,7 @@
7 7
 \alias{groupClade,beast-method}
8 8
 \alias{groupClade,codeml-method}
9 9
 \alias{groupClade,codeml_mlc-method}
10
-\alias{groupClade,gg-method}
11
-\alias{groupClade,ggplot-method}
10
+\alias{groupClade,ggtree-method}
12 11
 \alias{groupClade,hyphy-method}
13 12
 \alias{groupClade,jplace-method}
14 13
 \alias{groupClade,nhx-method}
... ...
@@ -34,9 +33,7 @@ groupClade(object, node, group_name = "group", ...)
34 33
 
35 34
 \S4method{groupClade}{codeml}(object, node, group_name = "group")
36 35
 
37
-\S4method{groupClade}{gg}(object, node, group_name)
38
-
39
-\S4method{groupClade}{ggplot}(object, node, group_name)
36
+\S4method{groupClade}{ggtree}(object, node, group_name)
40 37
 
41 38
 \S4method{groupClade}{jplace}(object, node, group_name = "group")
42 39
 
... ...
@@ -7,8 +7,7 @@
7 7
 \alias{groupOTU,beast-method}
8 8
 \alias{groupOTU,codeml-method}
9 9
 \alias{groupOTU,codeml_mlc-method}
10
-\alias{groupOTU,gg-method}
11
-\alias{groupOTU,ggplot-method}
10
+\alias{groupOTU,ggtree-method}
12 11
 \alias{groupOTU,hyphy-method}
13 12
 \alias{groupOTU,jplace-method}
14 13
 \alias{groupOTU,nhx-method}
... ...
@@ -34,9 +33,7 @@ groupOTU(object, focus, group_name = "group", ...)
34 33
 
35 34
 \S4method{groupOTU}{codeml_mlc}(object, focus, group_name = "group", ...)
36 35
 
37
-\S4method{groupOTU}{gg}(object, focus, group_name = "group", ...)
38
-
39
-\S4method{groupOTU}{ggplot}(object, focus, group_name = "group", ...)
36
+\S4method{groupOTU}{ggtree}(object, focus, group_name = "group", ...)
40 37
 
41 38
 \S4method{groupOTU}{jplace}(object, focus, group_name = "group", ...)
42 39
 
... ...
@@ -7,7 +7,7 @@
7 7
 \alias{gzoom,beast-method}
8 8
 \alias{gzoom,codeml-method}
9 9
 \alias{gzoom,codeml_mlc-method}
10
-\alias{gzoom,gg-method}
10
+\alias{gzoom,ggtree-method}
11 11
 \alias{gzoom,hyphy-method}
12 12
 \alias{gzoom,nhx-method}
13 13
 \alias{gzoom,paml_rst-method}
... ...
@@ -28,7 +28,8 @@ gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7), ...)
28 28
 \S4method{gzoom}{hyphy}(object, focus, subtree = FALSE, widths = c(0.3,
29 29
   0.7))
30 30
 
31
-\S4method{gzoom}{gg}(object, focus, widths = c(0.3, 0.7), xmax_adjust = 0)
31
+\S4method{gzoom}{ggtree}(object, focus, widths = c(0.3, 0.7),
32
+  xmax_adjust = 0)
32 33
 
33 34
 \S4method{gzoom}{apeBootstrap}(object, focus, subtree = FALSE,
34 35
   widths = c(0.3, 0.7))
35 36
new file mode 100644
... ...
@@ -0,0 +1,21 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/tidytree.R
3
+\name{is.ggtree}
4
+\alias{is.ggtree}
5
+\title{is.ggtree}
6
+\usage{
7
+is.ggtree(x)
8
+}
9
+\arguments{
10
+\item{x}{object}
11
+}
12
+\value{
13
+TRUE or FALSE
14
+}
15
+\description{
16
+test whether input object is produced by ggtree function
17
+}
18
+\author{
19
+guangchuang yu
20
+}
21
+
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/tree-utilities.R
2
+% Please edit documentation in R/tidytree.R
3 3
 \name{nodeid}
4 4
 \alias{nodeid}
5 5
 \title{nodeid}
... ...
@@ -1,5 +1,5 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/NHX.R
2
+% Please edit documentation in R/treeio.R
3 3
 \name{read.nhx}
4 4
 \alias{read.nhx}
5 5
 \title{read.nhx}