To run every example in this vignette, install knowYourCG with the packages the examples load:

if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager")
BiocManager::install(c("knowYourCG", "SummarizedExperiment", "sesame",
                       "gprofiler2"))

There are four testing scenarios depending on the type format of the query set and database sets. They are shown with the respective testing scenario in the table below. testEnrichment, testEnrichmentSEA are for Fisher’s exact test and Set Enrichment Analysis respectively.

Four knowYourCG Testing Scenarios
Continuous Database Set Discrete Database Set
Continuous Query Correlation-based Set Enrichment Analysis
Discrete Query Set Enrichment Analysis Fisher’s Exact Test

CONTINUOUS VARIABLE ENRICHMENT

The query may be a named continuous vector. In that case, either a gene enrichment score will be calculated (if the database is discrete) or a Spearman correlation will be calculated (if the database is continuous as well). The two other cases are shown below using biologically relevant examples.

To display this functionality, let’s load two numeric database sets individually. One is a database set for CpG density and the other is a database set corresponding to the distance of the nearest transcriptional start site (TSS) to each probe.

library(knowYourCG)
query <- getDBs("KYCG.MM285.designGroup")[["TSS"]]
sesameDataCache(data_titles = c("KYCG.MM285.seqContextN.20210630"))
res <- testEnrichmentSEA(query, "MM285.seqContextN")
main_stats <- c("dbname", "test", "estimate", "FDR", "nQ", "nD", "overlap")
res[,main_stats]

The estimate here is enrichment score.

NOTE: Positive enrichment score suggests enrichment of the categorical database with the higher values (in the numerical database). Negative enrichment score represents enrichment with the smaller values. This is the sign GSEA uses; knowYourCG releases before 1.9.3 had the reverse. As expected, the designed TSS CpGs are significantly enriched in smaller TSS distance and higher CpG density.

Alternatively one can test the enrichment of a continuous query with discrete databases. Here we will use the methylation level from a sample as the query and test it against the chromHMM chromatin states.

The p-values come from permutation: each set is compared with random sets of the same size. Seed the generator to reproduce them.

library(sesame)
set.seed(1)
sesameDataCache(data_titles = c("MM285.1.SigDF"))
beta_values <- getBetas(sesameDataGet("MM285.1.SigDF"))
res <- testEnrichmentSEA(beta_values, "MM285.chromHMM")
main_stats <- c("dbname", "test", "estimate", "FDR", "nQ", "nD", "overlap")
res[,main_stats] 

As expected, chromatin states Tss and Enh have negative enrichment scores, meaning these databases are associated with small values of the query (DNA methylation level). On the contrary, Het and most quiescent states (Quies, QuiesG, Quies2, Quies3) have positive scores, meaning they are associated with high methylation level; Quies4 is the exception. Here nQ is the number of CpGs with a value and nD the size of each state.

SESSION INFO

sessionInfo()
## R version 4.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.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] sesame_1.31.9               knitr_1.52                 
##  [3] SummarizedExperiment_1.43.0 Biobase_2.73.2             
##  [5] GenomicRanges_1.65.4        Seqinfo_1.3.2              
##  [7] IRanges_2.47.5              S4Vectors_0.51.10          
##  [9] MatrixGenerics_1.25.0       matrixStats_1.5.0          
## [11] sesameData_1.31.2           ExperimentHub_3.3.2        
## [13] AnnotationHub_4.3.3         BiocFileCache_3.3.0        
## [15] dbplyr_2.6.0                BiocGenerics_0.59.12       
## [17] generics_0.1.4              knowYourCG_1.9.6           
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.1      dplyr_1.2.1           farver_2.1.2         
##  [4] blob_1.3.0            filelock_1.0.3        Biostrings_2.81.9    
##  [7] S7_0.2.2              fastmap_1.2.0         digest_0.6.39        
## [10] lifecycle_1.0.5       KEGGREST_1.53.6       RSQLite_3.53.3       
## [13] magrittr_2.0.5        compiler_4.6.1        rlang_1.3.0          
## [16] sass_0.4.10           tools_4.6.1           yaml_2.3.12          
## [19] S4Arrays_1.13.2       bit_4.6.0             curl_8.1.0           
## [22] DelayedArray_0.39.8   plyr_1.8.9            RColorBrewer_1.1-3   
## [25] BiocParallel_1.47.0   abind_1.4-8           withr_3.0.3          
## [28] purrr_1.2.2           grid_4.6.1            preprocessCore_1.75.1
## [31] wheatmap_0.2.4        colorspace_2.1-4      ggplot2_4.0.3        
## [34] scales_1.4.0          dichromat_2.0-1       cli_3.6.6            
## [37] rmarkdown_2.32        crayon_1.5.3          otel_0.2.0           
## [40] httr_1.4.9            reshape2_1.4.5        tzdb_0.5.0           
## [43] BiocBaseUtils_1.15.1  DBI_1.3.0             cachem_1.1.0         
## [46] stringr_1.6.0         parallel_4.6.1        AnnotationDbi_1.75.2 
## [49] BiocManager_1.30.27   XVector_0.53.0        vctrs_0.7.3          
## [52] Matrix_1.7-6          jsonlite_2.0.0        hms_1.1.4            
## [55] bit64_4.8.6           ggrepel_0.9.8         fontawesome_0.5.3    
## [58] jquerylib_0.1.4       glue_1.8.1            codetools_0.2-20     
## [61] stringi_1.8.9         gtable_0.3.6          BiocVersion_3.24.0   
## [64] tibble_3.3.1          pillar_1.11.1         rappdirs_0.3.4       
## [67] htmltools_0.5.9       R6_2.6.1              httr2_1.3.0          
## [70] evaluate_1.0.5        lattice_0.23-1        readr_2.2.0          
## [73] png_0.1-9             memoise_2.0.1         bslib_0.12.0         
## [76] Rcpp_1.1.2            SparseArray_1.13.4    xfun_0.62            
## [79] pkgconfig_2.0.3