Last updated: 2021-04-28

Checks: 7 0

Knit directory: STUtility_web_site/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191031) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version f91ff8c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/manual_annotation.png
    Ignored:    analysis/visualization_3D.Rmd
    Ignored:    pre_data/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/normalization.Rmd) and HTML (docs/normalization.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html 1e9c615 Ludvig Larsson 2021-04-28 Build site.
html 34884bd Ludvig Larsson 2020-10-06 Build site.
html d13dabd Ludvig Larsson 2020-06-05 Build site.
html 60407bb Ludvig Larsson 2020-06-05 Build site.
html e339ebc Ludvig Larsson 2020-06-05 Build site.
html 6606127 Ludvig Larsson 2020-06-05 Build site.
html f0ea9c1 Ludvig Larsson 2020-06-05 Build site.
html 651f315 Ludvig Larsson 2020-06-05 Build site.
html a6a0c51 Ludvig Larsson 2020-06-05 Build site.
html f3c5cb4 Ludvig Larsson 2020-06-05 Build site.
Rmd 5401a87 Ludvig Larsson 2020-06-05 downsampled points in 3D visualization
html f89df7e Ludvig Larsson 2020-06-05 Build site.
html 1e67a7c Ludvig Larsson 2020-06-04 Build site.
html f71b0c0 Ludvig Larsson 2020-06-04 Build site.
html 8a54a4d Ludvig Larsson 2020-06-04 Build site.
html 5e466eb Ludvig Larsson 2020-06-04 Build site.
html 377408d Ludvig Larsson 2020-06-04 Build site.
html ed54ffb Ludvig Larsson 2020-06-04 Build site.
html f14518c Ludvig Larsson 2020-06-04 Build site.
html efd885b Ludvig Larsson 2020-06-04 Build site.
html 4f42429 Ludvig Larsson 2020-06-04 Build site.
Rmd 3660612 Ludvig Larsson 2020-06-04 update website
html 2a6ee20 jbergenstrahle 2020-04-03 Build site.
Rmd b5f9314 jbergenstrahle 2020-04-01 removed regress
html fb06450 jbergenstrahle 2020-01-11 Build site.
Rmd efbbda3 jbergenstrahle 2020-01-01 update pek
Rmd 5cb8ab1 jbergenstrahle 2019-12-02 update2
html a3a1f1f Ludvig Larsson 2019-10-31 Changed font
Rmd 7cdf8e1 Ludvig Larsson 2019-10-31 Changed font size
Rmd 786357d Ludvig Larsson 2019-10-31 Fixed warnings
html 786357d Ludvig Larsson 2019-10-31 Fixed warnings
Rmd f10ef37 Ludvig Larsson 2019-10-31 Added section noramlization and spatial faetures
html f10ef37 Ludvig Larsson 2019-10-31 Added section noramlization and spatial faetures

SCTransform (Seurat)


In order to normalize the data we recommend using variance stabilized transformation available in the SCTransform function in Seurat as of v3.0.

Following the rationale expressed below, we transform the data according to the Seurat workflow.

se <- SCTransform(se)

Regressing out batch effects

When deciding on a normalization strategy using SCTransform it is important to consider potential batch effects that could confound downstream analysis. Such batch effects could for example arise between different sample specimens, storage times, array slides etc. and even between consecutive sections prepared on the same slide. The experimental setup is crucial to make it possible to combat such batch effects and it is important to carefully think through if and how they should be removed to make the biological variation in your data more meaningful. Some of these effects can be effectively removed during normalization with SCTransform by specifying your batches with the vars.to.regress option.

For example if you want to regress out the “section number” from the data you need to make sure that you have a variable in your meta.data giving a unique ID for each section. If you don’t have this information in your meta.data you can get it from the “Staffli” object stored inside the Seurat object. Then you can simply set vars.to.regress = "section" when running SCTransform to correct for potential technical effects separating the two sections.

# Add a section column to your meta.data
se$section <- paste0("section_", GetStaffli(se)[[, "sample", drop = T]])

# Run normalization with "vars.to.regress" option
se.batch.cor <- SCTransform(se, vars.to.regress = "section")

Note: for comprehensive tutorials in the different options and workflow possibilities available within Seurat, we recommend looking at their website https://satijalab.org/seurat/.


Rationale of approach


Each spot in a Spatial Transcriptomics dataset typically contains RNA from a mixture of cells so why would we apply a workflow that was developed for single-cell RNAseq data? We can calculate some properties to visually inspect the data to see that ST data have similar properties to that of scRNAseq data.

library(Matrix)
library(magrittr)
library(dplyr)
library(ggplot2)

# Get raw count data 
umi_data <- GetAssayData(object = se, slot = "counts", assay = "RNA")
dim(umi_data)

# Calculate gene attributes
gene_attr <- data.frame(mean = rowMeans(umi_data),
                        detection_rate = rowMeans(umi_data > 0),
                        var = apply(umi_data, 1, var), 
                        row.names = rownames(umi_data)) %>%
  mutate(log_mean = log10(mean), log_var = log10(var))

# Obtain spot attributes from Seurat meta.data slot
spot_attr <- se[[c("nFeature_RNA", "nCount_RNA")]]

p1 <- ggplot(gene_attr, aes(log_mean, log_var)) + 
  geom_point(alpha = 0.3, shape = 16, color = "white") + 
  geom_density_2d(size = 0.3) +
  geom_abline(intercept = 0, slope = 1, color = 'red') +
  ggtitle("Mean-variance relationship") + DarkTheme()

# add the expected detection rate under Poisson model
x = seq(from = -2, to = 2, length.out = 1000)
poisson_model <- data.frame(log_mean = x, detection_rate = 1 - dpois(0, lambda = 10^x))
p2 <- ggplot(gene_attr, aes(log_mean, detection_rate)) + 
  geom_point(alpha = 0.3, shape = 16, color = "white") + 
  geom_line(data = poisson_model, color='red') +
  ggtitle("Mean-detection-rate relationship") + DarkTheme()

cowplot::plot_grid(p1, p2, nrow = 2)

Version Author Date
4f42429 Ludvig Larsson 2020-06-04
2a6ee20 jbergenstrahle 2020-04-03
fb06450 jbergenstrahle 2020-01-11
f10ef37 Ludvig Larsson 2019-10-31

We can see from the mean-variance and Mean-detection-rate scatter plots that genes show overdispersion compared to what would be expected under a Poisson model. Because these properties are shared between ST and scRNAseq data we have reasoned that the workflow presented in the Seurat package should be applicable for ST data as well. It is important however to keep in mind that each spots contains a mixture of cell types and should be interpreted as a morphological unit in the context of a tissue section.

 

A work by Joseph Bergenstråhle and Ludvig Larsson

 


sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] dplyr_1.0.5     magrittr_2.0.1  Matrix_1.3-2    STutility_0.1.0
[5] ggplot2_3.3.3   Seurat_3.1.5    workflowr_1.6.2

