Authoring a data package with a disk-backed Monocle 3 CDS

This vignette is for dataset authors: people who want to ship a Monocle 3 cell_data_set (CDS) whose counts live in a BPCells on-disk matrix as part of an ordinary R package. People who only use such a package should read vignette("using-monocle3-data", package = "projectData.monocle3"). How the files are stored, restored and kept alive is explained in vignette("storage", package = "projectData.monocle3").

The problem, and who does what

A CDS whose counts are a BPCells matrix holds only a reference to a directory on the author’s disk, so save() and saveRDS() give a file that is useless on another machine. Monocle’s own save_monocle_objects() and load_monocle_objects() handle this by copying the matrix directory, but a data package needs help at both ends: something has to save the directory into the package, and something has to restore it, at first access, from a read-only installation. If the object is comfortable in memory, ship it as an ordinary .rda dataset instead; no adapter is needed.

projectData (core) projectData.monocle3 (this adapter)
Registers installed data packages, builds the catalog from metadata Declares the loader projectData.monocle3::cds
Resolves names, attaches collections, handles conflicts Saves a CDS into a package source tree, writes the placeholder and manifest entry
Opens a resource lazily on first access, caches and retries it Restores the CDS with Monocle’s own loader
Allocates a private working directory for each opening Puts the restored BPCells files in that directory

projectData is described in vignette("projectData", package = "projectData") and the adapter mechanism in vignette("adapters", package = "projectData"). The same division of work is used by projectData.Seurat for Seurat v5 objects; the two adapters have similar entry points but different persistence mechanisms (see vignette("storage", package = "projectData.monocle3")).

Prerequisites

Authoring needs monocle3, BPCells, SingleCellExperiment and SummarizedExperiment in addition to projectData and this package. BPCells and monocle3 are not on CRAN; with pak, which also resolves monocle3’s Bioconductor dependencies for your version of R:

install.packages("pak")
pak::pak(c(
  "projectData",
  "bnprks/BPCells/r",
  "cole-trapnell-lab/monocle3",
  "blaserlab/projectData.monocle3"
))
library(monocle3)
library(projectData.monocle3)
work <- file.path(tempdir(), "authoring-demo")
dir.create(work)

1. Create a small CDS

A synthetic CDS with sparse integer counts, cell and gene metadata, an extra in-memory assay and a UMAP embedding. In practice this is your real object.

set.seed(9215)
genes <- sprintf("gene%02d", 1:40)
cells <- sprintf("cell%02d", 1:25)
counts <- matrix(rpois(40 * 25, 2), 40, 25, dimnames = list(genes, cells))
# Every gene and cell must be observed
counts[cbind(1:40, (1:40 %% 25) + 1)] <- 3
counts <- Matrix::Matrix(counts, sparse = TRUE)

cds <- new_cell_data_set(
  counts,
  cell_metadata = data.frame(row.names = cells, group = rep(c("a", "b"), length.out = 25)),
  gene_metadata = data.frame(row.names = genes, gene_short_name = toupper(genes))
)
SummarizedExperiment::assay(cds, "logcounts") <- log1p(as.matrix(counts))
SingleCellExperiment::reducedDims(cds) <- list(UMAP = cbind(cos(1:25), sin(1:25)))

class(SummarizedExperiment::assay(cds, "counts"))
#> [1] "dgCMatrix"
#> attr(,"package")
#> [1] "Matrix"

The counts are an ordinary in-memory dgCMatrix. They do not have to be BPCells-backed already: see step 4.

2. Prepare an ordinary R package

A data package is an R package whose source directory has a DESCRIPTION (create it as you would any other, for example with usethis::create_package()). The adapter requires only that the directory has a DESCRIPTION with a Package field, because save_monocle_cds() refuses to write anywhere else.

pkg <- file.path(work, "myMonocleData")
dir.create(file.path(pkg, "R"), recursive = TRUE)
writeLines(c(
  "Package: myMonocleData",
  "Title: Example Monocle 3 Data",
  "Version: 0.1.0",
  "Authors@R: person('Ada', 'Author', email = '[email protected]', role = c('aut', 'cre'))",
  "Description: A tiny synthetic data package that ships a disk-backed Monocle 3 CDS.",
  "License: MIT + file LICENSE",
  "Suggests: projectData.monocle3",
  "Encoding: UTF-8"
), file.path(pkg, "DESCRIPTION"))
writeLines(c("YEAR: 2026", "COPYRIGHT HOLDER: Ada Author"), file.path(pkg, "LICENSE"))
writeLines(character(), file.path(pkg, "NAMESPACE"))

projectData.monocle3 is listed in Suggests: your users need it only to open the object, and projectData loads it (without attaching it) from the manifest. If you distribute the package from a repository that does not carry projectData.monocle3, add it to Remotes as well.

