Browse code

now mapping parameter will passed to segment layer in geom_tiplab(align=T) <2017-06-19, Mon>

guangchuang yu authored on 19/06/2017 13:28:29
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,281 +0,0 @@
1
-
2
-## ##' read beast output
3
-## ##'
4
-## ##'
5
-## ##' @title read.beast
6
-## ##' @param file beast file
7
-## ##' @return \code{beast} object
8
-## ##' @importFrom ape read.nexus
9
-## ##' @export
10
-## ##' @author Guangchuang Yu \url{http://ygc.name}
11
-## ##' @examples
12
-## ##' file <- system.file("extdata/BEAST", "beast_mcc.tree", package="ggtree")
13
-## ##' read.beast(file)
14
-## read.beast <- function(file) {
15
-##     translation <- read.trans_beast(file)
16
-##     treetext <- read.treetext_beast(file)
17
-##     stats <- read.stats_beast(file)
18
-##     phylo <- read.nexus(file)
19
-
20
-##     if (length(treetext) == 1) {
21
-##         obj <- BEAST(file, treetext, translation, stats, phylo)
22
-##     } else {
23
-##         obj <- lapply(seq_along(treetext), function(i) {
24
-##             BEAST(file, treetext[i], translation, stats[[i]], phylo[[i]])
25
-##         })
26
-##         class(obj) <- "beastList"
27
-##     }
28
-##     return(obj)
29
-## }
30
-
31
-
32
-## BEAST <- function(file, treetext, translation, stats, phylo) {
33
-##     stats$node %<>% gsub("\"*'*", "", .)
34
-
35
-##     fields <- sub("_lower|_upper", "", names(stats)) %>% unique
36
-##     fields %<>% `[`(.!="node")
37
-
38
-##     phylo <- remove_quote_in_tree_label(phylo)
39
-
40
-##     obj <- new("beast",
41
-##                fields      = fields,
42
-##                treetext    = treetext,
43
-##                phylo       = phylo,
44
-##                translation = translation,
45
-##                stats       = stats,
46
-##                file        = filename(file)
47
-##                )
48
-##     return(obj)
49
-## }
50
-
51
-## remove_quote_in_tree_label <- function(phylo) {
52
-##     if (!is.null(phylo$node.label)) {
53
-##         phylo$node.label %<>% gsub("\"*'*", "", .)
54
-##     }
55
-##     if ( !is.null(phylo$tip.label)) {
56
-##         phylo$tip.label %<>% gsub("\"*'*", "", .)
57
-##     }
58
-##     return(phylo)
59
-## }
60
-
61
-
62
-## ##' @rdname get.fields-methods
63
-## ##' @exportMethod get.fields
64
-## setMethod("get.fields", signature(object="beast"),
65
-##           function(object, ...) {
66
-##               get.fields.tree(object)
67
-##           }
68
-##           )
69
-
70
-
71
-## read.treetext_beast <- function(file) {
72
-##     beast <- readLines(file)
73
-
74
-##     ii <- grep("[Bb]egin trees;", beast)
75
-##     jj <- grep("[Ee]nd;", beast)
76
-##     jj <- jj[jj > max(ii)][1]
77
-##     jj <- c(ii[-1], jj)
78
-
79
-##     trees <- sapply(seq_along(ii), function(i) {
80
-##         tree <- beast[(ii[i]+1):(jj[i]-1)]
81
-##         tree <- tree[grep("\\s*[Tt]ree", tree)]
82
-##         ## if (length(tree) > 1) {
83
-##         ##     tree <- paste0(tree, collapse='')
84
-##         ## }
85
-##         sub("[^(]*", "", tree)
86
-##     })
87
-
88
-##     return(trees)
89
-## }
90
-
91
-## read.trans_beast <- function(file) {
92
-##     beast <- readLines(file)
93
-##     i <- grep("TRANSLATE", beast, ignore.case = TRUE)
94
-##     if (length(i) == 0) {
95
-##         return(matrix())
96
-##     }
97
-##     end <- grep(";", beast)
98
-##     j <- end[end %>% `>`(i) %>% which %>% `[`(1)]
99
-##     trans <- beast[(i+1):j]
100
-##     trans %<>% gsub("\\t+", "", .)
101
-##     trans %<>% gsub(",|;", "", .)
102
-##     trans %<>% `[`(nzchar(trans))
103
-##     ## remove quote if strings were quoted
104
-##     trans %<>% gsub("'|\"", "",.)
105
-##     trans %<>% sapply(., strsplit, split="\\s+")
106
-##     trans %<>% do.call(rbind, .)
107
-##     ## trans is a matrix
108
-##     return(trans)
109
-## }
110
-
111
-
112
-## read.stats_beast <- function(file) {
113
-##     beast <- readLines(file)
114
-##     trees <- read.treetext_beast(file)
115
-##     if (length(trees) == 1) {
116
-##         return(read.stats_beast_internal(beast, trees))
117
-##     }
118
-##     lapply(trees, read.stats_beast_internal, beast=beast)
119
-## }
120
-
121
-## read.stats_beast_internal <- function(beast, tree) {
122
-##     tree2 <- gsub("\\[[^\\[]*\\]", "", tree)
123
-##     phylo <- read.tree(text = tree2)
124
-
125
-##     tree2 <- add_pseudo_nodelabel(phylo, tree2)
126
-
127
-##     ## node name corresponding to stats
128
-##     nn <- strsplit(tree2, split=",") %>% unlist %>%
129
-##         strsplit(., split="\\)") %>% unlist %>%
130
-##         gsub("\\(*", "", .) %>%
131
-##         gsub("[:;].*", "", .)
132
-
133
-##     phylo <- read.tree(text = tree2)
134
-##     root <- getRoot(phylo)
135
-##     nnode <- phylo$Nnode
136
-
137
-##     ## phylo2 <- read.nexus(file)
138
-##     ## treeinfo <- fortify.phylo(phylo)
139
-##     ## treeinfo2 <- fortify.phylo(phylo2)
140
-##     ## treeinfo$label2 <- NA
141
-##     ## treeinfo$label2[treeinfo$isTip] <- treeinfo2$node[as.numeric(treeinfo$label[treeinfo$isTip])]
142
-##     ## treeinfo$visited <- FALSE
143
-##     ## root <- getRoot(phylo2)
144
-##     ## treeinfo[root, "visited"] <- TRUE
145
-##     ## currentNode <- 1:Ntip(phylo2)
146
-##     ## while(any(treeinfo$visited == FALSE)) {
147
-##     ##     pNode <- c()
148
-##     ##     for (kk in currentNode) {
149
-##     ##         i <- which(treeinfo$label2 == kk)
150
-##     ##         treeinfo[i, "visited"] <- TRUE
151
-##     ##         j <- which(treeinfo2$node == kk)
152
-##     ##         ip <- treeinfo$parent[i]
153
-##     ##         if (ip != root) {
154
-##     ##             ii <- which(treeinfo$node == ip)
155
-##     ##             if (treeinfo$visited[ii] == FALSE) {
156
-##     ##                 jp <- treeinfo2$parent[j]
157
-##     ##                 jj <- which(treeinfo2$node == jp)
158
-##     ##                 treeinfo[ii, "label2"] <- treeinfo2[jj, "node"]
159
-##     ##                 pNode <- c(pNode, jp)
160
-##     ##             }
161
-##     ##             treeinfo[ii, "visited"] <- TRUE
162
-##     ##         }
163
-##     ##     }
164
-##     ##     currentNode <- unique(pNode)
165
-##     ## }
166
-##     ## treeinfo[root, "label2"] <- root
167
-##     ## ## convert nn to node that encoded in phylo2
168
-##     ## node <- treeinfo$label2[match(nn, treeinfo$label)]
169
-
170
-
171
-##     ####################################################
172
-##     ##                                                ##
173
-##     ##  after doing it in the hard way                ##
174
-##     ##  I finally figure out the following easy way   ##
175
-##     ##                                                ##
176
-##     ####################################################
177
-##     treeinfo <- fortify.phylo(phylo)
178
-
179
-##     if (any(grepl("TRANSLATE", beast, ignore.case = TRUE))) {
180
-##         label2 <- c(treeinfo[treeinfo$isTip, "label"],
181
-##                     root:(root+nnode-1))
182
-##         node <- label2[match(nn, treeinfo$label)]
183
-##     } else {
184
-##         node <- as.character(treeinfo$node[match(nn, treeinfo$label)])
185
-##     }
186
-
187
-##     ## stats <- unlist(strsplit(tree, "\\["))[-1]
188
-##     ## stats <- sub(":.+$", "", stats
189
-##     stats <- strsplit(tree, ":") %>% unlist
190
-##     names(stats) <- node
191
-##     stats <- stats[grep("\\[", stats)]
192
-##     stats <- sub("[^\\[]+\\[", "", stats)
193
-
194
-##     stats <- sub("^&", "", stats)
195
-##     stats <- sub("];*$", "", stats)
196
-
197
-##     stats2 <- lapply(stats, function(x) {
198
-##         y <- unlist(strsplit(x, ","))
199
-##         sidx <- grep("=\\{", y)
200
-##         eidx <- grep("\\}$", y)
201
-
202
-##         flag <- FALSE
203
-##         if (length(sidx) > 0) {
204
-##             flag <- TRUE
205
-##             SETS <- sapply(seq_along(sidx), function(k) {
206
-##                 p <- y[sidx[k]:eidx[k]]
207
-##                 gsub(".*=\\{", "", p) %>% gsub("\\}$", "", .) %>% list
208
-##             })
209
-##             names(SETS) <- gsub("=.*", "", y[sidx])
210
-
211
-##             kk <- sapply(seq_along(sidx), function(k) sidx[k]:eidx[k]) %>% unlist
212
-##             y <- y[-kk]
213
-##         }
214
-
215
-
216
-##         name <- gsub("=.*", "", y)
217
-##         val <- gsub(".*=", "", y) %>% gsub("^\\{", "", .) %>%
218
-##             gsub("\\}$", "", .)
219
-
220
-
221
-##         if (flag) {
222
-##             nn <- c(name, names(SETS))
223
-##         } else {
224
-##             nn <- name
225
-##         }
226
-
227
-##         res <- character(length(nn))
228
-##         names(res) <- nn
229
-
230
-##         for (i in seq_along(name)) {
231
-##             res[i] <- val[i]
232
-##         }
233
-##         if (flag) {
234
-##             j <- i
235
-##             for (i in seq_along(SETS)) {
236
-##                 res[i+j] <- SETS[i]
237
-##             }
238
-##         }
239
-
240
-##         return(res)
241
-##     })
242
-
243
-##     nn <- lapply(stats2, names) %>% unlist %>%
244
-##         unique %>% sort
245
-
246
-##     ## stats3 is a matrix
247
-##     stats3 <- t(sapply(stats2, function(x) {
248
-##         for (ii in nn[!nn %in% names(x)]) {
249
-##             x[ii] <- NA
250
-##         }
251
-##         x[nn]
252
-##     }))
253
-
254
-##     stats3 <- as.data.frame(stats3)
255
-##     if (nrow(stats3) == 1) {
256
-##         ## only has one evidence
257
-##         ## transpose
258
-##         stats3 <- data.frame(X=unlist(stats3[1,]))
259
-##         colnames(stats3) <- nn
260
-##     }
261
-##     colnames(stats3) <- gsub("(\\d+)%", "0.\\1", colnames(stats3))
262
-
263
-##     ## stats3$node <- node
264
-##     stats3$node <- names(stats)
265
-##     return(stats3)
266
-## }
267
-
268
-## add_pseudo_nodelabel <- function(phylo, treetext) {
269
-##     if(is.null(phylo$node.label)) {
270
-##         nnode <- phylo$Nnode
271
-##         nlab <- paste("X", 1:nnode, sep="")
272
-##         for (i in 1:nnode) {
273
-##             treetext <- sub("\\)([:;])", paste0("\\)", nlab[i], "\\1"), treetext)
274
-##         }
275
-##     }
276
-
277
-##    return(treetext)
278
-## }
279
-
280
-
281
-
Browse code

