Last updated: 2024-10-11

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 eaad1a0. 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

Untracked files:
    Untracked:  KODAMA.svg
    Untracked:  code/Acinar_Cell_Carcinoma.ipynb
    Untracked:  code/Adenocarcinoma.ipynb
    Untracked:  code/Adjacent_normal_section.ipynb
    Untracked:  code/DLFPC_preprocessing.R
    Untracked:  code/VisiumHD-CRC.ipynb
    Untracked:  data/DLPFC-general.RData
    Untracked:  data/spots_classification_ALL.csv
    Untracked:  data/trajectories.RData
    Untracked:  data/trajectories_VISIUMHD.RData
    Untracked:  output/DLFPC-All.RData
    Untracked:  output/DLFPC-Br5292.RData
    Untracked:  output/DLFPC-Br5595.RData
    Untracked:  output/DLFPC-Br8100.RData
    Untracked:  output/MERFISH.RData
    Untracked:  output/Prostate.RData
    Untracked:  output/VisiumHD.RData
    Untracked:  output/VisiumHD2.RData
    Untracked:  output/VisiumHD3.RData
    Untracked:  output/image.RData
    Untracked:  output/pca.RData

Unstaged changes:
    Deleted:    analysis/DLPFC-12.Rmd
    Deleted:    analysis/DLPFC-4.Rmd
    Modified:   analysis/Giotto.Rmd
    Deleted:    analysis/STARmap.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.


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 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")

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=1111

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)

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

#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

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_sub=data[sel,top[1:gene_number]]
        
RNA.scaled=scale(data_sub)
pca_results <- irlba(A = RNA.scaled, nv = 50)
pca_Br5595 <- pca_results$u %*% diag(pca_results$d)[,1:50]
rownames(pca_Br5595)=rownames(data_sub)
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
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09

KODAMA analysis

kk=KODAMA.matrix.parallel(pca_Br5595,
                          spatial = xy,
                          samples=samples,
                          FUN= "PLS" ,
                          landmarks = 100000,
                          splitting = splitting,
                          f.par.pls = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          aa_noise=aa_noise,
                          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
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09

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=as.factor(clu))

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
selFB=clu!=3

  kk_UMAP=kk_UMAP[selFB,]
  labels=labels[selFB]
  samples=samples[selFB]
  xy=xy[selFB,]
    
      
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
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
ref=refine_SVM(xy,clu,samples,cost=100)
[1] "151669"
[1] "151670"
[1] "151671"
[1] "151672"
u=unique(samples)
for(j in u){
  sel=samples==j
  print(mclust::adjustedRandIndex(labels[sel],ref[sel]))
}
[1] 0.6025143
[1] 0.5690666
[1] 0.5594779
[1] 0.5627897
      ###########
      
    
cols_cluster <- c("#0000b6", "#81b29a", "#f2cc8f","#e07a5f",
          "#cc00b6", "#81ccff", "#33b233")

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

Version Author Date
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
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,pca_Br5595,samples_Br5595,xy_Br5595,labels_Br5595,subject_names_Br5595,ref_Br5595,clu_Br5595,selFB,file="output/DLFPC-Br5595.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_sub=data[sel,top[1:gene_number]]
        
        RNA.scaled=scale(data_sub)
        pca_results <- irlba(A = RNA.scaled, nv = 50)
        pca_Br5292 <- pca_results$u %*% diag(pca_results$d)[,1:50]
        rownames(pca_Br5292)=rownames(data_sub)
        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
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

KODAMA analysis

kk=KODAMA.matrix.parallel(pca_Br5292,
                            spatial = xy,
                            samples=samples,
                            FUN= "PLS" ,
                            landmarks = 100000,
                            splitting = splitting,
                            f.par.pls = 50,
                            spatial.resolution = spatial.resolution,
                            n.cores=n.cores,
                            aa_noise=aa_noise,
                          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
c9d54ee Stefano Cacciatore 2024-10-11
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
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,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.451793
[1] 0.5006291
[1] 0.4551902
[1] 0.392062
        ###########

          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.451793
[1] 0.5006291
[1] 0.4551902
[1] 0.392062
      ###########
      
    
cols_cluster <- c("#0000b6", "#81b29a", "#f2cc8f","#e07a5f",
          "#cc00b6", "#81ccff", "#33b233")

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

Version Author Date
c9d54ee Stefano Cacciatore 2024-10-11
1352d91 Stefano Cacciatore 2024-10-10
35ce733 Stefano Cacciatore 2024-08-03
f8ca54a tkcaccia 2024-07-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")

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_sub=data[sel,top[1:gene_number]]
        
        RNA.scaled=scale(data_sub)
        pca_results <- irlba(A = RNA.scaled, nv = 50)
        pca_Br8100 <- pca_results$u %*% diag(pca_results$d)[,1:50]
        rownames(pca_Br8100)=rownames(data_sub)
        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
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

kk=KODAMA.matrix.parallel(pca_Br8100,
                            spatial = xy,
                            samples=samples,
                            FUN= "PLS" ,
                            landmarks = 100000,
                            splitting = splitting,
                            f.par.pls = 50,
                            spatial.resolution = spatial.resolution,
                            n.cores=n.cores,
                            aa_noise=aa_noise,
                          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
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))
        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.6032563