loaded via a namespace (and not attached):
  [1] uuid_0.1-4              backports_1.1.6         systemfonts_0.2.1      
  [4] plyr_1.8.6              igraph_1.2.6            lazyeval_0.2.2         
  [7] sp_1.4-1                splines_4.0.5           crosstalk_1.1.1        
 [10] listenv_0.8.0           digest_0.6.27           foreach_1.5.0          
 [13] htmltools_0.5.1.1       viridis_0.5.1           magick_2.3             
 [16] tiff_0.1-5              gdata_2.18.0            fansi_0.4.2            
 [19] cluster_2.1.1           doParallel_1.0.15       ROCR_1.0-11            
 [22] globals_0.14.0          gmodels_2.18.1          jpeg_0.1-8.1           
 [25] colorspace_2.0-0        blob_1.2.1              ggrepel_0.9.1          
 [28] xfun_0.13               crayon_1.4.1            jsonlite_1.7.2         
 [31] zeallot_0.1.0           survival_3.2-10         zoo_1.8-9              
 [34] iterators_1.0.12        ape_5.4-1               glue_1.4.2             
 [37] gtable_0.3.0            webshot_0.5.2           leiden_0.3.7           
 [40] future.apply_1.7.0      scales_1.1.1            DBI_1.1.0              
 [43] miniUI_0.1.1.1          Rcpp_1.0.6              isoband_0.2.4          
 [46] viridisLite_0.3.0       xtable_1.8-4            spData_0.3.5           
 [49] units_0.6-6             reticulate_1.18         spdep_1.1-3            
 [52] rsvd_1.0.3              akima_0.6-2             tsne_0.1-3             
 [55] htmlwidgets_1.5.3       httr_1.4.2              RColorBrewer_1.1-2     
 [58] ellipsis_0.3.1          ica_1.0-2               farver_2.1.0           
 [61] pkgconfig_2.0.3         uwot_0.1.10             deldir_0.1-25          
 [64] utf8_1.2.1              labeling_0.4.2          tidyselect_1.1.0       
 [67] rlang_0.4.10            manipulateWidget_0.10.1 reshape2_1.4.4         
 [70] later_1.1.0.1           munsell_0.5.0           tools_4.0.5            
 [73] dbscan_1.1-5            generics_0.1.0          ggridges_0.5.3         
 [76] evaluate_0.14           stringr_1.4.0           fastmap_1.0.1          
 [79] yaml_2.2.1              knitr_1.28              fs_1.5.0               
 [82] fitdistrplus_1.1-3      rgl_0.100.54            purrr_0.3.4            
 [85] RANN_2.6.1              readbitmap_0.1.5        pbapply_1.4-3          
 [88] future_1.21.0           nlme_3.1-152            whisker_0.4            
 [91] mime_0.10               ggiraph_0.7.7           compiler_4.0.5         
 [94] plotly_4.9.3            png_0.1-7               e1071_1.7-3            
 [97] Morpho_2.8              tibble_3.1.0            stringi_1.5.3          
[100] gdtools_0.2.2           lattice_0.20-41         classInt_0.4-3         
[103] shinyjs_1.1             vctrs_0.3.7             LearnBayes_2.15.1      
[106] pillar_1.5.1            lifecycle_1.0.0         lmtest_0.9-38          
[109] RcppAnnoy_0.0.18        data.table_1.14.0       cowplot_1.1.1          
[112] irlba_2.3.3             Rvcg_0.19.1             raster_3.1-5           
[115] httpuv_1.5.2            patchwork_1.1.1         colorRamps_2.3         
[118] R6_2.5.0                imager_0.42.1           promises_1.2.0.1       
[121] KernSmooth_2.23-18      gridExtra_2.3           bmp_0.3                
[124] parallelly_1.24.0       codetools_0.2-18        gtools_3.8.2           
[127] boot_1.3-27             MASS_7.3-53.1           assertthat_0.2.1       
[130] rprojroot_1.3-2         withr_2.4.1             sctransform_0.2.1      
[133] expm_0.999-4            parallel_4.0.5          grid_4.0.5             
[136] tidyr_1.1.3             coda_0.19-3             class_7.3-18           
[139] rmarkdown_2.1           Rtsne_0.15              git2r_0.27.1           
[142] sf_0.9-7                shiny_1.4.0.2