Last updated: 2024-12-20

Checks: 7 0

Knit directory: KODAMA-Analysis/

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(20240618) 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 16e454f. 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:    .RData
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/figure/

Untracked files:
    Untracked:  KODAMA.svg
    Untracked:  Rplots.pdf
    Untracked:  code/Acinar_Cell_Carcinoma.ipynb
    Untracked:  code/Adenocarcinoma.ipynb
    Untracked:  code/Adjacent_normal_section.ipynb
    Untracked:  code/DLFPC_preprocessing.R
    Untracked:  code/DLPFC - BANKSY.R
    Untracked:  code/DLPFC - BASS.R
    Untracked:  code/DLPFC - BAYESPACE.R
    Untracked:  code/DLPFC - Nonspatial.R
    Untracked:  code/DLPFC - PRECAST.R
    Untracked:  code/DLPFC_comparison.R
    Untracked:  code/DLPFC_results_analysis.R
    Untracked:  code/VisiumHD-CRC.ipynb
    Untracked:  data/DLFPC-Br5292-input.RData
    Untracked:  data/DLFPC-Br5595-input.RData
    Untracked:  data/DLFPC-Br8100-input.RData
    Untracked:  data/DLPFC-general.RData
    Untracked:  data/spots_classification_ALL.csv
    Untracked:  data/trajectories.RData
    Untracked:  data/trajectories_VISIUMHD.RData
    Untracked:  output/BANSKY-results.RData
    Untracked:  output/BASS-results.RData
    Untracked:  output/BayesSpace-results.RData
    Untracked:  output/CRC-image.RData
    Untracked:  output/CRC-image2.RData
    Untracked:  output/DL.RData
    Untracked:  output/DLFPC-All-2.RData
    Untracked:  output/DLFPC-All.RData
    Untracked:  output/DLFPC-Br5292.RData
    Untracked:  output/DLFPC-Br5595.RData
    Untracked:  output/DLFPC-Br8100.RData
    Untracked:  output/DLPFC1.svg
    Untracked:  output/DLPFC_all_cluster.svg
    Untracked:  output/Figure 1 - boxplot.pdf
    Untracked:  output/Figure 2 - DLPFC 10.pdf
    Untracked:  output/KODAMA-results.RData
    Untracked:  output/KODAMA_DLPFC_All_original.svg
    Untracked:  output/KODAMA_DLPFC_Br5595.svg
    Untracked:  output/KODAMA_DLPFC_Br5595_slide.svg
    Untracked:  output/MERFISH.RData
    Untracked:  output/Nonspatial-results.RData
    Untracked:  output/PRECAST-results.RData
    Untracked:  output/Prostate.RData
    Untracked:  output/VisiumHD3.RData
    Untracked:  output/image.RData
    Untracked:  remove.RData
    Untracked:  remove2.RData

