DMCHMM: Differentially Methylated CpG using Hidden Markov Model

Farhad Shokoohi

2022-04-26

DNA methylation studies have increased in number over the past decade thanks to the recent advances in next-generation sequencing (NGS) and microarray technology (MA), providing many data sets at high resolution, enabling researchers to understand methylation patterns and their regulatory roles in biological processes and diseases.
Notwithstanding that diverse methods and software have created ample opportunities for researchers to do quantitative analysis, they make it difficult for practitioners to choose the one that is suitable and efficient in analyzing DNA methylation data. Having examined most of differentially methylation identification tools for bisulfite sequencing (BS-Seq) data, we observed several drawbacks in the existing analytic tools. To address these issues we have developed a novel differentially methylated CpG site identification tool which is based on Hidden Markov models (HMM) called DMCHMM. This vignette provides some guidelines on how to use the package and analyze BS-Seq data.

Following topics will be discussed in this vignette:

S4 Classes

Two different classes are defined by extending the SummarizedExperiment-class. The BSData-class is designed to hold BS-Seq data. Similarly, cBSData-method is defined to create a BSData object. This class includes two slots: the methReads, a matrix with columns representing samples and rows representing genomic positions (CpG sites) and elements of matrix representing methylation counts at each position in each sample; the totalReads, a matrix with similar columns and rows except the elements representing total number of reads.

Reading BS-Seq data

For reading raw BS-Seq data we adopted The readBismark function from BiSeq package. The readBismark-method reads samples stored in different files with six columns of chromosome, start position, end position, methylation percentage, number of Cs and number of Ts.

Three data files are included in the DMCHMM package for illustration. The data can be imported using following code.

library(DMCHMM)
fn <- list.files(system.file("extdata",package = "DMCHMM"))
fn.f <- list.files(system.file("extdata",package="DMCHMM"), full.names=TRUE)
OBJ <- readBismark(fn.f, fn, mc.cores = 2)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |======================================================================| 100%
## 
## Building BSData object.
cdOBJ <- DataFrame(Cell = factor(c("BC", "TC","Mono"),
labels = c("BC", "TC", "Mono")), row.names = c("BCU1568","BCU173","BCU551"))
colData(OBJ) <- cdOBJ
OBJ
## class: BSData 
## dim: 25668 3 
## metadata(0):
## assays(2): totalReads methReads
## rownames(25668): 1 2 ... 25667 25668
## rowData names(0):
## colnames(3): BCU1568 BCU173 BCU551
## colData names(1): Cell

Simulating BS-Seq data

The above data set only include one sample for each cell type. We need more samples to be able to compare their methylations and find DMCs. For illustration we generate a sample of BS-Seq data as follows.

nr <- 150; nc <- 8
metht <- matrix(as.integer(runif(nr * nc, 0, 20)), nr)
methc <- matrix(rbinom(n=nr*nc,c(metht),prob = runif(nr*nc)),nr,nc)
r1 <- GRanges(rep("chr1", nr), IRanges(1:nr, width=1), strand="*")
names(r1) <- 1:nr
cd1 <- DataFrame(Group=rep(c("G1","G2"),each=nc/2),row.names=LETTERS[1:nc])
OBJ1 <- cBSData(rowRanges=r1,methReads=methc,totalReads=metht,colData=cd1)
OBJ1
## class: BSData 
## dim: 150 8 
## metadata(0):
## assays(2): totalReads methReads
## rownames(150): 1 2 ... 149 150
## rowData names(0):
## colnames(8): A B ... G H
## colData names(1): Group

Predicting methylation levels using HMM and EM algorithm

There are two approaches to smoothed the data before testing for DMCs. Either EM or MCMC can be used to predict methylation levels utilizing HMM. The methHMEM-method which is developed to predict methylation levels. The output is a BSDMCs-class that can be either used to find DMCs or use MCMC algorithm to re-smooth the raw data. The process is as follows.

OBJ2 <- methHMEM(OBJ1, MaxK=2)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |======================================================================| 100%
OBJ2
## class: BSDMCs 
## dim: 150 8 
## metadata(3): K Beta Pm
## assays(5): methReads totalReads methLevels methStates methVars
## rownames(150): 1 2 ... 149 150
## rowData names(0):
## colnames(8): A B ... G H
## colData names(1): Group

