Browse code

reroot method

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

g.yu authored on 07/08/2015 13:36:39
Showing 12 changed files

... ...
@@ -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: 1.1.14
4
+Version: 1.1.15
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
... ...
@@ -20,6 +20,7 @@ Imports:
20 20
     jsonlite,
21 21
     magrittr,
22 22
     methods,
23
+    phytools,
23 24
     reshape2,
24 25
     stats4
25 26
 Suggests:
... ...
@@ -65,6 +65,7 @@ export(read.hyphy)
65 65
 export(read.jplace)
66 66
 export(read.paml_rst)
67 67
 export(read.tree)
68
+export(reroot)
68 69
 export(rotate)
69 70
 export(rtree)
70 71
 export(scaleClade)
... ...
@@ -90,6 +91,7 @@ exportMethods(groupClade)
90 91
 exportMethods(groupOTU)
91 92
 exportMethods(gzoom)
92 93
 exportMethods(plot)
94
+exportMethods(reroot)
93 95
 exportMethods(scale_color)
94 96
 exportMethods(show)
95 97
 importFrom(Biostrings,GENETIC_CODE)
... ...
@@ -156,6 +158,7 @@ importFrom(magrittr,"%>%")
156 158
 importFrom(magrittr,add)
157 159
 importFrom(magrittr,equals)
158 160
 importFrom(methods,show)
161
+importFrom(phytools,reroot)
159 162
 importFrom(reshape2,melt)
160 163
 importFrom(stats4,plot)
161 164
 importMethodsFrom(Biostrings,width)
... ...
@@ -1,3 +1,7 @@
1
+CHANGES IN VERSION 1.1.15
2
+------------------------
3
+ o reroot methods for phylo and beast <2015-08-07, Fri>
4
+ 
1 5
 CHANGES IN VERSION 1.1.14
2 6
 ------------------------
3 7
  o update paml_rst to compatible with only marginal ancestral sequence or joint ancestral sequence available <2015-08-07, Fri>