Unstaged changes:
    Deleted:    analysis/D1.Rmd
    Deleted:    analysis/DLPFC-12.Rmd
    Deleted:    analysis/DLPFC-4.Rmd
    Deleted:    analysis/DLPFC1.Rmd
    Deleted:    analysis/DLPFC10.Rmd
    Deleted:    analysis/DLPFC2.Rmd
    Deleted:    analysis/DLPFC3.Rmd
    Deleted:    analysis/DLPFC4.Rmd
    Deleted:    analysis/DLPFC5.Rmd
    Deleted:    analysis/DLPFC6.Rmd
    Deleted:    analysis/DLPFC7.Rmd
    Deleted:    analysis/DLPFC8.Rmd
    Deleted:    analysis/DLPFC9.Rmd
    Deleted:    analysis/Du1.Rmd
    Deleted:    analysis/Du10.Rmd
    Deleted:    analysis/Du11.Rmd
    Deleted:    analysis/Du12.Rmd
    Deleted:    analysis/Du13.Rmd
    Deleted:    analysis/Du14.Rmd
    Deleted:    analysis/Du15.Rmd
    Deleted:    analysis/Du16.Rmd
    Deleted:    analysis/Du17.Rmd
    Deleted:    analysis/Du18.Rmd
    Deleted:    analysis/Du19.Rmd
    Deleted:    analysis/Du2.Rmd
    Deleted:    analysis/Du20.Rmd
    Deleted:    analysis/Du3.Rmd
    Deleted:    analysis/Du4.Rmd
    Deleted:    analysis/Du5.Rmd
    Deleted:    analysis/Du6.Rmd
    Deleted:    analysis/Du7.Rmd
    Deleted:    analysis/Du8.Rmd
    Deleted:    analysis/Du9.Rmd
    Modified:   analysis/Giotto.Rmd
    Deleted:    analysis/STARmap.Rmd
    Modified:   analysis/VisiumHD.Rmd
    Modified:   code/VisiumHD_CRC_download.sh

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/DLPFC.Rmd) and HTML (docs/DLPFC.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
Rmd 16e454f Stefano Cacciatore 2024-12-20 Start my new project
html 7b2cb8c Stefano Cacciatore 2024-12-16 Build site.
Rmd 8a5f6c8 Stefano Cacciatore 2024-12-16 Start my new project
html 374d5f0 Stefano Cacciatore 2024-12-14 Build site.
Rmd 6efcd94 Stefano Cacciatore 2024-12-14 Start my new project
html f6bab12 Stefano Cacciatore 2024-10-19 Build site.
Rmd 3490a11 Stefano Cacciatore 2024-10-19 Start my new project
Rmd 43ad12f Stefano Cacciatore 2024-10-18 Start my new project
Rmd 2c73e5e Stefano Cacciatore 2024-10-18 Start my new project
Rmd d75807e Stefano Cacciatore 2024-10-17 Start my new project
html ede33d2 Stefano Cacciatore 2024-10-17 Build site.
Rmd 1ec4751 Stefano Cacciatore 2024-10-17 Start my new project
html 773f9ef Stefano Cacciatore 2024-10-16 Build site.
Rmd df98881 Stefano Cacciatore 2024-10-16 Start my new project
Rmd 47b0163 Stefano Cacciatore 2024-10-15 Start my new project
html fd8d092 Stefano Cacciatore 2024-10-15 Build site.
Rmd e31e3e8 Stefano Cacciatore 2024-10-15 Start my new project
html 1edc32b Stefano Cacciatore 2024-10-11 Build site.
Rmd eaad1a0 Stefano Cacciatore 2024-10-11 Start my new project
html c9d54ee Stefano Cacciatore 2024-10-11 Build site.
Rmd fa049de Stefano Cacciatore 2024-10-11 Start my new project
Rmd 454b8fc Stefano Cacciatore 2024-10-11 Start my new project
html 1352d91 Stefano Cacciatore 2024-10-10 Build site.
Rmd 1b119a0 Stefano Cacciatore 2024-10-10 Start my new project
html 6038af1 Stefano Cacciatore 2024-10-09 Build site.
Rmd d141628 Stefano Cacciatore 2024-10-09 Start my new project
html d1192e9 Stefano Cacciatore 2024-08-12 Build site.
html 3374e66 Stefano Cacciatore 2024-08-06 Build site.
html 35ce733 Stefano Cacciatore 2024-08-03 Build site.
Rmd 06f7055 Stefano Cacciatore 2024-08-02 Start my new project
Rmd 7be8f59 tkcaccia 2024-07-15 updates
Rmd f8ca54a tkcaccia 2024-07-14 update
html f8ca54a tkcaccia 2024-07-14 update
html 3ea09a6 GitHub 2024-07-08 Update DLPFC.html
html 93915d8 GitHub 2024-07-04 Update DLPFC.html
html ee4ee17 GitHub 2024-06-19 Add files via upload
Rmd 615fc05 GitHub 2024-06-19 Add files via upload

Introduction

Here, we apply KODAMA to analyze the human dorsolateral prefrontal cortex (DLPFC) data by 10x Visium from Maynard et al., 2021. The links to download the raw data and H&E full resolution images can be found in the LieberInstitute/spatialLIBD github page.

Loading the required libraries

library("nnSVG")
library("scater")
library("scran")
library("scry")
library("SPARK")
library("harmony")
library("Seurat")
library("spatialLIBD")
library("KODAMAextra")
library("mclust")
library("slingshot")
library("irlba")
library("Rnanoflann")
library("ggpubr")

Download the dataset

spe <- fetch_data(type = 'spe')

Extract the metadata information

n.cores=40
splitting = 100
spatial.resolution = 0.3
aa_noise=3
gene_number=2000
graph = 20
seed=543210


set.seed(seed)
ID=unlist(lapply(strsplit(rownames(colData(spe)),"-"),function(x) x[1]))
samples=colData(spe)$sample_id
rownames(colData(spe))=paste(ID,samples,sep="-")

txtfile=paste(splitting,spatial.resolution,aa_noise,2,gene_number,sep="_")

sample_names=c("151507",
               "151508",
               "151509",
               "151510",
               "151669",
               "151670",
               "151671",
               "151672",
               "151673",
               "151674",
               "151675",
               "151676")
subject_names= c("Br5292","Br5595", "Br8100")
metaData = SingleCellExperiment::colData(spe)
expr = SingleCellExperiment::counts(spe)
sample_names <- paste0("sample_", unique(colData(spe)$sample_id))
sample_names <-  unique(colData(spe)$sample_id)
dim(spe)
[1] 33538 47681
# identify mitochondrial genes
is_mito <- grepl("(^MT-)|(^mt-)", rowData(spe)$gene_name)
table(is_mito)
is_mito
FALSE  TRUE 
33525    13 
# calculate per-spot QC metrics
spe <- addPerCellQC(spe, subsets = list(mito = is_mito))

# select QC thresholds
qc_lib_size <- colData(spe)$sum < 500
qc_detected <- colData(spe)$detected < 250
qc_mito <- colData(spe)$subsets_mito_percent > 30
qc_cell_count <- colData(spe)$cell_count > 12

# spots to discard
discard <- qc_lib_size | qc_detected | qc_mito | qc_cell_count
table(discard)
discard
FALSE  TRUE 
46653  1028 
colData(spe)$discard <- discard
# filter low-quality spots
spe <- spe[, !colData(spe)$discard]
dim(spe)
[1] 33538 46653
spe <- filter_genes(
  spe,
  filter_genes_ncounts = 2,   #ncounts
  filter_genes_pcspots = 0.5,
  filter_mito = TRUE
)

dim(spe)
[1]  6623 46653
sel= !is.na(colData(spe)$layer_guess_reordered)
spe = spe[,sel]
dim(spe)
[1]  6623 46318
spe <- computeLibraryFactors(spe)
spe <- logNormCounts(spe)

 subjects=colData(spe)$subject
 labels=as.factor(colData(spe)$layer_guess_reordered)
 xy=as.matrix(spatialCoords(spe))
 samples=colData(spe)$sample_id
 
 cols_cluster <- c("#0000b6", "#81b29a", "#f2cc8f","#e07a5f",
          "#cc00b6", "#81ccff", "#33b233")

  plot_slide(xy,samples,labels,col=cols_cluster)

Version Author Date
374d5f0 Stefano Cacciatore 2024-12-14
f6bab12 Stefano Cacciatore 2024-10-19
png 
  2 

Gene selection

The identification of genes that display spatial expression patterns is performed using the SPARKX method (Zhu et al. (2021)). The genes are ranked based on the median value of the logarithm value of the p-value obtained in each slide individually.

top=multi_SPARKX(spe,n.cores=n.cores)
Warning in asMethod(object): sparse->dense coercion: allocating vector of size
2.3 GiB
data=as.matrix(t(logcounts(spe)[top[1:gene_number],]))
samples=colData(spe)$sample_id
labels=as.factor(colData(spe)$layer_guess_reordered)
names(labels)=rownames(colData(spe))
subjects=colData(spe)$subject

Patient Br5595

subject_names="Br5595"
nclusters=5

spe_sub <- spe[, colData(spe)$subject ==  subject_names]
 # subjects=colData(spe_sub)$subject
dim(spe_sub)
[1]  6623 14646
#  spe_sub <- runPCA(spe_sub, 50,subset_row = top[1:gene_number], scale=TRUE)
#pca=reducedDim(spe_sub,type = "PCA")[,1:50]
  
spe_sub <- spe[, colData(spe)$subject ==  subject_names]
sel= subjects ==  subject_names
        
data_Br5595=data[sel,top[1:gene_number]]
        
RNA.scaled=scale(data_Br5595)
pca_results <- irlba(A = RNA.scaled, nv = 50)
pca_Br5595 <- pca_results$u %*% diag(pca_results$d)[,1:50]
rownames(pca_Br5595)=rownames(data_Br5595)
colnames(pca_Br5595)=paste("PC",1:50,sep="")
labels=as.factor(colData(spe_sub)$layer_guess_reordered)
names(labels)=rownames(colData(spe_sub))
xy=as.matrix(spatialCoords(spe_sub))
rownames(xy)=rownames(colData(spe_sub))
samples=colData(spe_sub)$sample_id


plot(pca_Br5595, pch=20,col=as.factor(colData(spe_sub)$sample_id))

Version Author Date
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09

KODAMA analysis

set.seed(seed)
kk=KODAMA.matrix.parallel(pca_Br5595,
                          spatial = xy,
                          samples=samples,
                          FUN= "fastpls" ,
                          landmarks = 100000,
                          splitting = splitting,
                          ncomp = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          seed = seed)
Calculating Network

Calculating Network spatial
socket cluster with 40 nodes on host 'localhost'
================================================================================
Finished parallel computation

[1] "Calculation of dissimilarity matrix..."
================================================================================
print("KODAMA finished")
[1] "KODAMA finished"
config=umap.defaults
config$n_threads = n.cores
config$n_sgd_threads = "auto"

kk_UMAP=KODAMA.visualization(kk,method="UMAP",config=config)
plot(kk_UMAP,pch=20,col=cols_cluster[labels])

Version Author Date
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
png 
  2 

Graph-based clustering

    # Graph-based clustering

g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = 20)
g_walk <- igraph::cluster_walktrap(g)
clu <- as.character(igraph::cut_at(g_walk, no = 2))
plot(kk_UMAP,pch=20,col=cols_cluster[as.factor(clu)])

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-14
  plot_slide(xy,as.factor(samples),clu,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
png 
  2 
FB=names(which.min(table(clu)))
selFB=clu!=FB

 # kk_UMAP=kk_UMAP[selFB,]
#  labels=labels[selFB]
#  samples=samples[selFB]
#  xy=xy[selFB,]
    
      
g <- bluster::makeSNNGraph(as.matrix(kk_UMAP[selFB,]), k = graph)
g_walk <- igraph::cluster_walktrap(g)
clu <- as.character(igraph::cut_at(g_walk, no = nclusters))
plot(kk_UMAP[selFB,],pch=20,col=as.factor(clu))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1edc32b Stefano Cacciatore 2024-10-11
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-14
ref=refine_SVM(xy[selFB,],clu,samples[selFB],cost=100)
[1] "151669"
[1] "151670"
[1] "151671"
[1] "151672"
u=unique(samples[selFB])
for(j in u){
  sel=samples[selFB]==j
  print(mclust::adjustedRandIndex(labels[selFB][sel],ref[sel]))
}
[1] 0.7433529
[1] 0.7503761
[1] 0.8065421
[1] 0.745585
      ###########
      


plot_slide(xy,samples,labels,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1edc32b Stefano Cacciatore 2024-10-11
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-14
plot_slide(xy[selFB,],samples[selFB],ref,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-14
kk_UMAP_Br5595=kk_UMAP
samples_Br5595=samples
xy_Br5595=xy
labels_Br5595=labels
subject_names_Br5595=subject_names
ref_Br5595=ref
clu_Br5595=clu

save(kk_UMAP_Br5595,samples_Br5595,xy_Br5595,labels_Br5595,subject_names_Br5595,ref_Br5595,clu_Br5595,selFB,file="output/DLFPC-Br5595.RData")

save(data_Br5595,pca_Br5595,samples_Br5595,xy_Br5595,labels_Br5595,subject_names_Br5595,selFB,file="data/DLFPC-Br5595-input.RData")

Patient Br5292

subject_names="Br5292"
nclusters=7

  spe_sub <- spe[, colData(spe)$subject ==  subject_names]
  dim(spe_sub)
[1]  6623 17734
#  spe_sub <- runPCA(spe_sub, 50,subset_row = top[1:gene_number], scale=TRUE)

  #pca=reducedDim(spe_sub,type = "PCA")[,1:50]
  
  
  
    
        
        spe_sub <- spe[, colData(spe)$subject ==  subject_names]
        sel= subjects ==  subject_names
        

        data_Br5292=data[sel,top[1:gene_number]]
        
        RNA.scaled=scale(data_Br5292)
        pca_results <- irlba(A = RNA.scaled, nv = 50)
        pca_Br5292 <- pca_results$u %*% diag(pca_results$d)[,1:50]
        rownames(pca_Br5292)=rownames(data_Br5292)
        colnames(pca_Br5292)=paste("PC",1:50,sep="")
        labels=as.factor(colData(spe_sub)$layer_guess_reordered)
        names(labels)=rownames(colData(spe_sub))
        xy=as.matrix(spatialCoords(spe_sub))
        rownames(xy)=rownames(colData(spe_sub))
        samples=colData(spe_sub)$sample_id
        
  plot(pca_Br5292, pch=20,col=as.factor(colData(spe_sub)$sample_id))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-14

KODAMA analysis

set.seed(seed)
kk=KODAMA.matrix.parallel(pca_Br5292,
                          
                          spatial = xy,
                          samples=samples,
                          FUN= "fastpls" ,
                          landmarks = 100000,
                          splitting = splitting,
                          ncomp = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          seed = seed)
Calculating Network

Calculating Network spatial
socket cluster with 40 nodes on host 'localhost'
================================================================================
Finished parallel computation

[1] "Calculation of dissimilarity matrix..."
================================================================================
  print("KODAMA finished")
[1] "KODAMA finished"
     config=umap.defaults
     config$n_threads = n.cores
     config$n_sgd_threads = "auto"
     kk_UMAP=KODAMA.visualization(kk,method="UMAP",config=config)

     plot(kk_UMAP,pch=20,col=as.factor(labels))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03

Graph-based clustering

    # Graph-based clustering

  
        
        g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = graph)
        
        g_walk <- igraph::cluster_walktrap(g)
        clu <- as.character(igraph::cut_at(g_walk, no = nclusters))
        
    plot(kk_UMAP,pch=20,col=as.factor(clu))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
        ref=refine_SVM(xy,clu,samples,cost=100)
[1] "151507"
[1] "151508"
[1] "151509"
[1] "151510"
        u=unique(samples)
        for(j in u){
          sel=samples==j
          
            print(mclust::adjustedRandIndex(labels[sel],ref[sel]))
        }
[1] 0.4462599
[1] 0.4843513
[1] 0.4496646
[1] 0.4064282
        ###########

          g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = graph)
          
          g_walk <- igraph::cluster_walktrap(g)
          clu <- as.character(igraph::cut_at(g_walk, no = nclusters))
          ref=refine_SVM(xy,clu,samples,cost=100)
[1] "151507"
[1] "151508"
[1] "151509"
[1] "151510"
          u=unique(samples)
          for(j in u){
            sel=samples==j
            print(mclust::adjustedRandIndex(labels[sel],ref[sel]))
          }
[1] 0.4462599
[1] 0.4843513
[1] 0.4496646
[1] 0.4064282
      ###########
      


plot_slide(xy,samples,labels,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
    plot_slide(xy,samples,ref,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
kk_UMAP_Br5292=kk_UMAP
samples_Br5292=samples
xy_Br5292=xy
labels_Br5292=labels
subject_names_Br5292=subject_names
ref_Br5292=ref
clu_Br5292=clu
save(kk_UMAP_Br5292,pca_Br5292,samples_Br5292,xy_Br5292,subject_names_Br5292,labels_Br5292,ref_Br5292,clu_Br5292,file="output/DLFPC-Br5292.RData")



save(data_Br5292,pca_Br5292,samples_Br5292,xy_Br5292,labels_Br5292,subject_names_Br5292,file="data/DLFPC-Br5292-input.RData")

Patient Br8100

subject_names="Br8100"

nclusters=7

  spe_sub <- spe[, colData(spe)$subject ==  subject_names]
  dim(spe_sub)
[1]  6623 13938
#  spe_sub <- runPCA(spe_sub, 50,subset_row = top[1:gene_number], scale=TRUE)

  #pca=reducedDim(spe_sub,type = "PCA")[,1:50]
  
  
  
    
        
        spe_sub <- spe[, colData(spe)$subject ==  subject_names]
        sel= subjects ==  subject_names
        

        data_Br8100=data[sel,top[1:gene_number]]
        
        RNA.scaled=scale(data_Br8100)
        pca_results <- irlba(A = RNA.scaled, nv = 50)
        pca_Br8100 <- pca_results$u %*% diag(pca_results$d)[,1:50]
        rownames(pca_Br8100)=rownames(data_Br8100)
        colnames(pca_Br8100)=paste("PC",1:50,sep="")
        labels=as.factor(colData(spe_sub)$layer_guess_reordered)
        names(labels)=rownames(colData(spe_sub))
        xy=as.matrix(spatialCoords(spe_sub))
        rownames(xy)=rownames(colData(spe_sub))
        samples=colData(spe_sub)$sample_id
        
  plot(pca_Br8100, pch=20,col=as.factor(colData(spe_sub)$sample_id))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03

KODAMA analysis

set.seed(seed)
kk=KODAMA.matrix.parallel(pca_Br8100,
                          spatial = xy,
                          samples=samples,
                          FUN= "fastpls" ,
                          landmarks = 100000,
                          splitting = splitting,
                          ncomp = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          seed = seed)
Calculating Network

Calculating Network spatial
socket cluster with 40 nodes on host 'localhost'
================================================================================
Finished parallel computation

[1] "Calculation of dissimilarity matrix..."
================================================================================
  print("KODAMA finished")
[1] "KODAMA finished"
     config=umap.defaults
     config$n_threads = n.cores
     config$n_sgd_threads = "auto"
     kk_UMAP=KODAMA.visualization(kk,method="UMAP",config=config)

     plot(kk_UMAP,pch=20,col=as.factor(labels))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10

Graph-based clustering

    # Graph-based clustering

  
        
        g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = graph)
        
        g_walk <- igraph::cluster_walktrap(g)
        clu <- as.character(igraph::cut_at(g_walk, no = nclusters))
        ref=refine_SVM(xy,clu,samples,cost=100)
[1] "151673"
[1] "151674"
[1] "151675"
[1] "151676"
        u=unique(samples)
        for(j in u){
          sel=samples==j
          
            print(mclust::adjustedRandIndex(labels[sel],ref[sel]))
        }
[1] 0.599265
[1] 0.658843
[1] 0.6486061
[1] 0.5928946
        ###########

          g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = graph)
          
          g_walk <- igraph::cluster_walktrap(g)
          clu <- as.character(igraph::cut_at(g_walk, no = nclusters))
          
           plot(kk_UMAP,pch=20,col=as.factor(clu))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
          ref=refine_SVM(xy,clu,samples,cost=100)
[1] "151673"
[1] "151674"
[1] "151675"
[1] "151676"
          u=unique(samples)
          for(j in u){
            sel=samples==j
            print(mclust::adjustedRandIndex(labels[sel],ref[sel]))
          }
[1] 0.599265
[1] 0.658843
[1] 0.6486061
[1] 0.5928946
      ###########
      
        

          
 plot_slide(xy,samples,labels,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
    plot_slide(xy,samples,ref,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
kk_UMAP_Br8100=kk_UMAP
samples_Br8100=samples
xy_Br8100=xy
labels_Br8100=labels
subject_names_Br8100=subject_names
ref_Br8100=ref
clu_Br8100=clu 
save(kk_UMAP_Br8100,pca_Br8100,samples_Br8100,xy_Br8100,subject_names_Br8100,labels_Br8100,ref_Br8100,clu_Br8100,file="output/DLFPC-Br8100.RData")


save(data_Br8100,pca_Br8100,samples_Br8100,xy_Br8100,labels_Br8100,subject_names_Br8100,file="data/DLFPC-Br8100-input.RData")

Saving the results

results_KODAMA <- list()
results_KODAMA$clusters <- list()
results_KODAMA$labels <- list()
results_KODAMA$feature_extraction <- list()
results_KODAMA$xy <- list()


results_KODAMA$clusters=c(results_KODAMA$clusters,tapply(ref_Br5292,samples_Br5292,function(x) x))
results_KODAMA$clusters=c(results_KODAMA$clusters,tapply(ref_Br5595,samples_Br5595[selFB],function(x) x))
results_KODAMA$clusters=c(results_KODAMA$clusters,tapply(ref_Br8100,samples_Br8100,function(x) x))

results_KODAMA$labels=c(results_KODAMA$labels,tapply(labels_Br5292,samples_Br5292,function(x) x))
results_KODAMA$labels=c(results_KODAMA$labels,tapply(labels_Br5595[selFB],samples_Br5595[selFB],function(x) x))
results_KODAMA$labels=c(results_KODAMA$labels,tapply(labels_Br8100,samples_Br8100,function(x) x))

results_KODAMA$feature_extraction=c(results_KODAMA$feature_extraction,by(kk_UMAP_Br5292,samples_Br5292,function(x) x))
results_KODAMA$feature_extraction=c(results_KODAMA$feature_extraction,by(kk_UMAP_Br5595[selFB,],samples_Br5595[selFB],function(x) x))
results_KODAMA$feature_extraction=c(results_KODAMA$feature_extraction,by(kk_UMAP_Br8100,samples_Br8100,function(x) x))

results_KODAMA$xy=c(results_KODAMA$xy,by(xy_Br5292,samples_Br5292,function(x) x))
results_KODAMA$xy=c(results_KODAMA$xy,by(xy_Br5595[selFB,],samples_Br5595[selFB],function(x) x))
results_KODAMA$xy=c(results_KODAMA$xy,by(xy_Br8100,samples_Br8100,function(x) x))

save(results_KODAMA,file="output/KODAMA-results.RData")

#seurat list preprocessing

source("code/DLFPC_preprocessing.R")
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12

12 Slides

PCA and HARMONY

 dim(spe_sub)
[1]  6623 13938
 spe <- runPCA(spe, 50,subset_row = top[1:gene_number], scale=TRUE)


 subjects=colData(spe)$subject
 labels=as.factor(colData(spe)$layer_guess_reordered)
 xy=as.matrix(spatialCoords(spe))
 samples=colData(spe)$sample_id
 
  
 

 spe <- RunHarmony(spe, "subject",lambda=NULL)
 pca=reducedDim(spe,type = "HARMONY")[,1:50]
 
 plot(pca, pch=20,col=as.factor(colData(spe_sub)$sample_id))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09

KODAMA

set.seed(seed)
kk=KODAMA.matrix.parallel(pca,
                          spatial = xy,
                          samples=samples,
                          FUN= "fastpls" ,
                          landmarks = 100000,
                          splitting = splitting,
                          ncomp = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          seed = seed)
Calculating Network

Calculating Network spatial
socket cluster with 40 nodes on host 'localhost'
================================================================================
Finished parallel computation

[1] "Calculation of dissimilarity matrix..."
================================================================================
print("KODAMA finished")
[1] "KODAMA finished"
config=umap.defaults
config$n_threads = n.cores
config$n_sgd_threads = "auto"

kk_UMAP=KODAMA.visualization(kk,method="UMAP",config=config)
plot(kk_UMAP,pch=20,col=as.factor(labels))

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
fd8d092 Stefano Cacciatore 2024-10-15
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03

CLUSTER

        g <- bluster::makeSNNGraph(as.matrix(kk_UMAP), k = graph)
        g_walk <- igraph::cluster_walktrap(g)
        clu <- as.character(igraph::cut_at(g_walk, no = 7))
        plot(kk_UMAP,pch=20,col=as.factor(clu)) 

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
    plot_slide(xy,samples,clu,col=cols_cluster)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
374d5f0 Stefano Cacciatore 2024-12-14
    mito=colData(spe)$subsets_mito_percent
    sel_local=(labels=="Layer3" | labels=="Layer4") & 
      (samples=="151669" | samples=="151670" | samples=="151671" | samples=="151672") &
      (clu==1 | clu==6)
  boxplot(mito[sel_local]~clu[sel_local],col=cols_cluster[c(1,6)] ,ylab="mito percent")

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
   wilcox.test(mito[sel_local]~clu[sel_local])

    Wilcoxon rank sum test with continuity correction

data:  mito[sel_local] by clu[sel_local]
W = 323194, p-value = 0.01216
alternative hypothesis: true location shift is not equal to 0

CLUSTER

  sel_rem=which(clu %in% names(sort(table(clu)))[1:2])

  kk_UMAP_clear=kk_UMAP[-sel_rem,]
  labels_clear=labels[-sel_rem]
  samples_clear=samples[-sel_rem]
  xy_clear=xy[-sel_rem,]
  data_clear=data[-sel_rem,]
  subjects_clear=subjects[-sel_rem]
  
  clu=kmeans(kk_UMAP_clear,7,nstart = 100)$cluster
  plot(kk_UMAP_clear,col=cols_cluster[labels_clear],pch=20)

Version Author Date
7b2cb8c Stefano Cacciatore 2024-12-16
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
  svg("output/KODAMA_DLPFC_All_original.svg")
  plot(kk_UMAP_clear,col=cols_cluster[labels_clear],pch=20)
dev.off()
png 
  2 
  plot(kk_UMAP_clear,col=cols_cluster[clu],pch=20)

u=unique(samples)
for(i in 1:length(u)){
  sel=samples==u[i]
  print(adjustedRandIndex(labels_clear[sel],clu[sel]))
}
[1] 0.5538318
[1] 0.5015062
[1] 0.4896804
[1] 0.4760243
[1] 0.3739001
[1] 0.3609056
[1] 0.4066322
[1] 0.4383128
[1] 0.548363
[1] 0.572728
[1] 0.536385
[1] 0.5275014
ref=refine_SVM(xy_clear,clu,samples_clear,cost=100)
[1] "151507"
[1] "151508"
[1] "151509"
[1] "151510"
[1] "151669"
[1] "151670"
[1] "151671"
[1] "151672"
[1] "151673"
[1] "151674"
[1] "151675"
[1] "151676"
names(ref)=rownames(data_clear)
names(labels_clear)=rownames(data_clear)
save(labels_clear,ref,subjects_clear,samples_clear,file="output/DL.RData")

u=unique(samples)
for(i in 1:length(u)){
  sel=samples[-sel_rem]==u[i]
  print(adjustedRandIndex(labels_clear[sel],ref[sel]))
}
[1] 0.5874289
[1] 0.5343093
[1] 0.507712
[1] 0.5028689
[1] 0.4241738
[1] 0.426007
[1] 0.4838957
[1] 0.5635095
[1] 0.5759575
[1] 0.6173067
[1] 0.6233622
[1] 0.5917995
plot_slide(xy_clear,samples_clear,ref,col=cols_cluster)

png 
  2 
subclusters=sort(names(sort(table(ref,labels_clear)[,"Layer3"],decreasing = TRUE)[1:3]))

sa_sel=(samples_clear=="151669" | samples_clear=="151670" | samples_clear=="151671" | samples_clear=="151672")

aa=apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[1] & sa_sel],x[(ref==subclusters[2] ) & sa_sel],alternative = "greater")$p.value) +
  apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[1] & sa_sel],x[(ref==subclusters[3]) & sa_sel],alternative = "greater")$p.value)
