Last updated: 2021-04-13

Checks: 7 0

Knit directory: methyl-geneset-testing/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


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.

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.

The command set.seed(20200302) 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.

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

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 62fdddc. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

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:    .Rproj.user/
    Ignored:    analysis/figures.nb.html
    Ignored:    code/.DS_Store
    Ignored:    code/.Rhistory
    Ignored:    code/.job/
    Ignored:    code/old/
    Ignored:    data/.DS_Store
    Ignored:    data/annotations/
    Ignored:    data/cache-intermediates/
    Ignored:    data/cache-region/
    Ignored:    data/cache-rnaseq/
    Ignored:    data/cache-runtime/
    Ignored:    data/datasets/.DS_Store
    Ignored:    data/datasets/GSE110554-data.RData
    Ignored:    data/datasets/GSE120854/
    Ignored:    data/datasets/GSE120854_RAW.tar
    Ignored:    data/datasets/GSE135446-data.RData
    Ignored:    data/datasets/GSE135446/
    Ignored:    data/datasets/GSE135446_RAW.tar
    Ignored:    data/datasets/GSE45459-data.RData
    Ignored:    data/datasets/GSE45459_Matrix_signal_intensities.txt
    Ignored:    data/datasets/GSE45460/
    Ignored:    data/datasets/GSE45460_RAW.tar
    Ignored:    data/datasets/GSE95460_RAW.tar
    Ignored:    data/datasets/GSE95460_RAW/
    Ignored:    data/datasets/GSE95462-data.RData
    Ignored:    data/datasets/GSE95462/
    Ignored:    data/datasets/GSE95462_RAW/
    Ignored:    data/datasets/SRP100803/
    Ignored:    data/datasets/SRP125125/.DS_Store
    Ignored:    data/datasets/SRP125125/SRR6298*/
    Ignored:    data/datasets/SRP125125/SRR_Acc_List.txt
    Ignored:    data/datasets/SRP125125/SRR_Acc_List_Full.txt
    Ignored:    data/datasets/SRP125125/SraRunTable.txt
    Ignored:    data/datasets/SRP125125/multiqc_data/
    Ignored:    data/datasets/SRP125125/multiqc_report.html
    Ignored:    data/datasets/SRP125125/quants/
    Ignored:    data/datasets/SRP166862/
    Ignored:    data/datasets/SRP217468/
    Ignored:    data/datasets/TCGA.BRCA.rds
    Ignored:    data/datasets/TCGA.KIRC.rds
    Ignored:    data/misc/
    Ignored:    output/.DS_Store
    Ignored:    output/FDR-analysis/
    Ignored:    output/compare-methods/
    Ignored:    output/figures/
    Ignored:    output/methylgsa-params/
    Ignored:    output/outputs.tar.gz
    Ignored:    output/random-cpg-sims/
    Ignored:    output/test/