Predicting methylation levels using HMM and MCMC algorithm

Although EM algorithm is a fast way to smooth the data but the results are not as good as the MCMC algorithm. The MCMC algorithm, however, is slow. In order to increase the speed, we first use methHMEM-method to find the HMM order for each sample and then we use methHMCMC-method to predict methylation levels. The procedure is as follows.

OBJ3 <- methHMMCMC(OBJ2)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |======================================================================| 100%
OBJ3
## class: BSDMCs 
## dim: 150 8 
## metadata(3): K Beta Pm
## assays(5): methReads totalReads methLevels methStates methVars
## rownames(150): 1 2 ... 149 150
## rowData names(0):
## colnames(8): A B ... G H
## colData names(1): Group

Identifying DMCs

Having smoothed the data using HMM, we run linear between predicted methylation levels and grouping covariate. In case other covariates exist, one can use the formula argument to specify a linear model. When there is no covariates no action is required. The following command identifies the DMCs. The results are stored in a BSDMCs-class and can be retrieved by calling metadata command.

OBJ4 <- findDMCs(OBJ3)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
## Warning in fdrtool(x, statistic = "pvalue", plot = FALSE, verbose = FALSE):
## There may be too few input test statistics for reliable FDR calculations!
## Warning: Censored sample for null model estimation has only size 7 !
head(metadata(OBJ4)$DMCHMM)
##   DMCs    pvalues    qvalues DMCsGroupG1vsG2 methDirGroupG1vsG2
## 1    0 0.87421710 0.08511057               0               hypo
## 2    1 0.19448975 0.02845538               0               hypo
## 3    1 0.16290290 0.02611294               0               hypo
## 4    1 0.04765048 0.02076186               1               hypo
## 5    0 0.51347675 0.05180979               0               hypo
## 6    1 0.11009476 0.02346491               0               hypo
sessionInfo()
## R version 4.2.0 RC (2022-04-19 r82224)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.15-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] DMCHMM_1.18.0               fdrtool_1.2.17             
##  [3] BiocParallel_1.30.0         SummarizedExperiment_1.26.0
##  [5] Biobase_2.56.0              GenomicRanges_1.48.0       
##  [7] GenomeInfoDb_1.32.0         IRanges_2.30.0             
##  [9] S4Vectors_0.34.0            BiocGenerics_0.42.0        
## [11] MatrixGenerics_1.8.0        matrixStats_0.62.0         
## 
## loaded via a namespace (and not attached):
##  [1] zoo_1.8-10               xfun_0.30                bslib_0.3.1             
##  [4] splines_4.2.0            lattice_0.20-45          htmltools_0.5.2         
##  [7] rtracklayer_1.56.0       yaml_2.3.5               survival_3.3-1          
## [10] XML_3.99-0.9             rlang_1.0.2              jquerylib_0.1.4         
## [13] calibrate_1.7.7          multcomp_1.4-19          GenomeInfoDbData_1.2.8  
## [16] stringr_1.4.0            zlibbioc_1.42.0          Biostrings_2.64.0       
## [19] mvtnorm_1.1-3            codetools_0.2-18         evaluate_0.15           
## [22] restfulr_0.0.13          knitr_1.38               fastmap_1.1.0           
## [25] parallel_4.2.0           TH.data_1.1-1            DelayedArray_0.22.0     
## [28] jsonlite_1.8.0           XVector_0.36.0           Rsamtools_2.12.0        
## [31] rjson_0.2.21             digest_0.6.29            stringi_1.7.6           
## [34] BiocIO_1.6.0             grid_4.2.0               cli_3.3.0               
## [37] tools_4.2.0              bitops_1.0-7             sandwich_3.0-1          
## [40] magrittr_2.0.3           sass_0.4.1               RCurl_1.98-1.6          
## [43] crayon_1.5.1             MASS_7.3-57              Matrix_1.4-1            
## [46] rmarkdown_2.14           R6_2.5.1                 GenomicAlignments_1.32.0
## [49] compiler_4.2.0