|
| 1 | +library(SpaNorm) |
| 2 | +library(SingleCellExperiment) |
| 3 | +library(SpatialExperiment) |
| 4 | +library(zellkonverter) |
| 5 | + |
| 6 | +## VIASH START |
| 7 | +par <- list( |
| 8 | + "input_spatial_aggregated_counts" = 'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_aggregated_counts.h5ad', |
| 9 | + "output" = 'tmp/spatial_spanormed_counts.h5ad' |
| 10 | +) |
| 11 | +## VIASH END |
| 12 | + |
| 13 | +# Read the input h5ad file and convert to SingleCellExperiment |
| 14 | +sce <- readH5AD(par$input_spatial_aggregated_counts) |
| 15 | +# Convert to SpatialExperiment for SpaNorm |
| 16 | +sce <- as(sce, "SpatialExperiment") |
| 17 | + |
| 18 | +# Extract spatial coordinates |
| 19 | +centroid_x <- colData(sce)$centroid_x |
| 20 | +centroid_y <- colData(sce)$centroid_y |
| 21 | + |
| 22 | +# Create spatial coordinates matrix for SpaNorm |
| 23 | +spatial_coords <- matrix(c(centroid_x, centroid_y), ncol = 2) |
| 24 | + |
| 25 | +# Set spatial coordinates in the SpatialExperiment object |
| 26 | +spatialCoords(sce) <- spatial_coords |
| 27 | + |
| 28 | +# Apply SpaNorm normalization to the spatial data |
| 29 | +result <- SpaNorm(sce) |
| 30 | + |
| 31 | +# Get the normalized matrix from SpaNorm result (log-transformed normalized counts) |
| 32 | +normalized_matrix <- assay(result, "logcounts") |
| 33 | + |
| 34 | +# Create final SCE with all original layers preserved |
| 35 | +final_sce <- SingleCellExperiment( |
| 36 | + assays = assays(sce), # Preserve all original assays |
| 37 | + rowData = rowData(sce), |
| 38 | + colData = colData(sce) |
| 39 | +) |
| 40 | + |
| 41 | +# Add the normalized matrix as a new layer called 'normalized' |
| 42 | +assay(final_sce, "normalized") <- normalized_matrix |
| 43 | + |
| 44 | +# Write the final object to h5ad format |
| 45 | +zellkonverter::writeH5AD(final_sce, par$output) |
0 commit comments