Contents

1 Introduction

In the previous vignette “Introduction to coMethDMR”, we discussed components of the coMethDMR pipeline and presented example R scripts for running an analysis with coMethDMR serially. However, because identifying co-methylated clusters and fitting mixed effects models on large numbers of genomic regions can be computationally expensive, we illustrate implementation of parallel computing for coMethDMR via the BiocParallel R package in this vignette.

1.1 Installation

The latest version can be installed by:

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("coMethDMR")

First, we load these two packages.

library(coMethDMR)
library(BiocParallel)

1.2 Overview

In Section 2, we give a brief re-introduction to the example data. In Section 3, we present example scripts for analyzing a single type (e.g. genic regions) of genomic regions using parallel computing. In Section 4, we present example scripts for analyzing genic and intergenic regions on the Illumina arrays using parallel computing.

2 Example Dataset

For illustration, we use a subset of prefrontal cortex methylation data (GEO GSE59685) described in Lunnon et al. (2014). This example dataset includes beta values for 8552 CpGs on chromosome 22 for a random selection of 20 subjects.

data(betasChr22_df)

We assume quality control and normalization of the methylation dataset have been performed by R packages such as minfi or RnBeads. Further, we assume that probes and samples with excessive missingness have been removed (this task can be completed by subsetting the original methylation data set with the probes and samples identified by the MarkMissing() function).

# Purge any probes or samples with excessive missing values
markCells_ls <- MarkMissing(dnaM_df = betasChr22_df)
betasChr22_df <- betasChr22_df[
  markCells_ls$keepProbes, 
  markCells_ls$keepSamples
]

# Inspect
betasChr22_df[1:5, 1:5]
##            GSM1443279 GSM1443663 GSM1443434 GSM1443547 GSM1443577
## cg00004192  0.9249942  0.8463296  0.8700718  0.9058205  0.9090382
## cg00004775  0.6523025  0.6247554  0.7573476  0.6590817  0.6726261
## cg00012194  0.8676339  0.8679048  0.8484754  0.8754985  0.8484458
## cg00013618  0.9466056  0.9475467  0.9566493  0.9588431  0.9419563
## cg00014104  0.3932388  0.5525716  0.4075900  0.3997278  0.3216956

The corresponding phenotype dataset included variables stage (Braak AD stage), subject.id, slide (batch effect), sex, Sample and age.brain (age of the brain donor).

data(pheno_df)
head(pheno_df)
##    stage subject.id         sex     Sample age.brain       slide
## 3      0          1 Sex: FEMALE GSM1443251        82  6042316048
## 8      2          2 Sex: FEMALE GSM1443256        82  6042316066
## 10    NA          3   Sex: MALE GSM1443258        89  6042316066
## 15     1          4 Sex: FEMALE GSM1443263        81  7786923107
## 21     2          5 Sex: FEMALE GSM1443269        92  6042316121
## 22     1          6   Sex: MALE GSM1443270        78  6042316099

Note that only samples with both methylation data and non-missing phenotype data are analyzed by coMethDMR. So in this example, the sample with subject.id = 3 which lacks AD stage information will be excluded from analysis. Please also note the phenotype file needs to have a variable called “Sample” that will be used by coMethDMR to link to the methylation dataset.

3 Analyzing One Type of Genomic Region via BiocParallel

As mentioned previously in “Introduction to coMethDMR”, there are several steps in the coMethDMR pipeline:

  1. Obtain CpGs located closely in pre-defined genomic regions,
  2. Identify co-methylated regions, and
  3. Test co-methylated regions against the outcome variable (AD stage).

Suppose we are interested in analyzing genic regions on the 450k arrays. In the following, closeByGeneAll_ls is a list, where each item includes at least 3 CpGs located closely (max distance between 2 CpGs is 200bp by default).

closeByGeneAll_ls <- readRDS(
  system.file(
    "extdata",
    "450k_Gene_3_200.rds",
    package = 'coMethDMR',
    mustWork = TRUE
  )
)

We can inspect the first region in the list via:

closeByGeneAll_ls[1]
## $`chr4:91760141-91760229`
## [1] "cg01583657" "cg25325192" "cg06044899"

