---
title: "Clustering analysis on mouse brain (sagittal posterior) using aKNNO"
author: "Jia Li, Qi Liu"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    toc: true
    theme: united
linkcolor: blue
urlcolor: blue
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```



## Load the library and the processed data
 The raw data were downloaded from <span style="text-decoration:underline"> [10x genomics](https://www.10xgenomics.com/cn/resources/datasets/mouse-brain-serial-section-1-sagittal-posterior-1-standard-1-1-0) </span>


```{r, echo=TRUE}

# load the library and the data using Seurat

library(aKNNO)
library(RColorBrewer)

# load the processed object (the data were normalized by SCTtransform and filter genes expressed less than 10% spots)
obj<-readRDS(url("https://www.dropbox.com/s/a5n93ilan1u0vqx/Mouse_Brain_Serial_Section1_SagittalPosterior.rds?dl=1"))




```


```{r,eval=F,echo=FALSE}

# process the data using Seurat
# normalize the data 
obj<-SCTransform(obj,assay="Spatial",verbose = F)

#filter genes expressed less than 10% spots
geneperc<-rowSums(obj[["Spatial"]]@counts>0)/ncol(obj)
VariableFeatures(obj)<-setdiff(VariableFeatures(obj),names(which(geneperc<=0.05)))
# reduce dimension via PCA
obj <- RunPCA(obj,npcs=30) 


```

## Clustering based on aKNNO (the adaptive k-nearest neighbor graph with optimization)

```{r,fig.width=12,fig.height=5}

# build the optimized adaptive k-nearest neighbor graph

obj<-FindNeighbors_aKNNO(obj,verbose = F)

# clustering on the optimized adaptive k-nearest neighbor graph

obj<-FindClusters(obj,graph.name="aKNN_O",verbose=F)

# visualization

# define colors
color_aknno <- c(brewer.pal(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Pastel2"),brewer.pal(8,"Accent"))[-c(6,8,10,17,20)]
color_aknno[c(22,26,27,31,28,23)] <- c("#2171b5","#FF7F00","#238b45","#E41A1C","#dd3497","#984EA3")
color_aknno[c(3,12,11,24,2,30)] <- c(color_aknno[c(11,3)],"#6a51a3","#cc4c02","#E6AB02","#525252")
names(color_aknno) <- sort(as.integer(levels(obj$aKNN_O_res.0.8)))

SpatialDimPlot(obj,alpha=0,image.alpha = 1) + ggtitle("H&E image") +NoLegend() -> p_image
SpatialDimPlot(obj,group.by="aKNN_O_res.0.8",cols=color_aknno)+ggtitle("aKNNO")+NoLegend() -> p_aKNNO
SpatialDimPlot(obj[,obj$aKNN_O_res.0.8 %in% c(21:30)],group.by="aKNN_O_res.0.8",label=T,label.size=3,cols=color_aknno[22:31],crop=F)+ggtitle("aKNNO: Rare")+NoLegend() -> p_aKNNO_Rare
p_image+p_aKNNO+p_aKNNO_Rare
```

## Comparision between aKNNO, KNN and KNN_high

```{r,fig.width=12,fig.height=5,eval=T}

# build the k-nearest neighbor graph using the same number of dims (1:50) and the method (rann) in the aKNNO 

DefaultAssay(obj)<-"SCT"
obj<-FindNeighbors(obj,nn.method="rann",dims=1:50,verbose=F)

# clustering based on kNN at the default resolution of 0.8 and a high resolution of 2 
obj<-FindClusters(obj,verbose=F)
obj<-FindClusters(obj,resolution=2,verbose=F)

#visualization

color_knn <- c(brewer.pal(11,"Set3"),brewer.pal(11,"Paired"))[c(1:15)]
names(color_knn) <- levels(obj$SCT_snn_res.0.8)

SpatialDimPlot(obj,group.by="SCT_snn_res.0.8",cols=color_knn)+ggtitle("KNN")+NoLegend() -> p_KNN

color_knn_high <- c(brewer.pal(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Pastel2"),brewer.pal(8,"Accent"))[-c(6,8,10,17,20,28,36,31)]
color_knn_high[c(22,18,16)] <- c("#E31A1C","#4292c6","#FB9A99")
names(color_knn_high) <- levels(obj$SCT_snn_res.2)

SpatialDimPlot(obj,group.by="SCT_snn_res.2",cols=color_knn_high)+ggtitle("KNN_high")+NoLegend() -> p_KNN_high
p_aKNNO+p_KNN+p_KNN_high

# Hippocampus structures
SpatialDimPlot(obj[,obj$aKNN_O_res.0.8 %in% c(21,22, 25,26,27,30)],label=T,group.by="aKNN_O_res.0.8",label.size=3,cols=color_aknno[c(22,23,26,27,28,31)])+ggtitle("aKNNO: Hippo")+NoLegend() -> p_aKNNO_hippo
SpatialDimPlot(obj[,obj$SCT_snn_res.0.8 %in% c(7,9)],label=T,group.by="SCT_snn_res.0.8",label.size=3,cols=color_knn[c(8,10)])+ggtitle("KNN: Hippo")+NoLegend() -> p_KNN_hippo
SpatialDimPlot(obj[,obj$SCT_snn_res.2 %in% c(7,15,17,21)],label=T,group.by="SCT_snn_res.2",label.size=3,cols=color_knn_high[c(8,16,18,22)])+ggtitle("KNN_high: Hippo")+NoLegend() -> p_KNN_high_hippo
p_aKNNO_hippo+p_KNN_hippo+p_KNN_high_hippo


```

## Comparsion between the optimized delta and the default delta

```{r,fig.width=10,fig.height=5,eval=T}

# use the default delta (-0.5) to build the aKNN

obj<-FindNeighbors_aKNN(obj)

# clustering based on the aKNN 

obj<-FindClusters(obj,graph.name="aKNN",verbose=F)

#visualization

color_aknn <- c(brewer.pal(11,"Set3"),brewer.pal(11,"Paired"))[c(1:16,22,18,19,20,21,17)]
names(color_aknn) <- levels(obj$aKNN_res.0.8)
SpatialDimPlot(obj,group.by="aKNN_res.0.8",cols = color_aknn)+ggtitle("aKNN")+NoLegend() -> p_aKNN
obj@misc$aKNNO_delta$plot_delta +ggtitle("delta optimization") -> p_delta
p_aKNNO+p_aKNN+p_delta

```


