Using disk-backed Seurat data from a data package

This vignette is for researchers who use a data package that ships Seurat objects through projectData.Seurat. If you are creating such a package, read vignette("authoring-seurat-data", package = "projectData.Seurat"). For the general projectData workflow (install, attach, use, conflicts) see vignette("projectData", package = "projectData"); this vignette covers only what is specific to Seurat objects. What projectData does and what this adapter does are kept apart below.

What you need

  • The data package (installed like any package) and projectData.
  • projectData.Seurat, installed. You never call library() on it: the data package’s manifest names it and projectData loads its namespace when needed.
  • SeuratObject and BPCells to open an object. Registering and listing a data package works without them; opening needs them (see Troubleshooting).
  • Seurat is needed only for your own analysis (library(Seurat)).

BPCells is not on CRAN: remotes::install_github("bnprks/BPCells/r").

A demonstration package

To keep this vignette self-contained it builds two throwaway data packages in a temporary library (using the authoring tools described in the other vignette). Skip this chunk in real use, and omit lib.loc below.

library(projectData)
source(system.file("examples", "demo-package.R", package = "projectData.Seurat"))

work <- file.path(tempdir(), "using-demo")
dir.create(work)
lib <- file.path(work, "library")
dir.create(lib)
invisible(demo_data_package(work, lib, name = "demoSeuratData"))
invisible(demo_data_package(work, lib, name = "otherSeuratData"))

Attach, and what is registered

data_config(messages = FALSE)
data_attach("demoSeuratData", lib.loc = lib)
data_catalog()
#> <projectData catalog>: 2 row(s); metadata only, no data opened.
#>      collection exposed_name      object    resource    binding   origin
#>   myDataPackage  demo_seurat demo_seurat demo_seurat unattached manifest
#>  demoSeuratData  demo_seurat demo_seurat demo_seurat      bound manifest
#>   mapping    state
#>  verified    ready
#>  recorded unopened
as.data.frame(data_catalog())[, c("resource", "state", "storage", "loader_status")]
#>      resource    state     storage loader_status
#> 1 demo_seurat    ready disk-backed    registered
#> 2 demo_seurat unopened disk-backed    registered

projectData reads the package’s manifest, and records the resource. The manifest entry names the adapter (AdapterPackage: projectData.Seurat), so projectData knows which loader will open it. Nothing was opened: the state is unopened, the object is not in memory, and the loader has not yet been resolved (loader_status is unresolved and storage is unknown until the adapter has been loaded, which happens on first access). No call to data_loader_adapter() is needed when the manifest names the adapter.

When is the object actually loaded?

On first access, and only then. This is projectData behavior:

seu <- demo_seurat
class(seu)
#> [1] "Seurat"
#> attr(,"package")
#> [1] "SeuratObject"
as.data.frame(data_catalog())[, c("resource", "state", "storage", "loader_status")]
#>      resource state     storage loader_status
#> 1 demo_seurat ready disk-backed    registered
#> 2 demo_seurat ready disk-backed    registered

Now the resource is ready, storage is disk-backed and the loader is registered. The restore itself is done by projectData.Seurat: it reads object.rds from the installed package and points the BPCells layers at the directories next to it. It copies nothing, writes nothing (the installation can be read-only) and does not change your working directory. Access again returns the same cached object:

identical(demo_seurat, seu)
#> [1] TRUE

To open objects without attaching the package, or in scripts and functions, prefer data_get() (a projectData function):

identical(data_get("demo_seurat", collection = "demoSeuratData"), seu)
#> [1] TRUE

Do not use data("demo_seurat") to get the object: that returns a small placeholder character vector which can mask the real one. If you did, rm(demo_seurat) and use data_attach().

What the object is, and what it is not

The restored object is a Seurat object whose layers are lazy BPCells matrices. projectData.Seurat::seurat_storage_report() shows where each layer reads from (call it with ::; the package is not attached):

projectData.Seurat::seurat_storage_report(seu)
#>  assay  layer representation operations    backing
#>    RNA counts        BPCells          0 RNA_counts
#>    RNA   data   Lazy BPCells          2 RNA_counts
#>    ADT counts        BPCells          0 ADT_counts
#> 
#>       store files  bytes               layers
#>  RNA_counts     8 139141 RNA/counts, RNA/data
#>  ADT_counts     8   4594           ADT/counts
#> 
#> Logical layers: 3
#> Physical backing stores: 2
#> (files and bytes are measured file sizes of the store directories, not matrix dimensions)

The paths are inside the installed data package. “Disk-backed” means the matrix storage stays on disk; metadata and anything you compute are ordinary R objects (see vignette("storage", package = "projectData.Seurat")). Ordinary Seurat functions work on it. You need library(Seurat) for them, which is your choice, not the adapter’s.