3. Save the CDS into the package

res <- save_monocle_cds(cds, name = "demo_cds", package_dir = pkg)
#> Saved Monocle CDS 'demo_cds':
#>   inst/extdata/demo_cds/
#>   data/demo_cds.rda (placeholder)
#>   inst/projectData/manifest.dcf (added)
#> No documentation topic for 'demo_cds' found in R/. Add a roxygen2 block ending in "demo_cds" (usually in R/data.R) and run devtools::document().

name must be a syntactically valid R name; it becomes the saved directory, the placeholder, the manifest resource and the object users will access. The call returns, invisibly, a list describing what happened:

str(res[c("name", "manifest_status", "replaced", "removed_stale")])
#> List of 4
#>  $ name           : chr "demo_cds"
#>  $ manifest_status: chr "added"
#>  $ replaced       : logi FALSE
#>  $ removed_stale  : chr(0)

The call does not modify cds, does not edit R/data.R, and writes nothing to the working directory. It validates everything first (the name, the counts, any existing files and the manifest), then builds the files in a hidden staging directory inside the package, checks them with the loader’s own validation and only then moves them into place. If anything fails the previous state is restored. It is not a filesystem transaction: if R is killed mid-commit, a hidden .projectData.monocle3-stage-* directory may remain and may hold the previous files.

4. Counts are converted to BPCells automatically

The package is meant for CDS objects whose counts are a BPCells directory. What save_monocle_cds() does depends on what it finds:

counts in the CDS Handling
BPCells directory matrix (MatrixDir), including lazy subsets Saved as it is, without reconversion
dgCMatrix / dgTMatrix, or a BPCells in-memory matrix Converted automatically with Monocle, on a copy of the CDS
Dense matrix, DelayedMatrix, HDF5-backed BPCells, lgCMatrix, no counts Error, before anything is written

Here the counts were a dgCMatrix, so a BPCells directory was written into the package, while cds is unchanged:

class(SummarizedExperiment::assay(cds, "counts"))
#> [1] "dgCMatrix"
#> attr(,"package")
#> [1] "Matrix"
list.files(file.path(pkg, "inst", "extdata", "demo_cds", "bpcells_matrix_dir"))
#> [1] "col_names"     "idxptr"        "index"         "row_names"    
#> [5] "shape"         "storage_order" "val"           "version"

Conversion applies to counts only. Other assays are never converted (see “What is refused” below).

5. The generated package layout

list.files(pkg, recursive = TRUE, all.files = TRUE)
#>  [1] "data/demo_cds.rda"                                     
#>  [2] "DESCRIPTION"                                           
#>  [3] "inst/extdata/demo_cds/bpcells_matrix_dir/col_names"    
#>  [4] "inst/extdata/demo_cds/bpcells_matrix_dir/idxptr"       
#>  [5] "inst/extdata/demo_cds/bpcells_matrix_dir/index"        
#>  [6] "inst/extdata/demo_cds/bpcells_matrix_dir/row_names"    
#>  [7] "inst/extdata/demo_cds/bpcells_matrix_dir/shape"        
#>  [8] "inst/extdata/demo_cds/bpcells_matrix_dir/storage_order"
#>  [9] "inst/extdata/demo_cds/bpcells_matrix_dir/val"          
#> [10] "inst/extdata/demo_cds/bpcells_matrix_dir/version"      
#> [11] "inst/extdata/demo_cds/cds_object.rds"                  
#> [12] "inst/extdata/demo_cds/file_index.rds"                  
#> [13] "inst/projectData/manifest.dcf"                         
#> [14] "LICENSE"                                               
#> [15] "NAMESPACE"
  • inst/extdata/demo_cds/ is a directory written by Monocle’s native save_monocle_objects() with no tar archive, so the BPCells matrix and Monocle’s index files are installed as ordinary files.
  • data/demo_cds.rda is the placeholder.
  • inst/projectData/manifest.dcf is the manifest entry.

The placeholder is not the CDS

The placeholder is mandatory, not configurable. It exists so that data(), R CMD check and roxygen2 treat the dataset conventionally. It is a small classed character vector that prints its own instructions with base R, without projectData or this package:

