In this vignette we demonstrate how to merge multiple Seurat objects containing single-cell chromatin data. To demonstrate, we will use four scATAC-seq PBMC datasets provided by 10x Genomics:

View data download code

To download the required data, run the following lines in a shell:

# 500 cell
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_500_nextgem/atac_pbmc_500_nextgem_fragments.tsv.gz
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_500_nextgem/atac_pbmc_500_nextgem_fragments.tsv.gz.tbi
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_500_nextgem/atac_pbmc_500_nextgem_peaks.bed
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_500_nextgem/atac_pbmc_500_nextgem_singlecell.csv
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_500_nextgem/atac_pbmc_500_nextgem_filtered_peak_bc_matrix.h5

# 1k cell
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_1k_nextgem/atac_pbmc_1k_nextgem_fragments.tsv.gz
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_1k_nextgem/atac_pbmc_1k_nextgem_fragments.tsv.gz.tbi
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_1k_nextgem/atac_pbmc_1k_nextgem_peaks.bed
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_1k_nextgem/atac_pbmc_1k_nextgem_singlecell.csv
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_1k_nextgem/atac_pbmc_1k_nextgem_filtered_peak_bc_matrix.h5

# 5k cell
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_5k_nextgem/atac_pbmc_5k_nextgem_fragments.tsv.gz
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_5k_nextgem/atac_pbmc_5k_nextgem_fragments.tsv.gz.tbi
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_5k_nextgem/atac_pbmc_5k_nextgem_peaks.bed
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_5k_nextgem/atac_pbmc_5k_nextgem_singlecell.csv
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_5k_nextgem/atac_pbmc_5k_nextgem_filtered_peak_bc_matrix.h5

# 10k cell
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_10k_nextgem/atac_pbmc_10k_nextgem_fragments.tsv.gz
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_10k_nextgem/atac_pbmc_10k_nextgem_fragments.tsv.gz.tbi
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_10k_nextgem/atac_pbmc_10k_nextgem_peaks.bed
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_10k_nextgem/atac_pbmc_10k_nextgem_singlecell.csv
wget https://cf.10xgenomics.com/samples/cell-atac/1.1.0/atac_pbmc_10k_nextgem/atac_pbmc_10k_nextgem_filtered_peak_bc_matrix.h5

When merging multiple single-cell chromatin datasets, it’s important to be aware that if peak calling was performed on each dataset independently, the peaks are unlikely to be exactly the same. We therefore need to create a common set of peaks across all the datasets to be merged.

To create a unified set of peaks we can use functions from the GenomicRanges package. The reduce function from GenomicRanges will merge all intersecting peaks. Another option is to use the disjoin function, that will create distinct non-overlapping sets of peaks. Here is a visual example to illustrate the difference between reduce and disjoin:

gr <- GRanges(seqnames = "chr1", ranges = IRanges(start = c(20, 70, 300), end = c(120, 200, 400)))
gr
## GRanges object with 3 ranges and 0 metadata columns:
##       seqnames    ranges strand
##          <Rle> <IRanges>  <Rle>
##   [1]     chr1    20-120      *
##   [2]     chr1    70-200      *
##   [3]     chr1   300-400      *
##   -------
##   seqinfo: 1 sequence from an unspecified genome; no seqlengths

Creating a common peak set

If the peaks were identified independently in each experiment then they will likely not overlap perfectly. We can merge peaks from all the datasets to create a common peak set, and quantify this peak set in each experiment prior to merging the objects.

First we’ll load the peak coordinates for each experiment and convert them to genomic ranges, the use the GenomicRanges::reduce function to create a common set of peaks to quantify in each dataset.

library(Signac)
library(Seurat)
library(GenomicRanges)
library(future)

plan("multiprocess", workers = 4)
options(future.globals.maxSize = 50000 * 1024^2) # for 50 Gb RAM
# read in peak sets
peaks.500 <- read.table(
  file = "../vignette_data/pbmc500/atac_pbmc_500_nextgem_peaks.bed",
  col.names = c("chr", "start", "end")
)
peaks.1k <- read.table(
  file = "../vignette_data/pbmc1k/atac_pbmc_1k_nextgem_peaks.bed",
  col.names = c("chr", "start", "end")
)
peaks.5k <- read.table(
  file = "../vignette_data/pbmc5k/atac_pbmc_5k_nextgem_peaks.bed",
  col.names = c("chr", "start", "end")
)
peaks.10k <- read.table(
  file = "../vignette_data/pbmc10k/atac_pbmc_10k_nextgem_peaks.bed",
  col.names = c("chr", "start", "end")
)

