Last updated: 2024-03-21

Checks: 7 0

Knit directory: Celina/

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.


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(20231207) 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.

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:


Untracked files:
    Untracked:  .Rprofile
    Untracked:  .gitattributes
    Untracked:  .gitignore
    Untracked:  Celina.Rproj
    Untracked:  README.md
    Untracked:  _workflowr.yml
    Untracked:  analysis/
    Untracked:  code/
    Untracked:  data/
    Untracked:  output/

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.


Load in data

The processed example data can be downloaded from here.

# load in data
load(paste0("./data/artista_data.RData"))

Create cell type indicator matrix

For each cell, we assume we already know the cell type label, which is saved in data_use$cluster in the below example.

make_celltype_mat = function(celltype_annotation){

  celltype_mat = matrix(0,length(celltype_annotation),length(c(table(celltype_annotation))))

  for(i in 1:length(table(celltype_annotation))){
      celltypename = names(table(celltype_annotation))[i]
      celltype_mat[,i] = 0
      celltype_mat[which(celltype_annotation==celltypename),i] =1
  }
  colnames(celltype_mat) = names(table(celltype_annotation))
  return(celltype_mat)

}

# Now we can create the cell type matrix, just like the cell type proportion matrix we had in the spot level data, but now for single cell level data, the values are binary indicators rather than 0-1 proportion values.
data_use$celltype = make_celltype_mat(data_use$cluster)
rownames(data_use$celltype) = colnames(data_use$count_use)

Create Celina object

library(CELINA)
Obj = Create_Celina_Object(celltype_mat = t(data_use$celltype), 
                              gene_expression_mat = as.matrix(data_use$count_use), 
                              location = as.matrix(data_use$location_coord),
                              covariates = NULL,
                              project = "Artista")

Preprocess data

In this step, we preprocess the gene expression matrix by normalizing the counts and filtering genes.

filtered_cell_types = colnames(data_use$celltype)[which(colSums(data_use$celltype) > (dim(data_use$celltype)[1]*0.01))]
filtered_cell_types
Obj = preprocess_input(Obj, 
                       # Celina object
                       cell_types_to_test = filtered_cell_types,  
                       # a vector of cell types to be used for testing 
                       scRNA_count = as.matrix(data_use$count_use), 
                       # a gene x cell expression matrix of reference scRNA-seq data
                       # in single cell level data, we just use the spatial count matrix
                       sc_cell_type_labels = data_use$cluster,
                       # a vector of cell type labels for each cell
                       threshold = 5e-5)
print(names(Obj@genes_list))

Calculate the kernel matrix

Obj = Calculate_Kernel(Obj, approximation = FALSE)

Test genes

Obj = Testing_interaction_all(Obj,celltype_to_test = filtered_cell_types, num_cores=10)

sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 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/liblapack.so.3;  LAPACK version 3.9.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: America/New_York
tzcode source: system (glibc)

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

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.2         knitr_1.45        rlang_1.1.2      
 [5] xfun_0.41         stringi_1.8.3     promises_1.2.1    jsonlite_1.8.8   
 [9] workflowr_1.7.1   glue_1.7.0        rprojroot_2.0.4   git2r_0.33.0     
[13] htmltools_0.5.7   httpuv_1.6.13     sass_0.4.8        fansi_1.0.5      
[17] rmarkdown_2.25    jquerylib_0.1.4   evaluate_0.23     tibble_3.2.1     
[21] fastmap_1.1.1     yaml_2.3.8        lifecycle_1.0.4   stringr_1.5.1    
[25] compiler_4.3.3    fs_1.6.3          Rcpp_1.0.12       pkgconfig_2.0.3  
[29] rstudioapi_0.15.0 later_1.3.2       digest_0.6.33     R6_2.5.1         
[33] utf8_1.2.4        pillar_1.9.0      magrittr_2.0.3    bslib_0.6.1      
[37] tools_4.3.3       cachem_1.0.8