Skip to contents

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

#>             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”.

library(spacedeconv)
library(SpatialExperiment)

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):

# for performance reasons we are subsampling the data

single_cell_data_3 <- subsetSCE(single_cell_data_3, cell_type_col = "celltype_major", ncells = 180)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> 
[36mℹ
[39m testing parameter
#> 
[36mℹ
[39m Set seed to 12345
#> 
[36mℹ
[39m testing parameter

[32m✔
[39m parameter OK 
[38;5;249m[34ms]
[39m
#> 
#> 
[36mℹ
[39m extracting up to 180 cells
#> 
[32m✔
[39m extracting up to 180 cells 
[38;5;249m[216ms]
[39m
#> 
#> 
[36mℹ
[39m extracted 180 cells
#> 
[32m✔
[39m extracted 180 cells 
[38;5;249m[33ms]
[39m

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 [86ms]
#> 
#>  Removing 0 observations with umi count below threshold
#>  Removed 0 observations with umi count below threshold [1.2s]
#> 
#>  Removing 12976 variables with all zero expression
#>  Removed 12976 variables with all zero expression [15ms]
#> 
spatial_data_3 <- spacedeconv::preprocess(spatial_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [50ms]
#> 
#>  Removing 137 observations with umi count below threshold
#>  Removed 137 observations with umi count below threshold [241ms]
#> 
#>  Removing 13049 variables with all zero expression
#>  Removed 13049 variables with all zero expression [15ms]
#> 

single_cell_data_3 <- spacedeconv::normalize(single_cell_data_3, method = "cpm")
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#>  testing parameter
#>  parameter OK [32ms]
#> 
#>  Normalizing using cpm
#>  Finished normalization using cpm [178ms]
#> 
#>  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 [34ms]
#> 
#>  Normalizing using cpm
#>  Finished normalization using cpm [3s]
#> 
#>  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 = "card"
)

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))

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 = "card",
  batch_id_col = "orig.ident",
  assay_sp = "cpm",
  assay_sc = "cpm"
)
#> ## QC on scRNASeq dataset! ...
#> No cell types selected, we will use all the cell types in the scRNA-seq data
#> ## QC on spatially-resolved dataset! ...
#> ## create reference matrix from scRNASeq...
#> ## Select Informative Genes! ...
#> ## Deconvolution Starts! ...
#> ## Deconvolution Finish! ...

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] "card_B.cells"           "card_CAFs"              "card_Cancer.Epithelial"
#> [4] "card_Endothelial"       "card_Myeloid"           "card_Normal.Epithelial"
#> [7] "card_Plasmablasts"      "card_PVL"               "card_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 = "card",
  density = FALSE,
  smooth = T,
  title_size = 12
)


# ... or plot a specific result
spacedeconv::plot_celltype(deconv,
  cell_type = "card_Cancer.Epithelial",
  density = FALSE,
  smooth = T,
  title_size = 12
)