Skip to content

Commit 1f8e696

Browse files
habibrehman2002Habib RehmanLouisK92
authored
Add rctd (#119)
* Added RCTD * allowed for overwriting h5ad * Rename rctd dir and add method to workflow --------- Co-authored-by: Habib Rehman <[email protected]> Co-authored-by: LouisK92 <[email protected]>
1 parent 142e0f5 commit 1f8e696

File tree

9 files changed

+122
-3
lines changed

9 files changed

+122
-3
lines changed

scripts/run_benchmark/run_full_local.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ celltype_annotation_methods:
6060
# - mapmycells
6161
# - tangram
6262
# - singler
63+
# - rctd
6364
expression_correction_methods:
6465
- no_correction
6566
# - gene_efficiency_correction

scripts/run_benchmark/run_full_seqeracloud.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ celltype_annotation_methods:
5252
- mapmycells
5353
- tangram
5454
- singler
55+
- rctd
5556
expression_correction_methods:
5657
- no_correction
5758
- gene_efficiency_correction

scripts/run_benchmark/run_test_local.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ celltype_annotation_methods:
5555
# - mapmycells
5656
# - tangram
5757
# - singler
58+
# - rctd
5859
expression_correction_methods:
5960
- no_correction
6061
# - gene_efficiency_correction

scripts/run_benchmark/run_test_seqeracloud.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ celltype_annotation_methods:
5151
- mapmycells
5252
- tangram
5353
- singler
54+
- rctd
5455
expression_correction_methods:
5556
- no_correction
5657
- gene_efficiency_correction
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
__merge__: /src/api/comp_method_cell_type_annotation.yaml
2+
3+
name: rctd
4+
label: "RCTD"
5+
summary: "Annotate cells using the RCTD method"
6+
description: "Robust Cell Type Decomposition (RCTD) inputs a spatial transcriptomics dataset, which consists of a set of pixels, which are spatial locations that measure RNA counts across many genes. RCTD additionally uses a single cell RNA-seq (scRNA-seq) dataset, which is labeled for cell types. RCTD learns cell type profiles from the scRNA-seq dataset, and uses these to label the spatial transcriptomics pixels as cell types."
7+
links:
8+
documentation: "https://github.com/dmcable/spacexr"
9+
repository: "https://github.com/dmcable/spacexr"
10+
references:
11+
doi: "10.1038/s41587-021-00830-w"
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: docker
22+
# run: |
23+
# apt-get update && apt-get install -y wget
24+
- type: r
25+
bioc: [anndataR, rhdf5, devtools]
26+
#- type: r
27+
# bioc: [SummarizedExperiment,SingleCellExperiment,SpatialExperiment]
28+
# bioc_force_install: true
29+
- type: docker
30+
run: |
31+
Rscript -e "BiocManager::install('SingleCellExperiment', type = 'source', force = TRUE, ask = FALSE); devtools::install_github('dmcable/spacexr', build_vignettes = FALSE)"
32+
33+
# This can probably be left out again in the future. It currently fixes a bug described in these issues:
34+
# https://github.com/drighelli/SpatialExperiment/issues/171
35+
# https://github.com/satijalab/seurat/issues/9889
36+
# The reinstall of SingleCellExperiment triggers the correct re-install of SpatialExperiment.
37+
38+
# spacexr -> is there a better way to install an r package from github?
39+
- type: native
40+
41+
runners:
42+
- type: executable
43+
- type: nextflow
44+
directives:
45+
label: [ hightime, midcpu, highmem ]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
library(spacexr)
2+
library(Matrix)
3+
library(SingleCellExperiment)
4+
library(anndataR)
5+
6+
## VIASH START
7+
par <- list(
8+
"input_spatial_normalized_counts" = "task_ist_preprocessing/resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_aggregated_counts.h5ad",
9+
"input_scrnaseq_reference"= "task_ist_preprocessing/resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad",
10+
"output" = "task_ist_preprocessing/tmp/spatial_types.h5ad"
11+
)
12+
13+
meta <- list(
14+
'cpus': 4,
15+
)
16+
17+
## VIASH END
18+
19+
# Read the input h5ad file and convert to SingleCellExperiment
20+
sce <- read_h5ad(par$input_spatial_normalized_counts, as = "SingleCellExperiment")
21+
22+
# Extract spatial coordinates and counts matrix
23+
centroid_x <- colData(sce)$centroid_x
24+
centroid_y <- colData(sce)$centroid_y
25+
coords <- data.frame(centroid_x, centroid_y)
26+
counts <- assay(sce,"counts")
27+
rownames(coords) <- colData(sce)$cell_id
28+
puck <- SpatialRNA(coords, counts)
29+
30+
# Read reference scrnaseq
31+
ref <- read_h5ad(par$input_scrnaseq_reference, as = "SingleCellExperiment")
32+
33+
#filter reference cell types to those with >25 cells
34+
valid_celltypes <- names(table(colData(ref)$cell_type))[table(colData(ref)$cell_type) >= 25]
35+
filtered_ref <- ref[,colData(ref)$cell_type %in% valid_celltypes]
36+
37+
ref_counts <- assay(filtered_ref, "counts")
38+
# factor to drop filtered cell types
39+
colData(filtered_ref)$cell_type <- factor(colData(filtered_ref)$cell_type)
40+
cell_types <- colData(filtered_ref)$cell_type
41+
names(cell_types) <- colnames(ref_counts)
42+
reference <- Reference(ref_counts, cell_types, min_UMI = 0)
43+
44+
# check cores
45+
cores <- 1
46+
if ("cpus" %in% names(meta) && !is.null(meta$cpus)) cores <- meta$cpus
47+
cat(sprintf("Number of cores: %s\n", cores))
48+
49+
# Run the algorithm
50+
myRCTD <- create.RCTD(puck, reference, max_cores = cores)
51+
myRCTD <- run.RCTD(myRCTD, doublet_mode = "doublet")
52+
53+
# Extract results
54+
results <- myRCTD@results
55+
spatial_cell_types <- results$results_df$first_type
56+
# Include None Spatial cell type for the "reject" cells
57+
levels(spatial_cell_types) <- c(levels(spatial_cell_types), "None_sp")
58+
spatial_cell_types[results$results_df$spot_class == "reject"] <- "None_sp"
59+
names(spatial_cell_types) <- rownames(results$results_df)
60+
61+
#
62+
colData(sce)$cell_type <- "None_sp"
63+
colData(sce)[names(spatial_cell_types),"cell_type"] <- as.character(spatial_cell_types)
64+
65+
# Write the final object to h5ad format
66+
# set to 'w', is this ok?
67+
dir.create(dirname(par$output), showWarnings = FALSE, recursive = TRUE)
68+
write_h5ad(sce, par$output, mode = "w")

src/workflows/run_benchmark/config.vsh.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ argument_groups:
9898
A list of cell type annotation methods to run.
9999
type: string
100100
multiple: true
101-
default: "ssam:tacco:moscot:mapmycells:tangram:singler"
101+
default: "ssam:tacco:moscot:mapmycells:tangram:singler:rctd"
102102
- name: "--expression_correction_methods"
103103
description: |
104104
A list of expression correction methods to run.
@@ -171,6 +171,7 @@ dependencies:
171171
- name: methods_cell_type_annotation/mapmycells
172172
- name: methods_cell_type_annotation/tangram
173173
- name: methods_cell_type_annotation/singler
174+
- name: methods_cell_type_annotation/rctd
174175
- name: methods_expression_correction/no_correction
175176
- name: methods_expression_correction/gene_efficiency_correction
176177
- name: methods_expression_correction/resolvi_correction

src/workflows/run_benchmark/main.nf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ workflow run_wf {
377377
moscot,
378378
mapmycells,
379379
tangram,
380-
singler
380+
singler,
381+
rctd
381382
]
382383

383384
cta_ch = normalization_ch

0 commit comments

Comments
 (0)