Skip to content

Commit 6278779

Browse files
authored
Merge pull request #9556 from satijalab/feat/visiumv1-return
Feat/VisiumV1 return
2 parents 1c87c96 + 9616713 commit 6278779

File tree

8 files changed

+84
-15
lines changed

8 files changed

+84
-15
lines changed

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: Seurat
2-
Version: 5.1.0.9016
2+
Version: 5.1.0.9017
33
Title: Tools for Single Cell Genomics
44
Description: A toolkit for quality control, analysis, and exploration of single cell RNA sequencing data. 'Seurat' aims to enable users to identify and interpret sources of heterogeneity from single cell transcriptomic measurements, and to integrate diverse types of single cell data. See Satija R, Farrell J, Gennert D, et al (2015) <doi:10.1038/nbt.3192>, Macosko E, Basu A, Satija R, et al (2015) <doi:10.1016/j.cell.2015.05.002>, Stuart T, Butler A, et al (2019) <doi:10.1016/j.cell.2019.05.031>, and Hao, Hao, et al (2020) <doi:10.1101/2020.10.12.335331> for more details.
55
Authors@R: c(
@@ -68,7 +68,6 @@ Imports:
6868
plotly (>= 4.9.0),
6969
png,
7070
progressr,
71-
purrr,
7271
RANN,
7372
RColorBrewer,
7473
Rcpp (>= 1.0.7),

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ S3method(GetAssay,Seurat)
4949
S3method(GetImage,STARmap)
5050
S3method(GetImage,SlideSeq)
5151
S3method(GetImage,VisiumV1)
52+
S3method(GetImage,VisiumV2)
5253
S3method(GetTissueCoordinates,STARmap)
5354
S3method(GetTissueCoordinates,SlideSeq)
5455
S3method(GetTissueCoordinates,VisiumV1)
@@ -796,7 +797,6 @@ importFrom(plotly,plot_ly)
796797
importFrom(plotly,raster2uri)
797798
importFrom(png,readPNG)
798799
importFrom(progressr,progressor)
799-
importFrom(purrr,imap)
800800
importFrom(reticulate,import)
801801
importFrom(reticulate,py_module_available)
802802
importFrom(reticulate,py_set_seed)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
## Changes
4+
- Added `image.type` parameter to `Read10X_Image` enabling `VisiumV1` instances to be populated instead of instances of the default `VisiumV2` class ([#9556](https://github.com/satijalab/seurat/pull/9556))
45
- Fixed `IntegrateLayers` to respect the `dims.to.integrate` parameter.
56
- Added `stroke.size` parameter to `DimPlot` ([#8180](https://github.com/satijalab/seurat/pull/8180))
67
- Updated `RunLeiden` to use the `leidenbase` package instead of `leiden`; deprecated the `method` parameter for `RunLeiden` and `FindClusters`; updated `RunLeiden` to reset `random.seed` to 1 if the value is 0 or less ([#6792](https://github.com/satijalab/seurat/pull/6792))

R/objects.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,14 @@ GetImage.VisiumV1 <- function(
16301630
return(image)
16311631
}
16321632

1633+
1634+
#'
1635+
#' @rdname GetImage
1636+
#' @concept objects
1637+
#' @concept spatial
1638+
#' @method GetImage VisiumV2
1639+
#' @export
1640+
#'
16331641
GetImage.VisiumV2 <- GetImage.VisiumV1
16341642

16351643
#' Get Tissue Coordinates

R/preprocessing.R

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,7 @@ GetResidual <- function(
510510
#' @return A \code{Seurat} object
511511
#'
512512
#' @importFrom png readPNG
513-
#' @importFrom grid rasterGrob
514513
#' @importFrom jsonlite fromJSON
515-
#' @importFrom purrr imap
516514
#'
517515
#' @export
518516
#' @concept preprocessing
@@ -1213,6 +1211,7 @@ Read10X_h5 <- function(filename, use.names = TRUE, unique.features = TRUE) {
12131211
#' @param slice Name for the image, used to populate the instance's key
12141212
#' @param filter.matrix Filter spot/feature matrix to only include spots that
12151213
#' have been determined to be over tissue
1214+
#' @param image.type Image type to return, one of: "VisiumV1" or "VisiumV2"
12161215
#'
12171216
#' @return A \code{\link{VisiumV2}} object
12181217
#'
@@ -1226,37 +1225,68 @@ Read10X_Image <- function(
12261225
image.name = "tissue_lowres_image.png",
12271226
assay = "Spatial",
12281227
slice = "slice1",
1229-
filter.matrix = TRUE
1228+
filter.matrix = TRUE,
1229+
image.type = "VisiumV2"
12301230
) {
1231+
# Validate the `image.type` parameter.
1232+
image.type <- match.arg(image.type, choices = c("VisiumV1", "VisiumV2"))
1233+
1234+
# Read in the H&E stain image.
12311235
image <- png::readPNG(
12321236
source = file.path(
12331237
image.dir,
12341238
image.name
12351239
)
12361240
)
12371241

1238-
# read in the scale factors
1242+
# Read in the scale factors.
12391243
scale.factors <- Read10X_ScaleFactors(
12401244
filename = file.path(image.dir, "scalefactors_json.json")
12411245
)
12421246

1243-
# read in the tissue coordinates as a data.frame
1247+
# Read in the tissue coordinates as a data.frame.
12441248
coordinates <- Read10X_Coordinates(
12451249
filename = Sys.glob(file.path(image.dir, "*tissue_positions*")),
12461250
filter.matrix
12471251
)
1248-
# create an `sp` compatible `FOV` instance
1252+
1253+
# Use the `slice` value to populate a Seurat-style identifier for the image.
1254+
key <- Key(slice, quiet = TRUE)
1255+
1256+
# Return the specified `image.type`.
1257+
if (image.type == "VisiumV1") {
1258+
visium.v1 <- new(
1259+
Class = image.type,
1260+
assay = assay,
1261+
key = key,
1262+
coordinates = coordinates,
1263+
scale.factors = scale.factors,
1264+
image = image
1265+
)
1266+
1267+
# As of v5.1.0 `Radius.VisiumV1` no longer returns the value of the
1268+
# `spot.radius` slot and instead calculates the value on the fly, but we
1269+
# can populate the static slot in case it's depended on.
1270+
visium.v1@spot.radius <- Radius(visium.v1)
1271+
1272+
return(visium.v1)
1273+
}
1274+
1275+
# If `image.type` is not "VisiumV1" then it must be "VisiumV2".
1276+
stopifnot(image.type == "VisiumV2")
1277+
1278+
# Create an `sp` compatible `FOV` instance.
12491279
fov <- CreateFOV(
12501280
coordinates[, c("imagerow", "imagecol")],
12511281
type = "centroids",
12521282
radius = scale.factors[["spot"]],
12531283
assay = assay,
1254-
key = Key(slice, quiet = TRUE)
1284+
key = key
12551285
)
12561286

1257-
# build the final `VisiumV2` - essentially just adding `image` and
1258-
# `scale.factors` to the object
1259-
visium.fov <- new(
1287+
# Build the final `VisiumV2` instance, essentially just adding `image` and
1288+
# `scale.factors` to the `fov`.
1289+
visium.v2 <- new(
12601290
Class = "VisiumV2",
12611291
boundaries = fov@boundaries,
12621292
molecules = fov@molecules,
@@ -1266,7 +1296,7 @@ Read10X_Image <- function(
12661296
scale.factors = scale.factors
12671297
)
12681298

1269-
return(visium.fov)
1299+
return(visium.v2)
12701300
}
12711301

12721302
#' Load 10X Genomics Visium Tissue Positions

man/GetImage.Rd

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

man/Read10X_Image.Rd

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

tests/testthat/test_load_10X.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ test_that("Read10X_Image works as expected", {
132132
)
133133
# the size of the two images should be different
134134
expect_false(all(dim(image.hires) == dim(image.lowres)))
135+
136+
# `VisiumV1` image
137+
image.v1 <- Read10X_Image(
138+
path.to.image,
139+
image.name = "tissue_lowres_image.png",
140+
image.type = "VisiumV1"
141+
)
142+
coordinates <- GetTissueCoordinates(image.v1, scale = "lowres")
143+
spot.radius <- Radius(image.v1, scale = "lowres")
144+
scale.factors <- ScaleFactors(image.v1)
145+
# check that the scale factors were read in as expected
146+
expect_true(identical(scale.factors, scale.factors.expected))
147+
# check that `coordinates` contains values scaled for the low resolution PNG
148+
# also make sure that it has the expected column names
149+
coordinates.expected.v1 <- coordinates.expected
150+
colnames(coordinates.expected.v1) <- c("imagerow", "imagecol")
151+
expect_equal(
152+
coordinates[, c("imagerow", "imagecol")] / scale.factors[["lowres"]],
153+
coordinates.expected.v1
154+
)
155+
# check that the spot size is similarly scaled
156+
expect_equal(
157+
(spot.radius / scale.factors[["lowres"]] * max(dim(image.lowres))),
158+
scale.factors.expected[["spot"]],
159+
)
135160
}
136161
})
137162

0 commit comments

Comments
 (0)