Next, for demonstration, we select regions on chromosome 22.

indx <- grep("chr22:", names(closeByGeneAll_ls))

closeByGene_ls <- closeByGeneAll_ls[indx]
rm(closeByGeneAll_ls)
length(closeByGene_ls)
## [1] 676

There are 676 genic regions to be tested for chromosome 22. As an example, this vignette will analyze the first 10 regions, which contain the following CpGs:

closeByGene_ls[1:10]
## $`chr22:30662799-30663041`
## [1] "cg19018155" "cg10467217" "cg24727122" "cg02597698" "cg25666403"
## [6] "cg02026204" "cg05539509" "cg07498879"
## 
## $`chr22:18632404-18633123`
##  [1] "cg27281093" "cg18699242" "cg01963623" "cg02169692" "cg00077299"
##  [6] "cg07964128" "cg02518889" "cg01246421" "cg08205655" "cg10685559"
## 
## $`chr22:43253145-43253208`
## [1] "cg15656623" "cg10648908" "cg02351223"
## 
## $`chr22:43253517-43253559`
## [1] "cg09861871" "cg00079563" "cg26529516" "cg04527868" "cg01029450"
## 
## $`chr22:43254043-43254168`
## [1] "cg23778094" "cg00539564" "cg09367092"
## 
## $`chr22:30901532-30901886`
## [1] "cg03214697" "cg00093544" "cg02923264" "cg03014622" "cg19964641"
## [6] "cg15829116" "cg07217063"
## 
## $`chr22:39883190-39883347`
## [1] "cg00550928" "cg00101350" "cg26692811"
## 
## $`chr22:30685584-30685926`
## [1] "cg00101453" "cg23835894" "cg22088670" "cg03652890" "cg24843389"
## [6] "cg15272908" "cg20395643" "cg10998744"
## 
## $`chr22:45064086-45064238`
## [1] "cg10031995" "cg21293611" "cg11450750"
## 
## $`chr22:45072455-45072724`
## [1] "cg24019054" "cg20339720" "cg16166559" "cg06849477"

In order to make these examples as simple as possible, we will remove the CpGs from our data set which are not included in these 10 regions. In practice, you would keep the full data set.

keepCpGs_char <- unique(unlist(closeByGene_ls[1:10]))
betasChr22small_df <- 
  betasChr22_df[rownames(betasChr22_df) %in% keepCpGs_char, ]
dim(betasChr22small_df)
## [1] 54 20

3.1 Compute residuals

This step removes uninteresting technical and biological effects using the GetResiduals function, so that the resulting co-methylated clusters are only driven by the biological factors we are interested in. Note that there may be an error with the parallel back-end (1 parallel job did not deliver a result); this has occurred rarely for us (and not consistently enough for us to create a reproducible example), but it goes away when we execute the code a second time. We then recommend that you test this with a smaller subset of your data first. If you are able to consistently replicate a parallel error, please submit a bug report: https://github.com/TransBioInfoLab/coMethDMR/issues.

resid_mat <- GetResiduals(
  dnam = betasChr22small_df,
  # converts to Mvalues for fitting linear model
  betaToM = TRUE,  
  pheno_df = pheno_df,
  # Features in pheno_df used as covariates
  covariates_char = c("age.brain", "sex", "slide"),
  nCores_int = 2
)
## Phenotype data is not in the same order as methylation data. We used column Sample in phenotype data to put these two files in the same order.

3.2 Finding co-methylated regions

The cluster computing with the BiocParallel package involves simply changing the default value of one argument: nCores_int. This argument enables you to perform parallel computing when you set the number of cores to an integer value greater than 1. If you do not know how many cores your machine has, use the detectCores() function from the parallel package. Note that, if you have many applications open on your computer, you should not use all of the cores available.

Once we know how many cores we have available, we execute the CoMethAllRegions() function using each worker in the cluster, to find co-methylated clusters in the genic regions. As an example, we only analyze the first 10 CpG regions (CpGs_ls).