# convert to genomic ranges
gr.500 <- makeGRangesFromDataFrame(peaks.500)
gr.1k <- makeGRangesFromDataFrame(peaks.1k)
gr.5k <- makeGRangesFromDataFrame(peaks.5k)
gr.10k <- makeGRangesFromDataFrame(peaks.10k)

# Create a unified set of peaks to quantify in each dataset
combined.peaks <- reduce(x = c(gr.500, gr.1k, gr.5k, gr.10k))

# Filter out bad peaks based on length
peakwidths <- width(combined.peaks)
combined.peaks <- combined.peaks[peakwidths  < 10000 & peakwidths > 20]
combined.peaks
## GRanges object with 89951 ranges and 0 metadata columns:
##           seqnames            ranges strand
##              <Rle>         <IRanges>  <Rle>
##       [1]     chr1     565153-565499      *
##       [2]     chr1     569185-569620      *
##       [3]     chr1     713551-714783      *
##       [4]     chr1     752418-753020      *
##       [5]     chr1     762249-763345      *
##       ...      ...               ...    ...
##   [89947]     chrY 23422151-23422632      *
##   [89948]     chrY 23583994-23584463      *
##   [89949]     chrY 23602466-23602779      *
##   [89950]     chrY 28816593-28817710      *
##   [89951]     chrY 58855911-58856251      *
##   -------
##   seqinfo: 24 sequences from an unspecified genome; no seqlengths

Create Fragment objects

To quantify our combined set of peaks we’ll need to create a Fragment object for each experiment. The Fragment class is a specialized class defined in Signac to hold all the information related to a single fragment file.

First we’ll load the cell metadata for each experiment so that we know what cell barcodes are contained in each file, then we can create Fragment objects using the CreateFragmentObject function. The CreateFragmentObject function performs some checks to ensure that the file is present on disk and that it is compressed and indexed, computes the MD5 sum for the file and the tabix index so that we can tell if the file is modified at any point, and checks that the expected cells are present in the file.

# load metadata
md.500 <- read.table(
  file = "../vignette_data/pbmc500/atac_pbmc_500_nextgem_singlecell.csv",
  stringsAsFactors = FALSE,
  sep = ",",
  header = TRUE,
  row.names = 1
)[-1, ] # remove the first row

md.1k <- read.table(
  file = "../vignette_data/pbmc1k/atac_pbmc_1k_nextgem_singlecell.csv",
  stringsAsFactors = FALSE,
  sep = ",",
  header = TRUE,
  row.names = 1
)[-1, ]

md.5k <- read.table(
  file = "../vignette_data/pbmc5k/atac_pbmc_5k_nextgem_singlecell.csv",
  stringsAsFactors = FALSE,
  sep = ",",
  header = TRUE,
  row.names = 1
)[-1, ]

md.10k <- read.table(
  file = "../vignette_data/pbmc10k/atac_pbmc_10k_nextgem_singlecell.csv",
  stringsAsFactors = FALSE,
  sep = ",",
  header = TRUE,
  row.names = 1
)[-1, ]

# perform an initial filtering of low count cells
md.500 <- md.500[md.500$passed_filters > 500, ]
md.1k <- md.1k[md.1k$passed_filters > 500, ]
md.5k <- md.5k[md.5k$passed_filters > 500, ]
md.10k <- md.10k[md.10k$passed_filters > 1000, ] # sequenced deeper so set higher cutoff

# create fragment objects
frags.500 <- CreateFragmentObject(
  path = "../vignette_data/pbmc500/atac_pbmc_500_nextgem_fragments.tsv.gz",
  cells = rownames(md.500)
)
## Computing hash
frags.1k <- CreateFragmentObject(
  path = "../vignette_data/pbmc1k/atac_pbmc_1k_nextgem_fragments.tsv.gz",
  cells = rownames(md.1k)
)
## Computing hash
frags.5k <- CreateFragmentObject(
  path = "../vignette_data/pbmc5k/atac_pbmc_5k_nextgem_fragments.tsv.gz",
  cells = rownames(md.5k)
)
## Computing hash
frags.10k <- CreateFragmentObject(
  path = "../vignette_data/pbmc10k/atac_pbmc_10k_nextgem_fragments.tsv.gz",
  cells = rownames(md.10k)
)
## Computing hash

Quantify peaks in each dataset

We can now create a matrix of peaks x cell for each sample using the FeatureMatrix function. This function is parallelized using the future package. See the parallelization vignette for more information about using future.

