Browse code

new MRCA methods

Guangchuang Yu authored on 10/01/2019 13:18:05
Showing 6 changed files

... ...
@@ -1,5 +1,6 @@
1 1
 # Generated by roxygen2: do not edit by hand
2 2
 
3
+S3method(MRCA,gg)
3 4
 S3method(collapse,ggtree)
4 5
 S3method(fortify,multiPhylo)
5 6
 S3method(fortify,obkData)
... ...
@@ -18,7 +19,6 @@ export("%>%")
18 19
 export(.)
19 20
 export(Date2decimal)
20 21
 export(GeomHilight)
21
-export(MRCA)
22 22
 export(StatBalance)
23 23
 export(StatHilight)
24 24
 export(add_colorbar)
... ...
@@ -124,7 +124,6 @@ exportMethods(gzoom)
124 124
 exportMethods(scale_color)
125 125
 importFrom(ape,di2multi)
126 126
 importFrom(ape,extract.clade)
127
-importFrom(ape,getMRCA)
128 127
 importFrom(ape,ladderize)
129 128
 importFrom(ape,read.nexus)
130 129
 importFrom(ape,read.tree)
... ...
@@ -218,6 +217,7 @@ importFrom(rvcheck,get_fun_from_pkg)
218 217
 importFrom(scales,alpha)
219 218
 importFrom(tibble,data_frame)
220 219
 importFrom(tidyr,gather)
220
+importFrom(tidytree,MRCA)
221 221
 importFrom(tidytree,as_tibble)
222 222
 importFrom(tidytree,get_tree_data)
223 223
 importFrom(tidytree,groupClade)