system.time(
  coMeth_ls <- CoMethAllRegions(
    dnam = resid_mat,
    betaToM = FALSE,
    method = "spearman",
    arrayType = "450k",
    CpGs_ls = closeByGene_ls[1:10],
    nCores_int = 2
  )
)
##    user  system elapsed 
##   7.014   0.708   9.468
# Windows: NA
# Mac: ~14 seconds for 2 cores

The object coMeth_ls is a list, with each item containing the list of CpGs within an identified co-methylated region.

3.3 Testing the co-methylated regions

Next we test these co-methylated regions against continuous phenotype stage, adjusting for covariates age and sex, by executing the lmmTestAllRegions() function.

res_df <- lmmTestAllRegions(
  betas = betasChr22small_df,
  region_ls = coMeth_ls,
  pheno_df = pheno_df,
  contPheno_char = "stage",
  covariates_char = c("age.brain", "sex"),
  modelType = "randCoef",
  arrayType = "450k",
  nCores_int = 2
  # outLogFile = "res_lmm_log.txt"
)

Model fit messages and diagnostics for each region will be saved to the log file specified with the outLogFile argument. For a single region, this will return a one row of model fit statistics similar to the following:

chrom start end nCpGs Estimate StdErr Stat pValue
chr22 24823455 24823519 4 -0.0702 0.0290 -2.4184 0.0155

Finally, we can annotate these results:

AnnotateResults(res_df)
##   chrom    start      end nCpGs     Estimate     StdErr       Stat      pValue
## 1 chr22 30662799 30663041     8 -0.059977631 0.01878094 -3.1935379 0.001405409
## 2 chr22 43253521 43253559     4 -0.007602187 0.02873538 -0.2645584 0.791349642
## 3 chr22 30901532 30901886     7  0.018015623 0.01555638  1.1580854 0.246829176
## 4 chr22 39883190 39883347     3  0.069276861 0.02864119  2.4187847 0.015572455
##           FDR           UCSC_RefGene_Group UCSC_RefGene_Accession
## 1 0.005621636 1stExon;5'UTR;TSS1500;TSS200              NM_020530
## 2 0.791349642                       TSS200 NM_001142293;NM_014570
## 3 0.329105569    1stExon;5'UTR;Body;TSS200 NM_001161368;NM_174977
## 4 0.031144911         1stExon;5'UTR;TSS200 NM_001098270;NM_002409
##   UCSC_RefGene_Name Relation_to_Island
## 1               OSM            OpenSea
## 2           ARFGAP3             Island
## 3           SEC14L4     Island;S_Shore
## 4             MGAT3            N_Shore

4 coMethDMR Analysis Pipeline for 450k Methylation Arrays Datasets via BiocParallel

In this section, we provide example scripts for testing genic and intergenic regions using coMethDMR and BiocParallel R packages.

First, we read in clusters of CpGs located closely on the array, in genic and intergenic regions, then combine them into a single list.

closeByGene_ls <- readRDS(
  system.file(
    "extdata",
    "450k_Gene_3_200.rds",
    package = 'coMethDMR',
    mustWork = TRUE
  )
)

closeByInterGene_ls <- readRDS(
  system.file(
    "extdata",
    "450k_InterGene_3_200.rds",
    package = 'coMethDMR',
    mustWork = TRUE
  )
)

# put them together in one list
closeBy_ls <- c(closeByInterGene_ls, closeByGene_ls)
length(closeBy_ls)
## [1] 42536
closeBy_ls[1:3]
## $`chr1:569427-569687`
## [1] "cg08858441" "cg03348902" "cg01070250"
## 
## $`chr1:713921-714177`
## [1] "cg10037654" "cg14057946" "cg11422233" "cg16047670" "cg18263059"
## [6] "cg01014490"
## 
## $`chr1:834183-834356`
## [1] "cg13938959" "cg12445832" "cg23999112"

With this list of regions and their probe IDs, repeat the analysis code in the previous section.

5 A Comment on Using EPIC Methylation Arrays Datasets

The analysis for EPIC methylation arrays would be the same as those for 450k arrays, except by testing genomic regions in files “EPIC_Gene_3_200.rds” and “EPIC_InterGene_3_200.RDS”, instead of “450k_Gene_3_200.rds” and “450k_InterGene_3_200.rds”. These data sets are available at https://github.com/TransBioInfoLab/coMethDMR_data/tree/main/data.

