Contents

1 Introduction

TADCompare is an R package for differential analysis of TAD boundaries. It is designed to work on a wide range of formats and resolutions of Hi-C data. TADCompare package contains four functions: TADCompare, TimeCompare, ConsensusTADs, and DiffPlot. TADCompare function allows for the identification of differential TAD boundaries between two contact matrices. TimeCompare function takes a set of contact matrices, one matrix per time point, identifies TAD boundaries, and classifies how they change over time. ConsensusTADs function takes a list of TADs and identifies a consensus of TAD boundaries across all matrices using our novel consensus boundary score. DiffPlot allows for visualization of TAD boundary differences between two matrices. The required input includes matrices in sparse 3-column format, \(n \times n\), or \(n \times (n+3)\) formats. This vignette provides a complete overview of input data formats.

2 Getting Started

2.1 Installation

BiocManager::install("TADCompare")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(SpectralTAD)
library(TADCompare)

3 Working with different types of data

3.1 Working with \(n \times n\) matrices

\(n \times n\) contact matrices are most commonly associated with data coming from the Bing Ren lab (http://chromosome.sdsc.edu/mouse/hi-c/download.html). These contact matrices are square and symmetric with entry \(ij\) corresponding to the number of contacts between region \(i\) and region \(j\). Below is an example of a \(5 \times 5\) region of an \(n \times n\) contact matrix derived from Rao et al. 2014 data, GM12878 cell line (Rao et al. 2014), chromosome 22, 50kb resolution. Note the symmetry around the diagonal - the typical shape of chromatin interaction matrix. The figure was created using the pheatmap package.

3.2 Working with \(n \times (n+3)\) matrices

\(n \times (n+3)\) matrices are commonly associated with the TopDom TAD caller (http://zhoulab.usc.edu/TopDom/). These matrices consist of an \(n \times n\) matrix but with three additional leading columns containing the chromosome, the start of the region and the end of the region. Regions in this case are determined by the resolution of the data. The subset of a typical \(n \times (n+3)\) matrix is shown below.

##     chr    start      end X18500000 X18550000 X18600000 X18650000
## 1 chr22 18500000 18550000     13313      4817      1664        96
## 2 chr22 18550000 18600000      4817     15500      5120       178
## 3 chr22 18600000 18650000      1664      5120     11242       316
## 4 chr22 18650000 18700000        96       178       316       162

3.3 Working with sparse 3-column matrices

Sparse 3-column matrices are matrices where the first and second columns refer to region \(i\) and region \(j\) of the chromosome, and the third column is the number of contacts between them. This style is becoming increasingly popular and is associated with raw data from Lieberman-Aiden lab (e.g., https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE63525), and is the data output produced by the Juicer tool (Durand et al. 2016). 3-column matrices are handled internally in the package by converting them to \(n \times n\) matrices using the HiCcompare package’s sparse2full() function. The first 5 rows of a typical sparse 3-column matrix are shown below.

##     region1  region2 IF
## 1: 16050000 16050000 12
## 2: 16200000 16200000  4
## 3: 16150000 16300000  1
## 4: 16200000 16300000  1
## 5: 16250000 16300000  1
## 6: 16300000 16300000 10

3.4 Working with other data types

3.5 Working with .hic files

.hic files are a common form of files generally associated with the lab of Erez Lieberman-Aiden (http://aidenlab.org/data.html). To use .hic files you must use the following steps.

  1. Download straw from https://github.com/aidenlab/straw/ and follow instalation instructions.
  2. Download .hic data files. Here, we use data from Rao 2014 and download them using the following commands:

wget ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE63nnn/GSE63525/suppl/GSE63525_GM12878_insitu_primary_30.hic

wget ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE63nnn/GSE63525/suppl/GSE63525_GM12878_insitu_replicate.hic

  1. Extract chromosome 22 at 50kb resolution with no normalization:

./straw NONE GSE63525_GM12878_insitu_primary_30.hic 22 22 BP 50000 > primary.chr22.50kb.txt

./straw NONE GSE63525_GM12878_insitu_replicate_30.hic 22 22 BP 50000 > replicate.chr22.50kb.txt

  1. Analyze normally:
#Read in data
primary = read.table('primary.chr22.50kb.txt', header = FALSE)
replicate = read.table('replicate.chr22.50kb.txt', header = FALSE)
#Run TADCompare
tad_diff=TADCompare(primary, replicate, resolution=50000)

3.6 Working with .cool files

Users can also find TADs from data output by cooler (http://cooler.readthedocs.io/en/latest/index.html) and HiC-Pro (https://github.com/nservant/HiC-Pro) with minor pre-processing using the HiCcompare package.

The cooler software can be downloaded from https://mirnylab.github.io/cooler/. A catalog of popular HiC datasets can be found at ftp://cooler.csail.mit.edu/coolers. We can extract chromatin interaction data from .cool files using the following steps:

  1. Follow instructions to install the cooler software, https://mirnylab.github.io/cooler/
  2. Download the first contact matrix wget ftp://cooler.csail.mit.edu/coolers/hg19/Zuin2014-HEK293CtcfControl-HindIII-allreps-filtered.50kb.cool
  3. Convert the first matrix to a text file using cooler dump --join Zuin2014-HEK293CtcfControl-HindIII-allreps-filtered.50kb.cool > Zuin.HEK293.50kb.Control.txt
  4. Download the second contact matrix wget ftp://cooler.csail.mit.edu/coolers/hg19/Zuin2014-HEK293CtcfDepleted-HindIII-allreps-filtered.50kb.cool
  5. Convert the matrix to a text file using cooler dump --join Zuin2014-HEK293CtcfDepleted-HindIII-allreps-filtered.50kb.cool > Zuin.HEK293.50kb.Depleted.txt
  6. Run the code below
# Read in data
cool_mat1 <- read.table("Zuin.HEK293.50kb.Control.txt")
cool_mat2 <- read.table("Zuin.HEK293.50kb.Depleted.txt")

# Convert to sparse 3-column matrix using cooler2sparse from HiCcompare
sparse_mat1 <- HiCcompare::cooler2sparse(cool_mat1)
sparse_mat2 <- HiCcompare::cooler2sparse(cool_mat2)

# Run TADCompare
diff_tads = lapply(names(sparse_mat1), function(x) {
  TADCompare(sparse_mat1[[x]], sparse_mat2[[x]], resolution = 50000)
})

3.7 Working with HiC-Pro files

HiC-Pro data is represented as two files, the .matrix file and the .bed file. The .bed file contains four columns (chromosome, start, end, ID). The .matrix file is a three-column matrix where the 1st and 2nd columns contain region IDs that map back to the coordinates in the bed file, and the third column contains the number of contacts between the two regions. In this example we analyze two matrix files sample1_100000.matrix and sample2_100000.matrixand their corresponding bed files sample1_100000_abs.bed and sample2_100000_abs.bed. We do not include HiC-Pro data in the package, so these serve as placeholders for the traditional files output by HiC-Pro. The steps for analyzing these files is shown below:

# Read in both files
mat1 <- read.table("sample1_100000.matrix")
bed1 <- read.table("sample1_100000_abs.bed")

# Matrix 2

mat2 <- read.table("sample2_100000.matrix")
bed2 <- read.table("sample2_100000_abs.bed")

# Convert to modified bed format
sparse_mats1 <- HiCcompare::hicpro2bedpe(mat1,bed1)
sparse_mats2 <- HiCcompare::hicpro2bedpe(mat2,bed2)

# Remove empty matrices if necessary
# sparse_mats$cis = sparse_mats$cis[sapply(sparse_mats, nrow) != 0]


# Go through all pairwise chromosomes and run TADCompare
sparse_tads = lapply(1:length(sparse_mats1$cis), function(z) {
  x <- sparse_mats1$cis[[z]]
  y <- sparse_mats2$cis[[z]]
  
  #Pull out chromosome
  chr <- x[, 1][1]
  #Subset to make three column matrix
  x <- x[, c(2, 5, 7)]
  y <- y[, c(2, 5, 7)]
  #Run SpectralTAD
  comp <- TADCompare(x, y, resolution = 100000)
  return(list(comp, chr))
})

# Pull out differential TAD results
diff_res <- lapply(sparse_tads, function(x) x$comp)
# Pull out chromosomes
chr      <- lapply(sparse_tads, function(x) x$chr)
# Name list by corresponding chr
names(diff_res) <- chr

3.8 Effect of matrix type on runtime

The type of matrix input into the algorithm can affect runtimes for the algorithm. \(n \times n\) matrices require no conversion and are the fastest. Meanwhile, \(n \times (n+3)\) matrices take slightly longer to run due to the need to remove the first 3 columns. Sparse 3-column matrices have the highest runtimes due to the complexity of converting them to an \(n \times n\) matrix. The times are summarized below, holding all other parameters constant.

library(microbenchmark)
# Reading in the second matrix
data("rao_chr22_rep")
# Converting to sparse
prim_sparse <- HiCcompare::full2sparse(rao_chr22_prim)
rep_sparse  <- HiCcompare::full2sparse(rao_chr22_rep)
# Converting to nxn+3
# Primary
prim_n_n_3 <- data.frame(chr = "chr22",
                         start = as.numeric(colnames(rao_chr22_prim)),
                         end = as.numeric(colnames(rao_chr22_prim))+50000, 
                         rao_chr22_prim)

# Replicate
rep_n_n_3 <- data.frame(chr = "chr22", 
                        start = as.numeric(colnames(rao_chr22_rep)),
                        end = as.numeric(colnames(rao_chr22_rep))+50000,
                        rao_chr22_rep)
# Defining each function
# Sparse
sparse <- TADCompare(cont_mat1 = prim_sparse, cont_mat2 = rep_sparse, resolution = 50000)
# NxN
n_by_n <- TADCompare(cont_mat1 = prim_sparse, cont_mat2 = rep_sparse, resolution = 50000)
# Nx(N+3)
n_by_n_3 <- TADCompare(cont_mat1 = prim_n_n_3, cont_mat2 = rep_n_n_3, resolution = 50000)

# Benchmarking different parameters
bench <- microbenchmark(
# Sparse
sparse <- TADCompare(cont_mat1 = prim_sparse, cont_mat2 = rep_sparse, resolution = 50000),
# NxN
n_by_n <- TADCompare(cont_mat1 = rao_chr22_prim, cont_mat2 = rao_chr22_rep, resolution = 50000),
# Nx(N+3)
n_by_n_3 <- TADCompare(cont_mat1 = prim_n_n_3, cont_mat2 = rep_n_n_3, resolution = 50000), times = 5, unit = "s"
) 

summary_bench <- summary(bench) %>% dplyr::select(mean, median)
rownames(summary_bench) <- c("sparse", "n_by_n", "n_by_n_3")
summary_bench
##               mean    median
## sparse   0.2070027 0.2011388
## n_by_n   0.2694358 0.1094135
## n_by_n_3 0.1277519 0.1248287

The table above shows the mean and median of runtimes for different types of contact matrices measured in seconds. As we see, TADCompare is extremely fast irrespectively of the parameters. However, sparse matrix inputs will slow down the algorithm. This can become more apparent as the size of the contact matrices increase.

4 Session Info

sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
## 
## 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       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] microbenchmark_1.4-7 TADCompare_1.4.0     SpectralTAD_1.10.0  
## [4] dplyr_1.0.7          BiocStyle_2.22.0    
## 
## loaded via a namespace (and not attached):
##   [1] TH.data_1.1-0               colorspace_2.0-2           
##   [3] ggsignif_0.6.3              ellipsis_0.3.2             
##   [5] rio_0.5.27                  CGHcall_2.56.0             
##   [7] DNAcopy_1.68.0              XVector_0.34.0             
##   [9] GenomicRanges_1.46.0        ggpubr_0.4.0               
##  [11] listenv_0.8.0               mvtnorm_1.1-3              
##  [13] fansi_0.5.0                 HiCcompare_1.16.0          
##  [15] codetools_0.2-18            splines_4.1.1              
##  [17] R.methodsS3_1.8.1           impute_1.68.0              
##  [19] knitr_1.36                  jsonlite_1.7.2             
##  [21] Rsamtools_2.10.0            broom_0.7.9                
##  [23] R.oo_1.24.0                 pheatmap_1.0.12            
##  [25] BiocManager_1.30.16         compiler_4.1.1             
##  [27] backports_1.2.1             assertthat_0.2.1           
##  [29] Matrix_1.3-4                fastmap_1.1.0              
##  [31] limma_3.50.0                htmltools_0.5.2            
##  [33] tools_4.1.1                 gtable_0.3.0               
##  [35] glue_1.4.2                  GenomeInfoDbData_1.2.7     
##  [37] reshape2_1.4.4              Rcpp_1.0.7                 
##  [39] carData_3.0-4               Biobase_2.54.0             
##  [41] cellranger_1.1.0            jquerylib_0.1.4            
##  [43] vctrs_0.3.8                 Biostrings_2.62.0          
##  [45] rhdf5filters_1.6.0          nlme_3.1-153               
##  [47] QDNAseq_1.30.0              xfun_0.27                  
##  [49] stringr_1.4.0               globals_0.14.0             
##  [51] openxlsx_4.2.4              lifecycle_1.0.1            
##  [53] gtools_3.9.2                rstatix_0.7.0              
##  [55] InteractionSet_1.22.0       future_1.22.1              
##  [57] MASS_7.3-54                 zoo_1.8-9                  
##  [59] zlibbioc_1.40.0             scales_1.1.1               
##  [61] hms_1.1.1                   MatrixGenerics_1.6.0       
##  [63] sandwich_3.0-1              parallel_4.1.1             
##  [65] SummarizedExperiment_1.24.0 rhdf5_2.38.0               
##  [67] RColorBrewer_1.1-2          yaml_2.2.1                 
##  [69] curl_4.3.2                  gridExtra_2.3              
##  [71] ggplot2_3.3.5               sass_0.4.0                 
##  [73] CGHbase_1.54.0              stringi_1.7.5              
##  [75] highr_0.9                   S4Vectors_0.32.0           
##  [77] BiocGenerics_0.40.0         zip_2.2.0                  
##  [79] BiocParallel_1.28.0         GenomeInfoDb_1.30.0        
##  [81] rlang_0.4.12                pkgconfig_2.0.3            
##  [83] matrixStats_0.61.0          bitops_1.0-7               
##  [85] evaluate_0.14               lattice_0.20-45            
##  [87] purrr_0.3.4                 Rhdf5lib_1.16.0            
##  [89] cowplot_1.1.1               tidyselect_1.1.1           
##  [91] parallelly_1.28.1           plyr_1.8.6                 
##  [93] magrittr_2.0.1              bookdown_0.24              
##  [95] R6_2.5.1                    magick_2.7.3               
##  [97] IRanges_2.28.0              generics_0.1.1             
##  [99] multcomp_1.4-17             DelayedArray_0.20.0        
## [101] DBI_1.1.1                   pillar_1.6.4               
## [103] haven_2.4.3                 foreign_0.8-81             
## [105] mgcv_1.8-38                 survival_3.2-13            
## [107] abind_1.4-5                 RCurl_1.98-1.5             
## [109] tibble_3.1.5                future.apply_1.8.1         
## [111] PRIMME_3.2-1                crayon_1.4.1               
## [113] car_3.0-11                  KernSmooth_2.23-20         
## [115] utf8_1.2.2                  rmarkdown_2.11             
## [117] readxl_1.3.1                grid_4.1.1                 
## [119] data.table_1.14.2           marray_1.72.0              
## [121] forcats_0.5.1               digest_0.6.28              
## [123] tidyr_1.1.4                 R.utils_2.11.0             
## [125] stats4_4.1.1                munsell_0.5.0              
## [127] bslib_0.3.1

References

Durand, Neva C., Muhammad S. Shamim, Ido Machol, Suhas S.P. Rao, Miriam H. Huntley, Eric S. Lander, and Erez Lieberman Aiden. 2016. “Juicer Provides a One-Click System for Analyzing Loop-Resolution Hi-c Experiments.” Cell Systems 3 (1): 95–98. https://doi.org/10.1016/j.cels.2016.07.002.

Rao, Suhas S.P., Miriam H. Huntley, Neva C. Durand, Elena K. Stamenova, Ivan D. Bochkov, James T. Robinson, Adrian L. Sanborn, et al. 2014. “A 3D Map of the Human Genome at Kilobase Resolution Reveals Principles of Chromatin Looping.” Cell 159 (7): 1665–80. https://doi.org/10.1016/j.cell.2014.11.021.