load(file.path(pkg, "data", "demo_cds.rda"))
demo_cds
#>                                                                           dataset 
#>                                                                        "demo_cds" 
#>                                                                           package 
#>                                                                   "myMonocleData" 
#>                                                                            status 
#>           "This object is a placeholder, not the actual Monocle 3 cell_data_set." 
#>                                                                           storage 
#>          "The real dataset is stored separately in disk-backed Monocle 3 format." 
#>                                                                            step_1 
#>               "Remove this placeholder from the global environment: rm(demo_cds)" 
#>                                                                            step_2 
#>            "Attach the data package: projectData::data_attach(\"myMonocleData\")" 
#>                                                                            step_3 
#>                                                  "Use the real dataset: demo_cds" 
#>                                                                              note 
#> "data() does not print the real object, and loading it can mask an attached one." 
#>                                                                          requires 
#>                            "Required packages: projectData, projectData.monocle3" 
#>                                                                           install 
#> "Install if missing: remotes::install_github(\"blaserlab/projectData.monocle3\")" 
#> attr(,"class")
#> [1] "projectData_monocle3_placeholder"
#> attr(,"object_name")
#> [1] "demo_cds"
#> attr(,"package")
#> [1] "myMonocleData"
rm(demo_cds)

data("demo_cds") therefore gives only this text, and, if you load it into the global environment, it can mask the real object attached by projectData. Remove it with rm(demo_cds) and use projectData::data_attach() or projectData::data_get().

6. The manifest

writeLines(readLines(file.path(pkg, "inst", "projectData", "manifest.dcf")))
#> ManifestVersion: 1
#> 
#> Resource: demo_cds
#> Loader: projectData.monocle3::cds
#> Source: extdata/demo_cds
#> Objects: demo_cds
#> HelpTopic: demo_cds
#> RecordedClass: cell_data_set
#> AdapterPackage: projectData.monocle3

Source uses the installed layout (extdata/demo_cds, not inst/extdata/demo_cds). Each resource declares exactly one object. The manifest takes precedence over the placeholder for the same name, so projectData resolves the resource through the loader and never offers the placeholder as the dataset. (AdapterExport: projectData_loaders is the default and is omitted.)

save_monocle_cds() registers the resource itself. write_monocle_manifest() is for reconciling the manifest with what is on disk, for example after you saved a directory by other means or deleted one. It returns, invisibly, a data frame with one row per resource (added, updated, unchanged, removed, skipped or conflict). It is idempotent: the file is not rewritten, and no summary is printed, when nothing changes.

status <- write_monocle_manifest(pkg)
status
#>   resource    status detail
#> 1 demo_cds unchanged

Reconciliation covers only entries owned by this adapter (loader projectData.monocle3::cds). A saved directory with a valid placeholder is added or updated, and an entry whose directory or placeholder you deleted is removed as stale. Entries of other loaders are preserved unchanged; a resource or object name that another adapter owns is a conflict, reported and left alone. A manifest that cannot be parsed is never overwritten. Reconciliation never deletes a saved directory or a placeholder.

7. Document the dataset

save_monocle_cds() never edits R/data.R and tells you when it finds no "demo_cds" documentation line. Document the dataset as you would any other:

writeLines(c(
  "#' Demonstration Monocle 3 dataset",
  "#'",
  "#' @format A Monocle 3 `cell_data_set` with BPCells-backed counts.",
  "#' @details Access with `projectData::data_attach()`.",
  "#' @source Synthetic.",
  "\"demo_cds\""
), file.path(pkg, "R", "data.R"))

In a real package run devtools::document() to generate man/demo_cds.Rd. To keep this example free of roxygen2, write the equivalent Rd file directly:

dir.create(file.path(pkg, "man"))
writeLines(c(
  "\\name{demo_cds}", "\\alias{demo_cds}", "\\docType{data}",
  "\\title{Demonstration Monocle 3 dataset}",
  "\\format{A Monocle 3 cell_data_set with BPCells-backed counts.}",
  "\\description{Access it with \\code{projectData::data_attach()}.}",
  "\\keyword{datasets}"
), file.path(pkg, "man", "demo_cds.Rd"))

Practical notes for the real package:

  • The placeholder data/<name>.rda must be part of the package source. If your .gitignore excludes *.rda, add !data/<name>.rda, or the placeholder (and with it the manifest entry) will be missing from fresh checkouts.
  • Keep the script that generates the CDS in inst/data-raw/.
  • The BPCells files count towards the size of the package. Check the size limits of wherever you distribute it; CRAN, for example, expects far smaller packages than a typical single-cell dataset.

8. Build and install the data package

Installation is ordinary. Here it goes into a temporary library:

lib <- file.path(work, "library")
dir.create(lib)
install.packages(pkg, lib = lib, repos = NULL, type = "source", quiet = TRUE)
list.files(file.path(lib, "myMonocleData", "extdata", "demo_cds"))
#> [1] "bpcells_matrix_dir" "cds_object.rds"     "file_index.rds"

The installed resource is read-only data: nothing below writes to it.

9. Load it through projectData