6 Additional Comments on Computational Time and Resources

In this vignette, we have analyzed a small subset of a real EWAS dataset (i.e. only chromosome 22 data on 20 subjects). To give users a more realistic estimate of time for analyzing real EWAS datasets, we also measured time used for analyzing the entire Lunnon et al. (2014) dataset with 110 samples on all chromosomes. These computation times measured on a Dell Precision 5810 with 64Gb of RAM, an Intel Xeon E5-2640 CPU at 2.40Ghz, and using up to 20 cores. More specifically, in Section 4, the entire coMethDMR workflow for took 103 minutes with 6 cores and used a maximum of 24Gb of RAM (for the CoMethAllRegions() function). We’re currently working improving the speed and reducing the size of coMethDMR, so please check back soon for updates.

7 Session Information

utils::sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## 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       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] BiocParallel_1.36.0  corrplot_0.92        GenomicRanges_1.54.0
##  [4] GenomeInfoDb_1.38.0  IRanges_2.36.0       S4Vectors_0.40.0    
##  [7] sesameData_1.19.0    ExperimentHub_2.10.0 AnnotationHub_3.10.0
## [10] BiocFileCache_2.10.0 dbplyr_2.3.4         BiocGenerics_0.48.0 
## [13] coMethDMR_1.6.0      BiocStyle_2.30.0    
## 
## loaded via a namespace (and not attached):
##   [1] splines_4.3.1                                     
##   [2] later_1.3.1                                       
##   [3] BiocIO_1.12.0                                     
##   [4] bitops_1.0-7                                      
##   [5] filelock_1.0.2                                    
##   [6] tibble_3.2.1                                      
##   [7] preprocessCore_1.64.0                             
##   [8] XML_3.99-0.14                                     
##   [9] lifecycle_1.0.3                                   
##  [10] lattice_0.22-5                                    
##  [11] MASS_7.3-60                                       
##  [12] base64_2.0.1                                      
##  [13] scrime_1.3.5                                      
##  [14] magrittr_2.0.3                                    
##  [15] minfi_1.48.0                                      
##  [16] limma_3.58.0                                      
##  [17] sass_0.4.7                                        
##  [18] rmarkdown_2.25                                    
##  [19] jquerylib_0.1.4                                   
##  [20] yaml_2.3.7                                        
##  [21] httpuv_1.6.12                                     
##  [22] doRNG_1.8.6                                       
##  [23] askpass_1.2.0                                     
##  [24] DBI_1.1.3                                         
##  [25] minqa_1.2.6                                       
##  [26] RColorBrewer_1.1-3                                
##  [27] abind_1.4-5                                       
##  [28] zlibbioc_1.48.0                                   
##  [29] quadprog_1.5-8                                    
##  [30] purrr_1.0.2                                       
##  [31] RCurl_1.98-1.12                                   
##  [32] rappdirs_0.3.3                                    
##  [33] GenomeInfoDbData_1.2.11                           
##  [34] genefilter_1.84.0                                 
##  [35] annotate_1.80.0                                   
##  [36] DelayedMatrixStats_1.24.0                         
##  [37] codetools_0.2-19                                  
##  [38] DelayedArray_0.28.0                               
##  [39] xml2_1.3.5                                        
##  [40] tidyselect_1.2.0                                  
##  [41] lme4_1.1-34                                       
##  [42] beanplot_1.3.1                                    
##  [43] matrixStats_1.0.0                                 
##  [44] illuminaio_0.44.0                                 
##  [45] GenomicAlignments_1.38.0                          
##  [46] jsonlite_1.8.7                                    
##  [47] multtest_2.58.0                                   
##  [48] ellipsis_0.3.2                                    
##  [49] survival_3.5-7                                    
##  [50] iterators_1.0.14                                  
##  [51] foreach_1.5.2                                     
##  [52] tools_4.3.1                                       
##  [53] progress_1.2.2                                    
##  [54] Rcpp_1.0.11                                       
##  [55] glue_1.6.2                                        
##  [56] SparseArray_1.2.0                                 
##  [57] xfun_0.40                                         
##  [58] MatrixGenerics_1.14.0                             
##  [59] dplyr_1.1.3                                       
##  [60] HDF5Array_1.30.0                                  
##  [61] withr_2.5.1                                       
##  [62] numDeriv_2016.8-1.1                               
##  [63] IlluminaHumanMethylation450kanno.ilmn12.hg19_0.6.1
##  [64] BiocManager_1.30.22                               
##  [65] fastmap_1.1.1                                     
##  [66] boot_1.3-28.1                                     
##  [67] rhdf5filters_1.14.0                               
##  [68] fansi_1.0.5                                       
##  [69] openssl_2.1.1                                     
##  [70] digest_0.6.33                                     
##  [71] R6_2.5.1                                          
##  [72] mime_0.12                                         
##  [73] colorspace_2.1-0                                  
##  [74] biomaRt_2.58.0                                    
##  [75] RSQLite_2.3.1                                     
##  [76] tidyr_1.3.0                                       
##  [77] utf8_1.2.4                                        
##  [78] generics_0.1.3                                    
##  [79] data.table_1.14.8                                 
##  [80] rtracklayer_1.62.0                                
##  [81] prettyunits_1.2.0                                 
##  [82] httr_1.4.7                                        
##  [83] S4Arrays_1.2.0                                    
##  [84] pkgconfig_2.0.3                                   
##  [85] gtable_0.3.4                                      
##  [86] blob_1.2.4                                        
##  [87] siggenes_1.76.0                                   
##  [88] XVector_0.42.0                                    
##  [89] htmltools_0.5.6.1                                 
##  [90] bookdown_0.36                                     
##  [91] scales_1.2.1                                      
##  [92] Biobase_2.62.0                                    
##  [93] png_0.1-8                                         
##  [94] knitr_1.44                                        
##  [95] tzdb_0.4.0                                        
##  [96] rjson_0.2.21                                      
##  [97] nlme_3.1-163                                      
##  [98] curl_5.1.0                                        
##  [99] nloptr_2.0.3                                      
## [100] bumphunter_1.44.0                                 
## [101] cachem_1.0.8                                      
## [102] rhdf5_2.46.0                                      
## [103] stringr_1.5.0                                     
## [104] BiocVersion_3.18.0                                
## [105] parallel_4.3.1                                    
## [106] AnnotationDbi_1.64.0                              
## [107] restfulr_0.0.15                                   
## [108] GEOquery_2.70.0                                   
## [109] pillar_1.9.0                                      
## [110] grid_4.3.1                                        
## [111] reshape_0.8.9                                     
## [112] vctrs_0.6.4                                       
## [113] promises_1.2.1                                    
## [114] xtable_1.8-4                                      
## [115] evaluate_0.22                                     
## [116] readr_2.1.4                                       
## [117] GenomicFeatures_1.54.0                            
## [118] magick_2.8.1                                      
## [119] cli_3.6.1                                         
## [120] locfit_1.5-9.8                                    
## [121] compiler_4.3.1                                    
## [122] Rsamtools_2.18.0                                  
## [123] rlang_1.1.1                                       
## [124] crayon_1.5.2                                      
## [125] rngtools_1.5.2                                    
## [126] nor1mix_1.3-0                                     
## [127] mclust_6.0.0                                      
## [128] plyr_1.8.9                                        
## [129] stringi_1.7.12                                    
## [130] lmerTest_3.1-3                                    
## [131] munsell_0.5.0                                     
## [132] Biostrings_2.70.0                                 
## [133] Matrix_1.6-1.1                                    
## [134] hms_1.1.3                                         
## [135] sparseMatrixStats_1.14.0                          
## [136] bit64_4.0.5                                       
## [137] ggplot2_3.4.4                                     
## [138] Rhdf5lib_1.24.0                                   
## [139] KEGGREST_1.42.0                                   
## [140] statmod_1.5.0                                     
## [141] shiny_1.7.5.1                                     
## [142] SummarizedExperiment_1.32.0                       
## [143] interactiveDisplayBase_1.40.0                     
## [144] memoise_2.0.1                                     
## [145] bslib_0.5.1                                       
## [146] bit_4.0.5