Skip to contents

spacedeconv is a unified interface for the deconvolution of spatial transcriptomics data. In total spacedeconv gives access to 31 deconvolution methods:

#> Loading required package: SingleCellExperiment
#> Loading required package: SummarizedExperiment
#> Loading required package: MatrixGenerics
#> Loading required package: matrixStats
#> 
#> Attaching package: 'MatrixGenerics'
#> The following objects are masked from 'package:matrixStats':
#> 
#>     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
#>     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
#>     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
#>     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
#>     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
#>     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
#>     colWeightedMeans, colWeightedMedians, colWeightedSds,
#>     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
#>     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
#>     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
#>     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
#>     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
#>     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
#>     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
#>     rowWeightedSds, rowWeightedVars
#> Loading required package: GenomicRanges
#> Loading required package: stats4
#> Loading required package: BiocGenerics
#> 
#> Attaching package: 'BiocGenerics'
#> The following object is masked from 'package:spacedeconv':
#> 
#>     normalize
#> The following objects are masked from 'package:stats':
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from 'package:base':
#> 
#>     anyDuplicated, aperm, append, as.data.frame, basename, cbind,
#>     colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
#>     get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
#>     match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
#>     Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
#>     table, tapply, union, unique, unsplit, which.max, which.min
#> Loading required package: S4Vectors
#> 
#> Attaching package: 'S4Vectors'
#> The following object is masked from 'package:utils':
#> 
#>     findMatches
#> The following objects are masked from 'package:base':
#> 
#>     expand.grid, I, unname
#> Loading required package: IRanges
#> Loading required package: GenomeInfoDb
#> Loading required package: Biobase
#> Welcome to Bioconductor
#> 
#>     Vignettes contain introductory material; view with
#>     'browseVignettes()'. To cite Bioconductor, see
#>     'citation("Biobase")', and for packages 'citation("pkgname")'.
#> 
#> Attaching package: 'Biobase'
#> The following object is masked from 'package:MatrixGenerics':
#> 
#>     rowMedians
#> The following objects are masked from 'package:matrixStats':
#> 
#>     anyMissing, rowMedians
#>             RCTD        SPOTlight             CARD      spatialDWLS 
#>           "rctd"      "spotlight"           "card"    "spatialdwls" 
#>    cell2location        AutoGeneS       BayesPrism           Bisque 
#>  "cell2location"      "autogenes"     "bayesprism"         "bisque" 
#>          BSeq-sc       CIBERSORTx            CDSeq              CPM 
#>         "bseqsc"     "cibersortx"          "cdseq"            "cpm" 
#>             DWLS             MOMF            MuSiC           Scaden 
#>           "dwls"           "momf"          "music"         "scaden" 
#>             SCDC       MCPcounter             EPIC        quanTIseq 
#>           "scdc"    "mcp_counter"           "epic"      "quantiseq" 
#>            xCell        CIBERSORT CIBERSORT (abs.)            TIMER 
#>          "xcell"      "cibersort"  "cibersort_abs"          "timer" 
#>     ConsensusTME             ABIS         ESTIMATE      mMCPcounter 
#>  "consensus_tme"           "abis"       "estimate"   "mmcp_counter" 
#>        seqImmuCC              DCQ             BASE 
#>      "seqimmucc"            "dcq"           "base"

Sample Data

For this tutorial we will use one of spacedeconvs sample dataset and the deconvolution algorithm “SCDC”.

data("single_cell_data_3")
data("spatial_data_3")

Preprocessing and Normalization

To get a first insight into the data we type the dataset name into the terminal:

single_cell_data_3
#> class: SingleCellExperiment 
#> dim: 29733 7986 
#> metadata(1): Samples
#> assays(1): counts
#> rownames(29733): RP11-34P13.7 FO538757.3 ... KRTAP9-2 IGLVIV-66-1
#> rowData names(1): ID
#> colnames(7986): CID44971_AAGCCGCCACGCATCG CID44971_AAGGAGCTCAACACAC ...
#>   CID44971_TTTCCTCCAAGCCATT CID44971_TTTGGTTGTATCACCA
#> colData names(10): Sample Barcode ... celltype_minor celltype_major
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):

We can see that the single-cell data is available as a SingleCellExperiment. In total this dataset includes expression values from 29733 genes and 7986 cells. It can further be seen that cell type information is available in colData where cell related annotation is stored.

As the next step we normalize the data as cpm (“Counts Per Million”). The normalization is stored as an additional assay in the single-cell object.

