Browse code

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

Commit information:

Commit id: e468139bc733aa519c485a3c72ba41dcb9ea6fc3

read.beast now supports support values of sets such as {x, y, z} <2015-01-19, Mon>

Committed by: GuangchuangYu
Author Name: GuangchuangYu
Commit date: 2015-01-19 12:05:19 +0800
Author date: 2015-01-19 12:05:19 +0800


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

g.yu authored on 19/01/2015 04:05:44
Showing 4 changed files

... ...
@@ -1,6 +1,7 @@
1 1
 CHANGES IN VERSION 0.99.6
2 2
 ------------------------
3
- o now read.beast support characters in support values <2015-01-18, Sun>
3
+ o read.beast now supports support values of sets such as {x, y, z} <2015-01-19, Mon>
4
+ o now read.beast supports characters in support values <2015-01-18, Sun>
4 5
  o add example of gzoom and groupOTU in vignette <2015-01-14, Wed> 
5 6
  o implement groupOTU methods <2015-01-14, Wed>
6 7
  o export get.offspring.tip <2015-01-14, Wed>
... ...
@@ -223,89 +223,44 @@ read.stats_beast <- function(file) {
223 223
         
224 224
     stats2 <- lapply(stats, function(x) {
225 225
         y <- unlist(strsplit(x, ","))
226
-        idx <- grep("=\\{", y)
227
-        if (length(idx) > 0) {
228
-            si.idx <- grep("\\}$", y[idx]) ## single item {x}
229
-            if (length(si.idx) > 0) {
230
-                y[idx[si.idx]] %<>% sub("\\{", "", .) %>% sub("\\}$", "", .)
231
-                idx <- idx[-si.idx]
232
-            }
233
-        }
234
-        hasRange <- FALSE
235
-        if (length(idx) > 0) {
236
-            ii <- grep("\\}$", y[idx+1])
237
-            if (length(ii) > 0) {
238
-                idx <- idx[ii]
239
-                hasRange <- TRUE
240
-            }
241
-        }
242
-        
243
-        if (hasRange) {
244
-            names.range <- gsub("=\\{.*", "", y[idx])
245
-            nr.lower <- paste0(names.range, "_lower")
246
-            nr.upper <- paste0(names.range, "_upper")
247
-            
248
-            lo <- gsub(".*=\\{", "", y[idx])
249
-            hi <- gsub("\\}", "", y[idx+1])
250
-        
251
-            jj <- -c(idx, idx+1)
252
-        } else {
253
-            jj <- seq_along(y)
254
-        }
255
-        y <- y[jj]
226
+        sidx <- grep("=\\{", y)
227
+        eidx <- grep("\\}$", y)
256 228
 
257
-        a <- grep("\\{", y)
258
-        b <- grep("\\}", y)
259 229
         flag <- FALSE
260
-        if (length(a) > 0) {
230
+        if (length(sidx) > 0) {
261 231
             flag <- TRUE
262
-            m <- sapply(seq_along(a), function(k) {
263
-                p <- y[a[k]:b[k]]
264
-                ii <- c(ii,i) 
265
-                gsub(".*=\\{", "", p) %>% gsub("\\}$", "", .) %>% list
232
+            SETS <- sapply(seq_along(sidx), function(k) {
233
+                p <- y[sidx[k]:eidx[k]]
234
+                gsub(".*=\\{", "", p) %>% gsub("\\}$", "", .) %>% list                
266 235
             })
267
-            names(m) <- gsub("=.*", "", y[a])
268
-        }
236
+            names(SETS) <- gsub("=.*", "", y[sidx])
269 237
 
270
-        if (flag) {
271
-            k <- sapply(seq_along(a), function(i) a[i]:b[i]) %>% as.vector
272
-            y <- y[-k]
238
+            kk <- sapply(seq_along(sidx), function(k) sidx[k]:eidx[k]) %>% unlist
239
+            y <- y[-kk]
273 240
         }
274 241
         
275
-        names <- gsub("=.*", "", y)
242
+        
243
+        name <- gsub("=.*", "", y)
276 244
         val <- gsub(".*=", "", y) %>% gsub("^\\{", "", .) %>%
277 245
             gsub("\\}$", "", .) 
278 246
 
279
-        if (hasRange) {
280
-            nn <- c(nr.lower, nr.upper, names)
281
-        } else {
282
-            nn <- names
283
-        }
247
+
284 248
         if (flag) {
285
-            nn <- c(nn, names(m))
249
+            nn <- c(name, names(SETS))
250
+        } else {
251
+            nn <- name
286 252
         }
287 253
         
288 254
         res <- character(length(nn))
289 255
         names(res) <- nn
290
-        if (hasRange) {
291
-            for (i in seq_along(nr.lower)) {
292
-                res[i] <- lo[i]
293
-            }
294
-            j <- i
295
-            for (i in seq_along(nr.upper)) {
296
-                res[i+j] <- hi[i] 
297
-            }
298
-            j <- i+j
299
-        } else {
300
-            j <- 0
301
-        }
302
-        for (i in seq_along(names)) {
303
-            res[i+j] <- val[i]
256
+
257
+        for (i in seq_along(name)) {
258
+            res[i] <- val[i]
304 259
         }
305 260
         if (flag) {
306
-            j <- i+j
307
-            for (i in seq_along(m)) {
308
-                res[i+j] <- m[i]
261
+            j <- i
262
+            for (i in seq_along(SETS)) {
263
+                res[i+j] <- SETS[i]
309 264
             }
310 265
         }
311 266
         
... ...
@@ -324,42 +279,9 @@ read.stats_beast <- function(file) {
324 279
     }))
325 280
 
326 281
     stats3 <- as.data.frame(stats3)
327
-
328
-    hasQuote <- function(stats3, cn) {
329
-        for (i in 1:nrow(stats3)) {
330
-            if ( is.na(stats3[i,cn]) ) {
331
-                next
332
-            } else {
333
-                return(grepl("\"", stats3[i, cn]))
334
-            }
335
-        }
336
-    }
337
-    
338
-    for (j in 1:ncol(stats3)) {
339
-        if (hasQuote(stats3,j)) {
340
-            next
341
-        } else {
342
-            len <- sapply(stats3[,j], length)
343
-            if (any(len > 1)) {
344
-                next
345
-                ## for (i in 1:length(stats3[,j])) {
346
-                ##     print(i)
347
-                ##     stats3[i,j] %<>% unlist %<>%
348
-                ##           as.character %<>% as.numeric %<>% c
349
-                ## }
350
-            } else {
351
-                stats3[,j] %<>% as.character
352
-                i <- which(stats3[,j] == "NA")
353
-                if(length(i) > 0) {
354
-                    stats3[i,j] <- NA
355
-                }
356
-                stats3[,j] %<>% as.numeric
357
-            }
358
-        }
359
-    }
282
+    colnames(stats3) <- gsub("(\\d+)%", "0.\\1", colnames(stats3))
360 283
     
361 284
     stats3$node <- node
362
-    colnames(stats3) <- gsub("(\\d+)%", "0.\\1", colnames(stats3))
363 285
     return(stats3)
364 286
 }
365 287
 
... ...
@@ -115,20 +115,64 @@ fortify.beast <- function(model, data,
115 115
     
116 116
     stats <- model@stats
117 117
 
118
-    if (!is.null(ndigits)) {
119
-        idx <- which(colnames(stats) != "node")
120
-        for (ii in idx) {
121
-            if (is.numeric(stats[, ii])) {
122
-                stats[, ii] <- round(stats[,ii], ndigits)
118
+    idx <- which(colnames(stats) != "node")
119
+    for (ii in idx) {
120
+        if (is.character_beast(stats, ii)) {
121
+            len <- sapply(stats[,ii], length)
122
+            if (any(len > 1)) {
123
+                stats[,ii] %<>% sapply(., function(x) {
124
+                    y <- unlist(x) %>% as.character %>% gsub("\"", "", .)
125
+                    if (length(y) == 1) {
126
+                        return(y)
127
+                    } else {
128
+                        return(paste0('{', paste0(y, collapse = ','), '}'))
129
+                    }
130
+                })
131
+            } else {
132
+                stats[,ii] %<>% unlist %>% as.character %>% gsub("\"", "", .)
123 133
             }
134
+            next
135
+        }
136
+        
137
+        len <- sapply(stats[,ii], length)
138
+        if ( all(len == 1) ) {
139
+            stats[, ii] %<>% unlist %>% as.character %>% as.numeric
140
+            if (!is.null(ndigits)) {
141
+                stats[, ii] %<>% round(., ndigits)
142
+            }
143
+        } else if (all(len <= 2)) {
144
+            stats[, ii] %<>% sapply(., function(x) {
145
+                y <- unlist(x) %>% as.character %>% as.numeric
146
+                if (!is.null(ndigits)) {
147
+                    y %<>% round(., ndigits)
148
+                }
149
+                if (length(y) == 1) {
150
+                    return(y)
151
+                } else {
152
+                    return(paste0('[', paste0(y, collapse = ','), ']'))
153
+                }
154
+            })
155
+        } else {
156
+            stats[,ii] %<>% sapply(., function(x) {
157
+                y <- unlist(x) %>% as.character %>% as.numeric
158
+                if (!is.null(ndigits)) {
159
+                    y %<>% round(., ndigits)
160
+                }
161
+                if (length(y) == 1) {
162
+                    return(y)
163
+                } else {
164
+                    return(paste0('{', paste0(y, collapse = ','), '}'))
165
+                }
166
+            })  
124 167
         }
125 168
     }
126
-
169
+            
170
+      
127 171
     cn <- colnames(stats)
128 172
     lo <- cn[grep("_lower", cn)]
129 173
     hi <- gsub("lower$", "upper", lo)
130 174
     rid <- gsub("_lower$", "", lo)
131
-
175
+    
132 176
     for (i in seq_along(rid)) {
133 177
         stats[, rid[i]] <- paste0("[", stats[, lo[i]], ",", stats[, hi[i]], "]")
134 178
         stats[is.na(stats[, lo[i]]), rid[i]] <- NA
... ...
@@ -186,6 +186,20 @@ extract.treeinfo.jplace <- function(object, layout="phylogram", ladderize=TRUE,
186 186
     return(df)
187 187
 }
188 188
 
189
+
190
+is.character_beast <- function(stats3, cn) {
191
+    for (i in 1:nrow(stats3)) {
192
+        if ( is.na(stats3[i,cn]) ) {
193
+            next
194
+        } else {
195
+            res <- grepl("[a-df-zA-DF-Z]+", unlist(stats3[i, cn]))
196
+            return(all(res == TRUE))
197
+        }
198
+    }
199
+    return(FALSE)
200
+}
201
+
202
+
189 203
 is.tree <- function(x) {
190 204
     if (class(x) %in% c("phylo",
191 205
                         "phylo4",