sort(aa)[1:10]
ENSG00000115756 ENSG00000162545 ENSG00000251562 ENSG00000128989 ENSG00000156508 
   4.077044e-87    1.297111e-58    1.167145e-45    3.158706e-27    7.428920e-27 
ENSG00000170323 ENSG00000173432 ENSG00000162734 ENSG00000174444 ENSG00000144119 
   8.491823e-24    2.299894e-22    5.756291e-19    2.143882e-17    1.281161e-16 
box_sel=(samples_clear=="151669" | samples_clear=="151670" | samples_clear=="151671" | samples_clear=="151672") & (ref==subclusters[1] | ref==subclusters[2] | ref==subclusters[3])
tapply(data_clear[box_sel,names(sort(aa)[1])],as.numeric(as.vector(ref[box_sel])),mean)
        1         3         5 
0.8993759 0.2901012 0.4536130 
df=data.frame(variable=data_clear[box_sel,names(sort(aa)[1])],labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(1,3)
my_comparisons[[2]]=c(1,2)

Nplot1=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(names(sort(aa)[1]))+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot1

a1=apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[2] & sa_sel],x[(ref==subclusters[3]) & sa_sel],alternative = "greater")$p.value)
  a2=apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[2] & sa_sel],x[(ref==subclusters[1] ) & sa_sel],alternative = "greater")$p.value)
  
