Browse code

branch.length support phylo4d@data

guangchuang yu authored on 06/12/2016 05:42:11
Showing 5 changed files

... ...
@@ -1,5 +1,6 @@
1 1
 CHANGES IN VERSION 1.7.4
2 2
 ------------------------
3
+ o now branch.length can set to feature available in phylo4d@data and yscale is supported for phylo4d object <2016-12-06, Tue>
3 4
  o bug fixed of rm.singleton.newick, remove singleton parent instead of singleton <2016-12-01, Thu>
4 5
  o reorder phylo to postorder before ladderrize <2016-11-28, Mon>
5 6
  o allow yscale to use data stored in phylo4d object <2016-11-24, Thu>
... ...
@@ -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, ...)
... ...
@@ -65,3 +65,6 @@ setMethod("get.tree", signature(object="phylo"),
65 65
           function(object, ...) {
66 66
               return(object)
67 67
           })
68
+
69
+
70
+
... ...
@@ -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
         })
... ...
@@ -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