Browse code

Commit made by the Bioconductor Git-SVN bridge. Consists of 1 commit.

Commit information:

Commit id: 3e7dd00e6ef0bc44a664112acb5af61a845bea2d

support order nodes by yscale <2015-02-03, Tue>

Committed by: GuangchuangYu
Author Name: GuangchuangYu
Commit date: 2015-02-03 16:41:33 +0800
Author date: 2015-02-03 16:41:33 +0800


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

g.yu authored on 03/02/2015 08:41:54
Showing 4 changed files

  • DESCRIPTION index 89fdba92..7f658e2d 100644
  • NEWS index 4b225b7d..705f9371 100644
  • R/tree.R index 3c9c4251..f679534e 100644
  • R/treeIO.R index f340f380..fa019ff0 100644
... ...
@@ -1,7 +1,7 @@
1 1
 Package: ggtree
2 2
 Type: Package
3 3
 Title: a phylogenetic tree viewer for different types of tree annotations
4
-Version: 0.99.9
4
+Version: 0.99.10
5 5
 Author: Guangchuang Yu
6 6
 Maintainer: Guangchuang Yu <guangchuangyu@gmail.com>
7 7
 Description: ggtree extends the ggplot2 plotting system which implemented the
... ...
@@ -1,3 +1,7 @@
1
+CHANGES IN VERSION 0.99.10
2
+------------------------
3
+ o support order nodes by yscale <2015-02-03, Tue>
4
+
1 5
 CHANGES IN VERSION 0.99.9
2 6
 ------------------------
3 7
  o update vignette <2015-02-02, Mon>
... ...
@@ -255,6 +255,33 @@ getChild <- function(tr, node) {
255 255
     return(res)
256 256
 }
257 257
 
258
+getSibling <- function(tr, node) {
259
+    root <- getRoot(tr)
260
+    if (node == root) {
261
+        return(NA)
262
+    }
263
+    
264
+    parent <- getParent(tr, node)
265
+    child <- getChild(tr, parent)
266
+    sib <- child[child != node]
267
+    return(sib)
268
+}
269
+
270
+
271
+getAncestor <- function(tr, node) {
272
+    root <- getRoot(tr)
273
+    if (node == root) {
274
+        return(NA)
275
+    }
276
+    parent <- getParent(tr, node)
277
+    res <- parent
278
+    while(parent != root) {
279
+        parent <- getParent(tr, parent)
280
+        res <- c(res, parent)
281
+    }
282
+    return(res)
283
+}
284
+
258 285
 isRoot <- function(tr, node) {
259 286
     getRoot(tr) == node
260 287
 }
... ...
@@ -286,6 +313,30 @@ getRoot <- function(tr) {
286 313
     return(root)
287 314
 }
288 315
 
