Author: Eric S. Davis

# Matching case study I: CTCF occupancy

In this vignette we demonstrate generating covariate-matched, null-hypothesis GRanges using the `matchRanges()` function to test for the occupancy of CCCTC-binding factor (CTCF) at chromatin loop anchors.

## Background

One of the fundamental principles of chromatin-looping suggests that most loops are bound at both ends by the CTCF transcription factor (TF). CTCF-bound loops can be formed by loop-extrusion, where the ring-like cohesin complex extrudes chromatin until stopped by bound CTCF. By this mechanism, we expect most loop anchors will be bound by CTCF.

While we could test this hypothesis by simple overlap or permutation testing, these approaches fail to account for non-uniformly distributed covariate genomic features. For example, loop anchors are commonly bound by CTCF and located in open chromatin regions. We can use `matchRanges()` to test for CTCF occupancy at loop anchors controlling for open chromatin regions.

Here, we generate a set of null-hypothesis GRanges to more rigorously test CTCF occupancy at loop anchors independently from open chromatin regions. We use the `hg19_10kb_bins` dataset from the `nullrangesData` package, which contains ranges for every 10Kb bin along the genome with CTCF, DNase, and loop feature annotations from GM12878 (see `?nullrangesData::hg19_10kb_bins`).

## Matching with `matchRanges()`

Before we generate our null ranges, let’s take a look at our example dataset:

    library(nullrangesData)

    ## Load example data
    bins <- hg19_10kb_bins()

    bins

    ## GRanges object with 303641 ranges and 5 metadata columns:
    ##            seqnames              ranges strand | n_ctcf_sites ctcfSignal
    ##               <Rle>           <IRanges>  <Rle> |    <numeric>  <numeric>
    ##        [1]     chr1             1-10000      * |            0          0
    ##        [2]     chr1         10001-20000      * |            0          0
    ##        [3]     chr1         20001-30000      * |            0          0
    ##        [4]     chr1         30001-40000      * |            0          0
    ##        [5]     chr1         40001-50000      * |            0          0
    ##        ...      ...                 ...    ... .          ...        ...
    ##   [303637]     chrX 155230001-155240000      * |            0    0.00000
    ##   [303638]     chrX 155240001-155250000      * |            0    0.00000
    ##   [303639]     chrX 155250001-155260000      * |            1    4.09522
    ##   [303640]     chrX 155260001-155270000      * |            0    0.00000
    ##   [303641]     chrX 155270001-155270560      * |            0    0.00000
    ##            n_dnase_sites dnaseSignal    looped
    ##                 <factor>   <numeric> <logical>
    ##        [1]             0     0.00000     FALSE
    ##        [2]             0     5.03572     FALSE
    ##        [3]             0     0.00000     FALSE
    ##        [4]             0     0.00000     FALSE
    ##        [5]             0     0.00000     FALSE
    ##        ...           ...         ...       ...
    ##   [303637]             0     8.42068     FALSE
    ##   [303638]             0     4.08961     FALSE
    ##   [303639]             0     6.00443     FALSE
    ##   [303640]             0     8.07179     FALSE
    ##   [303641]             0     0.00000     FALSE
    ##   -------
    ##   seqinfo: 23 sequences from hg19 genome

`matchRanges()` works by selecting a set of covariate-matched controls from a pool of options based on an input focal set of interest. Here, we define `focal` as bins that contain a loop anchor, `pool` as bins that don’t contain a loop anchor, and `covar` as DNase signal and number of DNase sites per bin:

    library(nullranges)

    ## Match ranges
    set.seed(123)
    mgr <- matchRanges(focal = bins[bins$looped],
                       pool = bins[!bins$looped],
                       covar = ~dnaseSignal + n_dnase_sites)
    mgr

    ## MatchedGRanges object with 13979 ranges and 5 metadata columns:
    ##           seqnames              ranges strand | n_ctcf_sites ctcfSignal
    ##              <Rle>           <IRanges>  <Rle> |    <numeric>  <numeric>
    ##       [1]    chr10   25240001-25250000      * |            1    5.06009
    ##       [2]     chr2 192860001-192870000      * |            0    0.00000
    ##       [3]    chr10   90060001-90070000      * |            0    0.00000
    ##       [4]     chr7     3840001-3850000      * |            0    0.00000
    ##       [5]    chr14   77370001-77380000      * |            1    7.66659
    ##       ...      ...                 ...    ... .          ...        ...
    ##   [13975]     chr1 154910001-154920000      * |            2    7.70130
    ##   [13976]     chr4 185960001-185970000      * |            1    7.13074
    ##   [13977]     chr1 236080001-236090000      * |            0    0.00000
    ##   [13978]     chr5   35290001-35300000      * |            0    0.00000
    ##   [13979]    chr11 119300001-119310000      * |            0    0.00000
    ##           n_dnase_sites dnaseSignal    looped
    ##                <factor>   <numeric> <logical>
    ##       [1]            3+    12.33182     FALSE
    ##       [2]            1     10.80692     FALSE
    ##       [3]            1      8.56733     FALSE
    ##       [4]            0      7.96488     FALSE
    ##       [5]            3+    13.38591     FALSE
    ##       ...           ...         ...       ...
    ##   [13975]            3+    13.99531     FALSE
    ##   [13976]            1      9.63596     FALSE
    ##   [13977]            3+    11.32430     FALSE
    ##   [13978]            1      9.57321     FALSE
    ##   [13979]            1      8.58206     FALSE
    ##   -------
    ##   seqinfo: 23 sequences from hg19 genome

