Skip to content

Commit

Permalink
fix: makePseudoVisium()
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajic committed Oct 30, 2024
1 parent 4fb1b6a commit 61dbb56
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# GiottoClass 0.4.2 (2024/10/30)

## bug fixes
- fix y spacing of `makePseudoVisium()`

## changes
- `makePseudoVisium()` `micron_scale` (multiplicative scalefactor to get micron scaled values) supercedes `micron_size` which used the inverse.


# GiottoClass 0.4.1 (2024/10/28)

Expand Down
51 changes: 38 additions & 13 deletions R/generate_poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,26 +382,46 @@ orthoGrid <- function(extent, ccd, id_prefix = "ID_") {

#' @title makePseudoVisium
#' @name makePseudoVisium
#' @description Generates a pseudo-visium grid of spots across a provided
#' spatial extent
#' @description Generates a visium-like array of spots across a provided
#' spatial extent.
#' @param extent SpatExtent or anything else a SpatExtent can be extracted or
#' created from
#' @param micron_size size of a micrometer relative to spatial coordinates
#' @param micron_scale scalefactor needed to convert the target coordinate
#' space to microns. For supported datasets, this can be found from
#' `instructions(gobject, "micron_scale")`. See details.
#' @param micron_size deprecated. Use `micron_scale`
#' @param name character. (default is 'pseudo_visium') Name of giottoPolygon
#' object to create
#' @details This function generates a pseudo-Visium grid of spots based on the
#' input spatial locations. The micron_size param is used to determine the size
#' of the spots
#' @details This function generates a pseudo-Visium array of spots across the
#' spatial extent provided. The `micron_size` param is used to determine the
#' scaling of the array relative to the target coordinate system.
#'
#' @section `micron_scale`:
#' If `a` is microns and `b` is dataset coordinate units, `micron_scale` is
#' calculated as `a / b`.
#' @returns A giottoPolygon for the pseudo-visium spots.
#' @examples
#' e <- ext(0, 2000, 0, 2000)
#' x <- makePseudoVisium(extent = e, micron_size = 1)
#' x <- makePseudoVisium(extent = e, micron_scale = 1)
#' plot(x)
#' @concept spatial location
#' @export
makePseudoVisium <- function(extent = NULL,
micron_size = 1,
micron_scale = 1,
micron_size = deprecated(),
name = "pseudo_visium") {

if (is_present(micron_size)) {
micron_size <- 1 / micron_size
}

micron_scale <- deprecate_param(
x = micron_size,
y = micron_scale,
when = "0.4.2",
fun = "makePseudoVisium"
)

e <- ext(extent)[]

# Visium default scale parameters
Expand All @@ -410,27 +430,32 @@ makePseudoVisium <- function(extent = NULL,
visium_gap_um <- 45

# Compute metrics to visium scale
radius <- visium_radius_um / micron_size
cc <- visium_center_center_dist_um / micron_scale
radius <- visium_radius_um / micron_scale
gap <- (visium_gap_um / visium_radius_um) * radius

# Define a data.table with the vertices of a circle centered around (0,0)
stamp_dt <- circleVertices(radius = radius, npoints = 100)

# Create a grid of y points where the circles will be centered
y_seq <- seq(e[["ymin"]] + radius, e[["ymax"]] - radius,
by = 2 * radius + gap
y_seq <- seq(
e[["ymin"]] + radius,
e[["ymax"]] - radius,
# should be sqrt(3) or 1.732051, but seems like it was rounded
by = 1.74 * (cc / 2)
)

# Stagger center point of circles to match visium staggered grid
centers <- data.table::rbindlist(lapply(seq_len(length(y_seq)), function(i) {
dt_list <- lapply(seq_len(length(y_seq)), function(i) {
x_start <- if (i %% 2 == 0) {
e[["xmin"]] + radius + (2 * radius + gap) / 2
} else {
e[["xmin"]] + radius
}
x_seq <- seq(x_start, e[["xmax"]] - radius, by = 2 * radius + gap)
data.table::data.table(sdimx = x_seq, sdimy = y_seq[i])
}))
})
centers <- data.table::rbindlist(dt_list)
centers$cell_ID <- paste0("spot_", seq_len(nrow(centers)))

# Call polyStamp function on centers to generate the pseudo-visium grid
Expand Down
31 changes: 23 additions & 8 deletions man/makePseudoVisium.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61dbb56

Please sign in to comment.