Skip to content

Commit 11d6913

Browse files
authored
Merge pull request #14 from openproblems-bio/spanorm
spanorm is runable
2 parents d941f5b + 4cda75c commit 11d6913

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
__merge__: /src/api/comp_method_normalization.yaml
2+
3+
name: spanorm
4+
label: "SpaNorm normalization"
5+
summary: "Spatially-aware normalisation for spatial transcriptomics data"
6+
description: "Spatially-aware normalisation for spatial transcriptomics data"
7+
links:
8+
documentation: "https://github.com/openproblems-bio/task_ist_preprocessing"
9+
repository: "https://github.com/bhuvad/SpaNorm/tree/master"
10+
references:
11+
doi: "10.1101/2024.05.31.596908"
12+
13+
resources:
14+
- type: r_script
15+
path: script.R
16+
17+
engines:
18+
- type: docker
19+
image: openproblems/base_r:1
20+
setup:
21+
- type: r
22+
bioc: [SpaNorm, zellkonverter]
23+
#- type: r
24+
# bioc: [SummarizedExperiment,SingleCellExperiment,SpatialExperiment]
25+
# bioc_force_install: true
26+
- type: docker
27+
run: |
28+
Rscript -e "BiocManager::install('SingleCellExperiment', type = 'source', force = TRUE, ask = FALSE)"
29+
# This can probably be left out again in the future. It currently fixes a bug described in these issues:
30+
# https://github.com/drighelli/SpatialExperiment/issues/171
31+
# https://github.com/satijalab/seurat/issues/9889
32+
# The reinstall of SingleCellExperiment triggers the correct re-install of SpatialExperiment.
33+
- type: native
34+
35+
runners:
36+
- type: executable
37+
- type: nextflow
38+
directives:
39+
label: [ midtime, lowcpu, lowmem ]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)