Skip to contents

GeneCodeR spatial rotation to test invariance

Reload important files recently saved:

main_path <- "~/Documents/main_files/AskExplain/Q4_2022/gcode/"

# Please replace this path
path_to_save <- paste(main_path,"./temp_save_dir/",sep="")

load(file = paste(sep="",path_to_save,"all_genecoder.RData"))

Set up the test configuration for GeneCodeR

# Set up genecoder transform information
genecoder.config <- GeneCodeR::extract_config_framework(F)
genecoder.config$transform$from <- 1
genecoder.config$transform$to <- 2
genecoder.config$extract_spots$window_size <- 30

Set up validation functions to evaluate statistically significant differences via a t-test, and, cosine similarity.

# Testing functions

# cosine metric for similarity between observations

test_sample_and_genes <- function(a,b,non_zero_markers,test_type="cosine"){

  if (test_type == "t.test"){
    
    return(
      list(
        
        sample_wise = do.call('c',parallel::mclapply(c(1:dim(a)[1]),function(X){
          
          t.test(as.numeric(a[X,non_zero_markers[X,]]),as.numeric(b[X,non_zero_markers[X,]]))$p.value
          
        },mc.cores = 8)),
        
        gene_wise = do.call('c',parallel::mclapply(c(1:dim(a)[2]),function(X){
          
          t.test(as.numeric(a[non_zero_markers[,X],X]),as.numeric(b[non_zero_markers[,X],X]))$p.value
          
        },mc.cores = 8))
        
      )
    )
  } 

  if (test_type == "cosine"){
    return(
      list(
        
        sample_wise = do.call('c',parallel::mclapply(c(1:dim(a)[1]),function(X){
          
          lsa::cosine(as.numeric(a[X,non_zero_markers[X,]]),as.numeric(b[X,non_zero_markers[X,]]))
          
        },mc.cores = 8)),
        
        gene_wise = do.call('c',parallel::mclapply(c(1:dim(a)[2]),function(X){
          
          lsa::cosine(as.numeric(a[non_zero_markers[,X],X]),as.numeric(b[non_zero_markers[,X],X]))
          
        },mc.cores = 8))
        
      )
    )
  } 
}

Rotation validation

Rotation testing is used to evaluate how rotated images affect the differences between the rotated image transformed to gene expression and the observed gene expression. It is expected that regardless of rotation, the gene expression should not be different across rotated images transformed to gene expression, and thus have a high cosine similarity correlation (close to 1).

# Spatial rotation testing


rotate_spot2gex <- list()
for (rotate_val in c(0,90,180,270)){
  
  genecoder.config$extract_spots$rotation <- rotate_val

  rotate_test_spot_data <- GeneCodeR::prepare_spot(file_path_list = test_file_path_list,meta_info_list = meta_info_list,config = genecoder.config, gex_data = test_gex_data$gex)

  rotate_spot2gex[[as.character(rotate_val)]] <- GeneCodeR::genecoder(model=genecoder.model, x = rotate_test_spot_data$spot, config = genecoder.config, model_type = "gcode")

}
## [1] "Extracting spots"
## [1] "Preparing spot      1"
## [1] "Preparing spot      2"
## [1] "Preparing spot      3"
## [1] "Preparing spot      4"
## [1] "Preparing spot      5"
## [1] "Preparing spot      6"
## [1] "Preparing spot      7"
## [1] "Done preparation!"
## [1] "Extracting spots"
## [1] "Preparing spot      1"
## [1] "Preparing spot      2"
## [1] "Preparing spot      3"
## [1] "Preparing spot      4"
## [1] "Preparing spot      5"
## [1] "Preparing spot      6"
## [1] "Preparing spot      7"
## [1] "Done preparation!"
## [1] "Extracting spots"
## [1] "Preparing spot      1"
## [1] "Preparing spot      2"
## [1] "Preparing spot      3"
## [1] "Preparing spot      4"
## [1] "Preparing spot      5"
## [1] "Preparing spot      6"
## [1] "Preparing spot      7"
## [1] "Done preparation!"
## [1] "Extracting spots"
## [1] "Preparing spot      1"
## [1] "Preparing spot      2"
## [1] "Preparing spot      3"
## [1] "Preparing spot      4"
## [1] "Preparing spot      5"
## [1] "Preparing spot      6"
## [1] "Preparing spot      7"
## [1] "Done preparation!"
count <- 0
cosine.simil_scores <- c()
for (i in c(1:4)){
  for (j in c(1:4)){
    if (i>j){
      count <- count + 1
      cosine.simil_scores[[count]] <- test_sample_and_genes(a = rotate_spot2gex[[i]],b = rotate_spot2gex[[j]], non_zero_markers = non_zero_markers, test_type = "t.test") 
    }
  }
}
rotate_spot2gex <- cosine.simil_scores
print(c("rotate cosine correlation; sample-wise",paste(c("0vs90:    ","0vs180:    ","0vs270:    ", "90vs180","90vs270","180vs270"),round(do.call('c',lapply(rotate_spot2gex,function(X){mean(X$sample_wise)})),5))))
## [1] "rotate cosine correlation; sample-wise"
## [2] "0vs90:     0.99986"                    
## [3] "0vs180:     0.99983"                   
## [4] "0vs270:     0.99987"                   
## [5] "90vs180 0.99986"                       
## [6] "90vs270 0.99983"                       
## [7] "180vs270 0.99986"
print(c("rotate cosine correlation; gene-wise",paste(c("0vs90:    ","0vs180:    ","0vs270:    ", "90vs180","90vs270","180vs270"),round(do.call('c',lapply(rotate_spot2gex,function(X){mean(X$gene_wise)})),5))))
## [1] "rotate cosine correlation; gene-wise"
## [2] "0vs90:     1"                        
## [3] "0vs180:     1"                       
## [4] "0vs270:     1"                       
## [5] "90vs180 1"                           
## [6] "90vs270 1"                           
## [7] "180vs270 1"
rm(list=ls())
gc()
##           used (Mb) gc trigger    (Mb) limit (Mb)   max used    (Mb)
## Ncells  753687 40.3    2498008   133.5         NA    3122510   166.8
## Vcells 1371909 10.5 1535690547 11716.4      16384 1639285802 12506.8