When the focal and pool arguments are `GRanges` objects, `matchRanges()` returns a `MatchedGRanges` object. The `MatchedGRanges` class extends `GRanges`, so all of the same operations can be applied:

    library(GenomicRanges)
    library(plyranges)
    library(ggplot2)

    ## Summarize ctcfSignal by n_ctcf_sites
    mgr %>%
      group_by(n_ctcf_sites) %>%
      summarize(ctcfSignal = mean(ctcfSignal)) %>%
      as.data.frame() %>%
      ggplot(aes(x = n_ctcf_sites, y = ctcfSignal)) +
        geom_line() +
        geom_point(shape = 21, stroke = 1,  fill = 'white') +
        theme_minimal() +
        theme(panel.border = element_rect(color = 'black',
                                          fill = NA))

Here, we utilize the `plyranges` package which provides a set of “tidy” verbs for manipulating genomic ranges for a seamless and integrated genomic analysis workflow.

## Assessing quality of matching

We can get a quick summary of the matching quality with `overview()`:

    overview(mgr)

    ## MatchedGRanges object:
    ##        set      N dnaseSignal.mean dnaseSignal.sd n_dnase_sites.0
    ##     <char>  <num>            <num>          <num>           <num>
    ##      focal  13979             10.0            1.9            2341
    ##    matched  13979             10.0            1.9            2340
    ##       pool 289662              7.9            2.7          222164
    ##  unmatched 277229              7.8            2.7          219844
    ##  n_dnase_sites.1 n_dnase_sites.2 n_dnase_sites.3+ ps.mean ps.sd
    ##            <num>           <num>            <num>   <num> <num>
    ##             4829            2353             4456   0.130 0.072
    ##             5146            2447             4046   0.130 0.072
    ##            34826           13627            19045   0.042 0.061
    ##            30269           11504            15612   0.038 0.058
    ## --------
    ## focal - matched:
    ##  dnaseSignal.mean dnaseSignal.sd n_dnase_sites.0 n_dnase_sites.1
    ##             <num>          <num>           <num>           <num>
    ##             0.013         0.0085               1            -320
    ##  n_dnase_sites.2 n_dnase_sites.3+ ps.mean   ps.sd
    ##            <num>            <num>   <num>   <num>
    ##              -94              410 3.9e-07 1.1e-06

For continuous covariates (such as `dnaseSignal`), `overview()` shows the mean and standard deviation between each matched set. For categorical covariates, such as `n_dnase_sites`, `overview()` reports the number of observations per category and matched set. The bottom section shows the mean and s.d (or n, for factors) difference between focal and matched sets.

`overview()` also summarizes the propensity scores for each set to give a quick idea of overall matching quality.

### Visualizing matching results

Let’s visualize overall matching quality by plotting propensity scores for the focal, pool, and matched sets:

    plotPropensity(mgr, sets = c('f', 'p', 'm'), type = 'ridges')

From this plot, it is clear that the matched set is much closer to the focal set than the pool set.

We can ensure that covariate distributions have been matched appropriately by using the `covariates()` function to extract matched covariates along with `patchwork` and `plotCovarite` to visualize all distributions:

    library(patchwork)
    plots <- lapply(covariates(mgr), plotCovariate, x=mgr, sets = c('f', 'm', 'p'))
    Reduce('/', plots)

## Compare CTCF sites

Using our matched ranges, we can compare CTCF occupancy in bins that 1) contain a loop anchor (i.e. looped), 2) don’t contain a loop anchor (i.e. unlooped), or 3) don’t contain a loop anchor, but are also matched for the strength and number of DNase sites (i.e. matched). In this case, we calculate CTCF occupancy as the percent of bins that contain CTCF among our 3 sets by using the `focal()` and `pool()` accessor functions.

In order to pipe the data into plyranges, we bind the ranges together and give each group a meaningful label in this scientific context (e.g. that the focal set is looped, while the background/matched sets are unlooped).

    tidy_gr <- bind_ranges(
      looped_focal=focal(mgr),
      unlooped_pool=pool(mgr),
      unlooped_matched=mgr, .id="type"
    )

We define some custom colors for our barplot:

    cols <- c(looped_focal="#1F78B4",
              unlooped_matched="#A6CEE3",
              unlooped_pool="#33A02C")

