Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ inst/extdata/xenium1.zarr
inst/extdata/visiumhd.zarr
*.Rproj
*.html
R/_*
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SpatialData
Title: Representation of Python's SpatialData in R
Depends: R (>= 4.6)
Version: 0.99.28
Version: 0.99.29
Description: Interface to Python's 'SpatialData', currently including:
reticulate-based use of 'spatialdata-io' for reading of manufacturer
data and writing to .zarr, on-disk representation of images/labels as
Expand Down Expand Up @@ -51,7 +51,6 @@ Imports:
sf,
S4Arrays,
S4Vectors,
SparseArray,
SingleCellExperiment,
SummarizedExperiment
Suggests:
Expand All @@ -63,8 +62,7 @@ Suggests:
Rgraphviz,
SpatialData.data,
SpatialData.plot,
testthat,
DT
testthat
Remotes:
keller-mark/anndataR@spatialdata,
HelenaLC/SpatialData.data,
Expand Down
7 changes: 4 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ setGeneric("tables<-", \(x, value) standardGeneric("tables<-"))

# trs ----

setGeneric("axes", \(x, ...) standardGeneric("axes"))
setGeneric("CTlist", \(x, ...) standardGeneric("CTlist"))
setGeneric("CTdata", \(x, ...) standardGeneric("CTdata"))
setGeneric("CTname", \(x, ...) standardGeneric("CTname"))
Expand Down Expand Up @@ -79,11 +78,13 @@ setGeneric("meta", \(x, ...) standardGeneric("meta"))
setGeneric("query", \(x, ...) standardGeneric("query"))
setGeneric("mask", \(x, i, j, ...) standardGeneric("mask"))

setGeneric("axes", \(x, ...) standardGeneric("axes"))
setGeneric("extent", \(x, ...) standardGeneric("extent"))
setGeneric("channels", \(x, ...) standardGeneric("channels"))
setGeneric("centroids", \(x, ...) standardGeneric("centroids"))
setGeneric("data_type", \(x, ...) standardGeneric("data_type"))
setGeneric("geom_type", \(x, ...) standardGeneric("geom_type"))
setGeneric("centroids", \(x, ...) standardGeneric("centroids"))
setGeneric("extent", \(x, ...) standardGeneric("extent"))
setGeneric("multiscales", \(x, ...) standardGeneric("multiscales"))

# tbl ----

Expand Down
13 changes: 13 additions & 0 deletions R/CTgraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
#' @param fac,max scalar numeric; node labels with \code{nchar>max}
#' are split and hyphenated at position \code{floor(nchar/fac)}
#'
#' @returns
#' \itemize{
#' \item \code{CTgraph}:
#' \code{graph::graphAM} object with nodes for each element and
#' coordinate space, and edges for each transformation (if specified)
#' \item \code{CTpath}:
#' list of transformations from \code{i} to \code{j};
#' length > 1 if \code{type} is \code{"sequential"}, length-1 otherwise;
#' each element specifies \code{type} and \code{data} of the transformation
#' \item \code{CTplot}:
#' visualizes the element-coordinate space graph with \code{Rgraphviz}
#' }
#'
#' @examples
#' x <- file.path("extdata", "blobs.zarr")
#' x <- system.file(x, package="SpatialData")
Expand Down
70 changes: 46 additions & 24 deletions R/coord_utils.R → R/CTutils.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
#' @name coord-utils
#' @title Coordinate transformations
#' @name CTutils
#' @title Coord. trans. utilities
#' @aliases axes CTlist CTname CTtype CTdata addCT rmvCT
#'
#' @param x \code{SpatialData}, an element, or \code{Zattrs}.
#' @param i for \code{CTpath}, source node label; else, string or
#' scalar integer giving the name or index of a coordinate space.
#' @param j character string; name of target coordinate space.
#' @param name character(1); name of coordinate space
#' @param type character(1); type of transformation
#' @param data transformation data; size and shape depend on transformation and
#' element type (e.g., numeric(1) for rotation, numeric(2) for scaling in 2D)
#' @param ... option arguments passed to and from other methods.
#'
#' @returns
#' \itemize{
#' \item \code{CTname}: character string;
#' transformation name (e.g., "global")
#' \item \code{CTtype}: character string;
#' transformation type (e.g., "affine")
#' \item \code{CTdata}: list;
#' transformation data (e.g., scalar numeric for rotation)
#' \item \code{CTlist}: list;
#' list of transformation specifications per OME-NGFF spec
#' \item \code{add/rmvCT}:
#' \code{SpatialDataElement} or \code{Zattrs}
#' with transformation(s) added/removed
#' \item \code{axes}: list;
#' each element is a character string (name), or list
#' with axis name and type (e.g., "space" or "channel")
#' }
#'
#' @examples
#' x <- file.path("extdata", "blobs.zarr")
#' x <- system.file(x, package="SpatialData")
Expand All @@ -38,30 +55,31 @@ NULL