single_cell_data_3 <- spacedeconv::preprocess(single_cell_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [50ms]
#> 
#>  Removing 8 observations with umi count below threshold
#>  Removed 8 observations with umi count below threshold [1.2s]
#> 
#>  Removing 5862 variables with all zero expression
#> Warning in spacedeconv::preprocess(single_cell_data_3): There are 13
#> mitochondrial genes present. Consider removing them.
#>  Removed 5862 variables with all zero expression [682ms]
#> 
#>  Removing duplicated genes
#>  Removed duplicated genes [48ms]
#> 
#>  Checking for ENSEMBL Identifiers
#> ! Warning: ENSEMBL identifiers detected in gene names
#>  Checking for ENSEMBL Identifiers Consider using Gene Names for first-generation deconvolution tools
#>  Checking for ENSEMBL Identifiers Finished Preprocessing [6ms]
spatial_data_3 <- spacedeconv::preprocess(spatial_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [24ms]
#> 
#>  Removing 137 observations with umi count below threshold
#>  Removed 137 observations with umi count below threshold [92ms]
#> 
#>  Removing 13049 variables with all zero expression
#> Warning in spacedeconv::preprocess(spatial_data_3): There are 13 mitochondrial
#> genes present. Consider removing them.
#>  Removed 13049 variables with all zero expression [107ms]
#> 
#>  Removing duplicated genes
#>  Removed duplicated genes [46ms]
#> 
#>  Checking for ENSEMBL Identifiers
#> ! Warning: ENSEMBL identifiers detected in gene names
#>  Checking for ENSEMBL Identifiers Consider using Gene Names for first-generation deconvolution tools
#>  Checking for ENSEMBL Identifiers Finished Preprocessing [6ms]

single_cell_data_3 <- spacedeconv::normalize(single_cell_data_3, method = "cpm")
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [11ms]
#> 
#>  Normalizing using cpm
#> Warning in asMethod(object): sparse->dense coercion: allocating vector of size
#> 1.4 GiB
#>  Finished normalization using cpm [4.4s]
#> 
#>  Please note the normalization is stored in an additional assay
spatial_data_3 <- spacedeconv::normalize(spatial_data_3, method = "cpm")
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [11ms]
#> 
#>  Normalizing using cpm
#>  Finished normalization using cpm [456ms]
#> 
#>  Please note the normalization is stored in an additional assay

Build Model

To build a model we need to provide a single cell expression object annotated with cell type information. Let’s have a look into the object to see which annotation is available.

names(colData(single_cell_data_3))
#>  [1] "Sample"          "Barcode"         "orig.ident"      "nCount_RNA"     
#>  [5] "nFeature_RNA"    "percent.mito"    "subtype"         "celltype_subset"
#>  [9] "celltype_minor"  "celltype_major"

There are three different resolutions of cell type annotation available. For this tutorial we will choose "celltype_major" as cell type annotation which includes 9 different cell-types.

Next we can calculate a signature using the annotated single-data and the deconvolution tool “spatialDWLS”.

signature <- spacedeconv::build_model(
  single_cell_obj = single_cell_data_3,
  cell_type_col = "celltype_major",
  method = "dwls", verbose = TRUE, dwls_method = "mast_optimized", ncores = 10
)

The signature contains expression values for all celltypes and a subset of distinctly expressed genes. The following example shows the first 10 rows of the signature calculated in the previous step.

knitr::kable(round(signature[1:10, ], 4))
Endothelial CAFs PVL B-cells Plasmablasts T-cells Myeloid Cancer Epithelial Normal Epithelial
ACKR1 15.1019 0.0086 0.0110 0.0081 0.0208 0.0082 0.0118 0.0089 0.0068
PLVAP 13.4028 0.0172 0.0110 0.0081 0.1250 0.0112 0.1853 0.0168 0.0068
RBP7 10.5972 0.0103 0.0110 0.0027 0.8125 0.0055 0.1103 0.0213 0.3038
RAMP2 9.1157 0.2646 0.0440 0.0027 0.1250 0.0048 0.0176 0.0492 0.0817
FABP4 30.1806 0.1134 9.7582 0.0434 1.5000 0.0186 2.3059 0.2864 0.0272
AQP1 7.3241 0.2354 0.0549 0.0054 0.1250 0.0057 0.0118 0.0190 0.0518
VWF 5.4907 0.0017 0.0000 0.0027 0.0208 0.0021 0.0059 0.0089 0.0041
SDPR 5.7500 0.0052 0.3626 0.0000 0.0833 0.0032 0.0206 0.0268 0.0463
CLEC14A 4.8843 0.1340 0.0440 0.0108 0.0208 0.0021 0.0044 0.0078 0.0041
EMCN 3.8287 0.0155 0.0330 0.0027 0.0000 0.0018 0.0029 0.0045 0.0027

Deconvolution

To deconvolute the Visium slide we use the signature we just calculated. Depending on the selected deconvolution tool further parameters need to be passed, in this case cell_type_col and batch_id_col as well as the scRNA-seq reference dataset.

deconv <- spacedeconv::deconvolute(
  spatial_obj = spatial_data_3,
  single_cell_obj = single_cell_data_3,
  cell_type_col = "celltype_major",
  method = "dwls",
  signature = signature,
  assay_sp = "cpm"
)

Visualization

The deconvolution results are stored in the spatial object to simplify visualizations. spacedeconv offers 6 visualization functions with different scopes, further explained in the visualization vignette.

We can see the available deconvolution results by using available_results(deconv) or by accessing the objects colData:

available_results(deconv)
#> [1] "dwls_B.cells"           "dwls_CAFs"              "dwls_Cancer.Epithelial"
#> [4] "dwls_Endothelial"       "dwls_Myeloid"           "dwls_Normal.Epithelial"
#> [7] "dwls_Plasmablasts"      "dwls_PVL"               "dwls_T.cells"

For each Spot the estimated cell fraction is available as object annotation and can be used for visualizations.

# plot all available results: provide the tool as parameter
spacedeconv::plot_celltype(deconv,
  cell_type = "dwls_B.cells",
  density = FALSE,
  smooth = T,
  title_size = 12
)

sessionInfo()
#> R version 4.3.3 (2024-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=de_DE.UTF-8        LC_COLLATE=de_DE.UTF-8    
#>  [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=de_DE.UTF-8   
#>  [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] SpatialExperiment_1.13.1    SingleCellExperiment_1.24.0
#>  [3] SummarizedExperiment_1.32.0 Biobase_2.62.0             
#>  [5] GenomicRanges_1.54.1        GenomeInfoDb_1.38.8        
#>  [7] IRanges_2.36.0              S4Vectors_0.40.2           
#>  [9] BiocGenerics_0.48.1         MatrixGenerics_1.14.0      
#> [11] matrixStats_1.2.0           spacedeconv_0.1.1          
#> 
#> loaded via a namespace (and not attached):
#>   [1] RColorBrewer_1.1-3      jsonlite_1.8.8          shape_1.4.6.1          
#>   [4] magrittr_2.0.3          magick_2.8.3            farver_2.1.1           
#>   [7] corrplot_0.92           rmarkdown_2.26          GlobalOptions_0.1.2    
#>  [10] fs_1.6.3                zlibbioc_1.48.2         ragg_1.3.0             
#>  [13] vctrs_0.6.5             memoise_2.0.1           RCurl_1.98-1.14        
#>  [16] rstatix_0.7.2           htmltools_0.5.8         S4Arrays_1.2.1         
#>  [19] progress_1.2.3          curl_5.2.1              decoupleR_2.8.0        
#>  [22] broom_1.0.5             cellranger_1.1.0        SparseArray_1.2.4      
#>  [25] sass_0.4.9              pracma_2.4.4            KernSmooth_2.23-22     
#>  [28] bslib_0.6.2             desc_1.4.3              rootSolve_1.8.2.4      
#>  [31] lubridate_1.9.3         cachem_1.0.8            igraph_2.0.3           
#>  [34] lifecycle_1.0.4         pkgconfig_2.0.3         Matrix_1.6-5           
#>  [37] R6_2.5.1                fastmap_1.1.1           GenomeInfoDbData_1.2.11
#>  [40] digest_0.6.35           colorspace_2.1-0        textshaping_0.3.7      
#>  [43] ggpubr_0.6.0            labeling_0.4.3          testit_0.13            
#>  [46] fansi_1.0.6             timechange_0.3.0        httr_1.4.7             
#>  [49] abind_1.4-5             compiler_4.3.3          proxy_0.4-27           
#>  [52] remotes_2.5.0           withr_3.0.0             backports_1.4.1        
#>  [55] carData_3.0-5           DBI_1.2.2               psych_2.4.3            
#>  [58] logger_0.3.0            highr_0.10              OmnipathR_3.10.1       
#>  [61] ggsignif_0.6.4          rappdirs_0.3.3          DelayedArray_0.28.0    
#>  [64] rjson_0.2.21            classInt_0.4-10         units_0.8-5            
#>  [67] tools_4.3.3             glue_1.7.0              nlme_3.1-164           
#>  [70] grid_4.3.3              sf_1.0-16               checkmate_2.3.1        
#>  [73] generics_0.1.3          gtable_0.3.4            tzdb_0.4.0             
#>  [76] class_7.3-22            tidyr_1.3.1             hms_1.1.3              
#>  [79] xml2_1.3.6              car_3.1-2               utf8_1.2.4             
#>  [82] XVector_0.42.0          pillar_1.9.0            stringr_1.5.1          
#>  [85] limma_3.58.1            later_1.3.2             circlize_0.4.16        
#>  [88] dplyr_1.1.4             lattice_0.22-5          ks_1.14.2              
#>  [91] tidyselect_1.2.1        locfit_1.5-9.9          knitr_1.45             
#>  [94] edgeR_4.0.16            xfun_0.43               statmod_1.5.0          
#>  [97] diptest_0.77-0          stringi_1.8.3           yaml_2.3.8             
#> [100] evaluate_0.23           tibble_3.2.1            cli_3.6.2              
#> [103] reticulate_1.35.0       systemfonts_1.0.6       munsell_0.5.0          
#> [106] jquerylib_0.1.4         Rcpp_1.0.12             readxl_1.4.3           
#> [109] png_0.1-8               parallel_4.3.3          pkgdown_2.0.7          
#> [112] ggplot2_3.5.0           readr_2.1.5             prettyunits_1.2.0      
#> [115] mclust_6.1              bitops_1.0-7            multimode_1.5          
#> [118] mvtnorm_1.2-4           scales_1.3.0            ggridges_0.5.6         
#> [121] e1071_1.7-14            purrr_1.0.2             crayon_1.5.2           
#> [124] rlang_1.1.3             rvest_1.0.4             mnormt_2.1.1