Skip to content

Commit a49b09b

Browse files
HelenaLCBisaloo
andauthored
v0.99.40
* simplify code bits using seq_along(), lengths(), .data, etc. * assure layer lists are fully named (updated validity, tests, setters) * make SpatialDataImage/Label() accept single-arary (#216) * v0.99.40 --------- Co-authored-by: Hugo Gruson <git@hugogruson.fr>
1 parent a2cbcda commit a49b09b

17 files changed

Lines changed: 129 additions & 48 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: spatialdataR
22
Title: Representation of Python's spatialdata in R
33
Depends: R (>= 4.6)
4-
Version: 0.99.39
4+
Version: 0.99.40
55
Description: R interface to Python/scverse's 'spatialdata' framework for
66
unified spatial omics data handling. Adheres to OME-NGFF standards,
77
providing lazy, on-disk representations for multiscale images and

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ importFrom(ZarrArray,ZarrArray)
155155
importFrom(ZarrArray,path)
156156
importFrom(ZarrArray,type)
157157
importFrom(anndataR,read_zarr)
158+
importFrom(dplyr,.data)
158159
importFrom(dplyr,all_of)
159160
importFrom(dplyr,coalesce)
160161
importFrom(dplyr,collect)

R/SpatialData.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ SpatialData <- \(
6060
shapes=list(),
6161
tables=list())
6262
{
63-
.SpatialData(
64-
images=sdImageList(images),
65-
labels=sdLabelList(labels),
66-
points=sdPointList(points),
67-
shapes=sdShapeList(shapes),
68-
tables=sdTableList(tables))
63+
sd <- .SpatialData()
64+
for (l in .LAYERS)
65+
sd[[l]] <- get(l)
66+
return(sd)
6967
}

R/combine.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ NULL
2525
setMethod("combine", c("SpatialData", "SpatialData"), \(x, y, ...) {
2626
# ensure element names are unique across objects
2727
old <- list(unlist(colnames(x)), unlist(colnames(y)))
28-
idx <- rep.int(c(1, 2), vapply(old, length, integer(1)))
28+
idx <- rep.int(c(1, 2), lengths(old))
2929
new <- split(make.unique(unlist(old)), idx)
3030
for (i in c(1, 2)) {
3131
# get input element names

R/crop.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ setMethod("crop", "SpatialDataArray", \(x, y, j=1, ...) {
201201

202202
#' @export
203203
#' @rdname crop
204-
#' @importFrom dplyr pull
204+
#' @importFrom dplyr pull .data
205205
#' @importFrom duckspatial ddbs_intersects
206206
#' @importFrom sf st_sf st_sfc st_as_sfc st_bbox st_polygon st_geometry<-
207207
setMethod("crop", "SpatialDataFrame", \(x, y, j=1, ...) {
@@ -225,8 +225,7 @@ setMethod("crop", "SpatialDataFrame", \(x, y, j=1, ...) {
225225
df <- data(transform(x, j))
226226
fd <- data(SpatialDataShape(fd))
227227
ok <- ddbs_intersects(df, fd, sparse=TRUE)
228-
id_x <- NULL # R CMD check
229-
x[pull(ok, id_x), ]
228+
x[pull(ok, .data$id_x), ]
230229
})
231230

232231
#' @export

R/extent.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ NULL
2929
setMethod("extent", "SpatialData", \(x, i=1) {
3030
ex <- .lapplyLayer(x, extent, i=i)
3131
ex <- unlist(ex, recursive=FALSE)
32-
xy <- do.call(rbind, lapply(ex, do.call, what=cbind))
33-
list(x=range(xy[, 1]), y=range(xy[, 2]))
32+
xy <- dplyr::bind_rows(ex)
33+
list(x=range(xy$x), y=range(xy$y))
3434
})
3535

3636
#' @export

R/mask.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ setMethod("mask_i_by_j",
180180
if (nrow(collect(head(ij, 1))) == 0)
181181
stop("found no intersections",
182182
" between shapes 'i' and 'j'")
183-
id_x <- id_y <- NULL # R CMD check
184-
is <- pull(ij, id_y) # elements in i
185-
js <- pull(ij, id_x) # masks in j
183+
is <- pull(ij, .data$id_y) # elements in i
184+
js <- pull(ij, .data$id_x) # masks in j
186185
na <- setdiff(seq_along(i), is)
187186
# aggregation
188187
mx <- assay(table, assay)

R/methods.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ setMethod("data", "SpatialDataElement", \(x, k=1, ...) {
4343
"NULL or a scalar positive integer")
4444
# get number of available scales
4545
n <- length(x <- x@data)
46+
if (!length(x)) return(NULL)
4647
# input of Inf uses lowest
4748
if (is.infinite(k)) k <- n
4849
# return specified scale
@@ -262,6 +263,20 @@ NULL
262263
f <- \(l) setReplaceMethod(l,
263264
c("SpatialData", getSlots("SpatialData")[[l]]),
264265
\(x, value) {
266+
if (length(value)) {
267+
nms <- names(value)
268+
e <- gsub("s$", "", l)
269+
if (is.null(nms)) {
270+
nms <- paste0(e, seq_along(value))
271+
names(value) <- nms
272+
} else {
273+
na <- nchar(nms) == 0
274+
if (any(na)) {
275+
nms[na] <- paste0(e, which(na))
276+
names(value) <- nms
277+
}
278+
}
279+
}
265280
if (l != "tables") {
266281
old <- names(slot(x, l))
267282
new <- names(value)

R/read.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ readSpatialData <- function(x,
146146
stop("couldn't find ", l, " of name", .)
147147
j <- j[opt]
148148
}
149-
f <- get(paste0("read", toupper(substr(l, 1, 1)), substr(l, 2, nchar(l)-1)))
150-
lapply(j, \(.) do.call(f, list(.)))
149+
f <- paste0("read", toupper(substr(l, 1, 1)), substr(l, 2, nchar(l)-1))
150+
lapply(j, f)
151151
}
152152

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

R/sdArray.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ NULL
6767
#' @rdname SpatialDataArray
6868
#' @importFrom methods new
6969
#' @importFrom S4Vectors metadata<-
70-
SpatialDataImage <- function(data=list(), meta=SpatialDataAttrs(), metadata=list(), ...) {
70+
SpatialDataImage <- \(data=list(), meta=SpatialDataAttrs(), metadata=list(), ...) {
71+
if (is.array(data)) data <- list(data)
7172
x <- .SpatialDataImage(data=data, meta=meta, ...)
7273
metadata(x) <- metadata
7374
return(x)
@@ -77,7 +78,8 @@ SpatialDataImage <- function(data=list(), meta=SpatialDataAttrs(), metadata=list
7778
#' @rdname SpatialDataArray
7879
#' @importFrom methods new
7980
#' @importFrom S4Vectors metadata<-
80-
SpatialDataLabel <- function(data=list(), meta=SpatialDataAttrs(), metadata=list(), ...) {
81+
SpatialDataLabel <- \(data=list(), meta=SpatialDataAttrs(), metadata=list(), ...) {
82+
if (is.array(data)) data <- list(data)
8183
x <- .SpatialDataLabel(data=data, meta=meta, ...)
8284
metadata(x) <- metadata
8385
return(x)

0 commit comments

Comments
 (0)