Last updated: 2018-10-05

workflowr checks: (Click a bullet for more information)
  • R Markdown file: up-to-date

    Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

  • Environment: empty

    Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

  • Seed: set.seed(20180501)

    The command set.seed(20180501) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

  • Session information: recorded

    Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

  • Repository version: 877806a

    Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

    Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
    
    Ignored files:
        Ignored:    .DS_Store
        Ignored:    .Rhistory
        Ignored:    data/.DS_Store
    
    Untracked files:
        Untracked:  analysis/gam.Rmd
        Untracked:  analysis/literature.Rmd
        Untracked:  analysis/meeting1005.Rmd
        Untracked:  data/chipexo_examples/
        Untracked:  data/chipseq_examples/
    
    Unstaged changes:
        Modified:   analysis/sigma.Rmd
    
    
    Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
Expand here to see past versions:
    File Version Author Date Message
    Rmd 877806a Dongyue Xie 2018-10-05 add files


Introduction

I found a DNA methylation dataset from Hansen et al 2011. This dataset can be assessed through R pacakge bsseqData. A detailed tutorial on the dataset and method from Hansen et al 2011 is also avaiable here.

There are total 6 samples where three are from normal and three are from cancer. Each sample has 958541 CpGs from Chromesome 21 and 22. There are roughly 136019 CpGs with no data at all in any of the 6 samples. There are 572628 CpGs which are covered by at least 1 read in all 6 samples. Smoothing is done separately for each sample, only using the data where the coverage (for that sample) is non-zero. This estimates a genome-wide methylation profile, which is then evaluated in all CpGs in the BSseq object. As a result, after smoothing, every CpG in the object has an estimated methylation value.

They use t-test to find differentially methylated regions(DMRs). Before computing t-statistics, CpGs with little or no coverage are removed.

Initial try

I tried to apply smashgen to this DNA methylation dataset.

  1. The CpGs loci with no data at all in any of the 6 samples were removed. Should I?
  2. Choose bin size s.t. the resulted binomial sequence length is a power of 2. The last interval constains less CpGs. Any impreovement on this?
  3. In each bin, \(n_t\) is the total coverage, \(x_t\) is the total possible methylation. For example, coverages are 0,1,2,3,4; methylations are 0,0,1,3,1, then \(n_t=10\), \(x_t=5\). Should I remove no coverage area? If removed, how to compare diffwerent samples?
  4. After obtaining binomial sequence, I do a reflection on both side then smooth the data.

I may misunderstand it…Inside each window(at least 2000kb), they treat each base methylation follows binomial. So is Bionimial(\(n_{reads},p_{meth}\))???

library(bsseqData)
library(bsseq)
data("BS.cancer.ex")

reflect=function(x,direct='left',len){
  n=length(x)
  if(direct=='left'){
    x=c(rev(x[1:len]),x)
  }
  if(direct=='right'){
    x=c(x,rev(x[(n-len+1):n]))
  }
  if(direct=='both'){
    x=c(rev(x[1:len[1]]),x,rev(x[(n-len[2]+1):n]))
  }
  return(x)
}


# remove no coverage bases for all 6 sampels
remove.idx=which(rowSums(getCoverage(BS.cancer.ex)) == 0)
result=list()
for (s in 1:6) {
  # Cancer 1 samples
BS1=BS.cancer.ex[,s]
BS1=BS1[-remove.idx]
# Obtain coverage
BS1.cov=getCoverage(BS1,type='Cov')
# Obtain possible methylation
BS1.M=getCoverage(BS1,type='M')
# choose 2kb windows. 
w=1607
nw=floor(length(BS1)/w)
# Obtain binomial data n_t and x_t
n_t=c()
x_t=c()
for (i in 1:nw) {
  n_t[i]=sum(BS1.cov[(w*(i-1)+1):(w*i)])
  x_t[i]=sum(BS1.M[(w*(i-1)+1):(w*i)])
}

n_T=sum(BS1.cov[-w*nw])
x_T=sum(BS1.M[-w*nw])
n_t=c(n_t,n_T)
x_t=c(x_t,x_T)

#bio.sm=smash_gen_lite(x_t,ntri=n_t,dist_family = 'binomial')
result[[s]]=list(n_t=n_t,x_t=x_t)
}

