Skip to content

Commit 78efbc2

Browse files
committed
Pass attributes from consolidated metadata
for better performance
1 parent c7ea1f6 commit 78efbc2

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

R/read.R

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ NULL
4444

4545
#' @importFrom Rarr read_zarr_attributes
4646
#' @importFrom ZarrArray ZarrArray
47-
.readArray <- function(x, ...) {
48-
md <- read_zarr_attributes(x)
47+
.readArray <- function(x, md = NULL, ...) {
48+
md <- md %||% read_zarr_attributes(x)
4949
mdattr <- SpatialDataAttrs(md)
5050
# TODO: paths to datasets have to be validated properly in the future
5151
# https://ngff.openmicroscopy.org/specifications/0.5/index.html#images
@@ -66,15 +66,15 @@ NULL
6666

6767
#' @rdname readSpatialData
6868
#' @export
69-
readImage <- function(x, ...) {
70-
l <- .readArray(x, ...)
69+
readImage <- function(x, md = NULL, ...) {
70+
l <- .readArray(x, md = md, ...)
7171
SpatialDataImage(data=l$array, meta=l$mdattr, ...)
7272
}
7373

7474
#' @rdname readSpatialData
7575
#' @export
76-
readLabel <- function(x, ...) {
77-
l <- .readArray(x, ...)
76+
readLabel <- function(x, md = NULL, ...) {
77+
l <- .readArray(x, md = md, ...)
7878
SpatialDataLabel(data=l$array, meta=l$mdattr, ...)
7979
}
8080

@@ -83,9 +83,9 @@ readLabel <- function(x, ...) {
8383
#' @importFrom Rarr read_zarr_attributes
8484
#' @importFrom dplyr sql
8585
#' @export
86-
readPoint <- function(x, ...) {
86+
readPoint <- function(x, md = NULL, ...) {
8787
pq <- paste0(x, file.path("points.parquet", "part.0.parquet"))
88-
md <- read_zarr_attributes(x)
88+
md <- md %||% read_zarr_attributes(x)
8989
ax <- unlist(md$axes)
9090
df <- ddbs_open_dataset(pq, conn=.conn()) |>
9191
mutate(geometry=sql(sprintf("ST_Point(%s, %s)", ax[1], ax[2]))) |>
@@ -112,8 +112,8 @@ readPoint <- function(x, ...) {
112112
#' @importFrom duckspatial ddbs_open_dataset
113113
#' @import geoarrow
114114
#' @export
115-
readShape <- function(x, ...) {
116-
md <- read_zarr_attributes(x)
115+
readShape <- function(x, md = NULL, ...) {
116+
md <- md %||% read_zarr_attributes(x)
117117
# "shapes.parquet" currently hardcoded in SpatialData.io
118118
pq <- paste0(x, "shapes.parquet")
119119
df <- ddbs_open_dataset(pq, conn=.conn(), crs=NA_character_)
@@ -126,7 +126,7 @@ readShape <- function(x, ...) {
126126
#' @importFrom S4Vectors metadata metadata<-
127127
#' @importFrom SummarizedExperiment colData colData<-
128128
#' @importFrom SingleCellExperiment int_colData int_colData<- int_metadata int_metadata<-
129-
readTable <- function(x) {
129+
readTable <- function(x, ...) {
130130
suppressWarnings({ # suppress warnings related to hidden files
131131
sce <- anndataR::read_zarr(x, as="SingleCellExperiment")
132132
})
@@ -164,10 +164,7 @@ readSpatialData <- function(x,
164164
.readLayer <- \(l) {
165165
message(" reading ", l, "...")
166166
j <- store_groups[startsWith(store_groups, paste0(l, "/"))]
167-
j <- setNames(
168-
paste0(x, j, "/", recycle0 = TRUE),
169-
basename(j)
170-
)
167+
names(j) <- basename(j)
171168

172169
opt <- args[[l]]
173170
if (!isTRUE(opt)) {
@@ -178,7 +175,11 @@ readSpatialData <- function(x,
178175
j <- j[opt]
179176
}
180177
reader <- get(paste0("read", toupper(substr(l, 1, 1)), substr(l, 2, nchar(l)-1)))
181-
lapply(j, reader)
178+
lapply(j, function(el) {
179+
md <- store_meta[[el]]$attributes
180+
el <- paste0(x, el, "/", recycle0 = TRUE)
181+
reader(el, md=md)
182+
})
182183
}
183184

184185
names(ls) <- ls <- .LAYERS[!skip]

0 commit comments

Comments
 (0)