Browse code

update msaplot

guangchuang yu authored on 26/07/2017 07:46:20
Showing 4 changed files

... ...
@@ -1,5 +1,7 @@
1 1
 CHANGES IN VERSION 1.9.2
2 2
 ------------------------
3
+ o add bg_line and height parameter in msaplot <2017-07-26, Wed>
4
+   + use can set bg_line = FALSE and height = 1 to plot more beautiful alignment
3 5
  o extend parameter in geom_cladebar <2017-07-26, Wed>
4 6
    + https://github.com/GuangchuangYu/ggtree/issues/142#issuecomment-317817995
5 7
  o scaleClade works after calling viewClade <2017-07-20, Thu>
... ...
@@ -1,13 +1,15 @@
1 1
 ##' multiple sequence alignment with phylogenetic tree
2 2
 ##'
3
-##' 
3
+##'
4 4
 ##' @title msaplot
5 5
 ##' @param p tree view
6 6
 ##' @param fasta fasta file, multiple sequence alignment
7 7
 ##' @param offset offset of MSA to tree
8 8
 ##' @param width total width of alignment, compare to width of tree
9
-##' @param color color 
9
+##' @param color color
10 10
 ##' @param window specific a slice to display
11
+##' @param bg_line whether add background line in alignment
12
+##' @param height height ratio of sequence
11 13
 ##' @return tree view
12 14
 ##' @export
13 15
 ## @importFrom Biostrings readBStringSet
... ...
@@ -17,7 +19,7 @@
17 19
 ##' @importFrom ggplot2 geom_rect
18 20
 ##' @importFrom ggplot2 scale_fill_manual
19 21
 ##' @author Guangchuang Yu
20
-msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
22
+msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL, bg_line = TRUE, height = 0.8){
21 23
     if (missingArg(fasta)) {
22 24
         aln <- NULL
23 25
     } else if (is(fasta, "BStringSet")) {
... ...
@@ -28,7 +30,7 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
28 30
     } else {
29 31
         aln <- NULL
30 32
     }
31
-        
33
+
32 34
     if (is(p, "phylip")) {
33 35
         BStringSet <- get_fun_from_pkg("Biostrings", "BStringSet")
34 36
         aln <- BStringSet(p@sequence)
... ...
@@ -40,12 +42,12 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
40 42
     }
41 43
 
42 44
     width_fun <- get_fun_from_pkg("Biostrings", "width")
43
-    
45
+
44 46
     if (is.null(window)) {
45 47
         window <- c(1, width_fun(aln)[1])
46 48
     }
47 49
     slice <- seq(window[1], window[2], by=1)
48
-    
50
+
49 51
     seqs <- lapply(1:length(aln), function(i) {
50 52
         x <- toString(aln[i])
51 53
         seq <- substring(x, slice, slice)
... ...
@@ -56,7 +58,7 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
56 58
         return(seq)
57 59
     })
58 60
     names(seqs) <- names(aln)
59
-    
61
+
60 62
     if(is.null(color)) {
61 63
         alphabet <- unlist(seqs) %>% unique
62 64
         alphabet <- alphabet[alphabet != '-']
... ...
@@ -73,7 +75,7 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
73 75
 
74 76
     ## convert width to width of each cell
75 77
     width <- width * (df$x %>% range %>% diff) / diff(window)
76
-    
78
+
77 79
     df=df[df$isTip,]
78 80
     start <- max(df$x) * 1.02 + offset
79 81
 
... ...
@@ -84,14 +86,17 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
84 86
     xmax <- start + seq_along(slice) * width
85 87
     xmin <- xmax - width
86 88
     y <- sort(df$y)
87
-    ymin <- y - 0.4 *h
88
-    ymax <- y + 0.4 *h
89
+    ymin <- y - height/2 *h
90
+    ymax <- y + height/2 *h
89 91
 
90 92
     from <- to <- NULL
91
-    
93
+
92 94
     lines.df <- data.frame(from=min(xmin), to=max(xmax), y = y)
93 95
 
94
-    p <- p + geom_segment(data=lines.df, aes(x=from, xend=to, y=y, yend=y))
96
+    if (bg_line) {
97
+        p <- p + geom_segment(data=lines.df, aes(x=from, xend=to, y=y, yend=y), size=h*.2)
98
+    }
99
+
95 100
     msa <- lapply(1:length(y), function(i) {
96 101
         data.frame(name=names(seqs)[i],
97 102
                    xmin=xmin,
... ...
@@ -103,7 +108,7 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
103 108
 
104 109
     msa.df <- do.call("rbind", msa)
105 110
 
106
-    p <- p + geom_rect(data=msa.df, aes(x=xmin, y=ymin, 
111
+    p <- p + geom_rect(data=msa.df, aes(x=xmin, y=ymin,
107 112
                            xmin=xmin, xmax=xmax,
108 113
                            ymin=ymin, ymax=ymax, fill=seq)) +
109 114
                                scale_fill_manual(values=color)
... ...
@@ -112,7 +117,7 @@ msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
112 117
     pos <- start + breaks * width
113 118
     mapping <- data.frame(from=breaks+1, to=pos)
114 119
     attr(p, "mapping") <- mapping
115
-    
120
+
116 121
     return(p)
117 122
 }
118 123
 
... ...
@@ -27,7 +27,7 @@ Please cite the following article when using `ggtree`:
27 27
 
28 28
 **G Yu**, DK Smith, H Zhu, Y Guan, TTY Lam<sup>\*</sup>. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. ***Methods in Ecology and Evolution***. 2017, 8(1):28-36.
29 29
 
30
-[![](https://img.shields.io/badge/doi-10.1111/2041--210X.12628-green.svg?style=flat)](http://dx.doi.org/10.1111/2041-210X.12628) [![](https://img.shields.io/badge/Altmetric-351-green.svg?style=flat)](https://www.altmetric.com/details/10533079) [![citation](https://img.shields.io/badge/cited%20by-29-green.svg?style=flat)](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627)
30
+[![](https://img.shields.io/badge/doi-10.1111/2041--210X.12628-green.svg?style=flat)](http://dx.doi.org/10.1111/2041-210X.12628) [![](https://img.shields.io/badge/Altmetric-350-green.svg?style=flat)](https://www.altmetric.com/details/10533079) [![citation](https://img.shields.io/badge/cited%20by-29-green.svg?style=flat)](https://scholar.google.com.hk/scholar?oi=bibs&hl=en&cites=7268358477862164627)
31 31
 
32 32
 ------------------------------------------------------------------------
33 33
 
... ...
@@ -4,7 +4,8 @@
4 4
 \alias{msaplot}
5 5
 \title{msaplot}
6 6
 \usage{
7
-msaplot(p, fasta, offset = 0, width = 1, color = NULL, window = NULL)
7
+msaplot(p, fasta, offset = 0, width = 1, color = NULL, window = NULL,
8
+  bg_line = TRUE, height = 0.8)
8 9
 }
9 10
 \arguments{
10 11
 \item{p}{tree view}
... ...
@@ -18,6 +19,10 @@ msaplot(p, fasta, offset = 0, width = 1, color = NULL, window = NULL)
18 19
 \item{color}{color}
19 20
 
20 21
 \item{window}{specific a slice to display}
22
+
23
+\item{bg_line}{whether add background line in alignment}
24
+
25
+\item{height}{height ratio of sequence}
21 26
 }
22 27
 \value{
23 28
 tree view