--- title: "Using disk-backed Seurat data from a data package" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Using disk-backed Seurat data from a data package} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") have_stack <- all(vapply( c("projectData", "SeuratObject", "BPCells", "Seurat", "Matrix"), requireNamespace, logical(1), quietly = TRUE )) knitr::opts_chunk$set(eval = have_stack) ``` 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](#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. ```{r fixtures} 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 ```{r attach} data_config(messages = FALSE) data_attach("demoSeuratData", lib.loc = lib) data_catalog() as.data.frame(data_catalog())[, c("resource", "state", "storage", "loader_status")] ``` **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: ```{r first-access} seu <- demo_seurat class(seu) as.data.frame(data_catalog())[, c("resource", "state", "storage", "loader_status")] ``` 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: ```{r cached} identical(demo_seurat, seu) ``` To open objects without attaching the package, or in scripts and functions, prefer `data_get()` (a projectData function): ```{r data-get} identical(data_get("demo_seurat", collection = "demoSeuratData"), seu) ``` 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): ```{r report} projectData.Seurat::seurat_storage_report(seu) ``` 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: ```{r conflict, error = TRUE} data_attach("otherSeuratData", lib.loc = lib, id = "other") demo_seurat ``` 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: ```{r qualified} other <- data_get("demo_seurat", collection = "other") dim(other) ``` (Use `data_prefer()` or `data_primary()` to decide once for a whole session.) ## What happens after `data_detach()` ```{r detach} data_detach() dim(seu) nrow(data_collections()) ``` `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: ```{r damage, error = TRUE} lib2 <- file.path(work, "library2") dir.create(lib2) file.copy(file.path(lib, "demoSeuratData"), lib2, recursive = 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) unlink(victim, recursive = TRUE) data_attach("demoSeuratData", lib.loc = lib2, id = "damaged") ``` 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: ```{r repair} file.copy(file.path(backup, "ADT_counts"), dirname(victim), recursive = TRUE) data_attach("demoSeuratData", lib.loc = lib2, id = "damaged", replace = TRUE) dim(data_get("demo_seurat", collection = "damaged")) ``` 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("")`: 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("")` | | `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("")` | | "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 | ```{r cleanup, include = FALSE} data_detach() unlink(work, recursive = TRUE) ```