Using BPCells with Seurat Objects
Compiled: November 06, 2026
Source:vignettes/bpcells_interaction_vignette.Rmd
bpcells_interaction_vignette.RmdBPCells is an R package that allows for computationally efficient single-cell analysis. It utilizes bit-packing compression to store counts matrices on disk and C++ code to cache operations.
We leverage the high performance capabilities of BPCells to work with Seurat objects in memory while accessing the counts on disk. In this vignette, we show how to use BPCells to load data, work with Seurat objects in a more memory-efficient way, and write out Seurat objects with BPCells matrices.
We will show the methods for interacting with both a single dataset in one file and multiple datasets across multiple files using BPCells. BPCells allows us to easily analyze these large datasets in memory, and we encourage users to check out our sketch-based analysis and reference mapping vignettes to see further applications.
devtools::install_github("bnprks/BPCells")We use BPCells functionality to both load in our data and write the counts layers to bitpacked compressed binary files on disk to improve computation speeds. BPCells has multiple functions for reading in files.
Load Data
Load Data from one h5 file
In this section, we will load a dataset of mouse brain cells freely available from 10x Genomics. This includes 1.3 Million single cells that were sequenced on the Illumina NovaSeq 6000. The raw data can be found here.
To read in the file, we will use open_matrix_10x_hdf5, a BPCells function written to read in feature matrices from 10x. We then write a matrix directory, load the matrix, and create a Seurat object.
brain.data <- open_matrix_10x_hdf5(
path = "/brahms/hartmana/vignette_data/1M_neurons_filtered_gene_bc_matrices_h5.h5")
# Write the matrix to a directory
write_matrix_dir(
mat = brain.data,
dir = "/brahms/hartmana/vignette_data/bpcells/brain_counts")
# Now that we have the matrix on disk, we can load it
brain.mat <- open_matrix_dir(dir = "/brahms/hartmana/vignette_data/bpcells/brain_counts")
brain.mat <- Azimuth:::ConvertEnsembleToSymbol(mat = brain.mat, species = "mouse")
# Create Seurat Object
brain <- CreateSeuratObject(counts = brain.mat)What if I already have a Seurat Object?
You can use BPCells to convert the matrices in your already created Seurat objects to on-disk matrices. Note, that this is only possible for V5 assays. As an example, if you’d like to convert the counts matrix of your RNA assay to a BPCells matrix, you can use the following:
obj <- readRDS("/path/to/reference.rds")
# Write the counts layer to a directory
write_matrix_dir(mat = obj[["RNA"]]$counts, dir = "/brahms/hartmana/vignette_data/bpcells/brain_counts")
counts.mat <- open_matrix_dir(dir = "/brahms/hartmana/vignette_data/bpcells/brain_counts")
obj[["RNA"]]$counts <- counts.matExample Analysis
Once this conversion is done, you can perform typical Seurat functions on the object. For example, we can normalize data and visualize features by automatically accessing the on-disk counts.