sm.ref=c()
for (k in 1:6) {
  nn=reflect(result[[k]]$n_t,direct = 'both',len = c(256,256))
  xx=reflect(result[[k]]$x_t,direct = 'both',len = c(256,256))
  ss=smash_gen_lite(xx,ntri=nn,dist_family = 'binomial')
  sm.ref=rbind(sm.ref,ss[257:(512+256)])
}


plot(sm.ref[1,],type='l',ylim=c(0.2,1),main='n=512',ylab='')
for(j in 2:6){
  if(j>3){col=3}else{col=1}
  lines(sm.ref[j,],col=col)
}

plot(result[[1]]$x_t/result[[1]]$n_t,main='Normal1',col='grey80')
lines(sm.ref[1,])

legend('topleft',c('normal','cancer'),col=c(1,3),lty=c(1,1))
paste('smallest x_t')
unlist(lapply(result, function(x){min(x$x_t)}))
paste('smallest n_t')
unlist(lapply(result, function(x){min(x$n_t)}))
result=list()
for (s in 1:6) {
  # Cancer 1 samples
BS1=BS.cancer.ex[,s]
BS1=BS1[-remove.idx]
# Obtain coverage
BS1.cov=getCoverage(BS1,type='Cov')
# Obtain possible methylation
BS1.M=getCoverage(BS1,type='M')
# choose 2kb windows. 
w=804
nw=floor(length(BS1)/w)
# Obtain binomial data n_t and x_t
n_t=c()
x_t=c()
for (i in 1:nw) {
  n_t[i]=sum(BS1.cov[(w*(i-1)+1):(w*i)])
  x_t[i]=sum(BS1.M[(w*(i-1)+1):(w*i)])
}

n_T=sum(BS1.cov[-w*nw])
x_T=sum(BS1.M[-w*nw])
n_t=c(n_t,n_T)
x_t=c(x_t,x_T)

#bio.sm=smash_gen_lite(x_t,ntri=n_t,dist_family = 'binomial')
result[[s]]=list(n_t=n_t,x_t=x_t)
}

sm.ref=c()
for (k in 1:6) {
  nn=reflect(result[[k]]$n_t,direct = 'both',len = c(512,512))
  xx=reflect(result[[k]]$x_t,direct = 'both',len = c(512,512))
  ss=smash_gen_lite(xx,ntri=nn,dist_family = 'binomial')
  sm.ref=rbind(sm.ref,ss[513:(1024+512)])
}


plot(sm.ref[1,],type='l',ylim=c(0.2,1),main='n=1024',ylab='')
for(j in 2:6){
  if(j>3){col=3}else{col=1}
  lines(sm.ref[j,],col=col)
}

legend('topleft',c('normal','cancer'),col=c(1,3),lty=c(1,1))
paste('smallest x_t')
unlist(lapply(result, function(x){min(x$x_t)}))
paste('smallest n_t')
unlist(lapply(result, function(x){min(x$n_t)}))

What if remove all the no coverage area?

For normal 1:

BS1=BS.cancer.ex[,s]
idx=which(getCoverage(BS1,type='Cov')==0)
BS1=BS1[-idx]
w=ceiling(length(BS1)/512)
nw=floor(length(BS1)/w)

n_t=c()
x_t=c()
for (i in 1:nw) {
  n_t[i]=sum(BS1.cov[(w*(i-1)+1):(w*i)])
  x_t[i]=sum(BS1.M[(w*(i-1)+1):(w*i)])
}

n_T=sum(BS1.cov[-w*nw])
x_T=sum(BS1.M[-w*nw])
n_t=c(n_t,n_T)
x_t=c(x_t,x_T)


nn=reflect(n_t,direct = 'both',len = c(256,256))
xx=reflect(x_t,direct = 'both',len = c(256,256))
ss=smash_gen_lite(xx,ntri=nn,dist_family = 'binomial')

plot(x_t/n_t,col='grey80')
lines(ss[257:(512+256)])

Session information

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] workflowr_1.1.1   Rcpp_0.12.18      digest_0.6.17    
 [4] rprojroot_1.3-2   R.methodsS3_1.7.1 backports_1.1.2  
 [7] git2r_0.23.0      magrittr_1.5      evaluate_0.11    
[10] stringi_1.2.4     whisker_0.3-2     R.oo_1.22.0      
[13] R.utils_2.7.0     rmarkdown_1.10    tools_3.5.1      
[16] stringr_1.3.1     yaml_2.2.0        compiler_3.5.1   
[19] htmltools_0.3.6   knitr_1.20       

This reproducible R Markdown analysis was created with workflowr 1.1.1