# axes() ----

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("axes", "Zattrs", \(x, ...) {
if (!is.null(ms <- x$multiscales)) x <- ms[[1]]
ms <- multiscales(x)
if (!is.null(ms)) x <- ms[[1]]
if (is.null(x <- x$axes)) stop("couldn't find 'axes'")
return(x)
})

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("axes", "SpatialDataElement", \(x, ...) axes(meta(x)))

# CTlist/data/type/name() ----

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTlist", "Zattrs", \(x, ...) {
ms <- "multiscales"
ms <- multiscales(x)
ct <- "coordinateTransformations"
if (is.null(x[[ms]])) return(x[[ct]])
x[[ms]][[1]][[ct]]
if (is.null(ms)) return(x[[ct]])
ms[[1]][[ct]]
})

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTdata", "Zattrs", \(x, i=1, ...) {
stopifnot(length(i) == 1)
Expand All @@ -81,31 +99,35 @@ setMethod("CTdata", "Zattrs", \(x, i=1, ...) {
mapply(x=ts, i=names(ts), \(x, i) x[[i]], SIMPLIFY=FALSE)
})

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTtype", "Zattrs", \(x, ...) vapply(CTlist(x), \(.) .$type, character(1)))
setMethod("CTtype", "Zattrs", \(x, ...) {
vapply(CTlist(x), \(.) .$type, character(1))
})

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTname", "Zattrs", \(x, ...) vapply(CTlist(x), \(.) .$output$name, character(1)))
setMethod("CTname", "Zattrs", \(x, ...) {
vapply(CTlist(x), \(.) .$output$name, character(1))
})

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTlist", "SpatialDataElement", \(x, ...) CTlist(meta(x)))

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTdata", "SpatialDataElement", \(x, i=1, ...) CTdata(meta(x), i))

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTtype", "SpatialDataElement", \(x, ...) CTtype(meta(x)))

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTname", "SpatialDataElement", \(x, ...) CTname(meta(x)))

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("CTname", "SpatialData", \(x, ...) {
g <- CTgraph(x)
Expand All @@ -115,12 +137,12 @@ setMethod("CTname", "SpatialData", \(x, ...) {

# rmv ----

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("rmvCT", "SpatialDataElement",
\(x, i) { x@meta <- rmvCT(meta(x), i); x })

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("rmvCT", "Zattrs", \(x, i) {
nms <- CTname(x)
Expand Down Expand Up @@ -155,7 +177,7 @@ setMethod("rmvCT", "Zattrs", \(x, i) {

# add ----

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("addCT", "SpatialDataElement", \(x, name, type, data) {
x@meta <- addCT(meta(x), name, type, data); x })
Expand All @@ -173,7 +195,7 @@ setMethod("addCT", "SpatialDataElement", \(x, name, type, data) {
if (!.) f(t)
}

#' @rdname coord-utils
#' @rdname CTutils
#' @export
setMethod("addCT", "Zattrs", \(x, name, type="identity", data=NULL) {
stopifnot(
Expand Down
21 changes: 13 additions & 8 deletions R/ImageArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ ImageArray <- function(data=list(), meta=Zattrs(), metadata=list(), ...) {
#' @rdname ImageArray
#' @aliases channels
#' @export
setMethod("channels", "Zattrs", \(x, ...) unlist(x$omero$channels))
setMethod("channels", "Zattrs", \(x, ...) {
v <- x$spatialdata_attrs$version
Comment thread
HelenaLC marked this conversation as resolved.
if (!length(v)) stop("couldn't find 'version' in 'spatialdata_attrs'")
if (v == "0.3") x <- x$ome
unlist(x$omero$channels)
})

#' @rdname ImageArray
#' @aliases channels
Expand All @@ -43,11 +48,11 @@ setMethod("channels", "ImageArray", \(x, ...) channels(meta(x)))
setMethod("channels", "ANY", \(x, ...) stop("only 'images' have channels"))

#' @importFrom S4Vectors isSequence
.get_multiscales_dataset_paths <- function(md) {
.get_multiscales_dataset_paths <- function(za) {
# validate 'multiscales'
.validate_multiscales_dataset_path(md)
ms <- .check_ms(za)
# get & validate 'path's
ds <- md$multiscales[[1]]$datasets
ds <- ms[[1]]$datasets
ps <- vapply(ds, \(.) .$path, character(1))
ps <- suppressWarnings(as.numeric(sort(ps, decreasing=FALSE)))
if (length(ps)) {
Expand All @@ -59,10 +64,9 @@ setMethod("channels", "ANY", \(x, ...) stop("only 'images' have channels"))
return(ps)
}

#' @noRd
.validate_multiscales_dataset_path <- function(md) {
# validate 'multiscales'
ms <- md$multiscales
.check_ms <- \(za) {
# validate 'multiscales'
ms <- multiscales(za)
if (!is.null(ms)) {
# validate 'datasets'
ds <- ms[[1]]$datasets
Expand All @@ -78,6 +82,7 @@ setMethod("channels", "ANY", \(x, ...) stop("only 'images' have channels"))
} else stop(
"'ImageArray' paths are ill-defined,",
" no 'multiscales' attribute under '.zattrs'")
return(ms)
}

.check_jk <- \(x, .) {
Expand Down
8 changes: 7 additions & 1 deletion R/LabelArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
#' @return \code{LabelArray}
#'
#' @examples
#' # TODO
#' x <- file.path("extdata", "blobs.zarr")
#' x <- system.file(x, package="SpatialData")
#' x <- file.path(x, "labels", "blobs_labels")
#'
#' (y <- readLabel(x))
#' y[1:10, 1:10]
#' meta(y)
#'
#' @importFrom S4Vectors metadata<-
#' @importFrom methods new
Expand Down
6 changes: 3 additions & 3 deletions R/ShapeFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' content describing the overall object.
#' @param name character string for extraction (see \code{?base::`$`}).
#' @param i,j indices specifying elements to extract.
#' @param drop ignored.
#' @param drop,pattern ignored.
#' @param ... optional arguments passed to and from other methods.
#'
#' @return \code{ShapeFrame}
Expand Down Expand Up @@ -55,15 +55,15 @@ setMethod("names", "ShapeFrame", \(x) names(data(x)))
#' @export
#' @rdname ShapeFrame
#' @importFrom utils .DollarNames
.DollarNames.ShapeFrame <- \(x, pattern="") {
.DollarNames.ShapeFrame <- \(x, pattern="")
grep(pattern, names(x), value=TRUE)
}

#' @rdname ShapeFrame
#' @exportMethod $
setMethod("$", "ShapeFrame", \(x, name) data(x)[[name]])

#' @export
#' @rdname ShapeFrame
#' @importFrom sf st_as_sf st_geometry_type
setMethod("geom_type", "ShapeFrame", \(x) {
y <- st_as_sf(data(x[1, ]))
Expand Down
2 changes: 2 additions & 0 deletions R/SpatialData.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' image images image<- images<- imageNames
#' shape shapes shape<- shapes<- shapeNames
#' table tables table<- tables<- tableNames
#' [[<-,SpatialData,character,ANY-method
#' [[<-,SpatialData,numeric,ANY-method
#'
#' @description ...
#'
Expand Down
Loading
Loading