... ...
@@ -1,3 +1,8 @@
1
+# ggtree 1.15.4
2
+
3
++ reimplement `MRCA` as a method inherited from `tidytree` (2019-01-10, Thu)
4
++ mv vignettes to [treedata-book](https://yulab-smu.github.io/treedata-book/) 
5
+
1 6
 # ggtree 1.15.3
2 7
 
3 8
 + move `reroot` method to `treeio` package and rename to `root` (2018-12-28, Fri)
4 9
deleted file mode 100644
... ...
@@ -1,51 +0,0 @@
1
-##' Find Most Recent Common Ancestor among a vector of tips
2
-##'
3
-##'
4
-##' @title MRCA
5
-##' @param obj supported tree object or ggplot object
6
-##' @param tip a vector of mode numeric or character specifying the tips
7
-##' @return MRCA of two or more tips
8
-##' @importFrom ape getMRCA
9
-##' @export
10
-##' @author Guangchuang Yu
11
-MRCA <- function(obj, tip) {
12
-    if (is(obj,"gg")) {
13
-        return(getMRCA.df(obj$data, tip))
14
-    }
15
-
16
-    getMRCA(as.phylo(obj), tip)
17
-}
18
-
19
-
20
-getMRCA.df <- function(data, tip) {
21
-    if (length(tip) <= 1)
22
-        return(NULL)
23
-
24
-    anc <- getMRCA.df_internal(data, tip[1], tip[2])
25
-    if (length(tip) == 2) {
26
-        return(anc)
27
-    }
28
-    for (i in 3:length(tip)) {
29
-        anc <- getMRCA.df_internal(data, tip[i], anc)
30
-    }
31
-    return(anc)
32
-}
33
-
34
-
35
-getMRCA.df_internal <- function(data, node1, node2) {
36
-    node1 <- which(node1 == data$label | node1 == data[, "node"])
37
-    node2 <- which(node2 == data$label | node2 == data[, "node"])
38
-
39
-    anc1 <- getAncestor.df(data, node1)
40
-    anc2 <- getAncestor.df(data, node2)
41
-
42
-    if (length(anc1) == 0L) {
43
-      warning("getMRCA.df_internal(): node1 is root")
44
-    }
45
-    if (length(anc2) == 0L) {
46
-      warning("getMRCA.df_internal(): node2 is root")
47
-    }
48
-
49
-    # Return common ancestors.
50
-    intersect(c(node1, anc1), c(node2, anc2))[1]
51
-}
52 0
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+##' @importFrom tidytree MRCA
2
+##' @method MRCA gg
3
+##' @export
4
+MRCA.gg <- function(.data, .node1, .node2, ...) {
5
+    MRCA(.data$data, .node1, .node2, ...)[["node"]]
6
+}
7
+
8
+
9
+## ##' Find Most Recent Common Ancestor among a vector of tips
10
+## ##'
11
+## ##'
12
+## ##' @title MRCA
13
+## ##' @param obj supported tree object or ggplot object
14
+## ##' @param tip a vector of mode numeric or character specifying the tips
15
+## ##' @return MRCA of two or more tips
16
+## ##' @importFrom ape getMRCA
17
+## ##' @export
18
+## ##' @author Guangchuang Yu
19
+## MRCA <- function(obj, tip) {
20
+##     if (is(obj,"gg")) {
21
+##         return(getMRCA.df(obj$data, tip))
22
+##     }
23
+
24
+##     getMRCA(as.phylo(obj), tip)
25
+## }
26
+
27
+
28
+## getMRCA.df <- function(data, tip) {
29
+##     if (length(tip) <= 1)
30
+##         return(NULL)
31
+
32
+##     anc <- getMRCA.df_internal(data, tip[1], tip[2])
33
+##     if (length(tip) == 2) {
34
+##         return(anc)
35
+##     }
36
+##     for (i in 3:length(tip)) {
37
+##         anc <- getMRCA.df_internal(data, tip[i], anc)
38
+##     }
39
+##     return(anc)
40
+## }
41
+
42
+
43
+## getMRCA.df_internal <- function(data, node1, node2) {
44
+##     node1 <- which(node1 == data$label | node1 == data[, "node"])
45
+##     node2 <- which(node2 == data$label | node2 == data[, "node"])
46
+
47
+##     anc1 <- getAncestor.df(data, node1)
48
+##     anc2 <- getAncestor.df(data, node2)
49
+
50
+##     if (length(anc1) == 0L) {
51
+##       warning("getMRCA.df_internal(): node1 is root")
52
+##     }
53
+##     if (length(anc2) == 0L) {
54
+##       warning("getMRCA.df_internal(): node2 is root")
55
+##     }
56
+
57
+##     # Return common ancestors.
58
+##     intersect(c(node1, anc1), c(node2, anc2))[1]
59
+## }
... ...
@@ -39,7 +39,13 @@ gzoom.phylo <- function(phy, focus, subtree=FALSE, widths=c(.3, .7)) {
39 39
 }
40 40
 
41 41
 gzoom.ggtree <- function(tree_view, focus, widths=c(.3, .7), xmax_adjust=0) {
42
-    node <- MRCA(tree_view, focus)
42
+    node <- MRCA(tree_view, focus[1], focus[2])
43
+    if (length(focus) > 2) {
44
+        for (i in 3:length(focus)) {
45
+            node <- MRCA(tree_view, focus[i], node)
46
+        }
47
+    }
48
+
43 49
     cpos <- get_clade_position(tree_view, node)
44 50
     p2 <- with(cpos, tree_view+
45 51
                      xlim(xmin, xmax+xmax_adjust)+
46 52
deleted file mode 100644
... ...
@@ -1,22 +0,0 @@
1
-% Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/MRCA.R
3
-\name{MRCA}
4
-\alias{MRCA}
5
-\title{MRCA}
6
-\usage{
7
-MRCA(obj, tip)
8
-}
9
-\arguments{
10
-\item{obj}{supported tree object or ggplot object}
11
-
12
-\item{tip}{a vector of mode numeric or character specifying the tips}
13
-}
14
-\value{
15
-MRCA of two or more tips
16
-}
17
-\description{
18
-Find Most Recent Common Ancestor among a vector of tips
19
-}
20
-\author{
21
-Guangchuang Yu
22
-}