Skip to contents

Update a Seurat object created with semla for compatibility with Seurat's spatial functions

Usage

UpdateSeuratFromSemla(object, image_use = "raw", verbose = TRUE)

Arguments

object

An object of class Seurat with Visium data, created with semla

image_use

String specifying image type to use, either "raw" or "transformed"

verbose

Print messages

Value

A Seurat object compatible with with Seurat's spatial functions

Examples


library(semla)

# Load example Visium data
se_mbrain <- readRDS(system.file("extdata/mousebrain", "se_mbrain", package = "semla"))
se_mcolon <- readRDS(system.file("extdata/mousecolon", "se_mcolon", package = "semla"))
se_merged <- MergeSTData(se_mbrain, se_mcolon)

# Update object
se_merged <- UpdateSeuratFromSemla(se_merged)
#> 
#> ── Adding @images data from Staffli object 
#> 
#>  Loading images
#>  Converting raw coordinates
#>  Converting scale factors
#>  Creating VisiumV1 slices
#>  Storing VisiumV1 slices in @images slot
#> 
#>  Returning updated Seurat object.

# Use Seurat functions
SpatialFeaturePlot(se_merged, features = "Nrgn")


# Transform images in object
se_merged <- LoadImages(se_merged)
#> 
#> ── Loading H&E images ──
#> 
#>  Loading image from /private/var/folders/8r/bfm2m_q17znfk0m_8dt330tm0000gp/T/RtmpLHpYU7/temp_libpath305b1609463d/semla/extdata/mousebrain/spatial/tissue_lowres_image.jpg
#>  Scaled image from 600x565 to 400x377 pixels
#>  Loading image from /private/var/folders/8r/bfm2m_q17znfk0m_8dt330tm0000gp/T/RtmpLHpYU7/temp_libpath305b1609463d/semla/extdata/mousecolon/spatial/tissue_lowres_image.jpg
#>  Scaled image from 541x600 to 400x444 pixels
#>  Saving loaded H&E images as 'rasters' in Seurat object
rotation_angle <- get_array_rotation(se_merged, grid_pattern = "hexagonal")
#>  Finding rotation angle for sample 1
#> →    Scaling array coordinates
#> →    Scaling pixel coordinates
#> →    Running Kabsch algorithm
#> →    The rotation angle between the array and pixel coordinates is 0.101 degrees
#>  Finding rotation angle for sample 2
#> →    Scaling array coordinates
#> →    Scaling pixel coordinates
#> →    Running Kabsch algorithm
#> →    The rotation angle between the array and pixel coordinates is 0.254 degrees
transforms <- rbind(generate_rigid_transform(sampleID = 1L, angle = rotation_angle$`1`),
                    generate_rigid_transform(sampleID = 2L, angle = 12))
se_merged <- RigidTransformImages(se_merged, transforms = transforms)
#> 
#> ── Transforming images ──
#> 
#>  Found transformations for sample(s): 1, 2
#>  Transforming image 1
#>  Fetched spot coordinates
#>  Supplied transformations are valid
#> →   Mirror along x-axis: FALSE
#> →   Mirror along y-axis: FALSE
#> →   Rotation angle: 0.1
#> →   Translation along x axis: 0%
#> →   Translation along y axis: 0%
#> →   Scaling factor: 1
#>  Returning transformed image
#>  Transforming image 2
#>  Fetched spot coordinates
#>  Supplied transformations are valid
#> →   Mirror along x-axis: FALSE
#> →   Mirror along y-axis: FALSE
#> →   Rotation angle: 12
#> →   Translation along x axis: 0%
#> →   Translation along y axis: 0%
#> →   Scaling factor: 1
#>  Returning transformed image
#>  Image transformation complete.

# Update object
se_raw <- UpdateSeuratFromSemla(se_merged, image_use = "transformed")
#> 
#> ── Adding @images data from Staffli object 
#> 
#>  Loading transformed raster
#>  Converting transformed coordinates
#>  Converting scale factors
#>  Creating VisiumV1 slices
#>  Storing VisiumV1 slices in @images slot
#> 
#>  Returning updated Seurat object.
se_transformed <- UpdateSeuratFromSemla(se_merged, image_use = "raw")
#> 
#> ── Adding @images data from Staffli object 
#> 
#>  Loading images
#>  Converting raw coordinates
#>  Converting scale factors
#>  Creating VisiumV1 slices
#>  Storing VisiumV1 slices in @images slot
#> 
#>  Returning updated Seurat object.

# Use Seurat functions
p1 <- SpatialFeaturePlot(se_raw, features = "Nrgn")
p2 <- SpatialFeaturePlot(se_transformed, features = "Nrgn")
p1 | p2