[1] 0.689506
[1] 0.6975872
[1] 0.6478389
        ###########

          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
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.6032563
[1] 0.689506
[1] 0.6975872
[1] 0.6478389
      ###########
      
    
cols_cluster <- c("#0000b6", "#81b29a", "#f2cc8f","#e07a5f",
          "#cc00b6", "#81ccff", "#33b233")

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

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
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")

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
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03

KODAMA

kk=KODAMA.matrix.parallel(pca,
                          spatial = xy,
                          samples=samples,
                          FUN= "PLS" ,
                          landmarks = 100000,
                          splitting = 300,
                          f.par.pls = 50,
                          spatial.resolution = spatial.resolution,
                          n.cores=n.cores,
                          aa_noise=aa_noise,
                          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
1352d91 Stefano Cacciatore 2024-10-10

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
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"
[1] "151669"
[1] "151670"
[1] "151671"
[1] "151672"
[1] "151673"
[1] "151674"
[1] "151675"
[1] "151676"
cols_cluster <- c("#0000b6", "#81b29a", "#f2cc8f","#e07a5f",
          "#cc00b6", "#81ccff", "#33b233")

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

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10

CLUSTER

  clu=kmeans(kk_UMAP,7,nstart = 100)$cluster
  plot(kk_UMAP,col=labels,pch=20)

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
  plot(kk_UMAP,col=cols_cluster,pch=20)

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
u=unique(samples)
for(i in 1:length(u)){
  sel=samples==u[i]
  print(adjustedRandIndex(labels[sel],clu[sel]))
}
[1] 0.5229387
[1] 0.4808002
[1] 0.4792372
[1] 0.4615354
[1] 0.3342856
[1] 0.3190995
[1] 0.3645173
[1] 0.4042594
[1] 0.5427708
[1] 0.5655607
[1] 0.5426387
[1] 0.5276388
ref=refine_SVM(xy,clu,samples,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"
u=unique(samples)
for(i in 1:length(u)){
  sel=samples==u[i]
  print(adjustedRandIndex(labels[sel],ref[sel]))
}
[1] 0.5708956
[1] 0.5279227
[1] 0.5017788
[1] 0.4914887
[1] 0.3841959
[1] 0.3751429
[1] 0.4394351
[1] 0.5280842
[1] 0.5872196
[1] 0.6091544
[1] 0.6063361
[1] 0.5934526
plot_slide(xy,samples,ref,col=cols_cluster)

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10

TRAJECTORY

d <- slingshot(kk_UMAP, clusterLabels = clu)
trajectory=d@metadata$curves$Lineage1$s
k=knn_Armadillo(trajectory,kk_UMAP,1)
map_color=rainbow(nrow(trajectory))[k$nn_index]
plot(kk_UMAP,pch=20,col=map_color)

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03
plot_slide(xy,samples,k$nn_index,col=rainbow(nrow(trajectory)))

Version Author Date
1352d91 Stefano Cacciatore 2024-10-10
6038af1 Stefano Cacciatore 2024-10-09
35ce733 Stefano Cacciatore 2024-08-03

sessionInfo()
R version 4.4.1 (2024-06-14)
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] irlba_2.3.5.1               Matrix_1.7-0               
 [3] slingshot_2.12.0            TrajectoryUtils_1.12.0     
 [5] princurve_2.1.6             mclust_6.1.1               
 [7] KODAMAextra_1.0             bigmemory_4.6.4            
 [9] rgl_1.3.1                   misc3d_0.9-1               
[11] e1071_1.7-16                doParallel_1.0.17          
[13] iterators_1.0.14            foreach_1.5.2              
[15] KODAMA_3.1                  umap_0.2.10.0              
[17] Rtsne_0.17                  minerva_1.5.10             
[19] spatialLIBD_1.16.2          SpatialExperiment_1.14.0   
[21] Seurat_5.1.0                SeuratObject_5.0.2         
[23] sp_2.1-4                    harmony_1.2.1              
[25] Rcpp_1.0.12                 SPARK_1.1.1                
[27] scry_1.16.0                 scran_1.32.0               
[29] scater_1.32.1               ggplot2_3.5.1              
[31] scuttle_1.14.0              SingleCellExperiment_1.26.0
[33] SummarizedExperiment_1.34.0 Biobase_2.64.0             
[35] GenomicRanges_1.56.1        GenomeInfoDb_1.40.1        
[37] IRanges_2.38.1              S4Vectors_0.42.1           
[39] BiocGenerics_0.50.0         MatrixGenerics_1.16.0      
[41] matrixStats_1.3.0           nnSVG_1.8.0                
[43] workflowr_1.7.1            

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