Example
spacedeconv_example.Rmd
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, 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”.
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 [52ms]
#>
#> ℹ Removing 8 observations with umi count below threshold
#> ✔ Removed 8 observations with umi count below threshold [1.5s]
#>
#> ℹ 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 [811ms]
#>
#> ℹ 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 [7ms]
spatial_data_3 <- spacedeconv::preprocess(spatial_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> ℹ testing parameter
#> ✔ parameter OK [25ms]
#>
#> ℹ Removing 137 observations with umi count below threshold
#> ✔ Removed 137 observations with umi count below threshold [99ms]
#>
#> ℹ 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 [113ms]
#>
#> ℹ 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 [7ms]
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.9s]
#>
#> ℹ 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 [483ms]
#>
#> ℹ 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.
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.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04 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.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.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.14.0 SingleCellExperiment_1.26.0
#> [3] SummarizedExperiment_1.34.0 Biobase_2.64.0
#> [5] GenomicRanges_1.56.2 GenomeInfoDb_1.40.1
#> [7] IRanges_2.38.1 S4Vectors_0.42.1
#> [9] BiocGenerics_0.50.0 MatrixGenerics_1.16.0
#> [11] matrixStats_1.4.1 spacedeconv_0.1.1
#>
#> loaded via a namespace (and not attached):
#> [1] later_1.3.2 tibble_3.2.1 R.oo_1.26.0
#> [4] cellranger_1.1.0 lifecycle_1.0.4 sf_1.0-17
#> [7] rstatix_0.7.2 edgeR_4.2.1 lattice_0.22-6
#> [10] backports_1.5.0 magrittr_2.0.3 limma_3.60.6
#> [13] plotly_4.10.4 sass_0.4.9 rmarkdown_2.28
#> [16] jquerylib_0.1.4 yaml_2.3.10 remotes_2.5.0
#> [19] sp_2.1-4 reticulate_1.39.0 cowplot_1.1.3
#> [22] DBI_1.2.3 RColorBrewer_1.1-3 lubridate_1.9.3
#> [25] abind_1.4-8 zlibbioc_1.50.0 rvest_1.0.4
#> [28] purrr_1.0.2 R.utils_2.12.3 pracma_2.4.4
#> [31] rappdirs_0.3.3 multimode_1.5 circlize_0.4.16
#> [34] GenomeInfoDbData_1.2.12 ggrepel_0.9.6 terra_1.7-78
#> [37] units_0.8-5 parallelly_1.38.0 pkgdown_2.1.1
#> [40] colorRamp2_0.1.0 codetools_0.2-20 DelayedArray_0.30.1
#> [43] xml2_1.3.6 tidyselect_1.2.1 shape_1.4.6.1
#> [46] farver_2.1.2 UCSC.utils_1.0.0 jsonlite_1.8.9
#> [49] e1071_1.7-16 ks_1.14.3 Formula_1.2-5
#> [52] ggridges_0.5.6 systemfonts_1.1.0 dbscan_1.2-0
#> [55] tools_4.4.1 progress_1.2.3 GiottoClass_0.3.5
#> [58] ragg_1.3.3 Rcpp_1.0.13 glue_1.8.0
#> [61] mnormt_2.1.1 SparseArray_1.4.8 xfun_0.48
#> [64] decoupleR_2.10.0 dplyr_1.1.4 withr_3.0.1
#> [67] fastmap_1.2.0 fansi_1.0.6 digest_0.6.37
#> [70] timechange_0.3.0 R6_2.5.1 textshaping_0.4.0
#> [73] colorspace_2.1-1 scattermore_1.2 gtools_3.9.5
#> [76] diptest_0.77-1 R.methodsS3_1.8.2 utf8_1.2.4
#> [79] tidyr_1.3.1 generics_0.1.3 data.table_1.16.2
#> [82] class_7.3-22 prettyunits_1.2.0 httr_1.4.7
#> [85] htmlwidgets_1.6.4 S4Arrays_1.4.1 pkgconfig_2.0.3
#> [88] gtable_0.3.5 GiottoVisuals_0.2.4 XVector_0.44.0
#> [91] OmnipathR_3.12.4 htmltools_0.5.8.1 carData_3.0-5
#> [94] scales_1.3.0 GiottoUtils_0.1.12 png_0.1-8
#> [97] corrplot_0.94 knitr_1.48 tzdb_0.4.0
#> [100] reshape2_1.4.4 rjson_0.2.23 checkmate_2.3.2
#> [103] nlme_3.1-164 curl_5.2.3 proxy_0.4-27
#> [106] cachem_1.1.0 GlobalOptions_0.1.2 stringr_1.5.1
#> [109] testit_0.13 rootSolve_1.8.2.4 KernSmooth_2.23-24
#> [112] parallel_4.4.1 desc_1.4.3 pillar_1.9.0
#> [115] grid_4.4.1 logger_0.3.0 vctrs_0.6.5
#> [118] ggpubr_0.6.0 car_3.1-3 evaluate_1.0.1
#> [121] readr_2.1.5 magick_2.8.5 mvtnorm_1.3-1
#> [124] cli_3.6.3 locfit_1.5-9.10 compiler_4.4.1
#> [127] rlang_1.1.4 crayon_1.5.3 ggsignif_0.6.4
#> [130] labeling_0.4.3 mclust_6.1.1 classInt_0.4-10
#> [133] plyr_1.8.9 fs_1.6.4 stringi_1.8.4
#> [136] psych_2.4.6.26 viridisLite_0.4.2 deldir_2.0-4
#> [139] BiocParallel_1.38.0 munsell_0.5.1 lazyeval_0.2.2
#> [142] Matrix_1.7-0 hms_1.1.3 ggplot2_3.5.1
#> [145] statmod_1.5.0 highr_0.11 igraph_2.0.3
#> [148] broom_1.0.7 bslib_0.8.0 Giotto_4.1.0
#> [151] readxl_1.4.3