Skip to content

Commit 58722b5

Browse files
authored
Merge pull request #35 from HelenaLC/scms
PR #34 revision/expansion
2 parents 34904b0 + 74bf050 commit 58722b5

21 files changed

Lines changed: 574 additions & 2493 deletions

.github/workflows/R-CMD-check.yaml

Lines changed: 0 additions & 50 deletions
This file was deleted.

.github/workflows/r-universe.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Test R-universe
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
name: R-universe testing
11+
uses: r-universe-org/workflows/.github/workflows/build.yml@v3
12+
with:
13+
universe: bioc
14+
organization: bioconductor

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*.Rproj
66
*html
77
docs
8+
tests/testthat/Rplots.pdf

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ Imports:
3535
rlang,
3636
sf,
3737
S4Vectors,
38-
SingleCellExperiment,
39-
Rarr,
40-
ZarrArray
38+
SingleCellExperiment
4139
Suggests:
4240
BiocStyle,
4341
ggnewscale,
@@ -47,7 +45,8 @@ Suggests:
4745
Rgraphviz,
4846
SpatialData.data,
4947
testthat,
50-
vdiffr
48+
vdiffr,
49+
ZarrArray
5150
Remotes:
5251
HelenaLC/spatialdataR,
5352
HelenaLC/SpatialData.data

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(plotSpatialData)
4+
export(scalebar)
45
exportMethods(plotImage)
56
exportMethods(plotLabel)
67
exportMethods(plotPoint)
@@ -14,6 +15,7 @@ importFrom(SingleCellExperiment,int_colData)
1415
importFrom(SingleCellExperiment,int_metadata)
1516
importFrom(ggforce,geom_circle)
1617
importFrom(ggplot2,aes)
18+
importFrom(ggplot2,annotate)
1719
importFrom(ggplot2,annotation_raster)
1820
importFrom(ggplot2,coord_fixed)
1921
importFrom(ggplot2,coord_sf)
@@ -38,6 +40,7 @@ importFrom(ggplot2,theme_bw)
3840
importFrom(ggplot2,unit)
3941
importFrom(grDevices,col2rgb)
4042
importFrom(grDevices,colorRampPalette)
43+
importFrom(grDevices,colors)
4144
importFrom(grDevices,hcl.colors)
4245
importFrom(methods,as)
4346
importFrom(methods,is)
@@ -46,6 +49,7 @@ importFrom(sf,st_as_sf)
4649
importFrom(sf,st_buffer)
4750
importFrom(sf,st_coordinates)
4851
importFrom(sf,st_geometry_type)
52+
importFrom(spatialdataR,"element<-")
4953
importFrom(spatialdataR,channels)
5054
importFrom(spatialdataR,data_type)
5155
importFrom(spatialdataR,transform)

R/plotFrame.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#'
55
#' @param x \code{SpatialData} object.
66
#' @param i character string or index; the label element to plot.
7-
#' @param assay character string; in case of \code{c} denoting a row name,
8-
#' specifies which \code{assay} data to use (see \code{\link{valTable}}).
7+
#' @param j index or name of target coordinate system.
8+
#' @param assay character string; in case of \code{c}
9+
#' denoting a row name, specifies which \code{assay}
10+
#' data to use (see \code{\link[spatialdataR]{getTable}}).
911
#' (ignored when \code{x} is a \code{SpatialDataPoint}).
12+
#' @param ... option aesthetic arguments passed \code{geom_sf}.
1013
#'
1114
#' @examples
1215
#' x <- file.path("extdata", "blobs.zarr")
@@ -36,25 +39,24 @@ NULL
3639

