Browse code

small improvement for plotGenomeBrowser

ataudt authored on 18/04/2018 10:03:26
Showing 3 changed files

... ...
@@ -425,7 +425,9 @@ plotBoxplot <- function(model) {
425 425
 #' @param chr,start,end Chromosome, start and end coordinates for the plot.
426 426
 #' @param style One of \code{c('peaks', 'density')}.
427 427
 #' @param peakHeight Height of the peak track relative to the count track.
428
-#' @return A \code{\link[ggplot2:ggplot]{ggplot}} object.
428
+#' @param peakColor Color for the peak track.
429
+#' @param same.yaxis Whether or not the plots for the same mark have the same y-axis.
430
+#' @return A \code{list()} of \code{\link[ggplot2:ggplot]{ggplot}} objects.
429 431
 #' @export
430 432
 #' @examples
431 433
 #'## Get an example uniHMM ##
... ...
@@ -448,7 +450,7 @@ plotBoxplot <- function(model) {
448 450
 #'plotlist <- plotGenomeBrowser(model, chr='chr12', start=1, end=1e6, style='peaks',
449 451
 #'                  peakHeight=0.1)
450 452
 #'
451
-plotGenomeBrowser <- function(model, chr, start, end, style='peaks', peakHeight=0.2) {
453
+plotGenomeBrowser <- function(model, chr, start, end, style='peaks', peakHeight=0.2, peakColor='blue', same.yaxis=TRUE) {
452 454
   
453 455
     model <- loadHmmsFromFiles(model, check.class = c('uniHMM', 'multiHMM', 'combinedMultiHMM'))[[1]]
454 456
     
... ...
@@ -478,15 +480,20 @@ plotGenomeBrowser <- function(model, chr, start, end, style='peaks', peakHeight=
478 480
     ggplts <- list()
479 481
     for (i1 in 1:nrow(model$info)) {
480 482
       
481
-        ymax.counts <- round(maxcounts.permark[[as.character(model$info$mark[i1])]])
482 483
         ID <- model$info$ID[i1]
484
+        
485
+        if (same.yaxis) {
486
+            ymax.counts <- round(maxcounts.permark[[as.character(model$info$mark[i1])]])
487
+        } else {
488
+            ymax.counts <- round(maxcounts[[as.character(ID)]])
489
+        }
483 490
       
484 491
         if (style == 'peaks') {
485 492
             df <- data.frame(position=(start(bins)+end(bins))/2, RPKM=bins$counts.rpkm[,ID]) # plot triangles centered at middle of the bin
486 493
             ggplt <- ggplot(df) + geom_area(aes_string(x='position', y='RPKM')) + theme(panel.grid = element_blank(), panel.background = element_blank(), axis.line = element_blank())
487 494
         } else if (style == 'density') {
488 495
             df <- data.frame(xmin=start(bins), xmax=end(bins), RPKM=bins$counts.rpkm[,ID])
489
-            ggplt <- ggplot(df) + geom_rect(aes_string(xmin='xmin', xmax='xmax', ymin=0, ymax.counts=4, alpha='RPKM')) + theme(panel.grid = element_blank(), panel.background = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line = element_blank(), axis.title.y = element_blank())
496
+            ggplt <- ggplot(df) + geom_rect(aes_string(xmin='xmin', xmax='xmax', ymin=0, ymax=4, alpha='RPKM')) + theme(panel.grid = element_blank(), panel.background = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line = element_blank(), axis.title.y = element_blank())
490 497
             ggplt <- ggplt + xlab('position')
491 498
         } else {
492 499
             stop("Unknown value '", style, "' for parameter 'style'. Must be one of c('peaks', 'density').")
... ...
@@ -496,10 +503,11 @@ plotGenomeBrowser <- function(model, chr, start, end, style='peaks', peakHeight=
496 503
         peaks <- subsetByOverlaps(peaklist[[ID]], ranges2plot)
497 504
         if (length(peaks) > 0) {
498 505
             df <- data.frame(start=start(peaks), end=end(peaks), ymin=-peakHeight*ymax.counts, ymax=0.1*(-peakHeight*ymax.counts))
499
-            ggplt <- ggplt + geom_rect(data=df, mapping=aes_string(xmin='start', xmax='end', ymin='ymin', ymax='ymax'), col='blue', fill='blue')
506
+            ggplt <- ggplt + geom_rect(data=df, mapping=aes_string(xmin='start', xmax='end', ymin='ymin', ymax='ymax'), col=peakColor, fill=peakColor)
500 507
         }
501 508
       
502
-        ggplt <- ggplt + scale_y_continuous(breaks=c(0, ymax.counts)) + coord_cartesian(xlim=c(start, end), ylim=c(-peakHeight*ymax.counts,ymax.counts))
509
+        ggplt <- ggplt + scale_y_continuous(breaks=c(0, ymax.counts))
510
+        ggplt <- ggplt + coord_cartesian(xlim=c(start, end), ylim=c(-peakHeight*ymax.counts,ymax.counts))
503 511
         ggplt <- ggplt + ggtitle(ID)
504 512
         ggplts[[ID]] <- ggplt
505 513
     }
... ...
@@ -99,6 +99,7 @@ unis2pseudomulti <- function(hmms) {
99 99
     rownames(result$info) <- NULL
100 100
     result$bins <- bins
101 101
     ## Segmentation
102
+        ptm <- startTimedMessage("Segmentation ...")
102 103
         df <- as.data.frame(result$bins)
103 104
         ind.readcols <- grep('^counts', names(df))
104 105
         ind.widthcol <- grep('width', names(df))
... ...
@@ -107,6 +108,7 @@ unis2pseudomulti <- function(hmms) {
107 108
         red.gr <- GRanges(seqnames=red.df[,1], ranges=IRanges(start=red.df[,2], end=red.df[,3]), strand=red.df[,4], state=red.df[,'state'], combination=red.df[,'combination'])
108 109
         result$segments <- red.gr
109 110
         seqlengths(result$segments) <- seqlengths(result$bins)[seqlevels(result$segments)]
111
+        stopTimedMessage(ptm)
110 112
     ## Parameters
111 113
         result$mapping <- mapping
112 114
         # Weights
... ...
@@ -125,6 +127,10 @@ unis2pseudomulti <- function(hmms) {
125 127
     ## Add class
126 128
         class(result) <- class.multivariate.hmm
127 129
 
130
+    ## Get the peaks ##
131
+        result$peaks <- lapply(hmms, '[[', 'peaks')
132
+        names(result$peaks) <- result$info$ID
133
+        
128 134
     return(result)
129 135
 
130 136
 }
... ...
@@ -68,7 +68,8 @@ plotGenomeBrowser2 <- function(counts, peaklist=NULL, chr, start, end, countcol=
68 68
 }
69 69
 Plot a genome browser view}
70 70
 \usage{
71
-plotGenomeBrowser(model, chr, start, end, style = "peaks", peakHeight = 0.2)
71
+plotGenomeBrowser(model, chr, start, end, style = "peaks", peakHeight = 0.2,
72
+  peakColor = "blue", same.yaxis = TRUE)
72 73
 }
73 74
 \arguments{
74 75
 \item{model}{A \code{\link{uniHMM}}, \code{\link{multiHMM}} or \code{\link{combinedMultiHMM}} object or file that contains such an object.}
... ...
@@ -78,9 +79,13 @@ plotGenomeBrowser(model, chr, start, end, style = "peaks", peakHeight = 0.2)
78 79
 \item{style}{One of \code{c('peaks', 'density')}.}
79 80
 
80 81
 \item{peakHeight}{Height of the peak track relative to the count track.}
82
+
83
+\item{peakColor}{Color for the peak track.}
84
+
85
+\item{same.yaxis}{Whether or not the plots for the same mark have the same y-axis.}
81 86
 }
82 87
 \value{
83
-A \code{\link[ggplot2:ggplot]{ggplot}} object.
88
+A \code{list()} of \code{\link[ggplot2:ggplot]{ggplot}} objects.
84 89
 }
85 90
 \description{
86 91
 Plot a simple genome browser view of \code{\link{chromstaR-objects}}. This is useful for scripted genome browser snapshots.