library(projectData)
data_config(messages = FALSE)
restored <- data_get("demo_cds", package = "myMonocleData", lib.loc = lib)
class(restored)
#> [1] "cell_data_set"
#> attr(,"package")
#> [1] "monocle3"
dim(restored)
#> [1] 40 25

data_get() registered the package and opened the resource on this first access. data_attach("myMonocleData") would register it and let you use the bare name demo_cds; see vignette("using-monocle3-data", package = "projectData.monocle3").

10. Verify the result

The restored object has the same counts, metadata, extra assay and reduction as the original, which can be checked here because the object is small:

all.equal(as(SummarizedExperiment::assay(restored, "counts"), "dgCMatrix"), counts)
#> [1] TRUE
identical(as.data.frame(SummarizedExperiment::colData(restored))[, "group", drop = FALSE],
          as.data.frame(SummarizedExperiment::colData(cds))[, "group", drop = FALSE])
#> [1] TRUE
all.equal(SummarizedExperiment::assay(restored, "logcounts"),
          SummarizedExperiment::assay(cds, "logcounts"))
#> [1] TRUE
all.equal(SingleCellExperiment::reducedDim(restored, "UMAP"),
          SingleCellExperiment::reducedDim(cds, "UMAP"))
#> [1] TRUE

The counts are still a BPCells matrix, and the files they read were restored into the working directory that projectData allocated for this opening, not into the installed package or the original author’s directory:

count_dir <- BPCells::all_matrix_inputs(SummarizedExperiment::assay(restored, "counts"))[[1]]@dir
class(SummarizedExperiment::assay(restored, "counts"))
#> [1] "MatrixDir"
#> attr(,"package")
#> [1] "BPCells"
startsWith(normalizePath(count_dir), normalizePath(file.path(tempdir(), "projectData-work")))
#> [1] TRUE

The restored CDS keeps working after the collection is detached (detaching forgets the collection; it never deletes backing files):

data_detach()
sum(colSums(SummarizedExperiment::assay(restored, "counts")))
#> [1] 1933

Replacing a dataset

Saving a name that already exists needs overwrite = TRUE, and only replaces what this package can show that it wrote. An existing directory is replaceable when at least two of three kinds of evidence agree: a native Monocle file index, a manifest entry of this loader, and a recognized placeholder, and nothing else is in the directory. A directory that merely looks like a Monocle save, a directory that another loader’s manifest entry claims, and a data/<name>.rda that holds anything but a placeholder are never replaced.

save_monocle_cds(cds, name = "demo_cds", package_dir = pkg)
#> Error in `save_monocle_cds()`:
#> ! 'demo_cds' already exists in this package (inst/extdata/demo_cds, data/demo_cds.rda). Use `overwrite = TRUE` to replace it.
invisible(save_monocle_cds(cds, name = "demo_cds", package_dir = pkg, overwrite = TRUE))
#> Replaced Monocle CDS 'demo_cds':
#>   inst/extdata/demo_cds/
#>   data/demo_cds.rda (placeholder)
#>   inst/projectData/manifest.dcf (unchanged)

What is refused

Everything that cannot be shipped faithfully is refused before anything is written. The most common case is a secondary assay that is itself stored on disk. Monocle would save it only as a reference to its original directory, which does not exist for your users:

adt <- matrix(rpois(3 * 25, 5), 3, 25, dimnames = list(paste0("adt", 1:3), cells))
adt <- suppressMessages(suppressWarnings(BPCells::write_matrix_dir(
  BPCells::convert_matrix_type(as(Matrix::Matrix(adt, sparse = TRUE), "IterableMatrix"), "double"),
  file.path(work, "adt")
)))
external <- cds
SingleCellExperiment::altExp(external, "ADT") <- SingleCellExperiment::SingleCellExperiment(
  assays = list(counts = adt)
)
save_monocle_cds(external, name = "other_cds", package_dir = pkg)
#> Error in `save_monocle_cds()`:
#> ! Cannot save CDS 'other_cds':
#> 
#> The following assay(s) are disk-backed (or are BPCells/delayed representations not known to be in memory) and may reference external files that are not preserved by Monocle's native serialization, so the saved data package would not be portable:
#>   - altExp 'ADT' / assay 'counts' (MatrixDir: BPCells MatrixDir)
#> 
#> Only Monocle-managed BPCells counts of the main CDS are currently supported as disk-backed storage. Convert each of these assays to an in-memory representation before saving, or remove it if appropriate. Nothing was written.

The error has class projectData.monocle3_external_assay and names every offender, at any nesting depth. Convert such assays to in-memory matrices or drop them; the adapter never does it for you because it could need a lot of memory. The complete list of supported and refused representations is in vignette("storage", package = "projectData.monocle3").