
Apply morphological transformations to identify connected outlier regions
Source:R/edge_helper_functions_visium.R
focal_transformations.RdPerforms a series of focal operations to clean and connect outlier regions in spatial transcriptomics data through morphological operations.
Details
The function applies four sequential morphological operations:
3x3fill: Fills spots completely surrounded by outliers5x5outline: Fills spots outlined by outliers in larger windowStar pattern: Fills spots with outliers in cardinal directions
Small cluster removal: Removes isolated normal regions below threshold
Examples
library(terra)
#> terra 1.8.93
#>
#> Attaching package: ‘terra’
#> The following objects are masked from ‘package:SummarizedExperiment’:
#>
#> distance, nearest, shift, trim, values, values<-, width
#> The following objects are masked from ‘package:GenomicRanges’:
#>
#> distance, gaps, nearest, shift, trim, values, values<-, width
#> The following objects are masked from ‘package:IRanges’:
#>
#> distance, gaps, nearest, shift, trim, width
#> The following objects are masked from ‘package:S4Vectors’:
#>
#> values, values<-, width
#> The following object is masked from ‘package:BiocGenerics’:
#>
#> width
#> The following object is masked from ‘package:generics’:
#>
#> interpolate
# Create a 5x5 mock raster object with an outlier (1)
m <- matrix(0, nrow = 5, ncol = 5)
m[2, 2] <- 1 # A single outlier
r <- terra::rast(m) # Use terra instead of raster
# Apply morphological cleaning
r_cleaned <- focal_transformations(r, min_cluster_size = 3)
# See the original and cleaned values
print(terra::values(r))
#> lyr.1
#> [1,] 0
#> [2,] 0
#> [3,] 0
#> [4,] 0
#> [5,] 0
#> [6,] 0
#> [7,] 1
#> [8,] 0
#> [9,] 0
#> [10,] 0
#> [11,] 0
#> [12,] 0
#> [13,] 0
#> [14,] 0
#> [15,] 0
#> [16,] 0
#> [17,] 0
#> [18,] 0
#> [19,] 0
#> [20,] 0
#> [21,] 0
#> [22,] 0
#> [23,] 0
#> [24,] 0
#> [25,] 0
print(terra::values(r_cleaned))
#> lyr.1
#> [1,] 0
#> [2,] 0
#> [3,] 0
#> [4,] 0
#> [5,] 0
#> [6,] 0
#> [7,] 1
#> [8,] 0
#> [9,] 0
#> [10,] 0
#> [11,] 0
#> [12,] 0
#> [13,] 0
#> [14,] 0
#> [15,] 0
#> [16,] 0
#> [17,] 0
#> [18,] 0
#> [19,] 0
#> [20,] 0
#> [21,] 0
#> [22,] 0
#> [23,] 0
#> [24,] 0
#> [25,] 0