Last updated: 2020-06-01

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 4e77103. 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:    code/.job/
    Ignored:    code/old/
    Ignored:    data/
    Ignored:    output/.DS_Store
    Ignored:    output/450K.rds
    Ignored:    output/CD4vCD8.GO.csv
    Ignored:    output/CD4vCD8.KEGG.csv
    Ignored:    output/EPIC.rds
    Ignored:    output/FDR-analysis/
    Ignored:    output/compare-methods/
    Ignored:    output/random-cpg-sims/

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/gomethByFeature.Rmd) and HTML (docs/gomethByFeature.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 4e77103 JovMaksimovic 2020-06-01 wflow_publish(c(“analysis/index.Rmd”, “analysis/gomethByFeature.Rmd”))
Rmd 426c48d Jovana Maksimovic 2020-06-01 Added region results for restriction by genomic feature analysis.
Rmd ae4baaf Jovana Maksimovic 2020-05-18 Minor update to yaml title.
Rmd b9f7894 JovMaksimovic 2020-05-15 Code for testing gometh with restricting CpGs by genomic features.

library(here)
library(glue)
library(limma)
library(tidyverse)
library(patchwork)
if(packageVersion("missMethyl") != "1.21.5"){
    devtools::install_github("Oshlack/missMethyl", ref = "genomic-features")
}
library(missMethyl)

Test restricting significant CpGs by genomic feature in gometh

Load methylation blood cell comparison input data and annotation.

load(here("data/input.RData"))
anno <- minfi::getAnnotation(IlluminaHumanMethylationEPICanno.ilm10b4.hg19)

Effect of restricting by genomic features on No. Sig. CpGs

For this dataset and these comparisons, restricting the significant CpGs by genomic feature reduces their numbers more dramatically than changing the FDR threshold from 0.05 to 0.01.

flatAnn <- missMethyl:::.getFlatAnnotation("EPIC", anno = anno)

dat <- NULL
for(i in 1:ncol(tfit$contrasts)){
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = 5000)),
                      cut = "Top 5000",
                      features = "ALL",
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    dat <- bind_rows(dat, tmp)
    
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.05)),
                      cut = "FDR < 0.05",
                      features = "ALL",
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    dat <- bind_rows(dat, tmp)
    
    feats <- c("TSS200","TSS1500","1stExon")
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.05)),
                      cut = "FDR < 0.05",
                      features = paste(feats,collapse = "."),
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    tmp %>% filter(cpgs %in% flatAnn$cpg[flatAnn$group %in% feats]) -> tmp
    dat <- bind_rows(dat, tmp)
    
    feats <- c("Body","ExonBnd")
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.05)),
                      cut = "FDR < 0.05",                      
                      features = paste(feats, collapse = "."),
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    tmp %>% filter(cpgs %in% flatAnn$cpg[flatAnn$group %in% feats]) -> tmp
    dat <- bind_rows(dat, tmp)
    
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.01)),
                      cut = "FDR < 0.01",
                      features = "ALL",
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    dat <- bind_rows(dat, tmp)
    
    feats <- c("TSS200","TSS1500","1stExon")
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.01)),
                      cut = "FDR < 0.01",
                      features = paste(feats,collapse = "."),
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    tmp %>% filter(cpgs %in% flatAnn$cpg[flatAnn$group %in% feats]) -> tmp
    dat <- bind_rows(dat, tmp)
    
    feats <- c("Body","ExonBnd")
    tmp <- data.frame(cpgs = rownames(topTreat(tfit, coef = i, num = Inf, 
                                               p.value = 0.01)),
                      cut = "FDR < 0.01",                      
                      features = paste(feats, collapse = "."),
                      contrast = colnames(tfit$contrasts)[i],
                      stringsAsFactors = FALSE)
    tmp %>% filter(cpgs %in% flatAnn$cpg[flatAnn$group %in% feats]) -> tmp
    dat <- bind_rows(dat, tmp)
}

dat %>% mutate(method = paste(features, cut, sep=".")) %>% 
    group_by(contrast, method) %>% tally() -> sub

ggplot(sub, aes(x = method, y = n, fill = method)) +
    geom_bar(stat = "identity", show.legend = FALSE) +
    facet_wrap(vars(contrast)) + 
    labs(fill = "Method", y = "No. significant CpGs", x = "Method") + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