Untracked files:
    Untracked:  analysis/old/

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.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/01_exploreArrayBiasEPIC.Rmd) and HTML (docs/01_exploreArrayBiasEPIC.html) files. If you've configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 5e34ed3 JovMaksimovic 2021-04-12 Figure updates
html bbf77e1 JovMaksimovic 2020-08-21 Build site.
Rmd e3b14f4 JovMaksimovic 2020-08-21 wflow_publish(c("analysis/01_exploreArrayBiasEPIC.Rmd", "analysis/02_exploreArrayBias450.Rmd",
html e66c39a JovMaksimovic 2020-08-14 Build site.
Rmd 39bdd22 JovMaksimovic 2020-08-14 Renamed analysis files with numbering for run order.

library(here)
library(glue)
library(minfi)
library(IlluminaHumanMethylationEPICanno.ilm10b4.hg19)
library(IlluminaHumanMethylation450kanno.ilmn12.hg19)
library(missMethyl)
library(org.Hs.eg.db)
library(GO.db)
library(patchwork)
library(grid)
library(ggplot2)
library(tibble)
library(dplyr)
source(here("code/utility.R"))

Array properties

Get the array annotation data.

ann <- loadAnnotation(arrayType="EPIC")

Associate CpGs to genes (ENTREZ ID) using the Illumina annotation information.

flatAnn <- loadFlatAnnotation(ann)

The number of CpGs annotated to a gene is highly variable.

numCpgsPerGene <- as.vector(table(flatAnn$entrezid))
summary(numCpgsPerGene)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00   10.00   20.00   27.25   33.00 1485.00 
dat <- data.frame(table(table(flatAnn$entrezid)))
numCpgsPerGene <- as.vector(table(flatAnn$entrezid))
med <- median(numCpgsPerGene)
mod <- getMode(numCpgsPerGene)

p <- ggplot(dat, aes(x=Var1, y=Freq)) +
    geom_segment(aes(x=Var1, xend=Var1, y=0, yend=Freq), color="darkgrey") +
    geom_point( color="black", size=1) +
    geom_vline(xintercept = med, linetype = "dashed", color = "red") +
    annotate("text", x = med + 2, label=glue("median = {med}"), y=720, colour="red", 
             size = 3, hjust="left") +
    geom_vline(xintercept = mod, linetype = "dashed", color = "blue") +
    annotate("text", x = mod + 2, label=glue("mode = {mod}"), y=780, colour="blue", 
             size = 3, hjust="left") +
    scale_x_discrete(breaks = c(1,10,20,30,45,55,70,80,90,100,120,135,150,160,180,
                                200,215,250,330,429,1485)) +
    theme(axis.text.x=element_text(angle=45, hjust=1)) +
    xlab("No. CpGs per gene") +
    ylab("Frequency")

p

Version Author Date
e66c39a JovMaksimovic 2020-08-14

Save figure for use in manuscript.

outDir <- here::here("output/figures")
if (!dir.exists(outDir)) dir.create(outDir)

fig <- here("output/figures/Fig-1A.rds")
saveRDS(p, fig, compress = FALSE)

The number of genes a CpG maps to can also vary, although the majority of CpGs only map to one gene.

dat <- data.frame(table(table(flatAnn$cpg)))
dat$Split <- ifelse(dat$Freq > 2500, "A", 
                    ifelse(dat$Freq < 2500 & dat$Freq > 65,"B","C"))

a <- ggplot(dat[dat$Split == "A",], aes(x=Var1, y=Freq)) +
    geom_bar(stat = "identity") +
    theme(axis.title.x = element_blank(), 
          axis.text.y=element_text(angle=90, hjust=0.5)) +
    ylab("Frequency")

b <- ggplot(dat[dat$Split == "B",], aes(x=Var1, y=Freq)) +
    geom_bar(stat = "identity") +
    theme(axis.title.x=element_blank(),
          axis.title.y = element_blank())

c <- ggplot(dat[dat$Split == "C",], aes(x=Var1, y=Freq)) +
    geom_bar(stat = "identity") +
    theme(axis.title.y = element_blank(),
          axis.title.x = element_blank()) 

p <- a + b + c + plot_layout(widths = c(1,2,6)) + 
   plot_annotation(caption = "No. genes mapped to a CpG",
                   theme = theme(plot.caption = element_text(hjust = 0.5,
                                                             size = 12)))
p

Version Author Date
e66c39a JovMaksimovic 2020-08-14

Save figure for use in manuscript.

fig <- here("output/figures/Fig-1D.rds")
saveRDS(p, fig, compress = FALSE)

Explore the distribution of the average number of CpGs per gene, per GO category.

ann450 <- loadAnnotation("450k")
flatAnn450 <- loadFlatAnnotation(ann450)

cpgEgGoEPIC <- cpgsEgGoFreqs(flatAnn)
cpgEgGoEPIC$Array <- "EPIC"

cpgEgGo450 <- cpgsEgGoFreqs(flatAnn450)
cpgEgGo450$Array <- "450k"

cpgEgGoEPIC %>% bind_rows(cpgEgGo450) %>% 
    group_by(Array, GO) %>%
    summarise(avg = mean(Freq)) -> datSum
`summarise()` has grouped output by 'Array'. You can override using the `.groups` argument.
maxEPIC <- max(datSum$avg[datSum$Array == "EPIC"])
max450 <- max(datSum$avg[datSum$Array == "450k"])

bw <- 2
pal <- paletteer::paletteer_d("RColorBrewer::Set1", 2)
platform <- c("EPIC" = pal[1], "450k" = pal[2])
p <- ggplot(datSum, aes(x=avg)) +
    geom_histogram(dat = subset(datSum, Array == "450k"),
                   alpha = 0.5, aes(fill = "450k"), binwidth = bw) +
    geom_histogram(dat = subset(datSum, Array == "EPIC"),
                   alpha = 0.5, aes(fill = "EPIC"), binwidth = bw) +
    geom_vline(xintercept = maxEPIC, linetype="dashed", colour = pal[1]) +
    geom_vline(xintercept = max450, linetype="dashed", colour = pal[2]) +
    labs(x="Mean no. CpGs per gene per GO category", y = "Frequency",
         fill = "Platform") +
    annotate("text", x = maxEPIC - 4, label=glue("Max. EPIC = {maxEPIC}"),
             y = 1500, colour=pal[1], size = 2.5, hjust="right") +
    annotate("text", x = max450 - 4, label=glue("Max. 450k = {max450}"),
             y = 2500, colour=pal[2], size = 2.5, hjust="right") +
    scale_fill_manual(values = platform)
p

Version Author Date
e66c39a JovMaksimovic 2020-08-14

Save figure for use in manuscript.

fig <- here("output/figures/Fig-1C.rds")
saveRDS(p, fig, compress = FALSE)

Null simulations

We randomly select 50, 100, 500, 1000, 5000 and 10000 sets of CpGs and perform GO testing on each set 100 times, with and without adjusting for the various biases on the array.

The code used to produce the simulation results can be found in the code/random-cpg-sims directory. It consists of three scripts: genRandCpgSimJobs.R, randomCpgSim.R and processRandCpgSim.R. The genRandCpgSimJobs.R script creates and submits Slurm job scripts that run the randomCpgSim.R script, in parallel, on a HPC. Each job executes one of the 100 simulations, for a fixed number of randomly selected CpGs, using either the 450K or EPIC array annotation. The results of each job are saved as an RDS file named {arrayType}.{noCpgs}.{simNo}.rds in the output/random-cpg-sims directory. Once all simulation jobs are complete, the processRandCpgSim.R must be executed to collate the results into a single object for each array type, which are then saved as 450K.rds and EPIC.rds in the output/random-cpg-sims directory. The intermediate RDS files are moved into output/random-cpg-sims/.bin, which can then be deleted, if no longer required. The subsequent section requires EPIC.rds to be present in the output/random-cpg-sims directory for downstream analysis and plotting.

Results

The following boxplots show what proportion of the 100 simulations, at each level of CpGs sampled, had a raw p-value less than 0.05. This gives us an idea of the false discovery rate with and without adjustment for the number of CpGs annotated to a gene.

dat <- readRDS(here("output/random-cpg-sims/EPIC.rds"))

dat %>% filter(method %in% names(dict)) %>%
    mutate(method = unname(dict[method])) %>%
    group_by(simNo, noCpgs, method) %>% 
    summarise(pSig = sum(P.DE < 0.05)/length(P.DE)) -> sigDat
`summarise()` has grouped output by 'simNo', 'noCpgs'. You can override using the `.groups` argument.
p <- ggplot(sigDat, aes(x=noCpgs, y=pSig, fill=method)) + 
    geom_violin(size = 0.3) +
    geom_hline(yintercept=0.05, linetype="dashed", color = "red") +
    labs(y="Prop. GO cat. with p-value < 0.05", x="No. randomly sampled CpGs",
         fill="Method") +
    scale_fill_manual(values = methodCols)  +
    facet_grid(cols = vars(noCpgs), scales = "free_x") + 
    theme(strip.background = element_blank(), 
          strip.text = element_blank())
p

Version Author Date
e66c39a JovMaksimovic 2020-08-14
fig <- here("output/figures/Fig-3A.rds")
saveRDS(p, fig, compress = FALSE)

QQ plots of randomly selected simulations at each level of CpGs sampled.

set.seed(42)
s <- sample(1:100, 3)
dat %>% filter(simNo %in% s) %>%
    filter(method %in% names(dict)) %>%
    mutate(method = unname(dict[method])) %>%
    arrange(simNo, noCpgs, method, P.DE) %>%
    group_by(simNo, noCpgs, method) %>%
    mutate(exp = 1:n()/n()) -> subDat

p <- ggplot(subDat, aes(x=-log10(exp), y=-log10(P.DE), color=method)) + 
    geom_point(shape = 1, size = 0.5) + 
    facet_grid(noCpgs ~ simNo, scales = "free_y") +
    scale_color_manual(values = methodCols)

p + geom_line(aes(x=-log10(exp), y=-log10(exp)),
              linetype="dashed", color = "black") +
    labs(y=expression(Observed~~-log[10](italic(p))), 
         x=expression(Expected~~-log[10](italic(p))),
         color="Method") +
    theme(legend.position="bottom", strip.text.x = element_blank())

Version Author Date
e66c39a JovMaksimovic 2020-08-14

Explore the relationship between the median, average number of CpGs, per gene, per GO category and the various sources of bias on the array.

goFreq <- as_tibble(unique(cpgEgGoEPIC[,c("GO","Freq")]))

dat %>% filter(method %in% names(dict)) %>%
    mutate(method = unname(dict[method])) %>% 
    filter(P.DE < 0.05) %>%
    inner_join(goFreq, by=c("GO" = "GO")) %>%
    group_by(simNo, noCpgs, method, GO) %>%
    summarise(avgFreq=mean(Freq)) %>% 
    group_by(simNo, noCpgs, method) %>%
    summarise(medAvgFreq=median(avgFreq)) -> medAvgDat
`summarise()` has grouped output by 'simNo', 'noCpgs', 'method'. You can override using the `.groups` argument.
`summarise()` has grouped output by 'simNo', 'noCpgs'. You can override using the `.groups` argument.
goFreq %>% group_by(GO) %>% 
    summarise(med = median(Freq)) %>%
    dplyr::select(med) %>%
    summarise(med = median(med)) -> medPerGO

dat_text <- data.frame(
  label = c(rep("",4),
            "Med. Med. # Genes", 
            glue("per GO Cat. = {round(medPerGO$med, 2)}")),
  method = "GOmeth",
  noCpgs = unique(medAvgDat$noCpgs),
  x = unique(medAvgDat$noCpgs),
  y = rep(medPerGO$med + 1, 6)
)

p <- ggplot(medAvgDat, aes(x=noCpgs, y=medAvgFreq, fill=method)) +
    geom_violin(size = 0.3, scale = "area") +
    stat_summary(geom="point", size = 0.5, color="white",
                 position = position_dodge(0.9),
                 show.legend = FALSE, fun = median) +
    labs(y="Median avg. no. CpGs/gene/GO cat.",
         x="No. randomly sampled CpGs",
         fill="Method")  +
    scale_fill_manual(values = methodCols) +
    geom_hline(yintercept = medPerGO$med, linetype="dashed", color = "red") +
    geom_text(data = dat_text, colour="red", size = 2.5, hjust="left", 
              mapping = aes(x = -Inf, y = y, label = label)) +
    facet_grid(cols = vars(noCpgs), scales = "free_x") +
    theme(strip.background = element_blank(),
          strip.text = element_blank())
p

Version Author Date
bbf77e1 JovMaksimovic 2020-08-21
e66c39a JovMaksimovic 2020-08-14

Save figure for use in manuscript.

fig <- here("output/figures/Fig-3B.rds")
saveRDS(p, fig, compress = FALSE)

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6

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

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

attached base packages:
 [1] grid      stats4    parallel  stats     graphics  grDevices utils    
 [8] datasets  methods   base     

other attached packages:
 [1] dplyr_1.0.5                                        
 [2] tibble_3.1.0                                       
 [3] ggplot2_3.3.3                                      
 [4] patchwork_1.1.1                                    
 [5] GO.db_3.12.1                                       
 [6] org.Hs.eg.db_3.12.0                                
 [7] AnnotationDbi_1.52.0                               
 [8] missMethyl_1.24.0                                  
 [9] IlluminaHumanMethylation450kanno.ilmn12.hg19_0.6.0 
[10] IlluminaHumanMethylationEPICanno.ilm10b4.hg19_0.6.0
[11] minfi_1.36.0                                       
[12] bumphunter_1.32.0                                  
[13] locfit_1.5-9.4                                     
[14] iterators_1.0.13                                   
[15] foreach_1.5.1                                      
[16] Biostrings_2.58.0                                  
[17] XVector_0.30.0                                     
[18] SummarizedExperiment_1.20.0                        
[19] Biobase_2.50.0                                     
[20] MatrixGenerics_1.2.1                               
[21] matrixStats_0.58.0                                 
[22] GenomicRanges_1.42.0                               
[23] GenomeInfoDb_1.26.7                                
[24] IRanges_2.24.1                                     
[25] S4Vectors_0.28.1                                   
[26] BiocGenerics_0.36.0                                
[27] glue_1.4.2                                         
[28] here_1.0.1                                         
[29] workflowr_1.6.2                                    

loaded via a namespace (and not attached):
  [1] BiocFileCache_1.14.0      plyr_1.8.6               
  [3] splines_4.0.3             BiocParallel_1.24.1      
  [5] digest_0.6.27             htmltools_0.5.1.1        
  [7] fansi_0.4.2               magrittr_2.0.1           
  [9] memoise_2.0.0             paletteer_1.3.0          
 [11] limma_3.46.0              readr_1.4.0              
 [13] annotate_1.68.0           askpass_1.1              
 [15] siggenes_1.64.0           prettyunits_1.1.1        
 [17] colorspace_2.0-0          blob_1.2.1               
 [19] rappdirs_0.3.3            xfun_0.22                
 [21] prismatic_1.0.0           crayon_1.4.1             
 [23] RCurl_1.98-1.3            jsonlite_1.7.2           
 [25] genefilter_1.72.1         GEOquery_2.58.0          
 [27] survival_3.2-10           gtable_0.3.0             
 [29] zlibbioc_1.36.0           DelayedArray_0.16.3      
 [31] Rhdf5lib_1.12.1           HDF5Array_1.18.1         
 [33] scales_1.1.1              DBI_1.1.1                
 [35] rngtools_1.5              Rcpp_1.0.6               
 [37] xtable_1.8-4              progress_1.2.2           
 [39] bit_4.0.4                 mclust_5.4.7             
 [41] preprocessCore_1.52.1     httr_1.4.2               
 [43] RColorBrewer_1.1-2        ellipsis_0.3.1           
 [45] farver_2.1.0              pkgconfig_2.0.3          
 [47] reshape_0.8.8             XML_3.99-0.6             
 [49] sass_0.3.1                dbplyr_2.1.1             
 [51] utf8_1.2.1                reshape2_1.4.4           
 [53] labeling_0.4.2            tidyselect_1.1.0         
 [55] rlang_0.4.10              later_1.1.0.1            
 [57] munsell_0.5.0             tools_4.0.3              
 [59] cachem_1.0.4              generics_0.1.0           
 [61] RSQLite_2.2.5             evaluate_0.14            
 [63] stringr_1.4.0             fastmap_1.1.0            
 [65] yaml_2.2.1                rematch2_2.1.2           
 [67] knitr_1.31                bit64_4.0.5              
 [69] fs_1.5.0                  beanplot_1.2             
 [71] scrime_1.3.5              purrr_0.3.4              
 [73] nlme_3.1-152              doRNG_1.8.2              
 [75] sparseMatrixStats_1.2.1   whisker_0.4              
 [77] nor1mix_1.3-0             xml2_1.3.2               
 [79] biomaRt_2.46.3            compiler_4.0.3           
 [81] curl_4.3                  statmod_1.4.35           
 [83] bslib_0.2.4               stringi_1.5.3            
 [85] highr_0.8                 GenomicFeatures_1.42.3   
 [87] lattice_0.20-41           Matrix_1.3-2             
 [89] multtest_2.46.0           vctrs_0.3.7              
 [91] pillar_1.5.1              lifecycle_1.0.0          
 [93] rhdf5filters_1.2.0        jquerylib_0.1.3          
 [95] data.table_1.14.0         bitops_1.0-6             
 [97] httpuv_1.5.5              rtracklayer_1.50.0       
 [99] R6_2.5.0                  promises_1.2.0.1         
[101] codetools_0.2-18          MASS_7.3-53.1            
[103] assertthat_0.2.1          rhdf5_2.34.0             
[105] openssl_1.4.3             rprojroot_2.0.2          
[107] withr_2.4.1               GenomicAlignments_1.26.0 
[109] Rsamtools_2.6.0           GenomeInfoDbData_1.2.4   
[111] hms_1.0.0                 quadprog_1.5-8           
[113] tidyr_1.1.3               base64_2.0               
[115] rmarkdown_2.7             DelayedMatrixStats_1.12.3
[117] illuminaio_0.32.0         git2r_0.28.0