pbmc500.counts <- FeatureMatrix(
  fragments = frags.500,
  features = combined.peaks,
  cells = rownames(md.500)
)

pbmc1k.counts <- FeatureMatrix(
  fragments = frags.1k,
  features = combined.peaks,
  cells = rownames(md.1k)
)

pbmc5k.counts <- FeatureMatrix(
  fragments = frags.5k,
  features = combined.peaks,
  cells = rownames(md.5k)
)

pbmc10k.counts <- FeatureMatrix(
  fragments = frags.10k,
  features = combined.peaks,
  cells = rownames(md.10k)
)

Create the objects

We will now use the quantified matrices to create a Seurat object for each dataset, storing the Fragment object for each dataset in the assay.

pbmc500_assay <- CreateChromatinAssay(pbmc500.counts, fragments = frags.500)
pbmc500 <- CreateSeuratObject(pbmc500_assay, assay = "ATAC", meta.data=md.500)

pbmc1k_assay <- CreateChromatinAssay(pbmc1k.counts, fragments = frags.1k)
pbmc1k <- CreateSeuratObject(pbmc1k_assay, assay = "ATAC", meta.data=md.1k)

pbmc5k_assay <- CreateChromatinAssay(pbmc5k.counts, fragments = frags.5k)
pbmc5k <- CreateSeuratObject(pbmc5k_assay, assay = "ATAC", meta.data=md.5k)

pbmc10k_assay <- CreateChromatinAssay(pbmc10k.counts, fragments = frags.10k)
pbmc10k <- CreateSeuratObject(pbmc10k_assay, assay = "ATAC", meta.data=md.10k)

Merge objects

Now that the objects each contain an assay with the same set of features, we can use the standard merge function to merge the objects. This will also merge all the fragment objects so that we retain the fragment information for each cell in the final merged object.

# add information to identify dataset of origin
pbmc500$dataset <- 'pbmc500'
pbmc1k$dataset <- 'pbmc1k'
pbmc5k$dataset <- 'pbmc5k'
pbmc10k$dataset <- 'pbmc10k'

# merge all datasets, adding a cell ID to make sure cell names are unique
combined <- merge(
  x = pbmc500,
  y = list(pbmc1k, pbmc5k, pbmc10k),
  add.cell.ids = c("500", "1k", "5k", "10k")
)
combined[["ATAC"]]
## ChromatinAssay data with 89951 features for 21688 cells
## Variable features: 0 
## Genome: 
## Annotation present: FALSE 
## Motifs present: FALSE 
## Fragment files: 4
combined <- RunTFIDF(combined)
combined <- FindTopFeatures(combined, min.cutoff = 20)
combined <- RunSVD(combined)
combined <- RunUMAP(combined, dims = 2:50, reduction = 'lsi')
DimPlot(combined, group.by = 'dataset', pt.size = 0.1)

The merged object contains all four fragment objects, and contains an internal mapping of cell names in the object to the cell names in each fragment file so that we can retrieve information from the files without having to change the cell names in each fragment file. We can check that functions that pull data from the fragment files work as expected on the merged object by plotting a region of the genome:

CoveragePlot(
  object = combined,
  group.by = 'dataset',
  region = "chr14-99700000-99760000"
)

Merging without a common feature set

The above approach requires that we have access to a fragment file for each dataset. In some cases we may not have this data (although we can create a fragment file from the BAM file using sinto). In these cases, we can still create a merged object, with the caveat that the resulting merged count matrix may not be as accurate.

The merge function defined in Signac for ChromatinAssay objects will consider overlapping peaks as equivalent, and adjust the genomic ranges spanned by the peak so that the features in each object being merged become equivalent. Note that this can result in inaccuracies in the count matrix, as some peaks will be extended to cover regions that were not originally quantified. This is the best that can be done without re-quantification, and we recommend always following the procedure outlined above for object merging whenever possible.

Here we demonstrate merging the same four PBMC datasets without creating a common feature set:

# load the count matrix for each object that was generated by cellranger
counts.500 <- Read10X_h5("../vignette_data/pbmc500/atac_pbmc_500_nextgem_filtered_peak_bc_matrix.h5")
counts.1k <- Read10X_h5("../vignette_data/pbmc1k/atac_pbmc_1k_nextgem_filtered_peak_bc_matrix.h5")
counts.5k <- Read10X_h5("../vignette_data/pbmc5k/atac_pbmc_5k_nextgem_filtered_peak_bc_matrix.h5")
counts.10k <- Read10X_h5("../vignette_data/pbmc10k/atac_pbmc_10k_nextgem_filtered_peak_bc_matrix.h5")

