Browse code

unify the fortify for pvclust and hclust class

xiangpin authored on 19/08/2022 07:06:16
Showing 1 changed files
... ...
@@ -68,6 +68,7 @@ fortify.phylo <- function(model, data,
68 68
         res <- calculate_angle(res)
69 69
     }
70 70
     res <- scaleY(as.phylo(model), res, yscale, layout, ...)
71
+    res <- adjust_hclust_tip.edge.len(res, x)
71 72
     class(res) <- c("tbl_tree", class(res))
72 73
     attr(res, "layout") <- layout
73 74
     return(res)
... ...
@@ -148,7 +149,6 @@ fortify.phylo4 <- function(model, data,
148 149
 
149 150
     df <- fortify.phylo(phylo, data,
150 151
                         layout, ladderize, right, mrsd=mrsd, ...)
151
-    df <- adjust_hclust_tip.edge.len(df, phylo)
152 152
     scaleY(phylo, df, yscale, layout, ...)
153 153
 }
154 154
 
... ...
@@ -202,7 +202,6 @@ fortify.phylo4d <- function(model, data,
202 202
                             ...) {
203 203
     model <- as.treedata(model, hang = hang)
204 204
     df <- fortify(model, data, layout, yscale, ladderize, right, branch.length, mrsd, ...)
205
-    df <- adjust_hclust_tip.edge.len(df, model@phylo)
206 205
     return (df)
207 206
 }
208 207
 
Browse code

update fortify for pvclust

xiangpin authored on 14/08/2022 08:23:47
Showing 1 changed files
... ...
@@ -148,14 +148,7 @@ fortify.phylo4 <- function(model, data,
148 148
 
149 149
     df <- fortify.phylo(phylo, data,
150 150
                         layout, ladderize, right, mrsd=mrsd, ...)
151
-    mx <- max(df$x, na.rm=TRUE)
152
-    df$x <- df$x - mx
153
-    df$branch <- df$branch - mx
154
-    tip.edge.len <- attr(phylo, 'tip.edge.len')
155
-    if (!is.null(tip.edge.len)){
156
-        df[df$isTip, "x", drop=TRUE] <- tip.edge.len
157
-    }
158
-    attr(df, 'revts.done') = TRUE
151
+    df <- adjust_hclust_tip.edge.len(df, phylo)
159 152
     scaleY(phylo, df, yscale, layout, ...)
160 153
 }
161 154
 
... ...
@@ -205,8 +198,12 @@ fortify.phylo4d <- function(model, data,
205 198
                             right         = FALSE,
206 199
                             branch.length = "branch.length",
207 200
                             mrsd          = NULL,
201
+							hang          = 0.1,
208 202
                             ...) {
209
-    fortify(as.treedata(model), data, layout, yscale, ladderize, right, branch.length, mrsd, ...)
203
+    model <- as.treedata(model, hang = hang)
204
+    df <- fortify(model, data, layout, yscale, ladderize, right, branch.length, mrsd, ...)
205
+    df <- adjust_hclust_tip.edge.len(df, model@phylo)
206
+    return (df)
210 207
 }
211 208
 
212 209
 ##' @method fortify pvclust
Browse code

linkage support

