Skip to content

Commit 0c38992

Browse files
author
Habib Rehman
committed
denoist, but transcripts aren't working yet
1 parent f995cb8 commit 0c38992

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
__merge__: /src/api/comp_method_expression_correction.yaml
2+
3+
name: denoist_correction
4+
label: "denoist_correction"
5+
summary: "Correct counts / remove contamination using the DenoIST methods"
6+
description: >-
7+
DenoIST is a package for denoising image-based spatial transcriptomics data. It takes a IST count matrix and returns a adjusted count matrix with contamination removed.
8+
links:
9+
documentation: "https://github.com/aaronkwc/DenoIST"
10+
repository: "https://github.com/aaronkwc/DenoIST"
11+
references:
12+
doi: "10.1101/2025.11.13.688387"
13+
14+
arguments:
15+
- name: --celltype_key
16+
required: false
17+
direction: input
18+
type: string
19+
default: cell_type
20+
21+
- name: --nbins
22+
required: false
23+
direction: input
24+
type: integer
25+
default: 200
26+
27+
- name: --distance
28+
required: false
29+
direction: input
30+
type: integer
31+
default: 50
32+
33+
34+
resources:
35+
- type: r_script
36+
path: script.R
37+
38+
engines:
39+
- type: docker
40+
image: openproblems/base_r:1
41+
setup:
42+
#- type: docker
43+
# run: |
44+
# apt-get update && apt-get install -y wget
45+
- type: r
46+
bioc: [anndataR, rhdf5, scuttle, DenoIST, devtools]
47+
#- type: r
48+
# bioc: [SummarizedExperiment,SingleCellExperiment,SpatialExperiment]
49+
# bioc_force_install: true
50+
# - type: docker
51+
# run: |
52+
# Rscript -e "BiocManager::install('SingleCellExperiment', type = 'source', force = TRUE, ask = FALSE); devtools::install_github('aaronkwc/DenoIST')"
53+
54+
# SingleCellExperiment part can probably be left out again in the future. It currently fixes a bug described in these issues:
55+
# https://github.com/drighelli/SpatialExperiment/issues/171
56+
# https://github.com/satijalab/seurat/issues/9889
57+
# The reinstall of SingleCellExperiment triggers the correct re-install of SpatialExperiment.
58+
59+
# Is there a better way to install an r package from github?
60+
# The 6 million timeout thing stops it from breaking
61+
62+
- type: native
63+
64+
runners:
65+
- type: executable
66+
- type: nextflow
67+
directives:
68+
label: [ hightime, highcpu, highmem ]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)