## ----message=FALSE------------------------------------------------------------

library(dplyr)
library(Seurat)
library(patchwork)
library(mclust)
library(robin)

## -----------------------------------------------------------------------------
tabMur <- system.file("example/filtered_tabulamuris_mtx.rds", package="robin")
tabData <- readRDS(file=tabMur)

SingleCellData <- CreateSeuratObject(counts = tabData, project = "singleCell", min.cells = round(dim(tabData)[2]*5/100), min.features = 0)
SingleCellData 

## -----------------------------------------------------------------------------

SingleCellData <- NormalizeData(SingleCellData, normalization.method = "LogNormalize", scale.factor = 10000)

SingleCellData[["RNA"]]@layers$counts[1:5,1:5]



## -----------------------------------------------------------------------------

SingleCellData <- FindVariableFeatures(object = SingleCellData, selection.method="vst", nfeatures = 2000)



## -----------------------------------------------------------------------------
SingleCellData <- ScaleData(SingleCellData, do.center = TRUE, do.scale = TRUE)
SingleCellData[["RNA"]]@layers$scale.data[1:5,1:5]

## -----------------------------------------------------------------------------

SingleCellData <- RunPCA(SingleCellData, features = VariableFeatures(object = SingleCellData))

## -----------------------------------------------------------------------------

SingleCellData <- FindNeighbors(SingleCellData, dims = 1:50) 

SingleCellData@graphs$RNA_snn[1:5,1:5] # Adjacency matrix

## -----------------------------------------------------------------------------


AdjSNN <- SingleCellData@graphs$RNA_snn
graphSingleCell <- graph_from_adjacency_matrix(AdjSNN,mode="directed",weighted = TRUE,add.colnames = "NA",diag=FALSE)
edge <- as_edgelist(graphSingleCell)
graph <- igraph::graph_from_edgelist(edge, directed=FALSE)
graph <- igraph::simplify(graph)
graph


## -----------------------------------------------------------------------------
#Infomap
 comp_I <- robinCompare(graph=graph, method1="louvain",
                        method2="infomap")

#Walktrap
comp_W <- robinCompare(graph=graph, method1="louvain",
                       method2="walktrap")

#LabelProp
comp_La <- robinCompare(graph=graph, method1="louvain",
                        method2="labelProp")

#Fastgreedy
comp_F <- robinCompare(graph=graph, method1="louvain",
                        method2="fastGreedy")

plotMultiCompare(comp_F,comp_La, comp_I, comp_W)


## -----------------------------------------------------------------------------
graphRandom <- random(graph=graph)
proc <- robinRobust(graph=graph, graphRandom=graphRandom, method="louvain")

plot(proc)

robinFDATest(proc)

robinGPTest(proc)


set.seed(100)
cluster_louvain(graph)
mc <- membershipCommunities(graph=graph, method="louvain")

## ----message=FALSE------------------------------------------------------------

SingleCellData$seurat_clusters = mc
Idents(SingleCellData) <- "seurat_clusters"
markers <- FindAllMarkers(SingleCellData, only.pos = TRUE, logfc.threshold = 0.25)

markers %>%
    group_by(cluster) %>%
    dplyr::filter(avg_log2FC > 1) %>%
    slice_head(n = 10) %>%
    ungroup() -> top10
DoHeatmap(SingleCellData, features = top10$gene) + NoLegend()