aa=a1+a2
  
sort(aa)[1:10]
ENSG00000197971 ENSG00000100285 ENSG00000198963 ENSG00000134986 ENSG00000105711 
   8.272578e-43    7.250085e-27    4.083530e-18    4.807172e-18    1.834065e-17 
ENSG00000123560 ENSG00000123416 ENSG00000151552 ENSG00000118785 ENSG00000100362 
   2.257179e-17    2.991447e-17    6.118685e-17    5.887837e-16    8.522788e-16 
a_choice=names(sort(aa)[1])
tapply(data_clear[box_sel,a_choice],as.numeric(as.vector(ref[box_sel])),mean)
       1        3        5 
1.581691 2.245112 1.917619 
df=data.frame(variable=data_clear[box_sel,a_choice],labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(2,3)
my_comparisons[[2]]=c(1,2)

Nplot2=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(a_choice)+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot2

aa=apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[3] & sa_sel],x[(ref==subclusters[1] ) & sa_sel],alternative = "greater")$p.value) +
  apply(data_clear,2,function(x) wilcox.test(x[ref==subclusters[3] & sa_sel],x[(ref==subclusters[2]) & sa_sel],alternative = "greater")$p.value)
sort(aa)[1:10]
ENSG00000120885 ENSG00000154146 ENSG00000128245 ENSG00000162706 ENSG00000205542 
   4.442257e-09    1.696138e-06    1.515157e-05    7.404897e-04    1.177251e-03 
