Last updated: 2024-06-21

Checks: 6 1

Knit directory: diff_expression_spatial_linear_models/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). 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 file has unstaged changes. 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(20240118) 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 40a1708. 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:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/.Rhistory
    Ignored:    code/.DS_Store
    Ignored:    code/diff_expr_hpcrun_spamm/.DS_Store
    Ignored:    code/diff_expr_hpcrun_spamm/code/.DS_Store
    Ignored:    code/diff_expr_hpcrun_spamm/code/.Rhistory
    Ignored:    code/diff_expr_hpcrun_spamm/code/logs/
    Ignored:    code/diff_expr_hpcrun_spamm/data/.DS_Store
    Ignored:    code/diff_expr_hpcrun_spamm/data/geomx_stlist_w_clusters_soabrain.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/data/geomx_stlist_w_clusters_soaliver.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/data/smi_stlist_w_clusters_liver.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/data/smi_stlist_w_clusters_lungcancer.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/data/visium_stlist_w_clusters_maynardbrain.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/data/visium_stlist_w_clusters_raviglioblastoma.RDS
    Ignored:    code/diff_expr_hpcrun_spamm/results/
    Ignored:    code/stdiff_hpcrun_spamm_pairwise_tests/.DS_Store
    Ignored:    code/stdiff_hpcrun_spamm_pairwise_tests/results/
    Ignored:    data/.DS_Store
    Ignored:    data/cosmx_smi_liver_nanostring/
    Ignored:    data/cosmx_smi_lung_nsclc_nanostring/
    Ignored:    data/geomx_spatial_organ_atlas_data/
    Ignored:    data/maynard_2021_prefrontal_cortex/
    Ignored:    data/ravi_2022_glioblastoma/
    Ignored:    data/ravi_gbm_spata_objects/

Unstaged changes:
    Modified:   README.md
    Modified:   analysis/about.Rmd
    Modified:   analysis/diff_expr_example_stdiff.Rmd
    Modified:   analysis/diff_expr_spatial_linear_models.Rmd
    Modified:   analysis/index.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.


The spatialGE software includes the STdiff function to test for differentially expressed genes using spatial covariance structures. The differential expression tests in the manuscript associated with this website were performed in an HPC environment. Nonetheless, the STdiff algorithm can be run on a laptop computer.

Here an example to run the STdiff function is presented on a small subset of genes to reduce computational time. This vignette assumes that an STlist object has been already created (click here for a tutorial to create an STlist object). The STlist used here will be the same as in the main vignette generating the figures for the manuscript.

Users are also encouraged to take a look at the vignette in the spatialGE package.

Load libraries

library('spatialGE')
library('tidyverse')
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Load STlist containing the CosMx-SMI lung cancer data set used in the manuscript.

stlist_obj = readRDS('code/diff_expr_hpcrun_spamm/data/smi_stlist_w_clusters_lungcancer.RDS')

In this small example, the STdiff fuction will be used to test for differentially expressed genes in tumor cells (“tumor_5”) and epithelial cells in the FOV “lung5rep3_fov28”. Only the 100 top most variable will be tested using non-spatial models. Then, 10% of the differentially expressed genes (i.e., p < 0.05), will be tested using spatial models.

Counts were already transfomed with the transform_data function. The STdiff function can be directly called on the STlist object:

degs = STdiff(stlist_obj, # STlist object
              annot='annots', # Name of the column in stlist_obj@spatial_meta[['lung5rep3_fov28']] containing cell types
              samples='lung5rep3_fov28', # Name of the sample (i.e., FOV)
              topgenes=100, # Top variable genes to run non-spatial models
              sp_topgenes=0.1, # THe percentage of non-spatial DE tests to re-run using spatial models
              pairwise=T, # Do pairwise comparisons
              clusters=c('epithelial', 'tumor_5')) # Specific cell types to test
Testing metadata: annots...
    Running non-spatial mixed models...
Registered S3 methods overwritten by 'registry':
  method               from 
  print.registry_field proxy
  print.registry_entry proxy
    Completed non-spatial mixed models (0.17 min).
    Running spatial tests...