Run gometh without and with restricting significant CpGs by genomic features. Examine imact on geneset testing results of restricting Cpgs significant at FDR < 0.05 to only certain genomic features.

outFile <- here("data/genomic-feature-go.rds")

if(!file.exists(outFile)){
    go <- NULL
    
    for(i in 1:ncol(tfit$contrasts)){
        top <- topTreat(tfit, coef = i, number = 5000)
        
        feats <- "ALL"
        tmp <- gometh(sig.cpg = rownames(top), 
                      all.cpg = rownames(tfit$coefficients), 
                      collection = "GO", array.type = "EPIC", anno = anno,
                      genomic.features = feats)
        tmp <- topGSA(tmp, number = Inf)
        tmp$FDR <- p.adjust(tmp$P.DE, method = "BH")
        tmp <- rownames_to_column(tmp, var = "ID")
        tmp$contrast <- colnames(tfit$contrasts)[i] 
        tmp$features <- paste(feats,collapse = ".")
        tmp$cut <- "Top 5000"
        go <- bind_rows(go, tmp)
        
        feats <- "ALL"
        tmp <- gometh(sig.cpg = rownames(topTreat(tfit, coef = i, number = Inf,
                                                  p.value = 0.05)), 
                      all.cpg = rownames(tfit$coefficients), 
                      collection = "GO", array.type = "EPIC", anno = anno,
                      genomic.features = feats)
        tmp <- topGSA(tmp, number = Inf)
        tmp$FDR <- p.adjust(tmp$P.DE, method = "BH")
        tmp <- rownames_to_column(tmp, var = "ID")
        tmp$contrast <- colnames(tfit$contrasts)[i] 
        tmp$features <- paste(feats,collapse = ".")
        tmp$cut <- "FDR < 0.05"
        go <- bind_rows(go, tmp)
        
        feats <- c("TSS200","TSS1500","1stExon")
        tmp <- gometh(sig.cpg = rownames(topTreat(tfit, coef = i, number = Inf,
                                                  p.value = 0.05)), 
                      all.cpg = rownames(tfit$coefficients), 
                      collection = "GO", array.type = "EPIC", anno = anno,
                      genomic.features = feats)
        tmp <- topGSA(tmp, number = Inf)
        tmp$FDR <- p.adjust(tmp$P.DE, method = "BH")
        tmp <- rownames_to_column(tmp, var = "ID")
        tmp$contrast <- colnames(tfit$contrasts)[i] 
        tmp$features <- paste(feats,collapse = ".")
        tmp$cut <- "FDR < 0.05"
        go <- bind_rows(go, tmp)
        
        feats <- c("Body","ExonBnd")
        tmp <- gometh(sig.cpg = rownames(topTreat(tfit, coef = i, number = Inf,
                                                  p.value = 0.05)), 
                      all.cpg = rownames(tfit$coefficients), 
                      collection = "GO", array.type = "EPIC", anno = anno,
                      genomic.features = feats)
        tmp <- topGSA(tmp, number = Inf)
        tmp$FDR <- p.adjust(tmp$P.DE, method = "BH")
        tmp <- rownames_to_column(tmp, var = "ID")
        tmp$contrast <- colnames(tfit$contrasts)[i] 
        tmp$features <- paste(feats,collapse = ".")
        tmp$cut <- "FDR < 0.05"
        go <- bind_rows(go, tmp)
        
    }
    saveRDS(go, file = outFile)
    
} else {
    go <- readRDS(outFile)
    
}

Gene set testing results with/without restriction by genomic feature

immuneGO <- unique(read.csv(here("data/GO-immune-system-process.txt"), 
                            stringsAsFactors = FALSE, header = FALSE, 
                            col.names = "GOID"))

go %>% mutate(method = paste(cut, features, sep="; ")) %>% 
    arrange(contrast, method, P.DE) %>%
    group_by(contrast, method) %>%
    mutate(csum = cumsum(ID %in% immuneGO$GOID)) %>%
    mutate(rank = 1:n()) %>%
    filter(rank <= 100) -> dat

p <- ggplot(dat, aes(x = rank, y = csum, colour = method)) +
    geom_line() +
    facet_wrap(vars(contrast), ncol=3) +
    geom_vline(xintercept = 10, linetype = "dotted") +
    labs(colour = "Method", x = "Rank", y = "Cumulative no. immune sets") +
    theme(legend.position = "bottom", 
          legend.text = element_text(size = 8)) +
    guides(col = guide_legend(nrow = 2))