ENSG00000167996 ENSG00000141433 ENSG00000117152 ENSG00000163032 ENSG00000143933 
   1.414809e-03    1.648107e-03    1.919411e-03    2.398576e-03    2.477883e-03 
tapply(data_clear[box_sel,names(sort(aa)[1])],as.numeric(as.vector(ref[box_sel])),mean)
       1        3        5 
3.288123 3.306209 3.391248 
df=data.frame(variable=data_clear[box_sel,names(sort(aa)[1])],labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(2,3)
my_comparisons[[2]]=c(1,3)

Nplot3=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(names(sort(aa)[1]))+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot3

library(ggalluvial)

sel_sub=subjects_clear=="Br5292"
al=data.frame(expand.grid(list(levels(labels_clear),1:7)),freq=as.numeric(table(labels_clear[sel_sub],ref[sel_sub])))
al$Var2=as.factor(al$Var2)


ggplot(data = al,
       aes(axis1 = Var1, axis2 = Var2, y = freq)) +
  geom_alluvium(aes(fill = Var2)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  theme_void()

sel_sub=subjects_clear=="Br5595"
al=data.frame(expand.grid(list(levels(labels_clear),1:7)),freq=as.numeric(table(labels_clear[sel_sub],ref[sel_sub])))
al$Var2=as.factor(al$Var2)


ggplot(data = al,
       aes(axis1 = Var1, axis2 = Var2, y = freq)) +
  geom_alluvium(aes(fill = Var2)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  theme_void()

sel_sub=subjects_clear=="Br8100"
al=data.frame(expand.grid(list(levels(labels_clear),1:7)),freq=as.numeric(table(labels_clear[sel_sub],ref[sel_sub])))
al$Var2=as.factor(al$Var2)


ggplot(data = al,
       aes(axis1 = Var1, axis2 = Var2, y = freq)) +
  geom_alluvium(aes(fill = Var2)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  theme_void()

 library("GSVA")
 library("GSA")
 library("VAM")
 geneset=GSA.read.gmt("../Genesets/msigdb_v2023.2.Hs_GMTs/h.all.v2023.2.Hs.symbols.gmt")
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 names(geneset$genesets)=geneset$geneset.names
 genesets=geneset$genesets

countdata <- as.matrix(t(logcounts(spe)))

# library("gprofiler2")
# genes=gconvert(rownames(spe),organism="hsapiens",target="GENECARDS",filter_na = F)$target

genes=rowData(spe)[,"gene_name"]
spot_name=colnames(spe)
colnames(countdata)=genes



li=lapply(genesets,function(x) which(genes %in% x))

VAM=vamForCollection(gene.expr=countdata, gene.set.collection=li)
pathway=VAM$distance.sq
  pathway_clear=pathway[-sel_rem,]







aa=apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[1] & sa_sel],x[(ref==subclusters[2] ) & sa_sel],alternative = "greater")$p.value) +
  apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[1] & sa_sel],x[(ref==subclusters[3]) & sa_sel],alternative = "greater")$p.value)
