|
| 1 | +#' @importFrom methods as |
| 2 | +#' @importFrom anndataR read_zarr |
| 3 | +#' @importFrom SpatialExperiment SpatialExperiment |
| 4 | +readSDasSPE <- \(x, i=1) { |
| 5 | + if (!requireNamespace("SpatialExperiment", quietly=TRUE)) |
| 6 | + stop("install the 'SpatialExperiment' package to use this function.") |
| 7 | + y <- list.files(file.path(x, "tables"), full.names=TRUE) |
| 8 | + if (!length(y)) |
| 9 | + stop("couldn't find any tables") |
| 10 | + if (is.character(i)) |
| 11 | + i <- match(i, basename(y)) |
| 12 | + sce <- readTable(y[i]) |
| 13 | + colData(sce) <- NULL |
| 14 | + for (l in setdiff(.LAYERS, "tables")) { |
| 15 | + y <- list.files(file.path(x, l), full.names=TRUE) |
| 16 | + y <- y[match(region(sce), basename(y))] |
| 17 | + if (!is.na(y)) { |
| 18 | + t <- basename(dirname(y)) |
| 19 | + f <- paste0("read", |
| 20 | + toupper(substr(t, 1, 1)), |
| 21 | + substr(t, 2, nchar(t)-1)) |
| 22 | + y <- get(f)(y) |
| 23 | + xy <- centroids(y, "matrix") |
| 24 | + xy <- xy[, c("x", "y")] |
| 25 | + } |
| 26 | + } |
| 27 | + toSpatialExperiment(sce, spatialCoords=xy) |
| 28 | +} |
| 29 | + |
| 30 | +require(sf, quietly=TRUE) |
| 31 | +x <- file.path("extdata", "blobs.zarr") |
| 32 | +x <- system.file(x, package="SpatialData") |
| 33 | +(spe <- readSDasSPE(x)) |
0 commit comments