diff --git a/NAMESPACE b/NAMESPACE index d91581e..75ddaa8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -49,6 +49,7 @@ exportMethods(axes) exportMethods(channels) exportMethods(colnames) exportMethods(data) +exportMethods(datasets) exportMethods(dim) exportMethods(element) exportMethods(getTable) diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 6e0a948..2ceafa2 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -80,6 +80,7 @@ setGeneric("query", \(x, ...) standardGeneric("query")) setGeneric("mask", \(x, i, j, ...) standardGeneric("mask")) setGeneric("channels", \(x, ...) standardGeneric("channels")) +setGeneric("datasets", \(x, ...) standardGeneric("datasets")) # tbl ---- diff --git a/R/ImageArray.R b/R/ImageArray.R index e582239..4c67e44 100644 --- a/R/ImageArray.R +++ b/R/ImageArray.R @@ -32,7 +32,7 @@ ImageArray <- function(data=list(), meta=Zattrs(), metadata=list(), ...) { #' @rdname ImageArray #' @aliases channels #' @export -setMethod("channels", "ImageArray", \(x, ...) meta(x)$omero$channels$label) +setMethod("channels", "ImageArray", \(x, ...) channels(meta(x))) #' @rdname ImageArray #' @export diff --git a/R/Zattrs.R b/R/Zattrs.R index 854443c..5c2a1fe 100644 --- a/R/Zattrs.R +++ b/R/Zattrs.R @@ -33,3 +33,22 @@ Zattrs <- \(x=list()) { #' @rdname Zattrs #' @exportMethod $ setMethod("$", "Zattrs", \(x, name) x[[name]]) + +#' @rdname Zattrs +#' @export +setMethod("channels", "Zattrs", \(x) { + if (!is.null(o <- x$omero)) x <- o + if (!is.null(c <- x$channel)) x <- c + x$label +}) + +#' @rdname Zattrs +#' @export +setMethod("datasets", "Zattrs", \(x) { + if (!is.null(ms <- x$multiscales)) x <- ms + if (!is.null(d <- x$datasets)){ + d[[1]] + } else { + NULL + } +}) \ No newline at end of file diff --git a/R/coord_utils.R b/R/coord_utils.R index 4dbe2c4..68f6c41 100644 --- a/R/coord_utils.R +++ b/R/coord_utils.R @@ -55,12 +55,16 @@ NULL setMethod("axes", "Zattrs", \(x, ...) { if (!is.null(ms <- x$multiscales)) x <- ms if (is.null(x <- x$axes)) stop("couldn't find 'axes'") - if (is.character(x)) x else x[[1]] + if (is.character(x)){ + return(x) + } else { + x[[1]] + } }) #' @rdname coord-utils #' @export -setMethod("axes", "SpatialDataElement", \(x, ...) axes(meta(x))) +setMethod("axes", "SpatialDataElement", \(x, ...) axes(meta(x), ...)) # CTdata/type/name() ---- @@ -119,6 +123,11 @@ setMethod("CTgraph", "SpatialData", \(x) { setMethod("CTgraph", "SpatialDataElement", \(x) .make_g(list("mock"=list("self"=meta(x))))) +#' @rdname coord-utils +#' @export +setMethod("CTgraph", "Zattrs", \(x) + .make_g(list("mock"=list("self"=x)))) + #' @rdname coord-utils #' @export setMethod("CTgraph", "ANY", \(x) stop("'x' should be a", @@ -196,6 +205,13 @@ setMethod("CTpath", "SpatialDataElement", \(x, j) { .path_ij(g, "self", j) }) +#' @rdname coord-utils +#' @export +setMethod("CTpath", "Zattrs", \(x, j) { + g <- CTgraph(x) + .path_ij(g, "self", j) +}) + #' @importFrom graph edgeData #' @importFrom RBGL sp.between .path_ij <- \(g, i, j) { diff --git a/R/methods.R b/R/methods.R index eaf7dc4..14ef197 100644 --- a/R/methods.R +++ b/R/methods.R @@ -71,6 +71,13 @@ setMethod("data", "SpatialDataElement", \(x) x@data) #' @export setMethod("meta", "SpatialDataElement", \(x) x@meta) +# datasets ---- + +#' @rdname SpatialData +#' @aliases datasets +#' @export +setMethod("datasets", "SpatialDataElement", \(x, ...) datasets(meta(x))) + # row/colnms ---- #' @rdname SpatialData diff --git a/R/misc.R b/R/misc.R index f208609..d480dbb 100644 --- a/R/misc.R +++ b/R/misc.R @@ -110,4 +110,90 @@ setMethod("show", "PointFrame", .showPointFrame) } #' @rdname misc -setMethod("show", "ShapeFrame", .showShapeFrame) \ No newline at end of file +setMethod("show", "ShapeFrame", .showShapeFrame) + +.showZattrsAxes <- function(object){ + ax <- axes(object) + if(!is(ax, "data.frame")) + ax <- data.frame(name = ax) + cat(sprintf("axes(%d): \n", nrow(ax))) + for(. in seq_along(ax)){ + cat(sprintf(paste0("- ", names(ax)[[.]], ": %s\n"), + paste(ax[[.]], collapse = " "))) + } +} + +.showZattrsDatasets <- function(object){ + if(!is.null(d <- datasets(object))){ + coolcat("datasets(%d): %s\n", d$path) + ct <- vapply(d$coordinateTransformations, \(x) + paste( + apply(x, 1, \(y) + paste0("(", x$type, ":", "[", + paste(x[[x$type]][[1]], collapse = ","), + "]", ")") + ), collapse = ", " + ), + character(1)) + for(. in seq_along(ct)){ + cat(sprintf("- %s: %s", d$path[.], ct[.]), "\n") + } + } +} + +.collapse_trans_data <- function(data){ + if(is.null(data)){ + return(NULL) + } else if(is.matrix(data)){ + return( + paste0("[", + paste( + apply(data, 1, \(x){ + paste0("[", + paste(x, collapse = ","), + "]") + }), + collapse = ","), + "]") + ) + } else{ + return(paste0("[",paste(data, collapse = ","), "]")) + } +} + +.showZattrsTransformations <- function(object){ + ctdata.name <- CTname(object) + cat(sprintf("coordTrans(%d):\n", length(ctdata.name))) + for(. in seq_along(ctdata.name)){ + ct <- CTpath(object, ctdata.name[[.]]) + cat(sprintf(paste0("- ", ctdata.name[[.]],": ", + paste( + vapply(ct, \(x) { + paste0("(", + x$type, + if(!is.null(x$data)) + paste0(":", .collapse_trans_data(x$data)), + ")") + }, character(1)), + collapse = ", "), + "\n"))) + } +} + +#' @importFrom S4Vectors coolcat +.showZattrs <- function(object) { + cat("class: Zattrs\n") + # axes + .showZattrsAxes(object) + # transformations + # coolcat("transformations(%d): %s\n", CTname(object)) + .showZattrsTransformations(object) + # datasets + .showZattrsDatasets(object) + # channel + if(!is.null(c <- channels(object))) + coolcat("channels(%d): %s\n", channels(object)) +} + +#' @rdname misc +setMethod("show", "Zattrs", .showZattrs) \ No newline at end of file diff --git a/man/SpatialData.Rd b/man/SpatialData.Rd index 9521d85..623ae19 100644 --- a/man/SpatialData.Rd +++ b/man/SpatialData.Rd @@ -40,6 +40,8 @@ \alias{[,SpatialData,ANY,ANY,ANY-method} \alias{data,SpatialDataElement-method} \alias{meta,SpatialDataElement-method} +\alias{datasets,SpatialDataElement-method} +\alias{datasets} \alias{rownames,SpatialData-method} \alias{colnames,SpatialData-method} \alias{layer,SpatialData,character-method} @@ -68,6 +70,8 @@ SpatialData(images, labels, points, shapes, tables) \S4method{meta}{SpatialDataElement}(x) +\S4method{datasets}{SpatialDataElement}(x, ...) + \S4method{rownames}{SpatialData}(x) \S4method{colnames}{SpatialData}(x) diff --git a/man/Zattrs.Rd b/man/Zattrs.Rd index b736e2e..43f223c 100644 --- a/man/Zattrs.Rd +++ b/man/Zattrs.Rd @@ -3,11 +3,17 @@ \name{Zattrs} \alias{Zattrs} \alias{$,Zattrs-method} +\alias{channels,Zattrs-method} +\alias{datasets,Zattrs-method} \title{The `Zattrs` class} \usage{ Zattrs(x = list()) \S4method{$}{Zattrs}(x, name) + +\S4method{channels}{Zattrs}(x) + +\S4method{datasets}{Zattrs}(x) } \arguments{ \item{x}{list extracted from a OME-NGFF compliant .zattrs file.} diff --git a/man/coord-utils.Rd b/man/coord-utils.Rd index a321588..a3078c9 100644 --- a/man/coord-utils.Rd +++ b/man/coord-utils.Rd @@ -21,9 +21,11 @@ \alias{CTname,SpatialData-method} \alias{CTgraph,SpatialData-method} \alias{CTgraph,SpatialDataElement-method} +\alias{CTgraph,Zattrs-method} \alias{CTgraph,ANY-method} \alias{CTpath,SpatialData-method} \alias{CTpath,SpatialDataElement-method} +\alias{CTpath,Zattrs-method} \alias{rmvCT,SpatialDataElement-method} \alias{rmvCT,Zattrs-method} \alias{addCT,SpatialDataElement-method} @@ -52,12 +54,16 @@ \S4method{CTgraph}{SpatialDataElement}(x) +\S4method{CTgraph}{Zattrs}(x) + \S4method{CTgraph}{ANY}(x) \S4method{CTpath}{SpatialData}(x, i, j) \S4method{CTpath}{SpatialDataElement}(x, j) +\S4method{CTpath}{Zattrs}(x, j) + \S4method{rmvCT}{SpatialDataElement}(x, i) \S4method{rmvCT}{Zattrs}(x, i) diff --git a/man/misc.Rd b/man/misc.Rd index b63a1e8..a867419 100644 --- a/man/misc.Rd +++ b/man/misc.Rd @@ -7,6 +7,7 @@ \alias{show,LabelArray-method} \alias{show,PointFrame-method} \alias{show,ShapeFrame-method} +\alias{show,Zattrs-method} \title{Miscellaneous `Miro` methods} \usage{ \S4method{show}{SpatialData}(object) @@ -18,6 +19,8 @@ \S4method{show}{PointFrame}(object) \S4method{show}{ShapeFrame}(object) + +\S4method{show}{Zattrs}(object) } \arguments{ \item{object}{\code{\link{SpatialData}} object or one of its diff --git a/tests/testthat/test-zattrs.R b/tests/testthat/test-zattrs.R index c618854..405925c 100644 --- a/tests/testthat/test-zattrs.R +++ b/tests/testthat/test-zattrs.R @@ -132,3 +132,14 @@ test_that("plotCoordGraph", { q <- f(plotCoordGraph(g, 0.9)) expect_true(p < q) }) + +test_that("show",{ + meta(image(x,1)) + meta(image(x,2)) + meta(label(x,1)) + meta(label(x,2)) + meta(shape(x,1)) + meta(shape(x,2)) + meta(shape(x,3)) + expect_equal(1L,1L) +}) \ No newline at end of file