Skip to content

Commit 273a4be

Browse files
author
Habib Rehman
committed
Added RCTD
1 parent 93c2914 commit 273a4be

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
dir.create(dirname(par$output), showWarnings = FALSE, recursive = TRUE)
67+
write_h5ad(sce, par$output)

0 commit comments

Comments
 (0)