# create objects
pbmc500_assay <- CreateChromatinAssay(counts = counts.500, sep = c(":", "-"), min.features = 500)
pbmc500 <- CreateSeuratObject(pbmc500_assay, assay = "peaks")
pbmc1k_assay <- CreateChromatinAssay(counts = counts.1k, sep = c(":", "-"), min.features = 500)
pbmc1k <- CreateSeuratObject(pbmc1k_assay, assay = "peaks")
pbmc5k_assay <- CreateChromatinAssay(counts = counts.5k, sep = c(":", "-"), min.features = 500)
pbmc5k <- CreateSeuratObject(pbmc5k_assay, assay = "peaks")
pbmc10k_assay <- CreateChromatinAssay(counts = counts.10k, sep = c(":", "-"), min.features = 1000)
pbmc10k <- CreateSeuratObject(pbmc10k_assay, assay = "peaks")

# add information to identify dataset of origin
pbmc500$dataset <- 'pbmc500'
pbmc1k$dataset <- 'pbmc1k'
pbmc5k$dataset <- 'pbmc5k'
pbmc10k$dataset <- 'pbmc10k'

# merge
combined <- merge(
  x = pbmc500,
  y = list(pbmc1k, pbmc5k, pbmc10k),
  add.cell.ids = c("500", "1k", "5k", "10k")
)

# process 
combined <- RunTFIDF(combined)
combined <- FindTopFeatures(combined, min.cutoff = 20)
combined <- RunSVD(combined)
combined <- RunUMAP(combined, dims = 2:50, reduction = 'lsi')
DimPlot(combined, group.by = 'dataset', pt.size = 0.1)

Session Info

