---
title: "Clustering analysis on mouse habenula 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/samples/GSM4411nnn/GSM4411753/suppl/GSM4411753_hab160822.1.counts.tsv.gz"),row.names=1)
data<-t(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:20)


```

## 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"),brewer.pal(8,"Dark2"))[-c(11)][c(1:3,17,5,16,7:15,6,4)]

DimPlot(obj,label=T,repel = T,group.by="aKNN_O_res.0.8",cols = color_aknno)+ggtitle("aKNNO")+NoLegend() -> p_aKNNO
DotPlot(obj[,obj$aKNN_O_res.0.8 %in% c(11:15)],features=c("Gap43","Slc17a6","Acta2","Myh11","Mrc1","Col1a1","Col3a1","Olig1","Top2a","Cdc20"))+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][c(1:5,11,7:10,6)]
color_knn_high <-  c(brewer.pal(12,"Paired"),brewer.pal(8,"Dark2"))[-c(11)][c(1:5,13,7:12,6)]

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=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("#F781BF","#E6AB02",brewer.pal(12, "Paired")[c(1:5,7:10,12,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

```


