Browse code

Still trying to implement mbiasPlot

Divyagash authored on 21/03/2018 21:45:34
Showing 4 changed files

... ...
@@ -6,8 +6,8 @@
6 6
 #'
7 7
 #'@param dir directory name with mbias files
8 8
 #'@param mbiasFiles list of mbias files
9
-#'@return Plot showing the methylation across the read position in original top
10
-#'and original bottom strand both in forward and reverse reads
9
+#'@return Returns a list containing the methylation across the read position in original top
10
+#'and original bottom strand both in forward and reverse reads for multiple samples
11 11
 #'@examples
12 12
 #'mbiasFile <- '2017-04-21_HG23KBCXY_2_AGGCAGAA_TATCTC_pe.M-bias.txt'
13 13
 #'mbiasplot(mbiasFiles=system.file("extdata",mbiasFile,package='scmeth'))
... ...
@@ -45,41 +45,17 @@ mbiasplot <- function(dir=NULL,mbiasFiles=NULL){
45 45
 
46 46
     CpG_Mbias_Read1 <- utils::read.csv(sep='\t',text=oracle[[1]])
47 47
     CpG_Mbias_Read1$read <- 'read-1'
48
+    colnames(CpG_Mbias_Read1)<-c('position','count.methylated','count.unmethylated',
49
+                                 'methylation','coverage','read')
48 50
     CpG_Mbias_Read2 <- utils::read.csv(sep='\t',text=oracle[[4]])
49 51
     CpG_Mbias_Read2$read <- 'read-2'
52
+    colnames(CpG_Mbias_Read2)<-c('position','count.methylated','count.unmethylated',
53
+                                 'methylation','coverage','read')
50 54
 
51
-    mbiasTable <- rbind(CpG_Mbias_Read1[,c('position','X..methylation','read')],
52
-                        CpG_Mbias_Read2[,c('position','X..methylation','read')])
53
-    colnames(mbiasTable)<-c('position','methylation','read')
55
+    mbiasTable <- rbind(CpG_Mbias_Read1[,c('position','methylation','read')],
56
+                        CpG_Mbias_Read2[,c('position','methylation','read')])
54 57
     mbiasTableList[[i]] <- mbiasTable
55 58
     }
56
-    mt <- do.call(rbind.data.frame, mbiasTableList)
57 59
 
58
-    meanTable <- stats::aggregate(methylation ~ position+read, data=mt, FUN=mean)
59
-    sdTable <- stats::aggregate(methylation ~ position+read, data=mt, FUN=sd)
60
-    seTable <- stats::aggregate(methylation ~ position+read, data=mt, FUN=function(x){sd(x)/sqrt(length(x))})
61
-
62
-    sum_mt<-data.frame('position'=meanTable$position,'read'=meanTable$read,
63
-                       'meth'=meanTable$methylation, 'sdMeth'=sdTable$methylation,
64
-                       'seMeth'=seTable$methylation)
65
-    #sum_mt <- plyr::ddply(mt, .(position, read), plyr::summarise,
66
-                    #meth = mean(X..methylation), sdMeth = sd(X..methylation),
67
-                    #seMeth = sd(X..methylation)/sqrt(length(X..methylation)))
68
-    #sum_mt <- mt %>% dplyr::group_by(position,read) %>%
69
-                        #dplyr::summarise(meth = mean(X..methylation),
70
-                           #     sdMeth=stats::sd(X..methylation))
71
-    #sum_mt$seMeth <- sum_mt$sdMeth/sqrt(nSamples)
72
-    sum_mt$upperCI <- sum_mt$meth+(1.96*sum_mt$seMeth)
73
-    sum_mt$lowerCI <- sum_mt$meth-(1.96*sum_mt$seMeth)
74
-    sum_mt$read_rep <- paste(sum_mt$read, sum_mt$position,sep="_")
75
-
76
-    g <- ggplot2::ggplot(sum_mt)
77
-    g <- g+ggplot2::geom_line(ggplot2::aes_string(x='position',y='meth',
78
-                                                colour='read'))
79
-    g <- g+ggplot2::geom_ribbon(ggplot2::aes_string(ymin = 'lowerCI',
80
-                                ymax = 'upperCI', x='position',fill = 'read'),
81
-                                alpha=0.4)
82
-    g <- g+ggplot2::ylim(0,100)+ggplot2::ggtitle('Mbias Plot')
83
-    g <- g+ggplot2::ylab('methylation')
84
-    return(g)
60
+    return(mbiasTableList)
85 61
 }