316
+get.path_length <- function(df, from, to, weight=NULL) {
317
+    ## df is output of fortify(tree)
318
+    ## from and to are nodes
319
+    ## weight is a numeric column of df
320
+    res <- 0
321
+    if (from == to) {
322
+        return(res)
323
+    }
324
+    
325
+    if (is.null(weight)) {
326
+        res <- res + 1
327
+    } else {
328
+        res <- res + df[from, weight]
329
+    }
330
+
331
+    newNode <- df[from, "parent"]
332
+    if (newNode == to) {
333
+        return(res)
334
+    } else {
335
+        res <- res + get.path_length(df, newNode, to, weight)
336
+    }
337
+    return(res)
338
+}
339
+
289 340
 getNodes_by_postorder <- function(tree) {
290 341
     tree <- reorder.phylo(tree, "postorder")
291 342
     nodes <- tree$edge[,c(2,1)] %>% t %>%
... ...
@@ -418,7 +469,8 @@ getYcoord <- function(tr, step=1) {
418 469
 }
419 470
 
420 471
 
421
-getYcoord_scale <- function(tr, yscale) {
472
+getYcoord_scale <- function(tr, df, yscale) {
473
+
422 474
     N <- getNodeNum(tr)
423 475
     y <- numeric(N)
424 476
     
... ...
@@ -429,7 +481,7 @@ getYcoord_scale <- function(tr, yscale) {
429 481
     edge <- tr$edge
430 482
     parent <- edge[,1]
431 483
     child <- edge[,2]
432
-    
484
+
433 485
     currentNodes <- root
434 486
     while(any(is.na(y))) {
435 487
         newNodes <- c()
... ...
@@ -438,12 +490,94 @@ getYcoord_scale <- function(tr, yscale) {
438 490
             newNode <- child[idx]
439 491
             direction <- -1
440 492
             for (i in seq_along(newNode)) {
441
-                y[newNode[i]] <- y[currentNode] + yscale[newNode[i]] * direction
493
+                y[newNode[i]] <- y[currentNode] + df[newNode[i], yscale] * direction
442 494
                 direction <- -1 * direction
443 495
             }
444 496
             newNodes <- c(newNodes, newNode)
445 497
         }
446 498
         currentNodes <- unique(newNodes)
447 499
     }
500
+    if (min(y) < 0) {
501
+        y <- y + abs(min(y))
502
+    }
503
+    return(y)
504
+}
505
+
506
+getYcoord_scale2 <- function(tr, df, yscale) {
507
+
508
+    root <- getRoot(tr)
509
+    
510
+    pathLength <- sapply(1:length(tr$tip.label), function(i) {
511
+        get.path_length(df, i, root, yscale)
512
+    })
513
+
514
+    ordered_tip <- order(pathLength, decreasing = TRUE)
515
+    ii <- 1
516
+    ntip <- length(ordered_tip)
517
+    while(ii < ntip) {
518
+        sib <- getSibling(tr, ordered_tip[ii])
519
+        if (length(sib) == 0) {
520
+            ii <- ii + 1
521
+            next
522
+        }
523
+        jj <- which(ordered_tip %in% sib)
524
+        if (length(jj) == 0) {
525
+            ii <- ii + 1
526
+            next
527
+        }
528
+        sib <- ordered_tip[jj]
529
+        ordered_tip <- ordered_tip[-jj]
530
+        nn <- length(sib)
531
+        if (ii < length(ordered_tip)) {
532
+            ordered_tip <- c(ordered_tip[1:ii],sib, ordered_tip[(ii+1):length(ordered_tip)])
533
+        } else {
534
+            ordered_tip <- c(ordered_tip[1:ii],sib)
535
+        }
536
+        
537
+        ii <- ii + nn + 1
538
+    }
539
+
540
+
541
+    long_branch <- getAncestor(tr, ordered_tip[1]) %>% rev
542
+    long_branch <- c(long_branch, ordered_tip[1])
543
+
544
+    N <- getNodeNum(tr)
545
+    y <- numeric(N)
546
+    
547
+    y[root] <- 0
548
+    y[-root] <- NA
549
+
550
+    ## yy <- df[, yscale]
551
+    ## yy[is.na(yy)] <- 0
552
+    
553
+    for (i in 2:length(long_branch)) {
554
+        y[long_branch[i]] <- y[long_branch[i-1]] + df[long_branch[i], yscale]
555
+    }
556
+    
557
+    parent <- df[, "parent"]
558
+    child <- df[, "node"]
559
+    
560
+    currentNodes <- root
561
+    while(any(is.na(y))) {
562
+        newNodes <- c()
563
+        for (currentNode in currentNodes) {
564
+            idx <- which(parent %in% currentNode)
565
+            newNode <- child[idx]
566
+            newNode <- c(newNode[! newNode %in% ordered_tip],
567
+                         rev(ordered_tip[ordered_tip %in% newNode]))
568
+            direction <- -1
569
+            for (i in seq_along(newNode)) {
570
+                if (is.na(y[newNode[i]])) {
571
+                    y[newNode[i]] <- y[currentNode] + df[newNode[i], yscale] * direction
572
+                    direction <- -1 * direction
573
+                }
574
+            }
575
+            newNodes <- c(newNodes, newNode)
576
+        }
577
+        currentNodes <- unique(newNodes)
578
+    }
579
+    if (min(y) < 0) {
580
+        y <- y + abs(min(y))
581
+    }
448 582
     return(y)
449 583
 }
... ...
@@ -185,7 +185,7 @@ fortify.beast <- function(model, data,
185 185
     stats <- stats[,colnames(stats) != "node"]
186 186
     
187 187
     df <- cbind(df, stats)
188
-    scaleY(phylo, df, yscale)
188
+    scaleY(phylo, df, yscale, ...)
189 189
 }
190 190
 
191 191
 
... ...
@@ -225,7 +225,7 @@ fortify.codeml <- function(model, data,
225 225
     
226 226
     res <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
227 227
     df <- merge_phylo_anno.paml_rst(res, model@rst)
228
-    scaleY(phylo, df, yscale)
228
+    scaleY(phylo, df, yscale, ...)
229 229
 }
230 230
 
231 231
 
... ...
@@ -248,7 +248,7 @@ fortify.codeml_mlc <- function(model, data,
248 248
     dNdS <- model@dNdS
249 249
 
250 250
     df <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
251
-    scaleY(phylo, df, yscale)
251
+    scaleY(phylo, df, yscale, ...)
252 252
 }
253 253
 
254 254
 merge_phylo_anno.codeml_mlc <- function(df, dNdS, ndigits = NULL) {
... ...
@@ -302,7 +302,7 @@ fortify.paml_rst <- function(model, data, layout = "phylogram", yscale="none",
302 302
                              ladderize=TRUE, right=FALSE, ...) {
303 303
     df <- fortify.phylo(model@phylo, data, layout, ladderize, right, ...)
304 304
     df <- merge_phylo_anno.paml_rst(df, model)
305
-    scaleY(model@phylo, df, yscale)
305
+    scaleY(model@phylo, df, yscale, ...)
306 306
 }
307 307
 
308 308
 merge_phylo_anno.paml_rst <- function(df, model) {
... ...
@@ -329,10 +329,10 @@ fortify.jplace <- function(model, data,
329 329
     df <- get.treeinfo(model, layout, ladderize, right, ...)
330 330
     place <- get.placements(model, by="best")
331 331
     df <- df %add2% place
332
-    scaleY(model@phylo, df, yscale)
332
+    scaleY(model@phylo, df, yscale, ...)
333 333
 }
334 334
 
335
-scaleY <- function(phylo, df, yscale) {
335
+scaleY <- function(phylo, df, yscale, order.y = TRUE) {
336 336
     if (yscale == "none") {
337 337
         return(df)
338 338
     }
... ...
@@ -344,7 +344,13 @@ scaleY <- function(phylo, df, yscale) {
344 344
         warning("yscale should be numeric...\n")
345 345
         return(df)
346 346
     }
347
-    y <- getYcoord_scale(phylo, df[, yscale])
347
+
348
+    if (order.y) {
349
+        y <- getYcoord_scale2(phylo, df, yscale)
350
+    } else {
351
+        y <- getYcoord_scale(phylo, df, yscale)
352
+    }
353
+    
348 354
     df[, "y"] <- y
349 355
     return(df)
350 356
 }