sort(aa)[1:10]
           HALLMARK_MYC_TARGETS_V1           HALLMARK_MITOTIC_SPINDLE 
                      1.990192e-46                       6.156770e-31 
  HALLMARK_TNFA_SIGNALING_VIA_NFKB            HALLMARK_G2M_CHECKPOINT 
                      7.508055e-31                       3.118049e-29 
              HALLMARK_P53_PATHWAY            HALLMARK_UV_RESPONSE_DN 
                      4.828974e-28                       3.861350e-26 
          HALLMARK_APICAL_JUNCTION       HALLMARK_ALLOGRAFT_REJECTION 
                      4.553265e-25                       4.117158e-24 
HALLMARK_UNFOLDED_PROTEIN_RESPONSE   HALLMARK_ESTROGEN_RESPONSE_EARLY 
                      2.893726e-23                       2.658940e-22 
box_sel=(samples_clear=="151669" | samples_clear=="151670" | samples_clear=="151671" | samples_clear=="151672") & (ref==subclusters[1] | ref==subclusters[2] | ref==subclusters[3])
tapply(pathway_clear[box_sel,names(sort(aa)[1])],as.numeric(as.vector(ref[box_sel])),mean)
       1        3        5 
284.1904 261.3498 267.3446 
df=data.frame(variable=log(1+pathway_clear[box_sel,names(sort(aa)[1])]),labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(1,3)
my_comparisons[[2]]=c(1,2)

Nplot1=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(names(sort(aa)[1]))+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot1

a1=apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[2] & sa_sel],x[(ref==subclusters[3]) & sa_sel],alternative = "greater")$p.value)
  a2=apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[2] & sa_sel],x[(ref==subclusters[1] ) & sa_sel],alternative = "greater")$p.value)
  