... ...
@@ -38,7 +38,32 @@ scmeth::readmetrics(bs)
38 38
 ```{r mbias,echo=FALSE,warning=FALSE,message=FALSE,eval=!is.null(params$mbias),fig.width=8, fig.height=7,fig.align='center'}
39 39
 ## Methylation bias plot
40 40
 # Methylation distribution for all the cells
41
-scmeth::mbiasplot(dir=params$mbias)
41
+mbiasTableList <- scmeth::mbiasplot(dir=params$mbias)
42
+
43
+mbiasDf <- do.call(rbind.data.frame, mbiasTableList)
44
+meanTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=mean)
45
+sdTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=sd)
46
+seTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=function(x){sd(x)/sqrt(length(x))})
47
+sum_mt<-data.frame('position'=meanTable$position,'read'=meanTable$read,
48
+                       'meth'=meanTable$methylation, 'sdMeth'=sdTable$methylation,
49
+                       'seMeth'=seTable$methylation)
50
+#sum_mt <- mt %>% dplyr::group_by(position,read) %>%
51
+                        #dplyr::summarise(meth = mean(X..methylation),
52
+                           #     sdMeth=stats::sd(X..methylation))
53
+#sum_mt$seMeth <- sum_mt$sdMeth/sqrt(nSamples)
54
+sum_mt$upperCI <- sum_mt$meth+(1.96*sum_mt$seMeth)
55
+sum_mt$lowerCI <- sum_mt$meth-(1.96*sum_mt$seMeth)
56
+sum_mt$read_rep <- paste(sum_mt$read, sum_mt$position,sep="_")
57
+
58
+g <- ggplot2::ggplot(sum_mt)
59
+g <- g+ggplot2::geom_line(ggplot2::aes_string(x='position',y='meth',
60
+                                                colour='read'))
61
+g <- g+ggplot2::geom_ribbon(ggplot2::aes_string(ymin = 'lowerCI',
62
+                        ymax = 'upperCI', x='position',fill = 'read'),
63
+                        alpha=0.4)
64
+g <- g+ggplot2::ylim(0,100)+ggplot2::ggtitle('Mbias Plot')
65
+g <- g+ggplot2::ylab('methylation')
66
+g
42 67
 ```
43 68
 
44 69
 
... ...
@@ -12,8 +12,8 @@ mbiasplot(dir = NULL, mbiasFiles = NULL)
12 12
 \item{mbiasFiles}{list of mbias files}
13 13
 }
14 14
 \value{
15
-Plot showing the methylation across the read position in original top
16
-and original bottom strand both in forward and reverse reads
15
+Returns a list containing the methylation across the read position in original top
16
+and original bottom strand both in forward and reverse reads for multiple samples
17 17
 }
18 18
 \description{
19 19
 Plot the methylation at each position of the read to observe any biases in
... ...
@@ -255,8 +255,31 @@ file generated from FireCloud pipeline and generates the methylation bias plot.
255 255
 ```{r,warning=FALSE,message=FALSE,fig.width=6,fig.height=6}
256 256
 
257 257
 methylationBiasFile <- '2017-04-21_HG23KBCXY_2_AGGCAGAA_TATCTC_pe.M-bias.txt'
258
-mbiasplot(mbiasFiles=system.file("extdata",methylationBiasFile,
258
+mbiasList <- mbiasplot(mbiasFiles=system.file("extdata",methylationBiasFile,
259 259
                                          package='scmeth'))
260
+
261
+mbiasDf <- do.call(rbind.data.frame, mbiasList)
262
+meanTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=mean)
263
+sdTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=sd)
264
+seTable <- stats::aggregate(methylation ~ position+read, data=mbiasDf, FUN=function(x){sd(x)/sqrt(length(x))})
265
+sum_mt<-data.frame('position'=meanTable$position,'read'=meanTable$read,
266
+                       'meth'=meanTable$methylation, 'sdMeth'=sdTable$methylation,
267
+                       'seMeth'=seTable$methylation)
268
+
269
+sum_mt$upperCI <- sum_mt$meth+(1.96*sum_mt$seMeth)
270
+sum_mt$lowerCI <- sum_mt$meth-(1.96*sum_mt$seMeth)
271
+sum_mt$read_rep <- paste(sum_mt$read, sum_mt$position,sep="_")
272
+
273
+g <- ggplot2::ggplot(sum_mt)
274
+g <- g+ggplot2::geom_line(ggplot2::aes_string(x='position',y='meth',
275
+                                                colour='read'))
276
+g <- g+ggplot2::geom_ribbon(ggplot2::aes_string(ymin = 'lowerCI',
277
+                        ymax = 'upperCI', x='position',fill = 'read'),
278
+                        alpha=0.4)
279
+g <- g+ggplot2::ylim(0,100)+ggplot2::ggtitle('Mbias Plot')
280
+g <- g+ggplot2::ylab('methylation')
281
+g
282
+
260 283
 ```
261 284
 </p>
262 285