Using paralellisation might be useful. See help("setNBThreads")
Using paralellisation might be useful. See help("setNBThreads")
Using paralellisation might be useful. See help("setNBThreads")
Using paralellisation might be useful. See help("setNBThreads")
Using paralellisation might be useful. See help("setNBThreads")
Using paralellisation might be useful. See help("setNBThreads")

    Completed spatial mixed models (5.39 min).
STdiff completed in 5.66 min.

The “exp_adj_p_val” column contains the FDR p-values from the spatial tests. The “adj_p_val” contains the FDR p-values from the non-spatial tests. The results cna be observed like so:

degs[['lung5rep3_fov28']] %>%
  filter(!is.na(exp_p_val)) # Show only genes with spatial tests 
# A tibble: 6 × 10
  sample       gene  avg_log2fc cluster_1 cluster_2 mm_p_val adj_p_val exp_p_val
  <chr>        <chr>      <dbl> <chr>     <chr>        <dbl>     <dbl>     <dbl>
1 lung5rep3_f… OLFM4       4.62 tumor_5   epitheli…        0         0  0       
2 lung5rep3_f… CEAC…       3.36 tumor_5   epitheli…        0         0  0       
3 lung5rep3_f… CXCL5       2.51 tumor_5   epitheli…        0         0  0       
4 lung5rep3_f… KRT19       2.20 tumor_5   epitheli…        0         0  0       
5 lung5rep3_f… KRT17       2.16 tumor_5   epitheli…        0         0  0       
6 lung5rep3_f… DMBT1       3.03 tumor_5   epitheli…        0         0  1.01e-13
# ℹ 2 more variables: exp_adj_p_val <dbl>, comments <chr>

sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
 [5] purrr_1.0.2     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1   
 [9] ggplot2_3.5.0   tidyverse_2.0.0 spatialGE_1.2.0 workflowr_1.7.1

loaded via a namespace (and not attached):
 [1] dotCall64_1.1-1     gtable_0.3.4        spam_2.10-0        
 [4] xfun_0.41           bslib_0.6.1         ggpolypath_0.3.0   
 [7] processx_3.8.3      lattice_0.21-9      numDeriv_2016.8-1.1
[10] callr_3.7.3         tzdb_0.4.0          vctrs_0.6.5        
[13] tools_4.3.2         ps_1.7.5            generics_0.1.3     
[16] parallel_4.3.2      proxy_0.4-27        fansi_1.0.6        
[19] pkgconfig_2.0.3     Matrix_1.6-4        checkmate_2.3.1    
[22] lifecycle_1.0.4     compiler_4.3.2      git2r_0.33.0       
[25] fields_15.2         munsell_0.5.0       getPass_0.2-4      
[28] httpuv_1.6.13       htmltools_0.5.7     maps_3.4.1.1       
[31] sass_0.4.8          yaml_2.3.8          nloptr_2.0.3       
[34] crayon_1.5.2        later_1.3.2         pillar_1.9.0       
[37] jquerylib_0.1.4     whisker_0.4.1       MASS_7.3-60        
[40] cachem_1.0.8        boot_1.3-28.1       nlme_3.1-163       
[43] spaMM_4.4.0         tidyselect_1.2.0    digest_0.6.33      
[46] slam_0.1-50         stringi_1.8.3       rprojroot_2.0.4    
[49] fastmap_1.1.1       grid_4.3.2          colorspace_2.1-0   
[52] cli_3.6.2           magrittr_2.0.3      utf8_1.2.4         
[55] withr_2.5.2         backports_1.4.1     scales_1.3.0       
[58] promises_1.2.1      registry_0.5-1      timechange_0.2.0   
[61] rmarkdown_2.25      httr_1.4.7          hms_1.1.3          
[64] ROI_1.0-1           pbapply_1.7-2       evaluate_0.23      
[67] knitr_1.45          viridisLite_0.4.2   rlang_1.1.2        
[70] Rcpp_1.0.11         glue_1.6.2          minqa_1.2.6        
[73] rstudioapi_0.15.0   jsonlite_1.8.8      R6_2.5.1           
[76] fs_1.6.3