... ...
@@ -22,12 +22,22 @@ as.binary <- function(tree, ...) {
22 22
 if ( !isGeneric("plot") )
23 23
 	setGeneric("plot", function(x, ...) standardGeneric("plot"))
24 24
 
25
+##' @docType methods
26
+##' @name reroot
27
+##' @rdname reroot-methods
28
+##' @title reroot method
29
+##' @param object one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object
30
+##' @param node internal nnode number
31
+##' @param ... additional parameter
32
+##' @return tree object
33
+##' @export
34
+setGeneric("reroot", function(object, node, ...) standardGeneric("reroot"))
25 35
 
26 36
 ##' @docType methods
27 37
 ##' @name get.tree
28 38
 ##' @rdname get.tree-methods
29 39
 ##' @title get.tree method
30
-##' @param object one of \code{jplace}, \code{beast} object
40
+##' @param object one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object
31 41
 ##' @param ... additional parameter
32 42
 ##' @return phylo object
33 43
 ##' @export
... ...
@@ -37,7 +47,7 @@ setGeneric("get.tree", function(object, ...) standardGeneric("get.tree"))
37 47
 ##' @name get.treetext
38 48
 ##' @rdname get.treetext-methods
39 49
 ##' @title get.treetext method
40
-##' @param object one of \code{jplace}, \code{beast} object
50
+##' @param object one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object
41 51
 ##' @param ... additional parameter
42 52
 ##' @return phylo object
43 53
 ##' @export
... ...
@@ -62,7 +72,7 @@ setGeneric("get.treeinfo", function(object, layout="phylogram", ladderize=TRUE,
62 72
 ##' @name get.fields
63 73
 ##' @rdname get.fields-methods
64 74
 ##' @title get.fields method
65
-##' @param object one of \code{jplace}, \code{beast} object
75
+##' @param object one of \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object
66 76
 ##' @param ... additional parameter
67 77
 ##' @return available annotation variables
68 78
 ##' @export
... ...
@@ -348,3 +348,17 @@ read.stats_beast <- function(file) {
348 348
 }
349 349
 
350 350
 
351
+##' @rdname reroot-methods
352
+##' @exportMethod reroot
353
+setMethod("reroot", signature(object="beast"),
354
+          function(object, node, ...) {
355
+              object@phylo <- reroot(object@phylo, node, ...)
356
+
357
+              node_map <- attr(object@phylo, "node_map")
358
+              idx <- match(object@stats$node, node_map[,1])
359
+              object@stats$node <- node_map[idx, 2]
360
+              
361
+              return(object)
362
+          })
363
+
364
+
... ...
@@ -1,3 +1,17 @@
1
+##' @rdname reroot-methods
2
+##' @exportMethod reroot
3
+setMethod("reroot", signature(object="phylo"),
4
+          function(object, node, ...) {
5
+              pos <- 0.5* object$edge.length[which(object$edge[,2] == node)]
6
+              tree <- phytools_reroot(object, node, pos)
7
+              attr(tree, "reroot") <- TRUE
8
+              node_map <- reroot_node_mapping(object, tree)
9
+              attr(tree, "node_map") <- node_map
10
+              return(tree)
11
+          })
12
+
13
+
14
+
1 15
 ##' @rdname get.tree-methods
2 16
 ##' @exportMethod get.tree
3 17
 setMethod("get.tree", signature(object="phylo"),
... ...
@@ -71,3 +85,7 @@ setMethod("gzoom", signature(object="phylo"),
71 85
           function(object, focus, subtree=FALSE, widths=c(.3, .7)) {
72 86
               gzoom.phylo(object, focus, subtree, widths)
73 87
           })
88
+
89
+
90
+##' @importFrom phytools reroot
91
+phytools_reroot <- phytools::reroot
... ...
@@ -1,3 +1,35 @@
1
+reroot_node_mapping <- function(tree, tree2) {
2
+    root <- getRoot(tree)
3
+
4
+    node_map <- data.frame(from=1:getNodeNum(tree), to=NA, visited=FALSE)
5
+    node_map[1:Ntip(tree), 2] <- match(tree$tip.label, tree2$tip.label)
6
+    node_map[1:Ntip(tree), 3] <- TRUE
7
+
8
+    node_map[root, 2] <- root
9
+    node_map[root, 3] <- TRUE
10
+
11
+    node <- rev(tree$edge[,2])
12
+    for (k in node) {
13
+        ip <- getParent(tree, k)
14
+        if (node_map[ip, "visited"])
15
+            next
16
+        
17
+        cc <- getChild(tree, ip)
18
+        node2 <- node_map[cc,2]
19
+        if (any(is.na(node2))) {
20
+            node <- c(node, k)
21
+            next
22
+        }
23
+        
24
+        to <- unique(sapply(node2, getParent, tr=tree2))
25
+        to <- to[! to %in% node_map[,2]]
26
+        node_map[ip, 2] <- to
27
+        node_map[ip, 3] <- TRUE
28
+    }
29
+    node_map <- node_map[, -3]
30
+    return(node_map)
31
+}
32
+
1 33
 
2 34
 ##' @importFrom colorspace rainbow_hcl
3 35
 scale_color_ <- function(phylo, by, low=NULL, high=NULL, na.color=NULL, default.color="darkgrey", interval=NULL) {
... ...
@@ -455,7 +455,7 @@ fortify.phylo <- function(model, data, layout="phylogram",
455 455
         df <- add_angle_cladogram(df)
456 456
     }
457 457
     aa <- names(attributes(tree))
458
-    group <- aa[ ! aa %in% c("names", "class", "order")]
458
+    group <- aa[ ! aa %in% c("names", "class", "order", "reroot", "node_map")]
459 459
     if (length(group) > 0) {
460 460
         ## groupOTU & groupClade
461 461
         group_info <- attr(tree, group)
... ...
@@ -26,7 +26,7 @@ get.fields(object, ...)
26 26
 \S4method{get.fields}{paml_rst}(object)
27 27
 }
28 28
 \arguments{
29
-\item{object}{one of \code{jplace}, \code{beast} object}
29
+\item{object}{one of \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object}
30 30
 
31 31
 \item{...}{additional parameter}
32 32
 }
... ...
@@ -29,7 +29,7 @@ get.tree(object, ...)
29 29
 \S4method{get.tree}{phylo}(object, ...)
30 30
 }
31 31
 \arguments{
32
-\item{object}{one of \code{jplace}, \code{beast} object}
32
+\item{object}{one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object}
33 33
 
34 34
 \item{...}{additional parameter}
35 35
 
... ...
@@ -11,7 +11,7 @@ get.treetext(object, ...)
11 11
 get.treetext(object, ...)
12 12
 }
13 13
 \arguments{
14
-\item{object}{one of \code{jplace}, \code{beast} object}
14
+\item{object}{one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object}
15 15
 
16 16
 \item{...}{additional parameter}
17 17
 }
18 18
new file mode 100644
... ...
@@ -0,0 +1,29 @@
1
+% Generated by roxygen2 (4.1.1): do not edit by hand
2
+% Please edit documentation in R/AllGenerics.R, R/beast.R, R/phylo.R
3
+\docType{methods}
4
+\name{reroot}
5
+\alias{reroot}
6
+\alias{reroot,beast-method}
7
+\alias{reroot,phylo-method}
8
+\title{reroot method}
9
+\usage{
10
+reroot(object, node, ...)
11
+
12
+\S4method{reroot}{beast}(object, node, ...)
13
+
14
+\S4method{reroot}{phylo}(object, node, ...)
15
+}
16
+\arguments{
17
+\item{object}{one of \code{phylo}, \code{jplace}, \code{beast}, \code{hyphy}, \code{codeml}, \code{codeml_mlc}, \code{paml_rst} object}
18
+
19
+\item{node}{internal nnode number}
20
+
21
+\item{...}{additional parameter}
22
+}
23
+\value{
24
+tree object
25
+}
26
+\description{
27
+reroot method
28
+}
29
+