p

immuneGO <- readRDS(here("data/RNAseq-GO.rds"))
immuneGO %>% group_by(contrast) %>%
    mutate(rank = 1:n()) %>%
    filter(rank <= 100) -> topSets

go %>% mutate(method = paste(cut, features, sep="; ")) %>%
    arrange(contrast, method, P.DE) %>%
    group_by(contrast, method) %>%
    mutate(csum = cumsum(ID %in% topSets$ID[topSets$contrast %in% contrast])) %>%
    mutate(rank = 1:n()) %>%
    filter(rank <= 100) -> sub

p <- ggplot(sub, aes(x = rank, y = csum, colour = method)) +
    geom_line() +
    facet_wrap(vars(contrast), ncol=3) +
    geom_vline(xintercept = 10, linetype = "dotted") +
    labs(colour = "Method", x = "Rank", 
         y = glue("Cumulative no. RNAseq sets")) +
    theme(legend.position = "bottom", 
          legend.text = element_text(size = 8)) +
    guides(col = guide_legend(nrow = 2))
p

Examine what the top 10 ranked gene sets are and how many genes they contain, for each approach and comparison.

go %>% mutate(method = paste(cut, features, sep="; ")) %>% 
    arrange(contrast, method, P.DE) %>%
    group_by(contrast, method) %>%
    mutate(rank = 1:n()) %>%
    filter(rank <= 10) -> sub

p <- vector("list", length(unique(sub$contrast)) * length(unique(sub$method)))
i = 1
for(cont in unique(sub$contrast)){
    c = 1
    for(meth in unique(sub$method)){
        tmp <- sub %>% filter(contrast == cont & method == meth) %>%
            mutate(rank = factor(rank), 
                   rank = factor(rank, levels = rev(levels(rank))))
        
        p[[i]] <- ggplot(tmp, aes(x = -log10(FDR), y = rank)) + 
            geom_point(aes(size = N), alpha = 0.5, 
                colour = scales::hue_pal()(length(unique(sub$method)))[c]) +
            scale_y_discrete(labels = rev(tmp$TERM)) +
            labs(y = "", size = "No. genes", title = meth) +
            theme(axis.text.y = element_text(size = 6),
                  plot.title = element_text(size = 8),
                  legend.position = "right", 
                  legend.key.size = unit(0.25, "cm"),
                  legend.text = element_text(size = 6),
                  legend.title = element_text(size = 8),
                  axis.text.x = element_text(size = 6),
                  axis.title.x = element_text(size = 8)) + 
            coord_cartesian(xlim = c(-log10(0.99), -log10(10^-30))) +
            geom_vline(xintercept = -log10(0.05), linetype = "dashed")
        i = i + 1
        c = c + 1
    }
}

(p[[1]] / p[[2]] / p[[3]] / p[[4]]) + 
    plot_annotation(title = unique(sub$contrast)[1],
                    theme = theme(plot.title = element_text(size = 10))) 

(p[[5]] / p[[6]] / p[[7]] / p[[8]]) + 
    plot_annotation(title = unique(sub$contrast)[2],
                    theme = theme(plot.title = element_text(size = 10))) 

(p[[9]] / p[[10]] / p[[11]] / p[[12]]) + 
    plot_annotation(title = unique(sub$contrast)[3],
                    theme = theme(plot.title = element_text(size = 10))) 


sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] stats4    parallel  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] missMethyl_1.21.5                                  
 [2] IlluminaHumanMethylationEPICanno.ilm10b4.hg19_0.6.0
 [3] IlluminaHumanMethylation450kanno.ilmn12.hg19_0.6.0 
 [4] minfi_1.32.0                                       
 [5] bumphunter_1.28.0                                  
 [6] locfit_1.5-9.4                                     
 [7] iterators_1.0.12                                   
 [8] foreach_1.5.0                                      
 [9] Biostrings_2.54.0                                  
