Skip to content

Commit aa4e628

Browse files
committed
simplify [ method for sdArrays
1 parent 80db7fd commit aa4e628

6 files changed

Lines changed: 14 additions & 45 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Imports:
6666
RBGL,
6767
rlang,
6868
sf,
69+
S4Arrays,
6970
S4Vectors,
7071
SingleCellExperiment,
7172
SummarizedExperiment,

NAMESPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ exportMethods(tableNames)
112112
exportMethods(tables)
113113
exportMethods(transform)
114114
exportMethods(translation)
115+
import(S4Arrays)
115116
importFrom(BiocGenerics,as.data.frame)
116117
importFrom(BiocGenerics,colnames)
117118
importFrom(BiocGenerics,combine)
@@ -122,6 +123,7 @@ importFrom(DBI,dbIsValid)
122123
importFrom(DelayedArray,DelayedArray)
123124
importFrom(EBImage,rotate)
124125
importFrom(ImageArray,ImageArray)
126+
importFrom(ImageArray,crop)
125127
importFrom(Matrix,sparseMatrix)
126128
importFrom(Matrix,sparseVector)
127129
importFrom(Matrix,summary)
@@ -208,5 +210,3 @@ importFrom(sf,st_polygon)
208210
importFrom(sf,st_sf)
209211
importFrom(sf,st_sfc)
210212
importFrom(utils,.DollarNames)
211-
importFrom(utils,head)
212-
importFrom(utils,tail)

R/sdArray.R

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,35 +156,13 @@ setMethod("channels", "SpatialDataElement", \(x, ...) stop("only 'images' have c
156156

157157
# sub ----
158158

159-
.check_jk <- \(x, .) {
160-
if (isTRUE(x)) return()
161-
tryCatch(
162-
stopifnot(
163-
is.numeric(x), x == round(x),
164-
diff(range(x)) == length(x)-1,
165-
(y <- abs(x)) == seq(min(y), max(y))
166-
),
167-
error=\(e) stop(sprintf("invalid '%s'", .))
168-
)
169-
}
170-
171-
#' @exportMethod [
172-
#' @rdname SpatialDataArray
173-
#' @importFrom utils head tail
174-
setMethod("[", "SpatialDataImage", \(x, i, j, k, ..., drop=FALSE) {
175-
if (missing(i)) i <- seq_len(dim(x)[1])
176-
if (missing(j)) j <- TRUE else if (isFALSE(j)) j <- 0 else .check_jk(j, "j")
177-
if (missing(k)) k <- TRUE else if (isFALSE(k)) k <- 0 else .check_jk(k, "k")
178-
data(x) <- data(x, NULL)[i,j,k]
179-
x
180-
})
181-
182159
#' @exportMethod [
183160
#' @rdname SpatialDataArray
184-
#' @importFrom utils head tail
185-
setMethod("[", "SpatialDataLabel", \(x, i, j, ..., drop=FALSE) {
186-
if (missing(i)) i <- TRUE else if (isFALSE(i)) i <- 0 else .check_jk(i, "i")
187-
if (missing(j)) j <- TRUE else if (isFALSE(j)) j <- 0 else .check_jk(j, "j")
188-
data(x) <- data(x, NULL)[i,j]
161+
#' @import S4Arrays
162+
#' @importFrom ImageArray crop
163+
setMethod("[", "SpatialDataArray", \(x, i, j, ...) {
164+
Nindex <- S4Arrays:::extract_Nindex_from_syscall(sys.call(), parent.frame())
165+
stopifnot(length(dim(x)) == length(Nindex))
166+
data(x) <- ImageArray::crop(data(x, NULL), index = Nindex)
189167
x
190168
})

man/SpatialDataArray.Rd

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/readSpatialData.Rd

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/trans.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)