Guangchuang Yu authored on 11/08/2022 09:03:41
Showing 1 changed files
... ...
@@ -135,7 +135,8 @@ fortify.phylo4 <- function(model, data,
135 135
                            mrsd      = NULL,
136 136
                            hang      = .1,
137 137
                            ...) {
138
-    if (inherits(model, c("dendrogram", "agnes", "diana", "twins"))) {
138
+    if (inherits(model, c("dendrogram", "linkage", 
139
+                        "agnes", "diana", "twins"))) {
139 140
         model <- stats::as.hclust(model)
140 141
     }
141 142
 
... ...
@@ -190,6 +191,9 @@ fortify.phylog <- fortify.phylo4
190 191
 ##' @export
191 192
 fortify.igraph <- fortify.phylo4
192 193
 
194
+##' @method fortify linkage
195
+##' @export
196
+fortify.linkage <- fortify.phylo4
193 197
 
194 198
 ##' @method fortify phylo4d
195 199
 ##' @importFrom treeio as.treedata
Browse code

hclust for all layout and update geom_tiplab type of dendrogram layout

xiangpin authored on 20/07/2022 12:05:44
Showing 1 changed files
... ...
@@ -139,7 +139,7 @@ fortify.phylo4 <- function(model, data,
139 139
         model <- stats::as.hclust(model)
140 140
     }
141 141
 
142
-    if (inherits(model, "hclust") && layout == 'dendrogram') {
142
+    if (inherits(model, "hclust")) {
143 143
         phylo <- as.phylo.hclust2(model, hang = hang)
144 144
     } else {
145 145
         phylo <- as.phylo(model)
... ...
@@ -147,9 +147,14 @@ fortify.phylo4 <- function(model, data,
147 147
 
148 148
     df <- fortify.phylo(phylo, data,
149 149
                         layout, ladderize, right, mrsd=mrsd, ...)
150
-    if (!is.null(attr(phylo, 'tip.edge.len'))){
151
-        attr(df, 'tip.edge.len') <- attr(phylo, 'tip.edge.len')
150
+    mx <- max(df$x, na.rm=TRUE)
151
+    df$x <- df$x - mx
152
+    df$branch <- df$branch - mx
153
+    tip.edge.len <- attr(phylo, 'tip.edge.len')
154
+    if (!is.null(tip.edge.len)){
155
+        df[df$isTip, "x", drop=TRUE] <- tip.edge.len
152 156
     }
157
+    attr(df, 'revts.done') = TRUE
153 158
     scaleY(phylo, df, yscale, layout, ...)
154 159
 }
155 160
 
Browse code

update plot method for hclust-like class

xiangpin authored on 19/07/2022 16:02:24
Showing 1 changed files
... ...
@@ -133,19 +133,23 @@ fortify.phylo4 <- function(model, data,
133 133
                            ladderize = TRUE,
134 134
                            right     = FALSE,
135 135
                            mrsd      = NULL,
136
+                           hang      = .1,
136 137
                            ...) {
137 138
     if (inherits(model, c("dendrogram", "agnes", "diana", "twins"))) {
138 139
         model <- stats::as.hclust(model)
139 140
     }
140 141
 
141
-    if (inherits(model, "hclust")) {
142
-        phylo <- as.phylo.hclust2(model)
142
+    if (inherits(model, "hclust") && layout == 'dendrogram') {
143
+        phylo <- as.phylo.hclust2(model, hang = hang)
143 144
     } else {
144 145
         phylo <- as.phylo(model)
145 146
     }
146 147
 
147 148
     df <- fortify.phylo(phylo, data,
148 149
                         layout, ladderize, right, mrsd=mrsd, ...)
150
+    if (!is.null(attr(phylo, 'tip.edge.len'))){
151
+        attr(df, 'tip.edge.len') <- attr(phylo, 'tip.edge.len')
152
+    }
149 153
     scaleY(phylo, df, yscale, layout, ...)
150 154
 }
151 155
 
Browse code

as.phylo.hclust2

Guangchuang Yu authored on 21/06/2022 08:59:44
Showing 1 changed files
... ...
@@ -134,12 +134,16 @@ fortify.phylo4 <- function(model, data,
134 134
                            right     = FALSE,
135 135
                            mrsd      = NULL,
136 136
                            ...) {
137
-    if (class(model) %in% c("dendrogram", "agnes", "diana", "twins")) {
137
+    if (inherits(model, c("dendrogram", "agnes", "diana", "twins"))) {
138 138
         model <- stats::as.hclust(model)
139 139
     }
140 140
 
141
-    
142
-    phylo <- as.phylo(model)
141
+    if (inherits(model, "hclust")) {
142
+        phylo <- as.phylo.hclust2(model)
143
+    } else {
144
+        phylo <- as.phylo(model)
145
+    }
146
+
143 147
     df <- fortify.phylo(phylo, data,
144 148
                         layout, ladderize, right, mrsd=mrsd, ...)
145 149
     scaleY(phylo, df, yscale, layout, ...)
Browse code

slanted layout when branch.length is none

xiangpin authored on 29/04/2022 06:50:09
Showing 1 changed files
... ...
@@ -32,14 +32,20 @@ fortify.phylo <- function(model, data,
32 32
     if (layout %in% c("equal_angle", "daylight", "ape")) {
33 33
         res <- layout.unrooted(model, layout.method = layout, branch.length = branch.length, ...)
34 34
     } else {
35
+        ypos <- getYcoord(x)
36
+        N <- Nnode(x, internal.only=FALSE)
35 37
         if (is.null(x$edge.length) || branch.length == "none") {
36
-            xpos <- getXcoord_no_length(x)
38
+            if (layout == 'slanted'){
39
+                sbp <- .convert_tips2ancestors_sbp(x, include.root = TRUE)
40
+                xpos <- getXcoord_no_length_slanted(sbp)
41
+                ypos <- getYcoord_no_length_slanted(sbp)  
42
+            }else{
43
+                xpos <- getXcoord_no_length(x)
44
+            }
37 45
         } else {
38 46
             xpos <- getXcoord(x)
39 47
         }
40 48
 
41
-        ypos <- getYcoord(x)
42
-        N <- Nnode(x, internal.only=FALSE)
43 49
         xypos <- tibble::tibble(node=1:N, x=xpos + root.position, y=ypos)
44 50
 
45 51
         df <- as_tibble(model) %>%
Browse code

branch.x and branch.y for branch label of unrooted layout

xiangpin authored on 11/06/2021 08:26:23
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ fortify.phylo <- function(model, data,
49 49
     }
50 50
 
51 51
     ## add branch mid position
52
-    res <- calculate_branch_mid(res)
52
+    res <- calculate_branch_mid(res, layout=layout)
53 53
 
54 54
     if (!is.null(mrsd)) {
55 55
         res <- scaleX_by_time_from_mrsd(res, mrsd, as.Date)
Browse code

fortify method for treedataList

Guangchuang Yu authored on 20/09/2020 06:49:31
Showing 1 changed files
... ...
@@ -89,6 +89,10 @@ fortify.multiPhylo <-  function(model, data,
89 89
     return(df)
90 90
 }
91 91
 
92
+##' @method fortify treedataList
93
+##' @export
94
+fortify.treedataList <- fortify.multiPhylo
95
+
92 96
 ##' @importFrom ggplot2 fortify
93 97
 ##' @method fortify treedata
94 98
 ##' @export
Browse code

fortify method for pvclust

Guangchuang Yu authored on 22/06/2020 02:48:45
Showing 1 changed files
... ...
@@ -182,7 +182,9 @@ fortify.phylo4d <- function(model, data,
182 182
     fortify(as.treedata(model), data, layout, yscale, ladderize, right, branch.length, mrsd, ...)
183 183
 }
184 184
 
185
-
185
+##' @method fortify pvclust
186
+##' @export
187
+fortify.pvclust <- fortify.phylo4d
186 188
 
187 189
 ##' @method fortify obkData
188 190
 ##' @export
Browse code

bug fixed of layoutEqualAngle

Guangchuang Yu authored on 09/04/2020 02:07:12
Showing 1 changed files
... ...
@@ -40,7 +40,7 @@ fortify.phylo <- function(model, data,
40 40
 
41 41
         ypos <- getYcoord(x)
42 42
         N <- Nnode(x, internal.only=FALSE)
43
-        xypos <- tibble::data_frame(node=1:N, x=xpos + root.position, y=ypos)
43
+        xypos <- tibble::tibble(node=1:N, x=xpos + root.position, y=ypos)
44 44
 
45 45
         df <- as_tibble(model) %>%
46 46
             mutate(isTip = ! .data$node %in% .data$parent)
Browse code

remove mutate_

Guangchuang Yu authored on 25/03/2020 03:58:15
Showing 1 changed files
... ...
@@ -2,7 +2,7 @@
2 2
 ##' @importFrom treeio as.phylo
3 3
 ##' @importFrom treeio Nnode
4 4
 ##' @importFrom dplyr full_join
5
-##' @importFrom dplyr mutate_
5
+##' @importFrom dplyr mutate
6 6
 ##' @importFrom tidytree as_tibble
7 7
 ##' @method fortify phylo
8 8
 ##' @export
... ...
@@ -43,7 +43,7 @@ fortify.phylo <- function(model, data,
43 43
         xypos <- tibble::data_frame(node=1:N, x=xpos + root.position, y=ypos)
44 44
 
45 45
         df <- as_tibble(model) %>%
46
-            mutate_(isTip = ~(! node %in% parent))
46
+            mutate(isTip = ! .data$node %in% .data$parent)
47 47
 
48 48
         res <- full_join(df, xypos, by = "node")
49 49
     }
Browse code

bug fixed

Guangchuang Yu authored on 26/01/2020 15:25:32
Showing 1 changed files
... ...
@@ -63,6 +63,7 @@ fortify.phylo <- function(model, data,
63 63
     }
64 64
     res <- scaleY(as.phylo(model), res, yscale, layout, ...)
65 65
     class(res) <- c("tbl_tree", class(res))
66
+    attr(res, "layout") <- layout
66 67
     return(res)
67 68
 }
68 69
 
... ...
@@ -84,6 +85,7 @@ fortify.multiPhylo <-  function(model, data,
84 85
     df <- do.call("rbind", df.list)
85 86
     df$.id <- rep(names(df.list), times=sapply(df.list, nrow))
86 87
     df$.id <- factor(df$.id, levels=names(df.list))
88
+    attr(df, "layout") <- layout
87 89
     return(df)
88 90
 }
89 91
 
Browse code

added new layout 'ape' which is a copy of the 'unrooted' type in ape::plot.phylo

brj1 authored on 24/01/2020 03:03:49
Showing 1 changed files
... ...
@@ -29,7 +29,7 @@ fortify.phylo <- function(model, data,
29 29
         }
30 30
     }
31 31
 
32
-    if (layout %in% c("equal_angle", "daylight")) {
32
+    if (layout %in% c("equal_angle", "daylight", "ape")) {
33 33
         res <- layout.unrooted(model, layout.method = layout, branch.length = branch.length, ...)
34 34
     } else {
35 35
         if (is.null(x$edge.length) || branch.length == "none") {
Browse code

fortify for tree graph

Guangchuang Yu authored on 28/09/2019 09:25:27
Showing 1 changed files
... ...
@@ -161,6 +161,11 @@ fortify.twins <- fortify.phylo4
161 161
 ##' @export
162 162
 fortify.phylog <- fortify.phylo4
163 163
 
164
+##' @method fortify igraph
165
+##' @export
166
+fortify.igraph <- fortify.phylo4
167
+
168
+
164 169
 ##' @method fortify phylo4d
165 170
 ##' @importFrom treeio as.treedata
166 171
 ##' @export
... ...
@@ -176,6 +181,7 @@ fortify.phylo4d <- function(model, data,
176 181
 }
177 182
 
178 183
 
184
+
179 185
 ##' @method fortify obkData
180 186
 ##' @export
181 187
 fortify.obkData <- function(model, data,
Browse code

fortify methods for agnes, diana and twins

Guangchuang Yu authored on 30/08/2019 05:01:11
Showing 1 changed files
... ...
@@ -122,8 +122,8 @@ fortify.phylo4 <- function(model, data,
122 122
                            right     = FALSE,
123 123
                            mrsd      = NULL,
124 124
                            ...) {
125
-    if (is(model, "dendrogram")) {
126
-        as.phylo <- phylogram::as.phylo
125
+    if (class(model) %in% c("dendrogram", "agnes", "diana", "twins")) {
126
+        model <- stats::as.hclust(model)
127 127
     }
128 128
 
129 129
     
... ...
@@ -145,6 +145,18 @@ fortify.hclust <- fortify.phylo4
145 145
 ##' @export
146 146
 fortify.dendrogram <- fortify.phylo4
147 147
 
148
+##' @method fortify agnes
149
+##' @export
150
+fortify.agnes <- fortify.phylo4
151
+
152
+##' @method fortify diana
153
+##' @export
154
+fortify.diana <- fortify.phylo4
155
+
156
+##' @method fortify twins
157
+##' @export
158
+fortify.twins <- fortify.phylo4
159
+
148 160
 ##' @method fortify phylog
149 161
 ##' @export
150 162
 fortify.phylog <- fortify.phylo4
Browse code

support phylog

Guangchuang Yu authored on 21/08/2019 05:59:44
Showing 1 changed files
... ...
@@ -145,6 +145,10 @@ fortify.hclust <- fortify.phylo4
145 145
 ##' @export
146 146
 fortify.dendrogram <- fortify.phylo4
147 147
 
148
+##' @method fortify phylog
149
+##' @export
150
+fortify.phylog <- fortify.phylo4
151
+
148 152
 ##' @method fortify phylo4d
149 153
 ##' @importFrom treeio as.treedata
150 154
 ##' @export
Browse code

support of dendrogram

Guangchuang Yu authored on 31/07/2019 07:45:15
Showing 1 changed files
... ...
@@ -122,6 +122,11 @@ fortify.phylo4 <- function(model, data,
122 122
                            right     = FALSE,
123 123
                            mrsd      = NULL,
124 124
                            ...) {
125
+    if (is(model, "dendrogram")) {
126
+        as.phylo <- phylogram::as.phylo
127
+    }
128
+
129
+    
125 130
     phylo <- as.phylo(model)
126 131
     df <- fortify.phylo(phylo, data,
127 132
                         layout, ladderize, right, mrsd=mrsd, ...)
Browse code

support of dendrogram

Guangchuang Yu authored on 31/07/2019 02:21:33
Showing 1 changed files
... ...
@@ -128,6 +128,18 @@ fortify.phylo4 <- function(model, data,
128 128
     scaleY(phylo, df, yscale, layout, ...)
129 129
 }
130 130
 
131
+## `ape::as.phylo` (for `hclust`)
132
+
133
+##' @method fortify hclust
134
+##' @export
135
+fortify.hclust <- fortify.phylo4
136
+
137
+## `phylogram::as.phylo` (for `dendrogram`).
138
+
139
+##' @method fortify dendrogram
140
+##' @export
141
+fortify.dendrogram <- fortify.phylo4
142
+
131 143
 ##' @method fortify phylo4d
132 144
 ##' @importFrom treeio as.treedata
133 145
 ##' @export
Browse code

root.position parameter

Guangchuang Yu authored on 27/05/2019 12:52:40
Showing 1 changed files
... ...
@@ -14,6 +14,7 @@ fortify.phylo <- function(model, data,
14 14
                           mrsd          = NULL,
15 15
                           as.Date       = FALSE,
16 16
                           yscale        = "none",
17
+                          root.position = 0,
17 18
                           ...) {
18 19
 
19 20
     x <- as.phylo(model) ## reorder.phylo(get.tree(model), "postorder")
... ...
@@ -39,7 +40,7 @@ fortify.phylo <- function(model, data,
39 40
 
40 41
         ypos <- getYcoord(x)
41 42
         N <- Nnode(x, internal.only=FALSE)
42
-        xypos <- tibble::data_frame(node=1:N, x=xpos, y=ypos)
43
+        xypos <- tibble::data_frame(node=1:N, x=xpos + root.position, y=ypos)
43 44
 
44 45
         df <- as_tibble(model) %>%
45 46
             mutate_(isTip = ~(! node %in% parent))
Browse code

reduce dependency

Guangchuang Yu authored on 27/01/2019 23:14:00
Showing 1 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 ##' @importFrom ape ladderize
2 2
 ##' @importFrom treeio as.phylo
3 3
 ##' @importFrom treeio Nnode
4
-##' @importFrom tibble data_frame
5 4
 ##' @importFrom dplyr full_join
6 5
 ##' @importFrom dplyr mutate_
7 6
 ##' @importFrom tidytree as_tibble
... ...
@@ -40,7 +39,7 @@ fortify.phylo <- function(model, data,
40 39
 
41 40
         ypos <- getYcoord(x)
42 41
         N <- Nnode(x, internal.only=FALSE)
43
-        xypos <- data_frame(node=1:N, x=xpos, y=ypos)
42
+        xypos <- tibble::data_frame(node=1:N, x=xpos, y=ypos)
44 43
 
45 44
         df <- as_tibble(model) %>%
46 45
             mutate_(isTip = ~(! node %in% parent))
Browse code

compatible with tibble 2.0.0

Guangchuang Yu authored on 29/11/2018 15:42:00
Showing 1 changed files
... ...
@@ -4,7 +4,7 @@
4 4
 ##' @importFrom tibble data_frame
5 5
 ##' @importFrom dplyr full_join
6 6
 ##' @importFrom dplyr mutate_
7
-##' @importFrom tidytree as_data_frame
7
+##' @importFrom tidytree as_tibble
8 8
 ##' @method fortify phylo
9 9
 ##' @export
10 10
 fortify.phylo <- function(model, data,
... ...
@@ -42,7 +42,7 @@ fortify.phylo <- function(model, data,
42 42
         N <- Nnode(x, internal.only=FALSE)
43 43
         xypos <- data_frame(node=1:N, x=xpos, y=ypos)
44 44
 
45
-        df <- as_data_frame(model) %>%
45
+        df <- as_tibble(model) %>%
46 46
             mutate_(isTip = ~(! node %in% parent))
47 47
 
48 48
         res <- full_join(df, xypos, by = "node")
Browse code

made data usable with treedata in 'equal_angle' and 'daylight' layouts

brj1 authored on 10/10/2018 23:33:37
Showing 1 changed files
... ...
@@ -30,7 +30,7 @@ fortify.phylo <- function(model, data,
30 30
     }
31 31
 
32 32
     if (layout %in% c("equal_angle", "daylight")) {
33
-        res <- layout.unrooted(x, layout.method = layout, branch.length = branch.length, ...)
33
+        res <- layout.unrooted(model, layout.method = layout, branch.length = branch.length, ...)
34 34
     } else {
35 35
         if (is.null(x$edge.length) || branch.length == "none") {
36 36
             xpos <- getXcoord_no_length(x)
Browse code

unrooted layout

guangchuang yu authored on 21/12/2017 12:07:05
Showing 1 changed files
... ...
@@ -29,20 +29,24 @@ fortify.phylo <- function(model, data,
29 29
         }
30 30
     }
31 31
 
32
-    if (is.null(x$edge.length) || branch.length == "none") {
33
-        xpos <- getXcoord_no_length(x)
32
+    if (layout %in% c("equal_angle", "daylight")) {
33
+        res <- layout.unrooted(x, layout.method = layout, branch.length = branch.length, ...)
34 34
     } else {
35
-        xpos <- getXcoord(x)
36
-    }
35
+        if (is.null(x$edge.length) || branch.length == "none") {
36
+            xpos <- getXcoord_no_length(x)
37
+        } else {
38
+            xpos <- getXcoord(x)
39
+        }
37 40
 
38
-    ypos <- getYcoord(x)
39
-    N <- Nnode(x, internal.only=FALSE)
40
-    xypos <- data_frame(node=1:N, x=xpos, y=ypos)
41
+        ypos <- getYcoord(x)
42
+        N <- Nnode(x, internal.only=FALSE)
43
+        xypos <- data_frame(node=1:N, x=xpos, y=ypos)
41 44
 
42
-    df <- as_data_frame(model) %>%
43
-        mutate_(isTip = ~(! node %in% parent))
45
+        df <- as_data_frame(model) %>%
46
+            mutate_(isTip = ~(! node %in% parent))
44 47
 
45
-    res <- full_join(df, xypos, by = "node")
48
+        res <- full_join(df, xypos, by = "node")
49
+    }
46 50
 
47 51
     ## add branch mid position
48 52
     res <- calculate_branch_mid(res)
Browse code

clean up code

guangchuang yu authored on 14/12/2017 08:47:21
Showing 1 changed files
... ...
@@ -1,352 +1,111 @@
1
-##' convert polytomy to binary tree
2
-##'
3
-##' as.binary method for \code{phylo} object
4
-##' @rdname as.binary
5
-##' @return binary tree
6
-##' @method as.binary phylo
7
-##' @importFrom ape is.binary.tree
1
+##' @importFrom ape ladderize
2
+##' @importFrom treeio as.phylo
3
+##' @importFrom treeio Nnode
4
+##' @importFrom tibble data_frame
5
+##' @importFrom dplyr full_join
6
+##' @importFrom dplyr mutate_
7
+##' @importFrom tidytree as_data_frame
8
+##' @method fortify phylo
8 9
 ##' @export
9
-##' @author Guangchuang Yu \url{http://ygc.name}
10
-##' @examples
11
-##' require(ape)
12
-##' tr <- read.tree(text="((A, B, C), D);")
13
-##' is.binary.tree(tr)
14
-##' tr2 <- as.binary(tr)
15
-##' is.binary.tree(tr2)
16
-as.binary.phylo <- function(tree, ...) {
17
-    if(is.binary.tree(tree)) {
18
-        message("The input tree is already binary...")
19
-        invisible(tree)
10
+fortify.phylo <- function(model, data,
11
+                          layout        = "rectangular",
12
+                          ladderize     = TRUE,
13
+                          right         = FALSE,
14
+                          branch.length = "branch.length",
15
+                          mrsd          = NULL,
16
+                          as.Date       = FALSE,
17
+                          yscale        = "none",
18
+                          ...) {
19
+
20
+    x <- as.phylo(model) ## reorder.phylo(get.tree(model), "postorder")
21
+    if (ladderize == TRUE) {
22
+        x <- ladderize(x, right=right)
20 23
     }
21 24
 
22
-    polyNode <- tree$edge[,1] %>% table %>% '>'(2) %>%
23
-        which %>% names %>% as.numeric
24
-
25
-    N <- getNodeNum(tree)
26
-    ii <- 0
27
-    for (pn in polyNode) {
28
-        idx <- which(tree$edge[,1] == pn)
29
-        while(length(idx) >2) {
30
-            ii <- ii + 1
31
-            newNode <- N+ii
32
-            tree$edge[idx[-1],1] <- newNode
33
-            newEdge <- matrix(c(tree$edge[idx[1],1], newNode), ncol=2)
34
-            tree$edge <- rbind(tree$edge, newEdge)
35
-            idx <- idx[-1]
25
+    if (! is.null(x$edge.length)) {
26
+        if (anyNA(x$edge.length)) {
27
+            warning("'edge.length' contains NA values...\n## setting 'edge.length' to NULL automatically when plotting the tree...")
28
+            x$edge.length <- NULL
36 29
         }
37 30
     }
38 31
 
39
-    tree$Nnode <- tree$Nnode+ii
40
-    tree$edge.length <- c(tree$edge.length, rep(0, ii))
41
-    return(tree)
42
-}
43
-
44
-
45
-##' remove singleton
46
-##'
47
-##'
48
-##' @title rm.singleton.newick
49
-##' @param nwk newick file
50
-##' @param outfile output newick file
51
-##' @return tree text
52
-##' @importFrom magrittr %<>%
53
-##' @importFrom magrittr add
54
-##' @importFrom ape write.tree
55
-##' @importFrom ape read.tree
56
-##' @author Guangchuang Yu \url{http://ygc.name}
57
-rm.singleton.newick <- function(nwk, outfile = NULL) {
58
-    tree <- readLines(nwk)
59
-
60
-    ## remove singleton of tips
61
-    nodePattern <- "\\w+:[\\.0-9Ee\\+\\-]+"
62
-    singletonPattern.with.nodename <- paste0(".*(\\(", nodePattern, "\\)\\w+:[\\.0-9Ee\\+\\-]+).*")
63
-    singletonPattern.wo.nodename <- paste0(".*(\\(", nodePattern, "\\):[\\.0-9Ee\\+\\-]+).*")
64
-
65
-    while(length(grep("\\([^,]+\\)", tree)) > 0) {
66
-        singleton <- gsub(singletonPattern.with.nodename, "\\1", tree)
67
-        if (singleton == tree) {
68
-            singleton <- gsub(singletonPattern.wo.nodename, "\\1", tree)
69
-        }
70
-        if (singleton == tree) {
71
-            stop("can't parse singleton node...")
72
-        }
32
+    if (is.null(x$edge.length) || branch.length == "none") {
33
+        xpos <- getXcoord_no_length(x)
34
+    } else {
35
+        xpos <- getXcoord(x)
36
+    }
73 37
 
74
-        tip <- gsub("\\((\\w+).*", "\\1", singleton)
38
+    ypos <- getYcoord(x)
39
+    N <- Nnode(x, internal.only=FALSE)
40
+    xypos <- data_frame(node=1:N, x=xpos, y=ypos)
75 41
 
76
-        len1 <- gsub(".*[^\\.0-9Ee\\+\\-]+([\\.0-9Ee\\+\\-]+)", "\\1", singleton)
77
-        len2 <- gsub(".*:([\\.0-9Ee\\+\\-]+)\\).*", "\\1", singleton)
78
-        len <- as.numeric(len1) + as.numeric(len2)
42
+    df <- as_data_frame(model) %>%
43
+        mutate_(isTip = ~(! node %in% parent))
79 44
 
80
-        tree <- gsub(singleton, paste0(tip, ":", len), tree, fixed = TRUE)
81
-    }
45
+    res <- full_join(df, xypos, by = "node")
82 46
 
83
-    tree <- read.tree(text=tree)
47
+    ## add branch mid position
48
+    res <- calculate_branch_mid(res)
84 49
 
85
-    ### remove singleton of internal nodes
86
-    p.singleton <- which(table(tree$edge[,1]) == 1)
87
-    if (length(p.singleton) > 0) {
88
-        p.singleton %<>% names %>% as.numeric
89
-        edge <- tree$edge
90
-        idx <- which(edge[,1] == p.singleton)
91
-        sidx <- which(edge[,2] == p.singleton)
92
-        edge[sidx,2] <- edge[idx, 2]
93
-        edge <- edge[-idx,]
94
-        tree$edge <- edge
95
-        tree$edge.length[sidx] %<>% add(., tree$edge.length[idx])
96
-        tree$edge.length <- tree$edge.length[-idx]
97
-        tree$Nnode <- tree$Nnode - 1
98
-        if (!is.null(tree$node.label)) {
99
-            tree$node.label <- tree$node.label[-(p.singleton - Ntip(tree))]
100
-        }
50
+    if (!is.null(mrsd)) {
51
+        res <- scaleX_by_time_from_mrsd(res, mrsd, as.Date)
101 52
     }
102 53
 
103
-    if (!is.null(outfile)) {
104
-        write.tree(tree, file=outfile)
54
+    if (layout == "slanted") {
55
+        res <- add_angle_slanted(res)
56
+    } else {
57
+        ## angle for all layout, if 'rectangular', user use coord_polar, can still use angle
58
+        res <- calculate_angle(res)
105 59
     }
106
-    invisible(tree)
60
+    res <- scaleY(as.phylo(model), res, yscale, layout, ...)
61
+    class(res) <- c("tbl_tree", class(res))
62
+    return(res)
107 63
 }
108 64
 
109
-## ##' @method fortify beast
110
-## ##' @export
111
-## fortify.beast <- function(model, data,
112
-##                           layout        = "rectangular",
113
-##                           yscale        = "none",
114
-##                           ladderize     = TRUE,
115
-##                           right         = FALSE,
116
-##                           branch.length = "branch.length",
117
-##                           ndigits       = NULL,
118
-##                           mrsd = NULL, ...) {
119
-
120
-##     model <- set_branch_length(model, branch.length)
121
-##     phylo <- model@phylo
122
-##     df    <- fortify(phylo,
123
-##                      layout = layout,
124
-##                      branch.length = branch.length,
125
-##                      ladderize = ladderize,
126
-##                      right = right,
127
-##                      mrsd = mrsd, ...)
128
-
129
-##     stats <- model@stats
130
-
131
-##     scn <- colnames(stats)
132
-##     scn <- scn[scn != 'node']
133
-
134
-##     for (cn in scn) {
135
-##         if (cn %in% colnames(df)) {
136
-##             colnames(stats)[colnames(stats) == cn] <- paste0(cn, "_")
137
-##             msg <- paste("feature", cn, "was renamed to", paste0(cn, "_"), "due to name conflict...")
138
-##             warning(msg)
139
-##         }
140
-##     }
141
-
142
-##     idx <- which(colnames(stats) != "node")
143
-##     for (ii in idx) {
144
-##         if (is.character_beast(stats, ii)) {
145
-##             len <- sapply(stats[,ii], length)
146
-##             if (any(len > 1)) {
147
-##                 stats[,ii] %<>% sapply(., function(x) {
148
-##                     y <- unlist(x) %>% as.character %>%
149
-##                         gsub("\"", "", .) %>% gsub("'", "", .)
150
-##                     if (length(y) == 1) {
151
-##                         return(y)
152
-##                     } else {
153
-##                         return(paste0('{', paste0(y, collapse = ','), '}'))
154
-##                     }
155
-##                 })
156
-##             } else {
157
-##                 stats[,ii] %<>% unlist %>% as.character %>%
158
-##                     gsub("\"", "", .) %>% gsub("'", "", .)
159
-##             }
160
-##             next
161
-##         }
162
-
163
-##         len <- sapply(stats[,ii], length)
164
-##         if ( all(len == 1) ) {
165
-##             stats[, ii] %<>% unlist %>% as.character %>% as.numeric
166
-##             if (!is.null(ndigits)) {
167
-##                 stats[, ii] %<>% round(., ndigits)
168
-##             }
169
-##         } else if (all(len <= 2)) {
170
-##             stats[, ii] %<>% sapply(., function(x) {
171
-##                 y <- unlist(x) %>% as.character %>% as.numeric
172
-##                 if (!is.null(ndigits)) {
173
-##                     y %<>% round(., ndigits)
174
-##                 }
175
-##                 if (length(y) == 1) {
176
-##                     return(y)
177
-##                 } else {
178
-##                     return(paste0('[', paste0(y, collapse = ','), ']'))
179
-##                 }
180
-##             })
181
-##         } else {
182
-##             stats[,ii] %<>% sapply(., function(x) {
183
-##                 y <- unlist(x) %>% as.character %>% as.numeric
184
-##                 if (!is.null(ndigits)) {
185
-##                     y %<>% round(., ndigits)
186
-##                 }
187
-##                 if (length(y) == 1) {
188
-##                     return(y)
189
-##                 } else {
190
-##                     return(paste0('{', paste0(y, collapse = ','), '}'))
191
-##                 }
192
-##             })
193
-##         }
194
-##     }
195
-
196
-
197
-##     cn <- colnames(stats)
198
-##     lo <- cn[grep("_lower", cn)]
199
-##     hi <- gsub("lower$", "upper", lo)
200
-##     rid <- gsub("_lower$", "", lo)
201
-
202
-##     for (i in seq_along(rid)) {
203
-##         stats[, rid[i]] <- paste0("[", stats[, lo[i]], ",", stats[, hi[i]], "]")
204
-##         stats[is.na(stats[, lo[i]]), rid[i]] <- NA
205
-##     }
206
-
207
-##     idx   <- match(df$node, stats$node)
208
-##     stats <- stats[idx,]
209
-##     cn_stats <- colnames(stats)
210
-##     stats <- stats[, cn_stats != "node"]
211
-
212
-##     df <- cbind(df, stats)
213
-##     if (is(stats, "data.frame") == FALSE) {
214
-##         colnames(df)[colnames(df) == "stats"] <- cn_stats[cn_stats != "node"]
215
-##     }
216
-
217
-##     df <- scaleY(phylo, df, yscale, layout, ...)
218
-
219
-##     append_extraInfo(df, model)
220
-## }
221
-
222 65
 
223
-## ##' @method fortify codeml
224
-## ##' @export
225
-## fortify.codeml <- function(model, data,
226
-##                            layout        = "rectangular",
227
-##                            yscale        = "none",
228
-##                            ladderize     = TRUE,
229
-##                            right         = FALSE,
230
-##                            branch.length = "mlc.branch.length",
231
-##                            ndigits       = NULL,
232
-##                            mrsd          = NULL,
233
-##                            ...) {
234
-
235
-##     dNdS <- model@mlc@dNdS
236
-##     if (branch.length == "branch.length") {
237
-##         message("branch.length setting to mlc.branch.length by default...")
238
-##         branch.length <- "mlc.branch.length"
239
-##     }
240
-##     length <- match.arg(branch.length,
241
-##                         c("none",
242
-##                           "mlc.branch.length",
243
-##                           "rst.branch.length",
244
-##                           colnames(dNdS)[-c(1,2)])
245
-##                         )
246
-
247
-##     if (length == "rst.branch.length") {
248
-##         phylo <- get.tree(model@rst)
249
-##     } else {
250
-##         if (length == "mlc.branch.length") {
251
-##             length <- "branch.length"
252
-##         }
253
-##         mlc <- set_branch_length(model@mlc, length)
254
-##         phylo <- get.tree(mlc)
255
-##     }
256
-
257
-##     df <- fortify(phylo, data, layout, ladderize, right,
258
-##                   branch.length=length, mrsd=mrsd, ...)
259
-
260
-##     res <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
261
-##     df <- merge_phylo_anno.paml_rst(res, model@rst)
262
-##     df <- scaleY(phylo, df, yscale, layout, ...)
263
-
264
-##     append_extraInfo(df, model)
265
-## }
266
-
267
-
268
-## ##' @method fortify codeml_mlc
269
-## ##' @export
270
-## fortify.codeml_mlc <- function(model, data,
271
-##                                layout        = "rectangular",
272
-##                                yscale        = "none",
273
-##                                ladderize     = TRUE,
274
-##                                right         = FALSE,
275
-##                                branch.length = "branch.length",
276
-##                                ndigits       = NULL,
277
-##                                mrsd          = NULL,
278
-##                                ...) {
279
-
280
-##     model <- set_branch_length(model, branch.length)
281
-##     phylo <- get.tree(model)
282
-##     df <- fortify(phylo, data, layout, ladderize, right,
283
-##                   branch.length=branch.length, mrsd=mrsd, ...)
284
-
285
-##     dNdS <- model@dNdS
286
-
287
-##     df <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
288
-##     df <- scaleY(phylo, df, yscale, layout, ...)
289
-
290
-##     append_extraInfo(df, model)
291
-## }
292
-
293
-## merge_phylo_anno.codeml_mlc <- function(df, dNdS, ndigits = NULL) {
294
-##     if (!is.null(ndigits)) {
295
-##         idx <- which(! colnames(dNdS) %in% c("node", "parent"))
296
-##         for (ii in idx) {
297
-##             if (is.numeric(dNdS[, ii])) {
298
-##                 dNdS[, ii] <- round(dNdS[,ii], ndigits)
299
-##             }
300
-##         }
301
-##     }
302
-
303
-##     res <- merge(df, dNdS,
304
-##                  by.x  = c("node", "parent"),
305
-##                  by.y  = c("node", "parent"),
306
-##                  all.x = TRUE)
307
-
308
-##     res[match(df$node, res$node),]
309
-## }
310
-
311
-
312
-## ##' @method fortify paml_rst
313
-## ##' @export
314
-## fortify.paml_rst <- function(model, data,
315
-##                              layout    = "rectangular",
316
-##                              yscale    = "none",
317
-##                              ladderize = TRUE,
318
-##                              right     = FALSE,
319
-##                              mrsd      = NULL,
320
-##                              ...) {
321
-##     df <- fortify.phylo(model@phylo, data, layout, ladderize, right, mrsd=mrsd, ...)
322
-##     df <- merge_phylo_anno.paml_rst(df, model)
323
-##     df <- scaleY(model@phylo, df, yscale, layout, ...)
324
-
325
-##     append_extraInfo(df, model)
326
-## }
327
-
328
-## merge_phylo_anno.paml_rst <- function(df, model) {
329
-##     types <- get.fields(model)
330
-##     types <- types[grepl('subs', types)]
331
-##     for (type in types) {
332
-##         anno <- get.subs(model, type=type)
333
-##         colnames(anno)[2] <- type
334
-##         df <- df %add2% anno
335
-##     }
336
-##     return(df)
337
-## }
338
-
339
-
340
-## ##' @method fortify phangorn
341
-## ##' @export
342
-## fortify.phangorn <- fortify.paml_rst
343
-
344
-
345
-## ##' @method fortify hyphy
346
-## ##' @export
347
-## fortify.hyphy <- fortify.paml_rst
66
+##' @method fortify multiPhylo
67
+##' @export
68
+fortify.multiPhylo <-  function(model, data,
69
+                                layout    = "rectangular",
70
+                                ladderize = TRUE,
71
+                                right     = FALSE,
72
+                                mrsd      = NULL, ...) {
348 73
 
74
+    df.list <- lapply(model, function(x) fortify(x, layout=layout, ladderize=ladderize, right=right, mrsd=mrsd, ...))
75
+    if (is.null(names(model))) {
76
+        names(df.list) <- paste0("Tree ", "#", seq_along(model))
77
+    } else {
78
+        names(df.list) <- names(model)
79
+    }
80
+    df <- do.call("rbind", df.list)
81
+    df$.id <- rep(names(df.list), times=sapply(df.list, nrow))
82
+    df$.id <- factor(df$.id, levels=names(df.list))
83
+    return(df)
84
+}
349 85
 
86
+##' @importFrom ggplot2 fortify
87
+##' @method fortify treedata
88
+##' @export
89
+fortify.treedata <- function(model, data,
90
+                             layout        = "rectangular",
91
+                             yscale        = "none",
92
+                             ladderize     = TRUE,
93
+                             right         = FALSE,
94
+                             branch.length = "branch.length",
95
+                             mrsd          = NULL,
96
+                             as.Date       = FALSE, ...) {
97
+
98
+    model <- set_branch_length(model, branch.length)
99
+
100
+    fortify.phylo(model, data,
101
+                  layout        = layout,
102
+                  yscale        = yscale,
103
+                  ladderize     = ladderize,
104
+                  right         = right,
105
+                  branch.length = branch.length,
106
+                  mrsd          = mrsd,
107
+                  as.Date       = as.Date, ...)
108