[10] XVector_0.26.0                                     
[11] SummarizedExperiment_1.16.1                        
[12] DelayedArray_0.12.3                                
[13] BiocParallel_1.20.1                                
[14] matrixStats_0.56.0                                 
[15] Biobase_2.46.0                                     
[16] GenomicRanges_1.38.0                               
[17] GenomeInfoDb_1.22.1                                
[18] IRanges_2.20.2                                     
[19] S4Vectors_0.24.4                                   
[20] BiocGenerics_0.32.0                                
[21] patchwork_1.0.0                                    
[22] forcats_0.5.0                                      
[23] stringr_1.4.0                                      
[24] dplyr_1.0.0                                        
[25] purrr_0.3.4                                        
[26] readr_1.3.1                                        
[27] tidyr_1.1.0                                        
[28] tibble_3.0.1                                       
[29] ggplot2_3.3.1                                      
[30] tidyverse_1.3.0                                    
[31] limma_3.42.2                                       
[32] glue_1.4.1                                         
[33] here_0.1                                           
[34] workflowr_1.6.2                                    

loaded via a namespace (and not attached):
  [1] readxl_1.3.1             backports_1.1.7          BiocFileCache_1.10.2    
  [4] plyr_1.8.6               splines_3.6.3            digest_0.6.25           
  [7] htmltools_0.4.0          fansi_0.4.1              magrittr_1.5            
 [10] memoise_1.1.0            annotate_1.64.0          modelr_0.1.7            
 [13] askpass_1.1              siggenes_1.60.0          prettyunits_1.1.1       
 [16] colorspace_1.4-1         blob_1.2.1               rvest_0.3.5             
 [19] rappdirs_0.3.1           haven_2.2.0              xfun_0.13               
 [22] crayon_1.3.4             RCurl_1.98-1.2           jsonlite_1.6.1          
 [25] genefilter_1.68.0        GEOquery_2.54.1          survival_3.1-12         
 [28] gtable_0.3.0             zlibbioc_1.32.0          Rhdf5lib_1.8.0          
 [31] HDF5Array_1.14.4         scales_1.1.1             DBI_1.1.0               
 [34] rngtools_1.5             Rcpp_1.0.4.6             xtable_1.8-4            
 [37] progress_1.2.2           bit_1.1-15.2             mclust_5.4.6            
 [40] preprocessCore_1.48.0    httr_1.4.1               RColorBrewer_1.1-2      
 [43] ellipsis_0.3.1           farver_2.0.3             pkgconfig_2.0.3         
 [46] reshape_0.8.8            XML_3.99-0.3             dbplyr_1.4.4            
 [49] labeling_0.3             tidyselect_1.1.0         rlang_0.4.6             
 [52] later_1.0.0              AnnotationDbi_1.48.0     munsell_0.5.0           
 [55] cellranger_1.1.0         tools_3.6.3              cli_2.0.2               
 [58] generics_0.0.2           RSQLite_2.2.0            broom_0.5.6             
 [61] evaluate_0.14            yaml_2.2.1               org.Hs.eg.db_3.10.0     
 [64] knitr_1.28               bit64_0.9-7              fs_1.4.1                
 [67] beanplot_1.2             scrime_1.3.5             nlme_3.1-147            
 [70] doRNG_1.8.2              whisker_0.4              nor1mix_1.3-0           
 [73] xml2_1.3.2               biomaRt_2.42.1           compiler_3.6.3          
 [76] rstudioapi_0.11          curl_4.3                 reprex_0.3.0            
 [79] statmod_1.4.34           stringi_1.4.6            GenomicFeatures_1.38.2  
 [82] lattice_0.20-41          Matrix_1.2-18            multtest_2.42.0         
 [85] vctrs_0.3.0              pillar_1.4.4             lifecycle_0.2.0         
 [88] data.table_1.12.8        bitops_1.0-6             httpuv_1.5.2            
 [91] rtracklayer_1.46.0       R6_2.4.1                 promises_1.1.0          
 [94] codetools_0.2-16         MASS_7.3-51.6            assertthat_0.2.1        
 [97] rhdf5_2.30.1             openssl_1.4.1            rprojroot_1.3-2         
[100] withr_2.2.0              GenomicAlignments_1.22.1 Rsamtools_2.2.3         
[103] GenomeInfoDbData_1.2.2   hms_0.5.3                quadprog_1.5-8          
[106] grid_3.6.3               base64_2.0               rmarkdown_2.1           
[109] DelayedMatrixStats_1.8.0 illuminaio_0.28.0        git2r_0.27.1            
[112] lubridate_1.7.8