Last updated: 2019-10-31

Checks: 5 1

Knit directory: STUtility_web_site/

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


The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

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! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

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/about_cache/
    Ignored:    analysis/getting_started_cache/
    Ignored:    analysis/image_processing_cache/
    Ignored:    docs/.DS_Store

Untracked files:
    Untracked:  analysis/normalization.Rmd

Unstaged changes:
    Modified:   analysis/image_processing.Rmd
    Modified:   analysis/index.Rmd
    Modified:   analysis/license.Rmd

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.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


library(STutility)
se <- readRDS("~/STUtility/se_object")

Normalization and scaling


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)

Attaching package: 'Matrix'
The following object is masked from 'package:S4Vectors':

    expand
library(magrittr)
library(dplyr)

Attaching package: 'dplyr'
The following object is masked from 'package:matrixStats':

    count
The following object is masked from 'package:Biobase':

    combine
The following objects are masked from 'package:GenomicRanges':

    intersect, setdiff, union
The following object is masked from 'package:GenomeInfoDb':

    intersect
The following objects are masked from 'package:IRanges':

    collapse, desc, intersect, setdiff, slice, union
The following objects are masked from 'package:S4Vectors':

    first, intersect, rename, setdiff, setequal, union
The following objects are masked from 'package:BiocGenerics':

    combine, intersect, setdiff, union
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)

# Get raw count data 
umi_data <- GetAssayData(object = se, slot = "counts", assay = "RNA")
dim(umi_data)
[1] 13437  5053
# 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)

Warning: The above code chunk cached its results, but it won’t be re-run if previous chunks it depends on are updated. If you need to use caching, it is highly recommended to also set knitr::opts_chunk$set(autodep = TRUE) at the top of the file (in a chunk that is not cached). Alternatively, you can customize the option dependson for each individual chunk that is cached. Using either autodep or dependson will remove this warning. See the knitr cache options for more details.

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.

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 above, we transform the data according to the Seurat workflow. Note: for comprehensive tutorials in the different options and workflow possibilities available within Seurat, we recommend looking at their website https://satijalab.org/seurat/

se <- SCTransform(se, vars.to.regress = c("sample_id", "nFeature_RNA"))
 

A work by Joseph Bergenstråhle and Ludvig Larsson

 


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] dplyr_0.8.3                 magrittr_1.5               
 [3] Matrix_1.2-17               STutility_0.1.0            
 [5] ggplot2_3.2.1               SingleCellExperiment_1.6.0 
 [7] SummarizedExperiment_1.14.1 DelayedArray_0.10.0        
 [9] BiocParallel_1.18.1         matrixStats_0.55.0         
[11] Biobase_2.44.0              GenomicRanges_1.36.1       
[13] GenomeInfoDb_1.20.0         IRanges_2.18.3             
[15] S4Vectors_0.22.1            BiocGenerics_0.30.0        
[17] Seurat_3.1.1               

loaded via a namespace (and not attached):
  [1] backports_1.1.5         workflowr_1.3.0        
  [3] systemfonts_0.1.1       plyr_1.8.4             
  [5] igraph_1.2.4.1          lazyeval_0.2.2         
  [7] splines_3.6.1           crosstalk_1.0.0        
  [9] listenv_0.7.0           digest_0.6.22          
 [11] foreach_1.4.7           htmltools_0.4.0        
 [13] viridis_0.5.1           magick_2.2             
 [15] tiff_0.1-5              gdata_2.18.0           
 [17] cluster_2.1.0           doParallel_1.0.15      
 [19] ROCR_1.0-7              globals_0.12.4         
 [21] RcppParallel_4.4.4      R.utils_2.9.0          
 [23] jpeg_0.1-8              colorspace_1.4-1       
 [25] ggrepel_0.8.1           xfun_0.10              
 [27] crayon_1.3.4            RCurl_1.95-4.12        
 [29] jsonlite_1.6            zeallot_0.1.0          
 [31] survival_2.44-1.1       zoo_1.8-6              
 [33] iterators_1.0.12        ape_5.3                
 [35] glue_1.3.1              gtable_0.3.0           
 [37] zlibbioc_1.30.0         XVector_0.24.0         
 [39] webshot_0.5.1           leiden_0.3.1           
 [41] future.apply_1.3.0      scales_1.0.0           
 [43] bibtex_0.4.2            miniUI_0.1.1.1         
 [45] Rcpp_1.0.2              metap_1.1              
 [47] viridisLite_0.3.0       xtable_1.8-4           
 [49] reticulate_1.13         rsvd_1.0.2             
 [51] SDMTools_1.1-221.1      tsne_0.1-3             
 [53] htmlwidgets_1.5.1       httr_1.4.1             
 [55] gplots_3.0.1.1          RColorBrewer_1.1-2     
 [57] ica_1.0-2               pkgconfig_2.0.3        
 [59] R.methodsS3_1.7.1       uwot_0.1.4             
 [61] labeling_0.3            tidyselect_0.2.5       
 [63] rlang_0.4.1             manipulateWidget_0.10.0
 [65] reshape2_1.4.3          later_1.0.0            
 [67] munsell_0.5.0           tools_3.6.1            
 [69] ggridges_0.5.1          evaluate_0.14          
 [71] stringr_1.4.0           fastmap_1.0.1          
 [73] yaml_2.2.0              npsurv_0.4-0           
 [75] knitr_1.25              fs_1.3.1               
 [77] fitdistrplus_1.0-14     rgl_0.100.30           
 [79] caTools_1.17.1.2        purrr_0.3.2            
 [81] RANN_2.6.1              readbitmap_0.1.5       
 [83] pbapply_1.4-2           future_1.14.0          
 [85] nlme_3.1-141            mime_0.7               
 [87] R.oo_1.22.0             ggiraph_0.6.1          
 [89] xml2_1.2.2              compiler_3.6.1         
 [91] plotly_4.9.0            png_0.1-7              
 [93] lsei_1.2-0              Morpho_2.7             
 [95] tibble_2.1.3            stringi_1.4.3          
 [97] gdtools_0.2.0           lattice_0.20-38        
 [99] shinyjs_1.0             vctrs_0.2.0            
[101] pillar_1.4.2            lifecycle_0.1.0        
[103] Rdpack_0.11-0           lmtest_0.9-37          
[105] RcppAnnoy_0.0.13        data.table_1.12.2      
[107] cowplot_1.0.0           bitops_1.0-6           
[109] irlba_2.3.3             Rvcg_0.18              
[111] gbRd_0.4-11             httpuv_1.5.2           
[113] colorRamps_2.3          imager_0.41.2          
[115] R6_2.4.0                promises_1.1.0         
[117] bmp_0.3                 KernSmooth_2.23-15     
[119] gridExtra_2.3           codetools_0.2-16       
[121] MASS_7.3-51.4           gtools_3.8.1           
[123] assertthat_0.2.1        rprojroot_1.3-2        
[125] withr_2.1.2             sctransform_0.2.0      
[127] GenomeInfoDbData_1.2.1  grid_3.6.1             
[129] tidyr_1.0.0             rmarkdown_1.16         
[131] Rtsne_0.15              git2r_0.26.1           
[133] shiny_1.4.0