depends treeio

GuangchuangYu authored on 20/12/2016 16:39:07
Showing 1 changed files
... ...
@@ -1,281 +1,281 @@
1 1
 
2
-##' read beast output
3
-##'
4
-##'
5
-##' @title read.beast
6
-##' @param file beast file
7
-##' @return \code{beast} object
8
-##' @importFrom ape read.nexus
9
-##' @export
10
-##' @author Guangchuang Yu \url{http://ygc.name}
11
-##' @examples
12
-##' file <- system.file("extdata/BEAST", "beast_mcc.tree", package="ggtree")
13
-##' read.beast(file)
14
-read.beast <- function(file) {
15
-    translation <- read.trans_beast(file)
16
-    treetext <- read.treetext_beast(file)
17
-    stats <- read.stats_beast(file)
18
-    phylo <- read.nexus(file)
19
-
20
-    if (length(treetext) == 1) {
21
-        obj <- BEAST(file, treetext, translation, stats, phylo)
22
-    } else {
23
-        obj <- lapply(seq_along(treetext), function(i) {
24
-            BEAST(file, treetext[i], translation, stats[[i]], phylo[[i]])
25
-        })
26
-        class(obj) <- "beastList"
27
-    }
28
-    return(obj)
29
-}
30
-
31
-
32
-BEAST <- function(file, treetext, translation, stats, phylo) {
33
-    stats$node %<>% gsub("\"*'*", "", .)
34
-
35
-    fields <- sub("_lower|_upper", "", names(stats)) %>% unique
36
-    fields %<>% `[`(.!="node")
37
-
38
-    phylo <- remove_quote_in_tree_label(phylo)
39
-
40
-    obj <- new("beast",
41
-               fields      = fields,
42
-               treetext    = treetext,
43
-               phylo       = phylo,
44
-               translation = translation,
45
-               stats       = stats,
46
-               file        = filename(file)
47
-               )
48
-    return(obj)
49
-}
50
-
51
-remove_quote_in_tree_label <- function(phylo) {
52
-    if (!is.null(phylo$node.label)) {
53
-        phylo$node.label %<>% gsub("\"*'*", "", .)
54
-    }
55
-    if ( !is.null(phylo$tip.label)) {
56
-        phylo$tip.label %<>% gsub("\"*'*", "", .)
57
-    }
58
-    return(phylo)
59
-}
60
-
61
-
62
-##' @rdname get.fields-methods
63
-##' @exportMethod get.fields
64
-setMethod("get.fields", signature(object="beast"),
65
-          function(object, ...) {
66
-              get.fields.tree(object)
67
-          }
68
-          )
69
-
70
-
71
-read.treetext_beast <- function(file) {
72
-    beast <- readLines(file)
73
-
74
-    ii <- grep("[Bb]egin trees;", beast)
75
-    jj <- grep("[Ee]nd;", beast)
76
-    jj <- jj[jj > max(ii)][1]
77
-    jj <- c(ii[-1], jj)
78
-
79
-    trees <- sapply(seq_along(ii), function(i) {
80
-        tree <- beast[(ii[i]+1):(jj[i]-1)]
81
-        tree <- tree[grep("\\s*[Tt]ree", tree)]
82
-        ## if (length(tree) > 1) {
83
-        ##     tree <- paste0(tree, collapse='')
84
-        ## }
85
-        sub("[^(]*", "", tree)
86
-    })
87
-
88
-    return(trees)
89
-}
90
-
91
-read.trans_beast <- function(file) {
92
-    beast <- readLines(file)
93
-    i <- grep("TRANSLATE", beast, ignore.case = TRUE)
94
-    if (length(i) == 0) {
95
-        return(matrix())
96
-    }
97
-    end <- grep(";", beast)
98
-    j <- end[end %>% `>`(i) %>% which %>% `[`(1)]
99
-    trans <- beast[(i+1):j]
100
-    trans %<>% gsub("\\t+", "", .)
101
-    trans %<>% gsub(",|;", "", .)
102
-    trans %<>% `[`(nzchar(trans))
103
-    ## remove quote if strings were quoted
104
-    trans %<>% gsub("'|\"", "",.)
105
-    trans %<>% sapply(., strsplit, split="\\s+")
106
-    trans %<>% do.call(rbind, .)
107
-    ## trans is a matrix
108
-    return(trans)
109
-}
110
-
111
-
112
-read.stats_beast <- function(file) {
113
-    beast <- readLines(file)
114
-    trees <- read.treetext_beast(file)
115
-    if (length(trees) == 1) {
116
-        return(read.stats_beast_internal(beast, trees))
117
-    }
118
-    lapply(trees, read.stats_beast_internal, beast=beast)
119
-}
120
-
121
-read.stats_beast_internal <- function(beast, tree) {
122
-    tree2 <- gsub("\\[[^\\[]*\\]", "", tree)
123
-    phylo <- read.tree(text = tree2)
124
-
125
-    tree2 <- add_pseudo_nodelabel(phylo, tree2)
126
-
127
-    ## node name corresponding to stats
128
-    nn <- strsplit(tree2, split=",") %>% unlist %>%
129
-        strsplit(., split="\\)") %>% unlist %>%
130
-        gsub("\\(*", "", .) %>%
131
-        gsub("[:;].*", "", .)
132
-
133
-    phylo <- read.tree(text = tree2)
134
-    root <- getRoot(phylo)
135
-    nnode <- phylo$Nnode
136
-
137
-    ## phylo2 <- read.nexus(file)
138
-    ## treeinfo <- fortify.phylo(phylo)
139
-    ## treeinfo2 <- fortify.phylo(phylo2)
140
-    ## treeinfo$label2 <- NA
141
-    ## treeinfo$label2[treeinfo$isTip] <- treeinfo2$node[as.numeric(treeinfo$label[treeinfo$isTip])]
142
-    ## treeinfo$visited <- FALSE
143
-    ## root <- getRoot(phylo2)
144
-    ## treeinfo[root, "visited"] <- TRUE
145
-    ## currentNode <- 1:Ntip(phylo2)
146
-    ## while(any(treeinfo$visited == FALSE)) {
147
-    ##     pNode <- c()
148
-    ##     for (kk in currentNode) {
149
-    ##         i <- which(treeinfo$label2 == kk)
150
-    ##         treeinfo[i, "visited"] <- TRUE
151
-    ##         j <- which(treeinfo2$node == kk)
152
-    ##         ip <- treeinfo$parent[i]
153
-    ##         if (ip != root) {
154
-    ##             ii <- which(treeinfo$node == ip)
155
-    ##             if (treeinfo$visited[ii] == FALSE) {
156
-    ##                 jp <- treeinfo2$parent[j]
157
-    ##                 jj <- which(treeinfo2$node == jp)
158
-    ##                 treeinfo[ii, "label2"] <- treeinfo2[jj, "node"]
159
-    ##                 pNode <- c(pNode, jp)
160
-    ##             }
161
-    ##             treeinfo[ii, "visited"] <- TRUE
162
-    ##         }
163
-    ##     }
164
-    ##     currentNode <- unique(pNode)
165
-    ## }
166
-    ## treeinfo[root, "label2"] <- root
167
-    ## ## convert nn to node that encoded in phylo2
168
-    ## node <- treeinfo$label2[match(nn, treeinfo$label)]
169
-
170
-
171
-    ####################################################
172
-    ##                                                ##
173
-    ##  after doing it in the hard way                ##
174
-    ##  I finally figure out the following easy way   ##
175
-    ##                                                ##
176
-    ####################################################
177
-    treeinfo <- fortify.phylo(phylo)
178
-
179
-    if (any(grepl("TRANSLATE", beast, ignore.case = TRUE))) {
180
-        label2 <- c(treeinfo[treeinfo$isTip, "label"],
181
-                    root:(root+nnode-1))
182
-        node <- label2[match(nn, treeinfo$label)]
183
-    } else {
184
-        node <- as.character(treeinfo$node[match(nn, treeinfo$label)])
185
-    }
186
-
187
-    ## stats <- unlist(strsplit(tree, "\\["))[-1]
188
-    ## stats <- sub(":.+$", "", stats
189
-    stats <- strsplit(tree, ":") %>% unlist
190
-    names(stats) <- node
191
-    stats <- stats[grep("\\[", stats)]
192
-    stats <- sub("[^\\[]+\\[", "", stats)
193
-
194
-    stats <- sub("^&", "", stats)
195
-    stats <- sub("];*$", "", stats)
196
-
197
-    stats2 <- lapply(stats, function(x) {
198
-        y <- unlist(strsplit(x, ","))
199
-        sidx <- grep("=\\{", y)
200
-        eidx <- grep("\\}$", y)
201
-
202
-        flag <- FALSE
203
-        if (length(sidx) > 0) {
204
-            flag <- TRUE
205
-            SETS <- sapply(seq_along(sidx), function(k) {
206
-                p <- y[sidx[k]:eidx[k]]
207
-                gsub(".*=\\{", "", p) %>% gsub("\\}$", "", .) %>% list
208
-            })
209
-            names(SETS) <- gsub("=.*", "", y[sidx])
210
-
211
-            kk <- sapply(seq_along(sidx), function(k) sidx[k]:eidx[k]) %>% unlist
212
-            y <- y[-kk]
213
-        }
214
-
215
-
216
-        name <- gsub("=.*", "", y)
217
-        val <- gsub(".*=", "", y) %>% gsub("^\\{", "", .) %>%
218
-            gsub("\\}$", "", .)
219
-
220
-
221
-        if (flag) {
222
-            nn <- c(name, names(SETS))
223
-        } else {
224
-            nn <- name
225
-        }
226
-
227
-        res <- character(length(nn))
228
-        names(res) <- nn
229
-
230
-        for (i in seq_along(name)) {
231
-            res[i] <- val[i]
232
-        }
233
-        if (flag) {
234
-            j <- i
235
-            for (i in seq_along(SETS)) {
236
-                res[i+j] <- SETS[i]
237
-            }
238
-        }
239
-
240
-        return(res)
241
-    })
242
-
243
-    nn <- lapply(stats2, names) %>% unlist %>%
244
-        unique %>% sort
245
-
246
-    ## stats3 is a matrix
247
-    stats3 <- t(sapply(stats2, function(x) {
248
-        for (ii in nn[!nn %in% names(x)]) {
249
-            x[ii] <- NA
250
-        }
251
-        x[nn]
252
-    }))
253
-
254
-    stats3 <- as.data.frame(stats3)
255
-    if (nrow(stats3) == 1) {
256
-        ## only has one evidence
257
-        ## transpose
258
-        stats3 <- data.frame(X=unlist(stats3[1,]))
259
-        colnames(stats3) <- nn
260
-    }
261
-    colnames(stats3) <- gsub("(\\d+)%", "0.\\1", colnames(stats3))
262
-
263
-    ## stats3$node <- node
264
-    stats3$node <- names(stats)
265
-    return(stats3)
266
-}
267
-
268
-add_pseudo_nodelabel <- function(phylo, treetext) {
269
-    if(is.null(phylo$node.label)) {
270
-        nnode <- phylo$Nnode
271
-        nlab <- paste("X", 1:nnode, sep="")
272
-        for (i in 1:nnode) {
273
-            treetext <- sub("\\)([:;])", paste0("\\)", nlab[i], "\\1"), treetext)
274
-        }
275
-    }
276
-
277
-   return(treetext)
278
-}
2
+## ##' read beast output
3
+## ##'
4
+## ##'
5
+## ##' @title read.beast
6
+## ##' @param file beast file
7
+## ##' @return \code{beast} object
8
+## ##' @importFrom ape read.nexus
9
+## ##' @export
10
+## ##' @author Guangchuang Yu \url{http://ygc.name}
11
+## ##' @examples
12
+## ##' file <- system.file("extdata/BEAST", "beast_mcc.tree", package="ggtree")
13
+## ##' read.beast(file)
14
+## read.beast <- function(file) {
15
+##     translation <- read.trans_beast(file)
16
+##     treetext <- read.treetext_beast(file)
17
+##     stats <- read.stats_beast(file)
18
+##     phylo <- read.nexus(file)
19
+
20
+##     if (length(treetext) == 1) {
21
+##         obj <- BEAST(file, treetext, translation, stats, phylo)
22
+##     } else {
23
+##         obj <- lapply(seq_along(treetext), function(i) {
24
+##             BEAST(file, treetext[i], translation, stats[[i]], phylo[[i]])
25
+##         })
26
+##         class(obj) <- "beastList"
27
+##     }
28
+##     return(obj)
29
+## }
30
+
31
+
32
+## BEAST <- function(file, treetext, translation, stats, phylo) {
33
+##     stats$node %<>% gsub("\"*'*", "", .)
34
+
35
+##     fields <- sub("_lower|_upper", "", names(stats)) %>% unique
36
+##     fields %<>% `[`(.!="node")
37
+
38
+##     phylo <- remove_quote_in_tree_label(phylo)
39
+
40
+##     obj <- new("beast",
41
+##                fields      = fields,
42
+##                treetext    = treetext,
43
+##                phylo       = phylo,
44
+##                translation = translation,
45
+##                stats       = stats,
46
+##                file        = filename(file)
47
+##                )
48
+##     return(obj)
49
+## }
50
+
51
+## remove_quote_in_tree_label <- function(phylo) {
52
+##     if (!is.null(phylo$node.label)) {
53
+##         phylo$node.label %<>% gsub("\"*'*", "", .)
54
+##     }
55
+##     if ( !is.null(phylo$tip.label)) {
56
+##         phylo$tip.label %<>% gsub("\"*'*", "", .)
57
+##     }
58
+##     return(phylo)
59
+## }
60
+
61
+
62
+## ##' @rdname get.fields-methods
63
+## ##' @exportMethod get.fields
64
+## setMethod("get.fields", signature(object="beast"),
65
+##           function(object, ...) {
66
+##               get.fields.tree(object)
67
+##           }
68
+##           )
69
+
70
+
71
+## read.treetext_beast <- function(file) {
72
+##     beast <- readLines(file)
73
+
74
+##     ii <- grep("[Bb]egin trees;", beast)
75
+##     jj <- grep("[Ee]nd;", beast)
76
+##     jj <- jj[jj > max(ii)][1]
77
+##     jj <- c(ii[-1], jj)
78
+
79
+##     trees <- sapply(seq_along(ii), function(i) {
80
+##         tree <- beast[(ii[i]+1):(jj[i]-1)]
81
+##         tree <- tree[grep("\\s*[Tt]ree", tree)]
82
+##         ## if (length(tree) > 1) {
83
+##         ##     tree <- paste0(tree, collapse='')
84
+##         ## }
85
+##         sub("[^(]*", "", tree)
86
+##     })
87
+
88
+##     return(trees)
89
+## }
90
+
91
+## read.trans_beast <- function(file) {
92
+##     beast <- readLines(file)
93
+##     i <- grep("TRANSLATE", beast, ignore.case = TRUE)
94
+##     if (length(i) == 0) {
95
+##         return(matrix())
96
+##     }
97
+##     end <- grep(";", beast)
98
+##     j <- end[end %>% `>`(i) %>% which %>% `[`(1)]
99
+##     trans <- beast[(i+1):j]
100
+##     trans %<>% gsub("\\t+", "", .)
101
+##     trans %<>% gsub(",|;", "", .)
102
+##     trans %<>% `[`(nzchar(trans))
103
+##     ## remove quote if strings were quoted
104
+##     trans %<>% gsub("'|\"", "",.)
105
+##     trans %<>% sapply(., strsplit, split="\\s+")
106
+##     trans %<>% do.call(rbind, .)
107
+##     ## trans is a matrix
108
+##     return(trans)
109
+## }
110
+
111
+
112
+## read.stats_beast <- function(file) {
113
+##     beast <- readLines(file)
114
+##     trees <- read.treetext_beast(file)
115
+##     if (length(trees) == 1) {
116
+##         return(read.stats_beast_internal(beast, trees))
117
+##     }
118
+##     lapply(trees, read.stats_beast_internal, beast=beast)
119
+## }
120
+
121
+## read.stats_beast_internal <- function(beast, tree) {
122
+##     tree2 <- gsub("\\[[^\\[]*\\]", "", tree)
123
+##     phylo <- read.tree(text = tree2)
124
+
125
+##     tree2 <- add_pseudo_nodelabel(phylo, tree2)
126
+
127
+##     ## node name corresponding to stats
128
+##     nn <- strsplit(tree2, split=",") %>% unlist %>%
129
+##         strsplit(., split="\\)") %>% unlist %>%
130
+##         gsub("\\(*", "", .) %>%
131
+##         gsub("[:;].*", "", .)
132
+
133
+##     phylo <- read.tree(text = tree2)
134
+##     root <- getRoot(phylo)
135
+##     nnode <- phylo$Nnode
136
+
137
+##     ## phylo2 <- read.nexus(file)
138
+##     ## treeinfo <- fortify.phylo(phylo)
139
+##     ## treeinfo2 <- fortify.phylo(phylo2)
140
+##     ## treeinfo$label2 <- NA
141
+##     ## treeinfo$label2[treeinfo$isTip] <- treeinfo2$node[as.numeric(treeinfo$label[treeinfo$isTip])]
142
+##     ## treeinfo$visited <- FALSE
143
+##     ## root <- getRoot(phylo2)
144
+##     ## treeinfo[root, "visited"] <- TRUE
145
+##     ## currentNode <- 1:Ntip(phylo2)
146
+##     ## while(any(treeinfo$visited == FALSE)) {
147
+##     ##     pNode <- c()
148
+##     ##     for (kk in currentNode) {
149
+##     ##         i <- which(treeinfo$label2 == kk)
150
+##     ##         treeinfo[i, "visited"] <- TRUE
151
+##     ##         j <- which(treeinfo2$node == kk)
152
+##     ##         ip <- treeinfo$parent[i]
153
+##     ##         if (ip != root) {
154
+##     ##             ii <- which(treeinfo$node == ip)
155
+##     ##             if (treeinfo$visited[ii] == FALSE) {
156
+##     ##                 jp <- treeinfo2$parent[j]
157
+##     ##                 jj <- which(treeinfo2$node == jp)
158
+##     ##                 treeinfo[ii, "label2"] <- treeinfo2[jj, "node"]
159
+##     ##                 pNode <- c(pNode, jp)
160
+##     ##             }
161
+##     ##             treeinfo[ii, "visited"] <- TRUE
162
+##     ##         }
163
+##     ##     }
164
+##     ##     currentNode <- unique(pNode)
165
+##     ## }
166
+##     ## treeinfo[root, "label2"] <- root
167
+##     ## ## convert nn to node that encoded in phylo2
168
+##     ## node <- treeinfo$label2[match(nn, treeinfo$label)]
169
+
170
+
171
+##     ####################################################
172
+##     ##                                                ##
173
+##     ##  after doing it in the hard way                ##
174
+##     ##  I finally figure out the following easy way   ##
175
+##     ##                                                ##
176
+##     ####################################################
177
+##     treeinfo <- fortify.phylo(phylo)
178
+
179
+##     if (any(grepl("TRANSLATE", beast, ignore.case = TRUE))) {
180
+##         label2 <- c(treeinfo[treeinfo$isTip, "label"],
181
+##                     root:(root+nnode-1))
182
+##         node <- label2[match(nn, treeinfo$label)]
183
+##     } else {
184
+##         node <- as.character(treeinfo$node[match(nn, treeinfo$label)])
185
+##     }
186
+
187
+##     ## stats <- unlist(strsplit(tree, "\\["))[-1]
188
+##     ## stats <- sub(":.+$", "", stats
189
+##     stats <- strsplit(tree, ":") %>% unlist
190
+##     names(stats) <- node
191
+##     stats <- stats[grep("\\[", stats)]
192
+##     stats <- sub("[^\\[]+\\[", "", stats)
193
+
194
+##     stats <- sub("^&", "", stats)
195
+##     stats <- sub("];*$", "", stats)
196
+
197
+##     stats2 <- lapply(stats, function(x) {
198
+##         y <- unlist(strsplit(x, ","))
199
+##         sidx <- grep("=\\{", y)
200
+##         eidx <- grep("\\}$", y)
201
+
202
+##         flag <- FALSE
203
+##         if (length(sidx) > 0) {
204
+##             flag <- TRUE
205
+##             SETS <- sapply(seq_along(sidx), function(k) {
206
+##                 p <- y[sidx[k]:eidx[k]]
207
+##                 gsub(".*=\\{", "", p) %>% gsub("\\}$", "", .) %>% list
208
+##             })
209
+##             names(SETS) <- gsub("=.*", "", y[sidx])
210
+
211
+##             kk <- sapply(seq_along(sidx), function(k) sidx[k]:eidx[k]) %>% unlist
212
+##             y <- y[-kk]
213
+##         }
214
+
215
+
216
+##         name <- gsub("=.*", "", y)
217
+##         val <- gsub(".*=", "", y) %>% gsub("^\\{", "", .) %>%
218
+##             gsub("\\}$", "", .)
219
+
220
+
221
+##         if (flag) {
222
+##             nn <- c(name, names(SETS))
223
+##         } else {
224
+##             nn <- name
225
+##         }
226
+
227
+##         res <- character(length(nn))
228
+##         names(res) <- nn
229
+
230
+##         for (i in seq_along(name)) {
231
+##             res[i] <- val[i]
232
+##         }
233
+##         if (flag) {
234
+##             j <- i
235
+##             for (i in seq_along(SETS)) {
236
+##                 res[i+j] <- SETS[i]
237
+##             }
238
+##         }
239
+
240
+##         return(res)
241
+##     })
242
+
243
+##     nn <- lapply(stats2, names) %>% unlist %>%
244
+##         unique %>% sort
245
+
246
+##     ## stats3 is a matrix
247
+##     stats3 <- t(sapply(stats2, function(x) {
248
+##         for (ii in nn[!nn %in% names(x)]) {
249
+##             x[ii] <- NA
250
+##         }
251
+##         x[nn]
252
+##     }))
253
+
254
+##     stats3 <- as.data.frame(stats3)
255
+##     if (nrow(stats3) == 1) {
256
+##         ## only has one evidence
257
+##         ## transpose
258
+##         stats3 <- data.frame(X=unlist(stats3[1,]))
259
+##         colnames(stats3) <- nn
260
+##     }
261
+##     colnames(stats3) <- gsub("(\\d+)%", "0.\\1", colnames(stats3))
262
+
263
+##     ## stats3$node <- node
264
+##     stats3$node <- names(stats)
265
+##     return(stats3)
266
+## }
267
+
268
+## add_pseudo_nodelabel <- function(phylo, treetext) {
269
+##     if(is.null(phylo$node.label)) {
270
+##         nnode <- phylo$Nnode
271
+##         nlab <- paste("X", 1:nnode, sep="")
272
+##         for (i in 1:nnode) {
273
+##             treetext <- sub("\\)([:;])", paste0("\\)", nlab[i], "\\1"), treetext)
274
+##         }
275
+##     }
276
+
277
+##    return(treetext)
278
+## }
279 279
 