How names are resolved

Each saved dataset is one resource that provides exactly one object, with the same name. After data_attach() the name is bound on the search path; data_get(name, collection =) and data_get(name, package =) reach it without that. Several data packages can be used at once, and two of them may define the same name. That is handled by projectData, not by this adapter, and it never picks a winner by attachment order:

data_attach("otherSeuratData", lib.loc = lib, id = "other")
demo_seurat
#> Error:
#> ! 'demo_seurat' is ambiguous: it is provided by collections 'demoSeuratData', 'other'. Resolve with data_prefer("demo_seurat", "demoSeuratData"), designate a primary collection with data_primary(), use a suffix alias if one was registered, or access it explicitly with data_get("demo_seurat", collection = "demoSeuratData").

Resolve it in any of the ways described in the “Conflicts” section of vignette("projectData", package = "projectData"). The most robust for scripts is to say which collection you mean:

other <- data_get("demo_seurat", collection = "other")
dim(other)
#> [1] 200  60

(Use data_prefer() or data_primary() to decide once for a whole session.)

What happens after data_detach()

data_detach()
dim(seu)
#> [1] 200  60
nrow(data_collections())
#> [1] 0

data_detach() forgets the collections and their cached objects. It does not delete any files and does not invalidate objects you hold, so seu still works. But a restored Seurat object reads its layers from the installed data package. Keep that in mind:

  • Do not remove, reinstall or update the data package in the same session while you hold the object. (On Windows, files that are in use may not be removable at all; that has not been verified for this package.)
  • The object does not copy the data into your session or working directory, and does not remain valid if the files are removed. Save the results of your analysis, not the restored object, if you need them later.
  • Attaching again opens the resource afresh; you get a second object reading the same files.

Troubleshooting

A resource is missing or damaged

If an installed backing directory has been deleted, projectData reports it and names the directory. Simulating this on a copy of the library:

lib2 <- file.path(work, "library2")
dir.create(lib2)
file.copy(file.path(lib, "demoSeuratData"), lib2, recursive = TRUE)
#> [1] TRUE
victim <- file.path(lib2, "demoSeuratData", "extdata", "demo_seurat", "bpcells", "ADT_counts")
backup <- file.path(work, "backup")
dir.create(backup)
file.copy(victim, backup, recursive = TRUE)
#> [1] TRUE
unlink(victim, recursive = TRUE)

data_attach("demoSeuratData", lib.loc = lib2, id = "damaged")
#> Error:
#> ! Invalid manifest projectData/manifest.dcf: resource "demo_seurat": 'extdata/demo_seurat': missing backing directory: bpcells/ADT_counts. The installed data package looks damaged or incomplete: reinstall it or restore the missing files, then retry (projectData::data_retry() if the collection is attached, otherwise projectData::data_attach()).

The error comes from the loader’s validate step, which checks the directory structure without reading any data. Restore the files, for example by reinstalling the package, and attach again:

file.copy(file.path(backup, "ADT_counts"), dirname(victim), recursive = TRUE)
#> [1] TRUE
data_attach("demoSeuratData", lib.loc = lib2, id = "damaged", replace = TRUE)
dim(data_get("demo_seurat", collection = "damaged"))
#> [1] 200  60

If the package was already attached and the files disappeared afterwards, the first access fails with the same message and the resource is marked failed. Once the files are back, call data_retry("<collection>"): projectData keeps a failed opening failed until you do, even after the files are restored.

Common messages

Symptom Cause What to do
Catalog shows unavailable-loader, or “Adapter package ‘projectData.Seurat’ is not installed” The adapter is not installed. Other objects in the package are unaffected Install projectData.Seurat, then data_retry()
projectData.Seurat_dependency_error: packages ‘SeuratObject’ or ‘BPCells’ not installed Opening needs them Install them (install.packages("SeuratObject"), remotes::install_github("bnprks/BPCells/r")), then data_retry("<collection>")
projectData.Seurat_dependency_error: installed SeuratObject/BPCells not compatible The installed versions lack an interface the package relies on Use a combination from the README compatibility table, or report it
projectData.Seurat_resource_error naming a layer “not restored faithfully” A BPCells version restored the layer differently from what was saved. The object is refused rather than returned wrong Report it with the versions, and use a tested BPCells
“missing backing directory”, “not a BPCells matrix directory” A damaged installation Reinstall the data package, then attach again (or data_retry() if already attached)
data("name") gives text instead of an object The placeholder rm(name), then data_attach("<package>")
“Facade bindings mask objects elsewhere on the search path” The dataset name equals an existing object Use data_get(), or data_attach(..., on_mask = "ignore")
The object stops working after reinstalling the package It reads from the installed files Open it again in a new session