aa=a1+a2
  
sort(aa)[1:10]
            HALLMARK_PANCREAS_BETA_CELLS 
                               0.4485750 
HALLMARK_REACTIVE_OXYGEN_SPECIES_PATHWAY 
                               0.6973665 
      HALLMARK_OXIDATIVE_PHOSPHORYLATION 
                               0.9973218 
      HALLMARK_INTERFERON_ALPHA_RESPONSE 
                               1.2778413 
                 HALLMARK_APICAL_SURFACE 
                               1.6199914 
                 HALLMARK_MYC_TARGETS_V2 
                               1.7282614 
              HALLMARK_KRAS_SIGNALING_UP 
                               1.8416405 
        HALLMARK_CHOLESTEROL_HOMEOSTASIS 
                               1.8770509 
     HALLMARK_WNT_BETA_CATENIN_SIGNALING 
                               1.9169702 
          HALLMARK_INFLAMMATORY_RESPONSE 
                               1.9761701 
a_choice=names(sort(aa)[1])
tapply(pathway_clear[box_sel,a_choice],as.numeric(as.vector(ref[box_sel])),mean)
       1        3        5 
24.94728 24.15937 24.28457 
df=data.frame(variable=pathway_clear[box_sel,a_choice],labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(2,3)
my_comparisons[[2]]=c(1,2)

Nplot2=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(a_choice)+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot2

aa=apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[3] & sa_sel],x[(ref==subclusters[1] ) & sa_sel],alternative = "greater")$p.value) +
  apply(pathway_clear,2,function(x) wilcox.test(x[ref==subclusters[3] & sa_sel],x[(ref==subclusters[2]) & sa_sel],alternative = "greater")$p.value)
sort(aa)[1:10]
      HALLMARK_OXIDATIVE_PHOSPHORYLATION 
                             0.002678399 
                   HALLMARK_ADIPOGENESIS 
                             0.129896240 
HALLMARK_REACTIVE_OXYGEN_SPECIES_PATHWAY 
                             0.304488866 
        HALLMARK_CHOLESTEROL_HOMEOSTASIS 
                             0.845445559 
          HALLMARK_FATTY_ACID_METABOLISM 
                             0.919218821 
                   HALLMARK_ANGIOGENESIS 
                             0.946597877 
             HALLMARK_HEDGEHOG_SIGNALING 
                             0.987543573 
               HALLMARK_MTORC1_SIGNALING 
                             0.994376725 
                HALLMARK_SPERMATOGENESIS 
                             0.995250421 
                     HALLMARK_PEROXISOME 
                             0.999158738 
tapply(pathway_clear[box_sel,names(sort(aa)[1])],as.numeric(as.vector(ref[box_sel])),mean)
       1        3        5 
397.0898 405.1837 409.4987 
df=data.frame(variable=pathway_clear[box_sel,names(sort(aa)[1])],labels=as.numeric(as.vector(ref[box_sel])))

my_comparisons=list()
my_comparisons[[1]]=c(2,3)
my_comparisons[[2]]=c(1,3)

Nplot3=ggboxplot(df, x = "labels", y = "variable",fill="labels",
                 width = 0.8,
                 palette=cols_cluster)+  
  ylab(names(sort(aa)[1]))+ xlab("")+
  stat_compare_means(comparisons = my_comparisons,method="wilcox.test")

Nplot3

TRAJECTORY

d <- slingshot(kk_UMAP_clear, clusterLabels = clu)
trajectory=d@metadata$curves$Lineage1$s
k=Rnanoflann::nn(trajectory,kk_UMAP_clear,1)
map_color=rainbow(nrow(trajectory))[k$indices]
plot(kk_UMAP_clear,pch=20,col=map_color)

plot_slide(xy_clear,samples_clear,k$indices,col=rainbow(nrow(trajectory)))


sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.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: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
 [1] VAM_1.1.0                   MASS_7.3-61                
 [3] GSA_1.03.3                  GSVA_1.52.3                
 [5] ggalluvial_0.12.5           ggpubr_0.6.0               
 [7] Rnanoflann_0.0.3            irlba_2.3.5.1              
 [9] slingshot_2.12.0            TrajectoryUtils_1.12.0     
[11] princurve_2.1.6             mclust_6.1.1               
[13] KODAMAextra_1.2             e1071_1.7-16               
[15] doParallel_1.0.17           iterators_1.0.14           
[17] foreach_1.5.2               KODAMA_4.0                 
[19] Matrix_1.7-1                umap_0.2.10.0              
[21] Rtsne_0.17                  minerva_1.5.10             
[23] spatialLIBD_1.16.2          SpatialExperiment_1.14.0   
[25] Seurat_5.1.0                SeuratObject_5.0.2         
[27] sp_2.1-4                    harmony_1.2.3              
[29] Rcpp_1.0.13-1               SPARK_1.1.1                
[31] scry_1.16.0                 scran_1.32.0               
[33] scater_1.32.1               ggplot2_3.5.1              
[35] scuttle_1.14.0              SingleCellExperiment_1.26.0
[37] SummarizedExperiment_1.34.0 Biobase_2.64.0             
[39] GenomicRanges_1.56.2        GenomeInfoDb_1.40.1        
[41] IRanges_2.38.1              S4Vectors_0.42.1           
[43] BiocGenerics_0.50.0         MatrixGenerics_1.16.0      
[45] matrixStats_1.4.1           nnSVG_1.8.0                
[47] workflowr_1.7.1            

