Browse code

update msaplot to use DNAbin/AAbin

guangchuang yu authored on 14/12/2017 10:29:44
Showing 7 changed files

... ...
@@ -13,4 +13,6 @@ __init__.pyc
13 13
 .web_cache
14 14
 ggtree.Rproj
15 15
 .Rproj.user
16
-ggtree*.html
16
+ggtree
17
+*.html
18
+
... ...
@@ -30,7 +30,6 @@ Imports:
30 30
     treeio (>= 1.3.3),
31 31
     utils
32 32
 Suggests:
33
-    Biostrings,
34 33
     colorspace,
35 34
     cowplot,
36 35
     emojifont,
... ...
@@ -200,6 +200,7 @@ importFrom(treeio,Nnode)
200 200
 importFrom(treeio,Ntip)
201 201
 importFrom(treeio,as.phylo)
202 202
 importFrom(treeio,as.treedata)
203
+importFrom(treeio,read.fasta)
203 204
 importFrom(utils,modifyList)
204 205
 importFrom(utils,packageDescription)
205 206
 importFrom(utils,packageVersion)
... ...
@@ -1,5 +1,6 @@
1 1
 CHANGES IN VERSION 1.11.3
2 2
 ------------------------
3
+ o update msaplot to use DNAbin/AAbin internally and also compatible with treedata object <2017-12-14, Thu>
3 4
  o clean up code <2017-12-13, Thu>
4 5
  o remove paml_rst, codeml_mlc, codeml and jplace fortify methods according to the change of treeio (v = 1.3.3) <2017-12-07, Thu>
5 6
 
... ...
@@ -12,52 +12,51 @@
12 12
 ##' @param height height ratio of sequence
13 13
 ##' @return tree view
14 14
 ##' @export
15
-## @importFrom Biostrings readBStringSet
16
-## @importMethodsFrom Biostrings width
17 15
 ## @importFrom colorspace rainbow_hcl
16
+##' @importFrom treeio read.fasta
18 17
 ##' @importFrom ggplot2 geom_segment
19 18
 ##' @importFrom ggplot2 geom_rect
20 19
 ##' @importFrom ggplot2 scale_fill_manual
21 20
 ##' @author Guangchuang Yu
22 21
 msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL, bg_line = TRUE, height = 0.8){
23 22
     if (missingArg(fasta)) {
24
-        aln <- NULL
25
-    } else if (is(fasta, "BStringSet")) {
26
-        aln <- fasta
23
+        x <- NULL
24
+    } else if (is(fasta, "DNAbin")) {
25
+        x <- fasta
27 26
     } else if (is(fasta, "character")) {
28
-        readBStringSet <- get_fun_from_pkg("Biostrings", "readBStringSet")
29
-        aln <- readBStringSet(fasta)
27
+        x <- read.fasta(fasta)
30 28
     } else {
31
-        aln <- NULL
29
+        x <- NULL
32 30
     }
33 31
 
34
-    if (is(p, "phylip")) {
35
-        BStringSet <- get_fun_from_pkg("Biostrings", "BStringSet")
36
-        aln <- BStringSet(p@sequence)
32
+
33
+    if (is.null(x) && is(p, "treedata") && length(p@tip_seq)) {
34
+        x <- p@tip_seq
37 35
         p <- ggtree(p) + geom_tiplab()
38 36
     }
39 37
 
40
-    if (is.null(aln)) {
38
+    if (is.null(x)) {
41 39
         stop("multiple sequence alignment is not available...\n-> check the parameter 'fasta'...")
42 40
     }
43 41
 
44
-    width_fun <- get_fun_from_pkg("Biostrings", "width")
42
+    x <- as.matrix(x)
45 43
 
46 44
     if (is.null(window)) {
47
-        window <- c(1, width_fun(aln)[1])
45
+        window <- c(1, ncol(x))
48 46
     }
49
-    slice <- seq(window[1], window[2], by=1)
50 47
 
51
-    seqs <- lapply(1:length(aln), function(i) {
52
-        x <- toString(aln[i])
53
-        seq <- substring(x, slice, slice)
48
+    slice <- seq(window[1], window[2], by=1)
49
+    x <- x[, slice]
54 50
 
51
+    seqs <- lapply(1:nrow(x), function(i) {
52
+        seq <- as.vector(as.character(x[i,]))
55 53
         seq[seq == '?'] <- '-'
56 54
         seq[seq == '*'] <- '-'
57 55
         seq[seq == ' '] <- '-'
58 56
         return(seq)
59 57
     })
60
-    names(seqs) <- names(aln)
58
+
59
+    names(seqs) <- labels(x)
61 60
 
62 61
     if(is.null(color)) {
63 62
         alphabet <- unlist(seqs) %>% unique
... ...
@@ -108,9 +107,10 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL, bg_lin
108 107
 
109 108
     msa.df <- do.call("rbind", msa)
110 109
 
111
-    p <- p + geom_rect(data=msa.df, aes(x=xmin, y=ymin,
112
-                           xmin=xmin, xmax=xmax,
113
-                           ymin=ymin, ymax=ymax, fill=seq)) +
110
+    p <- p + geom_rect(aes(xmin=xmin, xmax=xmax,
111
+                           ymin=ymin, ymax=ymax,
112
+                           fill=seq),
113
+                       data=msa.df, inherit.aes = FALSE) +
114 114
                                scale_fill_manual(values=color)
115 115
 
116 116
     breaks <- hist(seq_along(slice), breaks=10, plot=FALSE)$breaks
... ...
@@ -120,4 +120,3 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL, bg_lin
120 120
 
121 121
     return(p)
122 122
 }
123
-
... ...
@@ -16,7 +16,6 @@ output:
16 16
 vignette: >
17 17
   %\VignetteIndexEntry{05 Advance Tree Annotation}
18 18
   %\VignetteEngine{knitr::rmarkdown}
19
-  %\VignetteDepends{Biostrings}
20 19
   %\usepackage[utf8]{inputenc}
21 20
 ---
22 21
 
... ...
@@ -28,7 +27,6 @@ knitr::opts_chunk$set(tidy = FALSE,
28 27
 
29 28
 ```{r echo=FALSE, results="hide", message=FALSE}
30 29
 library("ape")
31
-library("Biostrings")
32 30
 library("treeio")
33 31
 library("ggplot2")
34 32
 library("ggtree")
... ...
@@ -27,7 +27,6 @@ knitr::opts_chunk$set(tidy = FALSE,
27 27
 
28 28
 ```{r echo=FALSE, results="hide", message=FALSE}
29 29
 library("colorspace")
30
-library("Biostrings")
31 30
 library("ape")
32 31
 library("ggplot2")
33 32
 library("ggtree")