And finally we can make the plot, with a grouped summarization followed by some ggplot2 code:

    tidy_gr %>%
      group_by(type) %>%
      summarize(CTCF_occupied = 100*mean(n_ctcf_sites >= 1)) %>%
      as.data.frame() %>%
      ggplot(aes(type, CTCF_occupied, fill=type)) +
      geom_col(show.legend = FALSE) +
      ylab("CTCF occupied bins (%)") +
      scale_fill_manual(values=cols) +
      ggtitle("CTCF occupancy")

# Session information

    sessionInfo()

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    ## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    ##  [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: Etc/UTC
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] plotgardener_1.18.0         ggplot2_4.0.3
    ##  [3] tidyr_1.3.2                 patchwork_1.3.2
    ##  [5] plyranges_1.32.0            dplyr_1.2.1
    ##  [7] nullranges_1.18.0           GenomeInfoDb_1.48.0
    ##  [9] EnsDb.Hsapiens.v86_2.99.0   ensembldb_2.36.0
    ## [11] AnnotationFilter_1.36.0     GenomicFeatures_1.64.0
    ## [13] AnnotationDbi_1.74.0        nullrangesData_1.17.0
    ## [15] InteractionSet_1.40.0       SummarizedExperiment_1.42.0
    ## [17] Biobase_2.72.0              MatrixGenerics_1.24.0
    ## [19] matrixStats_1.5.0           ExperimentHub_3.2.0
    ## [21] GenomicRanges_1.64.0        Seqinfo_1.2.0
    ## [23] IRanges_2.46.0              S4Vectors_0.50.0
    ## [25] AnnotationHub_4.2.0         BiocFileCache_3.2.0
    ## [27] dbplyr_2.5.2                BiocGenerics_0.58.0
    ## [29] generics_0.1.4              rmarkdown_2.31
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] DBI_1.3.0                bitops_1.0-9             httr2_1.2.2
    ##  [4] rlang_1.2.0              magrittr_2.0.5           ggridges_0.5.7
    ##  [7] compiler_4.6.0           RSQLite_2.4.6            png_0.1-9
    ## [10] vctrs_0.7.3              ProtGenerics_1.44.0      pkgconfig_2.0.3
    ## [13] crayon_1.5.3             fastmap_1.2.0            XVector_0.52.0
    ## [16] labeling_0.4.3           utf8_1.2.6               Rsamtools_2.28.0
    ## [19] UCSC.utils_1.8.0         strawr_0.0.92            purrr_1.2.2
    ## [22] bit_4.6.0                RcppHMM_1.2.2.1          xfun_0.57
    ## [25] cachem_1.1.0             cigarillo_1.2.0          jsonlite_2.0.0
    ## [28] progress_1.2.3           blob_1.3.0               rhdf5filters_1.24.0
    ## [31] DelayedArray_0.38.1      Rhdf5lib_2.0.0           BiocParallel_1.46.0
    ## [34] prettyunits_1.2.0        parallel_4.6.0           R6_2.6.1
    ## [37] bslib_0.10.0             RColorBrewer_1.1-3       rtracklayer_1.72.0
    ## [40] DNAcopy_1.86.0           jquerylib_0.1.4          Rcpp_1.1.1-1.1
    ## [43] knitr_1.51               Matrix_1.7-5             tidyselect_1.2.1
    ## [46] abind_1.4-8              yaml_2.3.12              codetools_0.2-20
    ## [49] curl_7.1.0               lattice_0.22-9           tibble_3.3.1
    ## [52] withr_3.0.2              KEGGREST_1.52.0          S7_0.2.2
    ## [55] evaluate_1.0.5           gridGraphics_0.5-1       Biostrings_2.80.0
    ## [58] pillar_1.11.1            BiocManager_1.30.27      filelock_1.0.3
    ## [61] RCurl_1.98-1.18          hms_1.1.4                BiocVersion_3.23.1
    ## [64] scales_1.4.0             glue_1.8.1               lazyeval_0.2.3
    ## [67] maketools_1.3.2          tools_4.6.0              BiocIO_1.22.0
    ## [70] sys_3.4.3                data.table_1.18.2.1      GenomicAlignments_1.48.0
    ## [73] fs_2.1.0                 buildtools_1.0.0         XML_3.99-0.23
    ## [76] rhdf5_2.56.0             grid_4.6.0               restfulr_0.0.16
    ## [79] cli_3.6.6                rappdirs_0.3.4           S4Arrays_1.12.0
    ## [82] gtable_0.3.6             yulab.utils_0.2.4        sass_0.4.10
    ## [85] digest_0.6.39            ggplotify_0.1.3          SparseArray_1.12.1
    ## [88] rjson_0.2.23             farver_2.1.2             memoise_2.0.1
    ## [91] htmltools_0.5.9          lifecycle_1.0.5          httr_1.4.8
    ## [94] bit64_4.8.0