3740
#' @importFrom sf st_as_sf st_coordinates st_geometry_type st_buffer
3841
#' @importFrom ggplot2 aes theme scale_type geom_sf coord_sf
39-
#' @importFrom spatialdataR transform
42+
#' @importFrom spatialdataR transform element<-
4043
#' @importFrom ggforce geom_circle
4144
#' @importFrom methods is
4245
#' @importFrom utils tail
43-
.plot <- \(x, y, key=NULL, n=Inf, assay=1, i=1, ...) {
46+
.plot <- \(x, y, key=NULL, n=NULL, assay=1, i=1, ...) {
4447
if (is(y, "SpatialDataPoint")) {
4548
if (!is.null(key)) {
49+
stopifnot(is.character(key), nchar(key) > 0)
4650
fk <- feature_key(y)
47-
y<- dplyr::filter(y, .data[[fk]] %in% key)
51+
y <- dplyr::filter(y, .data[[fk]] %in% key)
52+
if (!length(y)) stop("no instances of specified 'key'(s)")
4853
}
4954
}
50-
if (is.finite(n)) {
55+
if (!is.null(n)) {
56+
stopifnot(is.numeric(n), length(n) == 1, n > 0)
5157
n <- min(length(y), n)
5258
y <- y[sample(length(y), n)]
53-
if (is(y, "SpatialDataShape")) {
54-
shape(x, i) <- y
55-
} else {
56-
point(x, i) <- y
57-
}
59+
element(x, i) <- y
5860
}
5961
df <- st_as_sf(data(y))
6062
aes <- aes()

R/plotImage.R

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#'
55
#' @description ...
66
#'
7-
#' @param x \code{\link{SpatialData}} object.
7+
#' @param x \code{\link[spatialdataR]{SpatialData}} object.
88
#' @param i element to use from a given layer.
9-
#' @param j name of target coordinate system.
10-
#' @param k index of the scale of an image; by default (NULL), will auto-select
9+
#' @param j index or name of target coordinate system.
10+
#' @param k index of the scale to render; by default (NULL), will auto-select
1111
#' scale in order to minimize memory-usage and blurring for a target size of
1212
#' 800 x 800px; use Inf to plot the lowest resolution available.
1313
#' @param ch image channel(s) to be used for plotting (defaults to
@@ -18,6 +18,8 @@
1818
#' specifies channel-wise contrast limits - defaults to [0, 1] for all
1919
#' (ignored when \code{image(x, i)} is an RGB image;
2020
#' for convenience, any NULL = [0, 1], and n = [0, n]).
21+
#' @param t,z integer scalar to indicate a specific time- or z-slice;
22+
#' if left unspecified (default NULL), will perform a max-projection.
2123
#'
2224
#' @return ggplot
2325
#'
@@ -84,7 +86,7 @@ NULL
8486
n <- length(c)
8587
if (n < d) stop(
8688
"Only ", n, " default colors available, ",
87-
"but", d, " are needed; please specify 'c'")
89+
"but ", d, " are needed; please specify 'c'")
8890
c <- c[seq_len(d)]
8991
}
9092
}
@@ -117,8 +119,8 @@ NULL
117119
if (dt %in% names(.DTYPE_MAX_VALUES)) {
118120
a <- a / .DTYPE_MAX_VALUES[dt]
119121
} else if (max(a) > 1) {
120-
for (i in seq_len(d))
121-
a[i,,] <- a[i,,] / max(a[i,,])
122+
maxs <- apply(a, 1, max)
123+
a <- sweep(a, MARGIN = 1, STATS = maxs, FUN = "/")
122124
}
123125
return(a)
124126
}
@@ -159,33 +161,56 @@ NULL
159161
#' @importFrom methods as
160162
#' @importFrom DelayedArray realize
161163
#' @importFrom spatialdataR data_type
162-
.df_i <- \(x, k=NULL, ch=NULL, c=NULL, cl=NULL) {
164+
.df_i <- \(x, k=NULL, ch=NULL, t=NULL, c=NULL, cl=NULL, z=NULL) {
163165
a <- .get_ms_data(x, k)
166+
axisNames <- axes(x, "name")
164167
# 2D max-projection
165168
a <- .project(x, a)
166-
# subset channels of interest
167-
a <- a[.ch_idx(x, ch),,,drop=FALSE]
169+
axisNames <- axisNames[axisNames != "z"]
170+
ti <- which(axisNames == "t")
171+
tn <- length(ti)
172+
# subset channels and timepoint of interest
173+
if (tn) {
174+
if (is.null(t)) {
175+
t <- 1
176+
} else if (length(t) > 1) {
177+
stop("Only a single timepoint can be selected")
178+
}
179+
}
180+
a <- .subset_array_by_axes(a=a, axisNames=axisNames,
181+
c=.ch_idx(x, ch), t=t, drop=FALSE)
182+
# remove time axis if it exists
183+
if (tn) {
184+
dim(a) <- dim(a)[axisNames != "t"]
185+
axisNames <- axisNames[-ti]
186+
}
187+
# if no channel axis, add dummy axis
188+
if (!("c" %in% axisNames)) {
189+
dim(a) <- c(1, dim(a))
190+
axisNames <- c("c", axisNames)
191+
}
168192
a <- .norm_ia(a, data_type(x))
169193
# color merging & contrasts
170194
a <- .prep_ia(a, c, cl)
171195
}
172196

197+
#' @importFrom rlang .data
173198
#' @importFrom ggplot2 guides geom_point geom_blank annotation_raster
174199
#' @importFrom ggplot2 scale_color_identity scale_x_continuous scale_y_reverse
175200
.gg_i <- \(x, w, h, pal=NULL) {
176201
l <- if (!is.null(names(pal))) list(
177202
guides(col=guide_legend(override.aes=list(alpha=1, size=2))),
178203
geom_point(aes(col=.data$foo), data.frame(foo=pal), x=0, y=0, alpha=0))
179204
list(l,
180-
geom_blank(aes(x=x, y=y), data.frame(x=w, y=h)),
205+
geom_blank(aes(x=.data$x, y=.data$y), data.frame(x=w, y=h)),
181206
annotation_raster(x, w[1],w[2], h[2],h[1], interpolate=FALSE),
182207
scale_color_identity(NULL, guide="legend", breaks=pal, labels=names(pal)),
183208
ggnewscale::new_scale_color())
184209
}
185210

186211
#' @rdname plotImage
187212
#' @export
188-
setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl=NULL) {
213+
setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl=NULL, t=NULL, z=NULL) {
189214
if (is.numeric(i))
190215
i <- imageNames(x)[i]
191216
y <- image(x, i)
@@ -197,7 +222,7 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl
197222
ch <- ch %||% channels(y)
198223
cl <- cl %||% c(0, 1/3)
199224
}
200-
df <- .df_i(y, k, ch, c, cl)
225+
df <- .df_i(y, k, ch, t, c, cl, z)
201226
pal <- c %||% .DEFAULT_COLORS
202227
if (dim(y)[1] > 1 && !.is_rgb(y)) {
203228
nms <- unlist(channels(y))[idx <- .ch_idx(y, ch)]

0 commit comments

Comments
 (0)