## R version 4.1.0 (2021-05-18)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.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       
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] future_1.22.1        SeuratObject_4.0.2   Seurat_4.0.4        
##  [4] Signac_1.4.0         patchwork_1.1.1      ggbio_1.40.0        
##  [7] ggplot2_3.3.5        GenomicRanges_1.44.0 GenomeInfoDb_1.28.4 
## [10] IRanges_2.26.0       S4Vectors_0.30.0     BiocGenerics_0.38.0 
## 
## loaded via a namespace (and not attached):
##   [1] rappdirs_0.3.3              SnowballC_0.7.0            
##   [3] rtracklayer_1.52.1          scattermore_0.7            
##   [5] GGally_2.1.2                ragg_1.1.3                 
##   [7] tidyr_1.1.3                 bit64_4.0.5                
##   [9] knitr_1.34                  irlba_2.3.3                
##  [11] DelayedArray_0.18.0         data.table_1.14.0          
##  [13] rpart_4.1-15                KEGGREST_1.32.0            
##  [15] RCurl_1.98-1.5              AnnotationFilter_1.16.0    
##  [17] generics_0.1.0              GenomicFeatures_1.44.2     
##  [19] cowplot_1.1.1               RSQLite_2.2.8              
##  [21] RANN_2.6.1                  bit_4.0.4                  
##  [23] spatstat.data_2.1-0         xml2_1.3.2                 
##  [25] httpuv_1.6.3                SummarizedExperiment_1.22.0
##  [27] assertthat_0.2.1            xfun_0.26                  
##  [29] hms_1.1.0                   jquerylib_0.1.4            
##  [31] evaluate_0.14               promises_1.2.0.1           
##  [33] fansi_0.5.0                 restfulr_0.0.13            
##  [35] progress_1.2.2              dbplyr_2.1.1               
##  [37] igraph_1.2.6                DBI_1.1.1                  
##  [39] htmlwidgets_1.5.4           sparsesvd_0.2              
##  [41] reshape_0.8.8               spatstat.geom_2.2-2        
##  [43] purrr_0.3.4                 ellipsis_0.3.2             
##  [45] RSpectra_0.16-0             dplyr_1.0.7                
##  [47] backports_1.2.1             biomaRt_2.48.3             
##  [49] deldir_0.2-10               MatrixGenerics_1.4.3       
##  [51] vctrs_0.3.8                 Biobase_2.52.0             
##  [53] ensembldb_2.16.4            ROCR_1.0-11                
##  [55] abind_1.4-5                 cachem_1.0.6               
##  [57] withr_2.4.2                 ggforce_0.3.3              
##  [59] BSgenome_1.60.0             checkmate_2.0.0            
##  [61] sctransform_0.3.2           GenomicAlignments_1.28.0   
##  [63] prettyunits_1.1.1           goftest_1.2-2              
##  [65] cluster_2.1.2               lazyeval_0.2.2             
##  [67] crayon_1.4.1                hdf5r_1.3.4                
##  [69] pkgconfig_2.0.3             slam_0.1-48                
##  [71] labeling_0.4.2              tweenr_1.0.2               
##  [73] nlme_3.1-152                ProtGenerics_1.24.0        
##  [75] nnet_7.3-16                 rlang_0.4.11               
##  [77] globals_0.14.0              lifecycle_1.0.0            
##  [79] miniUI_0.1.1.1              filelock_1.0.2             
##  [81] BiocFileCache_2.0.0         dichromat_2.0-0            
##  [83] rprojroot_2.0.2             polyclip_1.10-0            
##  [85] matrixStats_0.61.0          lmtest_0.9-38              
##  [87] graph_1.70.0                Matrix_1.3-4               
##  [89] ggseqlogo_0.1               zoo_1.8-9                  
##  [91] base64enc_0.1-3             ggridges_0.5.3             
##  [93] png_0.1-7                   viridisLite_0.4.0          
##  [95] rjson_0.2.20                bitops_1.0-7               
##  [97] KernSmooth_2.23-20          Biostrings_2.60.2          
##  [99] blob_1.2.2                  stringr_1.4.0              
## [101] parallelly_1.28.1           jpeg_0.1-9                 
## [103] scales_1.1.1                memoise_2.0.0              
## [105] magrittr_2.0.1              plyr_1.8.6                 
## [107] ica_1.0-2                   zlibbioc_1.38.0            
## [109] compiler_4.1.0              BiocIO_1.2.0               
## [111] RColorBrewer_1.1-2          fitdistrplus_1.1-5         
## [113] Rsamtools_2.8.0             XVector_0.32.0             
## [115] listenv_0.8.0               pbapply_1.5-0              
## [117] htmlTable_2.2.1             Formula_1.2-4              
## [119] MASS_7.3-54                 mgcv_1.8-36                
## [121] tidyselect_1.1.1            stringi_1.7.4              
## [123] textshaping_0.3.5           highr_0.9                  
## [125] yaml_2.2.1                  latticeExtra_0.6-29        
## [127] ggrepel_0.9.1               grid_4.1.0                 
## [129] sass_0.4.0                  VariantAnnotation_1.38.0   
## [131] fastmatch_1.1-3             tools_4.1.0                
## [133] future.apply_1.8.1          rstudioapi_0.13            
## [135] foreign_0.8-81              lsa_0.73.2                 
## [137] gridExtra_2.3               farver_2.1.0               
## [139] Rtsne_0.15                  digest_0.6.27              
## [141] BiocManager_1.30.16         shiny_1.6.0                
## [143] qlcMatrix_0.9.7             Rcpp_1.0.7                 
## [145] later_1.3.0                 RcppAnnoy_0.0.19           
## [147] OrganismDbi_1.34.0          httr_1.4.2                 
## [149] AnnotationDbi_1.54.1        biovizBase_1.40.0          
## [151] colorspace_2.0-2            XML_3.99-0.8               
## [153] fs_1.5.0                    tensor_1.5                 
## [155] reticulate_1.22             splines_4.1.0              
## [157] uwot_0.1.10                 RBGL_1.68.0                
## [159] RcppRoll_0.3.0              spatstat.utils_2.2-0       
## [161] pkgdown_1.6.1.9001          plotly_4.9.4.1             
## [163] systemfonts_1.0.2           xtable_1.8-4               
## [165] jsonlite_1.7.2              R6_2.5.1                   
## [167] Hmisc_4.5-0                 pillar_1.6.2               
## [169] htmltools_0.5.2             mime_0.11                  
## [171] glue_1.4.2                  fastmap_1.1.0              
## [173] BiocParallel_1.26.2         codetools_0.2-18           
## [175] utf8_1.2.2                  lattice_0.20-44            
## [177] bslib_0.3.0                 spatstat.sparse_2.0-0      
## [179] tibble_3.1.4                curl_4.3.2                 
## [181] leiden_0.3.9                survival_3.2-11            
## [183] rmarkdown_2.11              docopt_0.7.1               
## [185] desc_1.3.0                  munsell_0.5.0              
## [187] GenomeInfoDbData_1.2.6      reshape2_1.4.4             
## [189] gtable_0.3.0                spatstat.core_2.3-0