# We then normalize and visualize again
brain <- NormalizeData(brain, normalization.method = "LogNormalize")
VlnPlot(brain, features = c("Sox10", "Slc17a7", "Aif1"), ncol = 3, layer = "data", alpha = 0.1)
Saving Seurat objects with on-disk layers
If you save your object and load it in the future, Seurat will access the on-disk matrices by their path, which is stored in the assay level data. To make it easy to ensure these are saved in the same place, we provide new functionality to the SaveSeuratRds function. In this function, you specify your filename. The pointer to the path in the Seurat object will change to the current directory.
This also makes it easy to share your Seurat objects with BPCells matrices by sharing a folder that contains both the object and the BPCells directory.
SaveSeuratRds(
object = brain,
file = "brain.Rds")If needed, a layer with an on-disk matrix can be converted to an
in-memory matrix using the as() function. For the purposes
of this demo, we’ll subset the object so that it takes up less space in
memory.
Load data from multiple h5ad files
You can also download data from multiple matrices. In this section, we create a Seurat object using multiple peripheral blood mononuclear cell (PBMC) samples that are freely available for download from CZI here. We download data from Ahern et al. (2022) Nature, Jin et al. (2021) Science, and Yoshida et al. (2022) Nature. We use the BPCells function to read h5ad files.
file.dir <- "/brahms/hartmana/vignette_data/h5ad_files/"
files.set <- c("ahern_pbmc.h5ad", "jin_pbmc.h5ad", "yoshida_pbmc.h5ad")
# Convert the h5ad files to on-disk BPCells matrices
for (i in seq_along(files.set)) {
path <- file.path(file.dir, files.set[i])
data <- open_matrix_anndata_hdf5(path)
matrix.dir <- file.path(file.dir, paste0(tools::file_path_sans_ext(files.set[i]), "_BP"))
write_matrix_dir(mat = data, dir = matrix.dir)
}
# Load the existing BPCells matrices and metadata
data.list <- c()
metadata.list <- c()
for (i in 1:length(files.set)) {
path <- paste0(file.dir, files.set[i])
matrix.dir <- file.path(file.dir, paste0(tools::file_path_sans_ext(files.set[i]), "_BP"))
# Load in BP matrices
mat <- open_matrix_dir(dir = matrix.dir)
mat <- Azimuth:::ConvertEnsembleToSymbol(mat = mat, species = "human")
# Get metadata
metadata.list[[i]] <- LoadH5ADobs(path = path)
data.list[[i]] <- mat
}
# Name layers
names(data.list) <- c("ahern", "jin", "yoshida")
# Add Metadata
for (i in 1:length(metadata.list)){
metadata.list[[i]]$publication <- names(data.list)[i]
}
metadata.list <- lapply(metadata.list, function(x) {
x <- x[, c("publication", "sex", "cell_type", "donor_id", "disease")]
return(x)
})
metadata <- Reduce(rbind, metadata.list)When we create the Seurat object with the list of matrices from each publication, we can then see that multiple counts layers exist that represent each dataset. This object contains over a million cells, yet only takes up minimal space in memory!
merged.object <- CreateSeuratObject(counts = data.list, meta.data = metadata)
merged.object## An object of class Seurat
## 26686 features across 1498064 samples within 1 assay
## Active assay: RNA (26686 features, 0 variable features)
## 3 layers present: counts.ahern, counts.jin, counts.yoshida
SaveSeuratRds(
object = merged.object,
file = "merged.Rds")Parse Biosciences
Here, we show how to load a 1 million cell data set from Parse Biosciences and create a Seurat Object. The data is available for download here
parse.data <- open_matrix_anndata_hdf5(
"/brahms/hartmana/vignette_data/h5ad_files/ParseBio_PBMC.h5ad")
write_matrix_dir(mat = parse.data, dir = "/brahms/hartmana/vignette_data/bpcells/parse_1m_pbmc")
parse.mat <- open_matrix_dir(dir = "/brahms/hartmana/vignette_data/bpcells/parse_1m_pbmc")
metadata <- readRDS("/brahms/hartmana/vignette_data/ParseBio_PBMC_meta.rds")
metadata$disease <- sapply(strsplit(x = metadata$sample, split = "_"), "[", 1)
parse.object <- CreateSeuratObject(counts = parse.mat, meta.data = metadata)
SaveSeuratRds(
object = parse.object,
file = "parse.Rds")Session Info
sessioninfo::session_info(pkgs = "attached")## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.5.2 (2025-10-31)
## os Ubuntu 24.04.4 LTS
## system x86_64, linux-gnu
## ui X11
## language en
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2026-11-06
## pandoc 3.9 @ /usr/bin/ (via rmarkdown)
## quarto 1.8.25 @ /usr/local/bin/quarto
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## Azimuth * 0.5.1 2026-11-06 [3] Github (satijalab/azimuth@ad59296)
## biomaRt * 2.68.5 2026-11-05 [3] Bioconductor 3.23 (R 4.5.2)
## BPCells * 0.3.1 2026-07-13 [3] https://b~
## dplyr * 1.2.0 2026-02-03 [3] RSPM (R 4.5.0)
## Seurat * 6.0.0 2026-11-06 [1] local
## SeuratDisk * 0.0.0.9021 2026-11-06 [3] Github (mojaveazure/seurat-disk@877d4e1)
## SeuratObject * 5.4.0 2026-04-11 [3] RSPM (R 4.5.0)
## shinyBS * 0.65.0 2026-03-18 [3] RSPM (R 4.5.0)
## sp * 2.2-4 2026-10-11 [3] RSPM (R 4.5.0)
##
## [1] /scratch/build-QLRyQPI9/library
## [2] /usr/local/lib/R/custom-lib
## [3] /usr/local/lib/R/site-library
## [4] /usr/local/lib/R/library
## * ── Packages attached to the search path.
##
## ──────────────────────────────────────────────────────────────────────────────