loaded via a namespace (and not attached):
  [1] GSEABase_1.66.0           goftest_1.2-3            
  [3] DT_0.33                   HDF5Array_1.32.1         
  [5] Biostrings_2.72.1         vctrs_0.6.5              
  [7] spatstat.random_3.3-2     digest_0.6.37            
  [9] png_0.1-8                 proxy_0.4-27             
 [11] git2r_0.33.0              ggrepel_0.9.6            
 [13] deldir_2.0-4              parallelly_1.41.0        
 [15] magick_2.8.4              reshape2_1.4.4           
 [17] httpuv_1.6.15             withr_3.0.1              
 [19] xfun_0.49                 survival_3.8-3           
 [21] memoise_2.0.1             benchmarkme_1.0.8        
 [23] ggbeeswarm_0.7.2          zoo_1.8-12               
 [25] pbapply_1.7-2             Formula_1.2-5            
 [27] rematch2_2.1.2            KEGGREST_1.44.1          
 [29] promises_1.3.2            httr_1.4.7               
 [31] rstatix_0.7.2             restfulr_0.0.15          
 [33] rhdf5filters_1.16.0       globals_0.16.3           
 [35] fitdistrplus_1.2-1        rhdf5_2.48.0             
 [37] ps_1.8.1                  rstudioapi_0.17.1        
 [39] UCSC.utils_1.0.0          miniUI_0.1.1.1           
 [41] generics_0.1.3            processx_3.8.4           
 [43] curl_6.0.1                fields_16.3              
 [45] zlibbioc_1.50.0           ScaledMatrix_1.12.0      
 [47] polyclip_1.10-7           doSNOW_1.0.20            
 [49] GenomeInfoDbData_1.2.12   ExperimentHub_2.12.0     
 [51] SparseArray_1.4.8         golem_0.5.1              
 [53] xtable_1.8-4              stringr_1.5.1            
 [55] pracma_2.4.4              evaluate_1.0.1           
 [57] S4Arrays_1.4.1            BiocFileCache_2.12.0     
 [59] colorspace_2.1-1          filelock_1.0.3           
 [61] ROCR_1.0-11               reticulate_1.38.0        
 [63] spatstat.data_3.1-4       shinyWidgets_0.8.7       
 [65] magrittr_2.0.3            lmtest_0.9-40            
 [67] later_1.4.0               viridis_0.6.5            
 [69] lattice_0.22-6            misc3d_0.9-1             
 [71] spatstat.geom_3.3-4       future.apply_1.11.3      
 [73] getPass_0.2-4             scattermore_1.2          
 [75] XML_3.99-0.17             cowplot_1.1.3            
 [77] RcppAnnoy_0.0.22          class_7.3-22             
 [79] pillar_1.9.0              nlme_3.1-166             
 [81] compiler_4.4.2            beachmat_2.20.0          
 [83] RSpectra_0.16-2           stringi_1.8.4            
 [85] tensor_1.5                GenomicAlignments_1.40.0 
 [87] plyr_1.8.9                crayon_1.5.3             
 [89] abind_1.4-8               BiocIO_1.14.0            
 [91] locfit_1.5-9.10           bit_4.5.0.1              
 [93] dplyr_1.1.4               whisker_0.4.1            
 [95] codetools_0.2-20          BiocSingular_1.20.0      
 [97] openssl_2.2.2             bslib_0.8.0              
 [99] paletteer_1.6.0           plotly_4.10.4            
[101] mime_0.12                 splines_4.4.2            
[103] fastDummies_1.7.4         dbplyr_2.5.0             
[105] sparseMatrixStats_1.16.0  attempt_0.3.1            
[107] knitr_1.49                blob_1.2.4               
[109] utf8_1.2.4                BiocVersion_3.19.1       
[111] fs_1.6.5                  listenv_0.9.1            
[113] DelayedMatrixStats_1.26.0 rdist_0.0.5              
[115] ggsignif_0.6.4            tibble_3.2.1             
[117] callr_3.7.6               statmod_1.5.0            
[119] pkgconfig_2.0.3           tools_4.4.2              
[121] BRISC_1.0.6               cachem_1.1.0             
[123] RhpcBLASctl_0.23-42       RSQLite_2.3.9            
[125] viridisLite_0.4.2         DBI_1.2.3                
[127] fastmap_1.2.0             rmarkdown_2.29           
[129] scales_1.3.0              grid_4.4.2               
[131] ica_1.0-3                 Rsamtools_2.20.0         
[133] broom_1.0.7               AnnotationHub_3.12.0     
[135] sass_0.4.9                patchwork_1.3.0          
[137] BiocManager_1.30.25       dotCall64_1.2            
[139] graph_1.82.0              carData_3.0-5            
[141] RANN_2.6.2                snow_0.4-4               
[143] farver_2.1.2              yaml_2.3.10              
[145] rtracklayer_1.64.0        cli_3.6.3                
[147] purrr_1.0.2               leiden_0.4.3.1           
[149] lifecycle_1.0.4           askpass_1.2.1            
[151] uwot_0.2.2                backports_1.5.0          
[153] bluster_1.14.0            sessioninfo_1.2.2        
[155] annotate_1.82.0           BiocParallel_1.38.0      
[157] gtable_0.3.5              rjson_0.2.23             
[159] ggridges_0.5.6            progressr_0.14.0         
[161] limma_3.60.3              jsonlite_1.8.9           
[163] edgeR_4.2.2               RcppHNSW_0.6.0           
[165] bitops_1.0-9              benchmarkmeData_1.0.4    
[167] bit64_4.5.2               spatstat.utils_3.1-1     
[169] BiocNeighbors_1.22.0      matlab_1.0.4.1           
[171] jquerylib_0.1.4           metapod_1.12.0           
[173] config_0.3.2              dqrng_0.4.1              
[175] spatstat.univar_3.1-1     lazyeval_0.2.2           
[177] shiny_1.10.0              htmltools_0.5.8.1        
[179] sctransform_0.4.1         rappdirs_0.3.3           
[181] glue_1.8.0                tcltk_4.4.2              
[183] spam_2.11-0               XVector_0.44.0           
[185] RCurl_1.98-1.16           rprojroot_2.0.4          
[187] gridExtra_2.3             igraph_2.0.3             
[189] R6_2.5.1                  tidyr_1.3.1              
[191] labeling_0.4.3            CompQuadForm_1.4.3       
[193] cluster_2.1.8             Rhdf5lib_1.26.0          
[195] DelayedArray_0.30.1       tidyselect_1.2.1         
[197] vipor_0.4.7               maps_3.4.2.1             
[199] car_3.1-3                 AnnotationDbi_1.66.0     
[201] future_1.34.0             rsvd_1.0.5               
[203] munsell_0.5.1             KernSmooth_2.23-24       
[205] data.table_1.15.4         htmlwidgets_1.6.4        
[207] RColorBrewer_1.1-3        rlang_1.1.4              
[209] spatstat.sparse_3.1-0     spatstat.explore_3.3-3   
[211] fansi_1.0.6               beeswarm_0.4.0