Spatial Transcriptomics Cell Type Deconvolution

Author

Ahmed M. Elhossiny

1 Setup & Data Loading

Here we load essential packages and visium_samples_manifest.xlsx from the data directory, that includes information about visium samples. We will use Robust Cell Type Deconvolution (RCTD) (Cable et al. 2022) for the deconvolution.

Code
suppressPackageStartupMessages({
  library(spacexr)
  library(Seurat)
  library(Matrix)
  library(doParallel)
  library(ggplot2)
  library(tidyverse)
  library(slurmR)
  library(qs)
})

outputDir <- "../outputs/Deconvolution/RCTD"
dir.create(outputDir, recursive = T, showWarnings = F)
samples_info <- readxl::read_xlsx("../data/visium_samples_manifest.xlsx")

2 Reference Creation

  • Here we create a reference object for the RCTD analysis using the filtered and annotated single-cell RNA-seq data created using the scRNAseq workflow described here. The scRNAseq reference object can be downloaded from Zenodo here.

  • We use slurmR to parallelize the RCTD analysis across multiple samples on U-M HPC, alternatively user can use lapply to run the analysis locally. The results are saved as .rds objects that will be loaded and added to the integrated seurat spatial object in Clustering Analysis

Code
scRef <- qread("../outputs/scRNAseq_Analysis/scRef.qs")

scRef <- Reference(
  counts = GetAssayData(scRef, assay = 'RNA', layer = 'count'),
  n_max_cells = max(table(scRef$cellType)),
  cell_types = factor(scRef$cellType)
)

slurm_jobs <- Slurm_lapply(
  njobs = length(samples),
  job_name = "RCTD",
  tmp_path = getwd(),
  plan = "none",
  sbatch_opt = list(
    partition = 'standard',
    account = '',
    mem = '180G',
    "cpus-per-task" = "8",
    time = "24:00:00"
  ),
  export = c("scRef"),
  overwrite = TRUE,
  X = as.list(samples),
  FUN = function(x) {
    sample <- readRDS(paste0(
      "../outputs/Preprocessing/spatial_seurat_objects/",
      x,
      ".rds"
    ))
    sample_coord <- GetTissueCoordinates(sample)[, c("x", "y")]
    spatial_obj <- SpatialRNA(
      coords = sample_coord,
      counts = round(GetAssayData(sample, assay = 'Spatial', layer = 'counts'))
    )
    rctd_res <- create.RCTD(
      spatial_obj,
      scRef,
      max_cores = parallel::detectCores()
    ) %>%
      run.RCTD(doublet_mode = 'multi')

    saveRDS(rctd_res, paste0('../outputs/Deconvolution/RCTD/', x, ".rds"))
    return(NULL)
  }
)
sbatch(slurm_jobs)

3 Session Info

Code
sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS 26.0

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Detroit
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.4.0    fastmap_1.2.0     cli_3.6.2        
 [5] tools_4.4.0       htmltools_0.5.8.1 yaml_2.3.8        rmarkdown_2.27   
 [9] knitr_1.47        jsonlite_1.8.8    xfun_0.52         digest_0.6.35    
[13] rlang_1.1.3       evaluate_0.23    

References

Cable, Dylan M, Evan Murray, Luli S Zou, Aleksandrina Goeva, Evan Z Macosko, Fei Chen, and Rafael A Irizarry. 2022. “Robust Decomposition of Cell Type Mixtures in Spatial Transcriptomics.” Nature Biotechnology 40 (4): 517–26.