280 280
 
281 281
 
Browse code

bug fixed of parsing treetext in beast file

guangchuang yu authored on 31/10/2016 11:29:54
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 
2 2
 ##' read beast output
3 3
 ##'
4
-##' 
4
+##'
5 5
 ##' @title read.beast
6 6
 ##' @param file beast file
7 7
 ##' @return \code{beast} object
... ...
@@ -16,7 +16,7 @@ read.beast <- function(file) {
16 16
     treetext <- read.treetext_beast(file)
17 17
     stats <- read.stats_beast(file)
18 18
     phylo <- read.nexus(file)
19
-    
19
+
20 20
     if (length(treetext) == 1) {
21 21
         obj <- BEAST(file, treetext, translation, stats, phylo)
22 22
     } else {
... ...
@@ -31,12 +31,12 @@ read.beast <- function(file) {
31 31
 
32 32
 BEAST <- function(file, treetext, translation, stats, phylo) {
33 33
     stats$node %<>% gsub("\"*'*", "", .)
34
-    
34
+
35 35
     fields <- sub("_lower|_upper", "", names(stats)) %>% unique
36 36
     fields %<>% `[`(.!="node")
37
-        
37
+
38 38
     phylo <- remove_quote_in_tree_label(phylo)
39
-        
39
+
40 40
     obj <- new("beast",
41 41
                fields      = fields,
42 42
                treetext    = treetext,
... ...
@@ -53,7 +53,7 @@ remove_quote_in_tree_label <- function(phylo) {
53 53
         phylo$node.label %<>% gsub("\"*'*", "", .)
54 54
     }
55 55
     if ( !is.null(phylo$tip.label)) {
56
-        phylo$tip.label %<>% gsub("\"*'*", "", .) 
56
+        phylo$tip.label %<>% gsub("\"*'*", "", .)
57 57
     }
58 58
     return(phylo)
59 59
 }
... ...
@@ -70,29 +70,18 @@ setMethod("get.fields", signature(object="beast"),
70 70
 
71 71
 read.treetext_beast <- function(file) {
72 72
     beast <- readLines(file)
73
-    ## ii <- grep("^tree TREE1\\s+=", beast)
74
-    ii <- grep("^tree ", beast)
75
-    if (length(ii) == 0) {
76
-        ii <- grep("[Bb]egin trees;", beast)
77
-    }
78
-    
73
+
74
+    ii <- grep("[Bb]egin trees;", beast)
79 75
     jj <- grep("[Ee]nd;", beast)
80 76
     jj <- jj[jj > max(ii)][1]
81