Skip to content

Commit

Permalink
removed VisiumV1 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zskylarli committed May 23, 2024
1 parent 4972cd8 commit c4483be
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 86 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Authors@R: c(
person(given = "Yuhan", family = "Hao", email = "[email protected]", role = "ctb", comment = c(ORCID = "0000-0002-1810-0822")),
person(given = "Austin", family = "Hartman", email = "[email protected]", role = "ctb", comment = c(ORCID = "0000-0001-7278-1852")),
person(given = 'Paul', family = 'Hoffman', email = '[email protected]', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-7693-8957')),
person(given = 'Skylar', family = 'Li', email = '[email protected]', role = 'ctb', comment = c(ORCID = '0009-0002-3486-5785'))
person(given = 'Skylar', family = 'Li', email = '[email protected]', role = 'ctb', comment = c(ORCID = '0009-0002-3486-5785')),
person(given = "Gesmira", family = "Molla", email = '[email protected]', role = 'ctb', comment = c(ORCID = '0000-0002-8628-5056')),
person(given = 'Rahul', family = 'Satija', email = '[email protected]', role = 'aut', comment = c(ORCID = '0000-0001-9448-8833')),
person(given = 'Tim', family = 'Stuart', email = '[email protected]', role = 'aut', comment = c(ORCID = '0000-0002-3044-0897'))
Expand Down
131 changes: 46 additions & 85 deletions R/SEraster.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ createRasterizedObject <- function(input, out, name) {
for (i in seq_along(out$meta_rast$cellID_list)) {
meta_rast$cellID_list[i] <- paste(unlist(out$meta_rast$cellID_list[[i]]), collapse = ", ")
}

output_image_name <- paste0("ras.", resolution,".", image)
output_coordinates <- as.data.frame(out$pos_rast)

Expand All @@ -23,86 +24,46 @@ createRasterizedObject <- function(input, out, name) {
meta.data = meta_rast
)

# VisiumV1 class should be deprecated in the future; this is the hack to deal with objects created previous to V2
# VisiumV1 plotting not working with latest SeuratObject as of May 2024
if(class(input[[image]]) == 'VisiumV1') {
scale.use <- ScaleFactors(input_fov)[['lowres']]
new_coordinates <- as.data.frame(cbind(tissue = rep(1, nrow(output_coordinates)),
row = output_coordinates$x,
col = output_coordinates$y,
imagerow = ceiling(output_coordinates$x / scale.use),
imagecol = ceiling(output_coordinates$y / scale.use)))
rownames(new_coordinates) <- rownames(output_coordinates)
input_fov@scale.factors$spot <- sqrt(nrow(input[[]])/nrow(meta_rast))*input_fov@scale.factors$spot

new.fov <- new(
Class = "VisiumV1",
coordinates = new_coordinates,
assay = name,
key = Key(output_image_name, quiet = TRUE),
image = input_fov@image,
scale.factors = input_fov@scale.factors,
spot.radius = input_fov@scale.factors$spot
)
output@images[[output_image_name]]<- new.fov
}
else {
input_centroids <- input_fov[["centroids"]]
input_segmentation <- tryCatch(input_fov[["segmentation"]], error = function(e) NULL)
input_molecules <- tryCatch(input_fov[["molecules"]], error = function(e) NULL)
input_molecules <- tryCatch(input_fov[["molecules"]], error = function(e) NULL)

output_radius <- sqrt(
nrow(input[[]]) / nrow(meta_rast)
) * input_centroids@radius
# `scale_factors` will be set to `NULL` unless there a matching
# implementation of the `ScaleFactors` generic available for `input_fov`
scale_factors <- tryCatch(
ScaleFactors(input_fov),
error = function(e) {
return (NULL)
}
)

output_centroids <- CreateCentroids(
coords = output_coordinates,
nsides = input_centroids@nsides,
radius = output_radius,
theta = input_centroids@theta
)
output_radius <- sqrt(nrow(input[[]]) / nrow(meta_rast)) * ifelse(!is.null(scale_factors), Radius(input_fov, scale = NULL), input_fov[['centroids']]@radius)

output_centroids <- CreateCentroids(
coords = output_coordinates,
radius = output_radius
)

# `scale_factors` will be set to `NULL` unless there a matching
# implementation of the `ScaleFactors` generic available for `input_fov`
# (i.e if `input_fov` is `VisiumV2`)
tryCatch(
scale_factors <- ScaleFactors(input_fov),
error = function(e) {
return (NULL)
}
output_fov <- CreateFOV(
coords = output_centroids,
type = 'centroids',
molecules = input_molecules,
assay = name,
key = Key(output_image_name, quiet = TRUE)
)

if (!is.null(scale_factors)) {
scale_factors$spot <- output_radius
output_fov <- new(
Class = "VisiumV2",
boundaries = output_fov@boundaries,
molecules = output_fov@molecules,
assay = output_fov@assay,
key = output_fov@key,
image = input_fov@image,
scale.factors = scale_factors
)

if (!is.null(scale_factors)) {
output_fov <- CreateFOV(
coords = output_centroids,
type = 'centroids',
molecules = input_molecules,
assay = name,
key = Key(output_image_name, quiet = TRUE)
)
output_fov <- new(
Class = "VisiumV2",
boundaries = output_fov@boundaries,
molecules = output_fov@molecules,
assay = output_fov@assay,
key = output_fov@key,
image = input_fov@image,
scale.factors = scale_factors
)
} else {
output_boundaries <- list(
"centroids" = output_centroids,
"segmentation" = input_segmentation
)
output_fov <- CreateFOV(
coords = output_boundaries,
molecules = input_molecules,
assay = name,
key = Key(output_image_name, quiet = TRUE)
)
}
output[[output_image_name]] <- output_fov
}
}

output[[output_image_name]] <- output_fov
return(output)
}

Expand All @@ -124,7 +85,7 @@ rasterizeGeneExpression <- function(
if (is.list(input)) {
## create a common bbox
bbox_mat <- do.call(rbind, lapply(seq_along(input), function(i) {
pos <- GetTissueCoordinates(input[[i]])
pos <- GetTissueCoordinates(input[[i]],scale = NULL)
if (!is.null(names(input))) {
dataset <- names(input)[[i]]
} else {
Expand All @@ -145,7 +106,7 @@ rasterizeGeneExpression <- function(
## get Seurat object of the given index
spe <- input[[i]]
image <- ifelse(is.null(image), Images(spe, assay=assay_name)[1], image)
coords <- GetTissueCoordinates(spe, image = image)
coords <- GetTissueCoordinates(spe, image = image, scale = NULL)
if("cell" %in% colnames(coords)){
rownames(coords) <- coords$cell
}
Expand Down Expand Up @@ -173,7 +134,7 @@ rasterizeGeneExpression <- function(
} else {
## create bbox
image <- ifelse(is.null(image), Images(input, assay=assay_name)[1], image)
pos <- GetTissueCoordinates(input, image = image)
pos <- GetTissueCoordinates(input, image = image, scale = NULL)
if("cell" %in% colnames(pos)){
rownames(pos) <- pos$cell
}
Expand Down Expand Up @@ -229,7 +190,7 @@ rasterizeCellType <- function(
if (is.list(input)) {
## create a common bbox
bbox_mat <- do.call(rbind, lapply(seq_along(input), function(i) {
pos <- GetTissueCoordinates(input[[i]])
pos <- GetTissueCoordinates(input[[i]], scale = NULL)
if (!is.null(names(input))) {
dataset <- names(input)[[i]]
} else {
Expand All @@ -249,7 +210,7 @@ rasterizeCellType <- function(
## get Seurat object of the given index
spe <- input[[i]]
image <- ifelse(is.null(image), Images(spe, assay=assay_name)[1], image)
coords <- GetTissueCoordinates(spe, image = image)
coords <- GetTissueCoordinates(spe, image = image, scale = NULL)
if("cell" %in% colnames(coords)){
rownames(coords) <- coords$cell
}
Expand Down Expand Up @@ -280,7 +241,7 @@ rasterizeCellType <- function(
} else {
## create bbox
image <- ifelse(is.null(image), Images(input, assay=assay_name)[1], image)
pos <- GetTissueCoordinates(input, image = image)
pos <- GetTissueCoordinates(input, image = image, scale = NULL)
if("cell" %in% colnames(pos)){
rownames(coords) <- pos$cell
}
Expand Down Expand Up @@ -334,7 +295,7 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {

if (is.list(input)) {
pos_comb <- do.call(rbind, lapply(seq_along(input), function(i) {
pos <- GetTissueCoordinates(input[[i]])
pos <- GetTissueCoordinates(input[[i]], scale = NULL)
dataset <- ifelse(!is.null(names(input)), names(input)[[i]], i)
return(data.frame(dataset = dataset, x = pos[, 1], y = pos[, 2]))
}))
Expand All @@ -346,7 +307,7 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {

output <- unlist(lapply(input, function(spe) {
assay_name <- DefaultAssay(spe)
pos_orig <- data.frame(GetTissueCoordinates(spe))
pos_orig <- data.frame(GetTissueCoordinates(spe, scale = NULL))
colnames(pos_orig) <- c("x", "y")
stopifnot("Column 1 and 2 of the spatialCoords slot should be named x and y, respectively." = colnames(pos_orig)[1:2] == c("x", "y"))

Expand Down Expand Up @@ -374,7 +335,7 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {

} else {
assay_name <- DefaultAssay(input)
pos_orig <- GetTissueCoordinates(input)
pos_orig <- GetTissueCoordinates(input, scale = NULL)
colnames(pos_orig) <- c("x", "y")
stopifnot("Column 1 and 2 of the spatialCoords slot should be named x and y, respectively." = colnames(pos_orig)[1:2] == c("x", "y"))

Expand Down

0 comments on commit c4483be

Please sign in to comment.