---
title: "Clustering analysis on mouse intestinal epithelium 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

data<-data.frame(fread("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE92nnn/GSE92332/suppl/GSE92332_FAE_UMIcounts.txt.gz"),row.names=1)
data<-data[,grep("epi3",colnames(data))]

```

## Process the data
```{r}

# process the data using Seurat

obj<-CreateSeuratObject(counts=data,min.cells=3,min.features=200)
obj <- NormalizeData(obj) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA() %>% RunUMAP(dims=1:10)


```

## 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_aknno <- c(brewer.pal(12,"Paired"))[-11][c(1:5,7:9,6,10)]

DimPlot(obj,label=T,group.by="aKNN_O_res.0.8",cols = color_aknno)+ggtitle("aKNNO")+NoLegend() -> p_aKNNO
DotPlot(obj,features=c("Krt19","Krt8","Top2a","Mki67","Muc2","Clca1","Lgr5","Ascl2","Fabp6","Apoa1","Gip","Chga","Chgb","Adh6a","Hck","Lrmp","Defa21","Defa22"))+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) -> p_marker
p_aKNNO+p_marker

```

## Comparision between aKNNO, KNN and KNN_high

```{r,fig.width=12,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(12,"Paired"))[-11][1:5]
color_knn_high <- c(brewer.pal(12,"Paired"))[-11][1:9]

DimPlot(obj,label=T,group.by="RNA_snn_res.0.8",cols = color_knn)+ggtitle("KNN")+NoLegend() -> p_KNN
DimPlot(obj,label=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=10,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(12,"Paired"))[-11][c(1:6)]

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

```


