1+ library(Matrix )
2+ library(DenoIST )
3+ library(SpatialExperiment )
4+ library(SingleCellExperiment )
5+ library(anndataR )
6+ library(scuttle )
7+
8+ # # VIASH START
9+ par <- list (
10+ " input_spatial_with_cell_types" = " task_ist_preprocessing/resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_aggregated_counts.h5ad" ,
11+ " input_tx" = " mouse_combined_transcripts.csv" ,
12+ " output" = " task_ist_preprocessing/tmp/split_corrected.h5ad" ,
13+ # "keep_all_cells" = FALSE,
14+ " distance" = 50 ,
15+ " nbins" = 200 ,
16+ )
17+
18+ meta <- list (
19+ ' cpus' : 4 ,
20+ )
21+
22+ # # VIASH END
23+
24+ # Read the input h5ad file and convert to SingleCellExperiment and Seurat
25+ sce <- read_h5ad(par $ input_spatial_with_cell_types , as = " SingleCellExperiment" )
26+ spe <- SpatialExperiment(
27+ assay = counts(sce ),
28+ colData = colData(sce ),
29+ spatialCoordsNames = c(" centroid_x" , " centroid_y" ))
30+
31+ tx <- read.csv(par $ input_tx )
32+
33+ # filter out 0 cells
34+ # if (!par$keep_all_cells) {
35+ # cat("Filtering cells with 0 counts\n")
36+ # sce <- sce[, colSums(counts(sce)) > 0]
37+ # xe <- subset(xe, subset = nCount_RNA > 0)
38+ # }
39+
40+
41+ # check cores
42+ cores <- 1
43+ if (" cpus" %in% names(meta ) && ! is.null(meta $ cpus )) cores <- meta $ cpus
44+ cat(sprintf(" Number of cores: %s\n " , cores ))
45+
46+ # Run the algorithm
47+
48+ res <- denoist(mat = spe ,
49+ tx = tx ,
50+ feature_label = " feature_name" ,
51+ coords = NULL ,
52+ distance = par $ distance , nbins = par $ nbins , cl = cores ) # TODO add in params
53+
54+ # format name
55+ corrected_counts <- res $ adjusted_counts
56+
57+ # create corrected counts layer in original SingleCell object
58+ cat(" Normalizing counts\n " )
59+
60+ # First copy in counts
61+ assay(sce , " corrected_counts" ) <- assay(sce , " counts" )
62+
63+ # Then, replace only the updated cells
64+ assay(sce , " corrected_counts" )[rownames(corrected_counts ), colnames(corrected_counts )] <- corrected_counts
65+
66+ # Library size normalization - see note in resolVI
67+ size_factors <- librarySizeFactors(assay(sce , " corrected_counts" ))
68+ assay(sce , " normalized" ) <- assay(logNormCounts(sce , size_factors = size_factors , assay.type = " corrected_counts" )," logcounts" )
69+
70+ # Write the final object to h5ad format
71+ cat(" Writing to h5ad\n " )
72+ dir.create(dirname(par $ output ), showWarnings = FALSE , recursive = TRUE )
73+ write_h5ad(sce , par $ output , mode = " w" )
0 commit comments