---
title: "Clustering analysis on human pancreas using aKNNO"
author: "Jia Li, Qi Liu"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    toc: true
    theme: united
---

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


## Load the library and the data

```{r, echo=TRUE}

# load the library

library(aKNNO)
library(data.table)
library(RColorBrewer)

# download the data from GEO

data1 <-data.frame(fread("https://ftp.ncbi.nlm.nih.gov/geo/samples/GSM2230nnn/GSM2230757/suppl/GSM2230757_human1_umifm_counts.csv.gz"),row.names=1)
data3 <- data.frame(fread("https://ftp.ncbi.nlm.nih.gov/geo/samples/GSM2230nnn/GSM2230759/suppl/GSM2230759_human3_umifm_counts.csv.gz"),row.names = 1)
data <- rbind(data1,data3)
meta <- data[,1:2]
data <- t(data[,3:ncol(data)])

```

## Process the data
```{r}

# process the data using Seurat

obj<-CreateSeuratObject(counts=data,min.features=200,meta.data = meta)
obj <- NormalizeData(obj) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA() %>% RunUMAP(dims=1: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
color_ref <- c(brewer.pal(8, "Set2"),brewer.pal(12, "Set3"))[-c(6,10,17)]
color_ref[c(1,2,3,6,7,11:14)] <- c(color_ref[c(2,3,6,1)],"#E41A1C","#3C5488FF","#386CB0","#E64B35FF","#00A087FF")
color_aknno <- c(brewer.pal(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Pastel2"))[-c(10,17,20)]
color_aknno[c(19,21,23,22,25,18,24,20,8,11,12,14)] <- c("#3C5488FF","#00A087FF","#E64B35FF","#ae017e","#d94801","#ec7014","#E41A1C","#238b45","#A6CEE3","#fe9929","#807dba","#4292c6")

Idents(obj) <- obj$assigned_cluster
DimPlot(obj,label = T,repel = T,cols = color_ref)+ggtitle("Ref")+NoLegend() -> p_ref
DimPlot(obj,label=T,repel = T,group.by="aKNN_O_res.0.8",cols = color_aknno)+ggtitle("aKNNO")+NoLegend() -> p_aKNNO

clusters <- c(18,20,22,23,5,15,16,9,24,13,17)
obj.sub <- obj[,obj$aKNN_O_res.0.8 %in% clusters]
obj.sub$aKNN_O_res.0.8 <- factor(obj.sub$aKNN_O_res.0.8,levels = clusters)
DotPlot(obj.sub,features=c("S100A9","CXCL8","CCR7","TRAC","KIT","ARX","SOX9","KRT19","OLFM4","TFF1","IGFBP3","PECAM1","PLVAP","PDPN","LYVE1","PDGFRB","FGF2","COL1A1","COL1A2"),group.by = "aKNN_O_res.0.8")+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) -> p_marker
p_ref+p_aKNNO+p_marker

```

## Comparision between aKNNO, KNN and KNN_high

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

# build the k-nearest neighbor graph using the same number of dims (1:50) and the method (rann) in the aKNNO 
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(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Pastel2"))[-c(6,8,10,17,20)]
color_knn_high <-  c(brewer.pal(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Accent"))[-c(6,8,10,17)][c(1:22,24,23)]
color_knn_high[c(16,20)] <- c("#41ae76","#FB9A99")

DimPlot(obj,label=T,repel = T,group.by="RNA_snn_res.0.8",cols = color_knn)+ggtitle("KNN")+NoLegend() -> p_KNN
DimPlot(obj,label=T,repel = T,group.by="RNA_snn_res.2",cols = color_knn_high)+ggtitle("KNN_high")+NoLegend() -> p_KNN_high
p_aKNNO+p_KNN+p_KNN_high

```

## Comparsion between the optimized delta and the default delta

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

# 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(8, "Set2"),brewer.pal(12, "Set3"),brewer.pal(8,"Pastel2"))[-c(10,17,20)]
color_aknn[c(17,18,19,21,20,12,14,11)]<- c("#3C5488FF","#00A087FF","#E64B35FF","#d94801","#E41A1C","#4292c6","#FCCDE5","#807dba")

DimPlot(obj,label=T,repel = T,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

```


