library(dplyr)
library(Seurat)
## Warning: il pacchetto 'Seurat' è stato creato con R versione 4.6.2
library(patchwork)
## Warning: il pacchetto 'patchwork' è stato creato con R versione 4.6.2
library(mclust)
## Warning: il pacchetto 'mclust' è stato creato con R versione 4.6.2
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
## An object of class Seurat
## 8694 features across 532 samples within 1 assay
## Active assay: RNA (8694 features, 0 variable features)
## 1 layer present: counts
SingleCellData <- NormalizeData(SingleCellData, normalization.method = "LogNormalize", scale.factor = 10000)
## Normalizing layer: counts
SingleCellData[["RNA"]]@layers$counts[1:5,1:5]
## 5 x 5 sparse Matrix of class "dgCMatrix"
##
## [1,] . . . . .
## [2,] . . 1 . 1
## [3,] . . 1 . 1
## [4,] 1 . 1 . .
## [5,] 1 1 2 . .
SingleCellData <- FindVariableFeatures(object = SingleCellData, selection.method="vst", nfeatures = 2000)
## Finding variable features for layer counts
SingleCellData <- ScaleData(SingleCellData, do.center = TRUE, do.scale = TRUE)
## Centering and scaling data matrix
SingleCellData[["RNA"]]@layers$scale.data[1:5,1:5]
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.2119967 -0.2119967 -0.2119967 -0.2119967 -0.2119967
## [2,] -0.2464207 -0.2464207 -0.2464207 6.4241312 -0.2464207
## [3,] -0.2325839 -0.2325839 -0.2325839 -0.2325839 -0.2325839
## [4,] -0.3036820 -0.3036820 -0.3036820 -0.3036820 -0.3036820
## [5,] -0.2199427 -0.2199427 -0.2199427 -0.2199427 -0.2199427
SingleCellData <- RunPCA(SingleCellData, features = VariableFeatures(object = SingleCellData))
## PC_ 1
## Positive: Coro1a, Arhgdib, Laptm5, Srgn, Gmfg, Cd74, Ptprcap, Cd37, Ctss, H2-Aa
## Klf2, Ltb, H2-Ab1, Lcp1, Tmsb4x, H2-Eb1, Slfn2, Cytip, Plac8, Ptpn6
## Cd79a, Ifi27l2a, Cd79b, Cd2, H2-DMa, Ly86, Napsa, Unc93b1, Vim, Selplg
## Negative: Perp, Dsp, S100a14, Trim29, Serpinb5, Sfn, Rab25, Dmkn, Krt6a, Aqp3
## Lgals7, Gpx2, Lypd3, Tacstd2, Them5, Anxa8, Pkp1, Calm4, Ckmt1, Sytl1
## Lad1, Fam25c, Sdc1, Mt4, Gsto1, Sbsn, Avpi1, Ovol1, Gsta4, Calml3
## PC_ 2
## Positive: Coro1a, Arhgdib, Laptm5, Ly6d, Ptprcap, Gmfg, Cd37, Ltb, Ptpn6, Ctss
## Lcp1, Pycard, Cd79b, Cd79a, Cytip, H2-Aa, Cd74, Napsa, Cd2, H2-Ab1
## Selplg, Cd48, Ly86, H2-DMa, Slfn2, H2-Eb1, Rplp0, Tbc1d10c, Ms4a1, 2010001M09Rik
## Negative: Bgn, Serpinh1, Sparc, Col1a2, Igfbp7, Col3a1, Plac9, Gsn, Col6a1, Col1a1
## Mgp, Serping1, Timp2, Nbl1, Fstl1, Ltbp4, Rcn3, Pcolce, Fxyd1, Mxra8
## Rarres2, Ppap2b, Prelp, Cd63, Timp3, Dcn, Cebpd, Col6a2, Lhfp, Mmp2
## PC_ 3
## Positive: Tmsb4x, Rplp0, Dynll1, Klf4, Cdkn1a, S100a6, Cd9, Tuba1a, Sparc, Dcn
## Btg2, Vim, Plac9, Tppp3, Gsn, Gja1, Serpinh1, Ptrf, Mt2, Dnajb1
## Gadd45b, Atf3, Hspb1, Anxa1, Socs3, Klf6, Cebpd, Csrp1, Tnfrsf12a, Bgn
## Negative: Aldob, Slc27a2, Pck1, Ttc36, Pah, Fbp1, Cml1, Acaa1b, Pcbd1, Stard10
## Hrsp12, Sardh, Fah, Cat, Ephx2, Acsl1, Aldh1l1, Sord, 4931406C07Rik, Dhcr24
## Khk, Gstz1, Pecr, Phyh, Acox1, Ass1, Aldh6a1, Pcx, Ugt2b34, Gcdh
## PC_ 4
## Positive: Wif1, Chad, Mia1, Fxyd1, Col11a1, Acan, Scara3, Fn1, Ucma, Serpine2
## Col6a1, Mgp, Col9a2, Cytl1, Meg3, 1500015O10Rik, Rcn3, 3110079O15Rik, Bgn, Col8a1
## Col2a1, Gpc6, Mmp2, Lect1, Col9a1, Cpe, Hapln1, Dkk3, Sdc4, Vasn
## Negative: Ecscr, Cdh5, Ptprb, Egfl7, Rasip1, Esam, Eltd1, Gpr116, Emcn, Eng
## Mmrn2, Slc9a3r2, Cd93, Pecam1, Apold1, Tie1, Flt1, Myct1, Podxl, Fabp4
## Tinagl1, Sox17, Slfn5, Cldn5, Sdpr, Ehd4, Tek, Cd36, Plvap, Tm4sf1
## PC_ 5
## Positive: Cytl1, Chad, Mia1, Wif1, Ucma, Col9a2, 3110079O15Rik, Col11a1, Col2a1, Acan
## Cpe, Hapln1, Lect1, Col9a3, 1500015O10Rik, Col9a1, Pla2g5, Mfi2, Col27a1, Comp
## Fzd9, Fmod, S100b, Col8a1, Sox9, Gpc6, Omd, Mdfi, Crim1, Tsc22d1
## Negative: Olfml3, Clec3b, Pdgfra, Serping1, Inmt, Mfap5, Rarres2, Mfap4, Dpep1, C1ra
## Tcf21, Tnfsf13b, Gyg, Ifi27l2a, C1s, Ppp1r14a, Ms4a4d, Tnxb, Fhl1, Ogn
## Nid1, Fgf7, Hsd11b1, Serpina3n, Abca8a, Alox5ap, Gpc3, C1qtnf7, Spon1, Lyz1
SingleCellData <- FindNeighbors(SingleCellData, dims = 1:50)
## Computing nearest neighbor graph
## Computing SNN
SingleCellData@graphs$RNA_snn[1:5,1:5] # Adjacency matrix
## 5 x 5 sparse Matrix of class "dgCMatrix"
## 10X_P4_7_CCTACACGTAAGGATT 10X_P7_6_ACACCGGGTAGCGTGA
## 10X_P4_7_CCTACACGTAAGGATT 1.0000000 0.1428571
## 10X_P7_6_ACACCGGGTAGCGTGA 0.1428571 1.0000000
## 10X_P7_9_TGATTTCAGCAGGTCA . .
## 10X_P8_14_CGAATGTAGAAGGTGA . .
## 10X_P4_6_CGATGGCCATCACAAC . .
## 10X_P7_9_TGATTTCAGCAGGTCA 10X_P8_14_CGAATGTAGAAGGTGA
## 10X_P4_7_CCTACACGTAAGGATT . .
## 10X_P7_6_ACACCGGGTAGCGTGA . .
## 10X_P7_9_TGATTTCAGCAGGTCA 1.00 .
## 10X_P8_14_CGAATGTAGAAGGTGA . 1
## 10X_P4_6_CGATGGCCATCACAAC 0.25 .
## 10X_P4_6_CGATGGCCATCACAAC
## 10X_P4_7_CCTACACGTAAGGATT .
## 10X_P7_6_ACACCGGGTAGCGTGA .
## 10X_P7_9_TGATTTCAGCAGGTCA 0.25
## 10X_P8_14_CGAATGTAGAAGGTGA .
## 10X_P4_6_CGATGGCCATCACAAC 1.00
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
## IGRAPH 47e8afa U--- 532 14249 --
## + edges from 47e8afa:
## [1] 1-- 2 1-- 8 1-- 13 1-- 14 1-- 17 1-- 27 1-- 42 1-- 63 1-- 65 1-- 71
## [11] 1-- 79 1-- 95 1-- 99 1--102 1--116 1--131 1--132 1--159 1--161 1--164
## [21] 1--172 1--179 1--194 1--197 1--205 1--220 1--258 1--260 1--264 1--267
## [31] 1--270 1--312 1--315 1--325 1--327 1--328 1--351 1--368 1--378 1--386
## [41] 1--391 1--393 1--395 1--397 1--406 1--407 1--409 1--420 1--421 1--426
## [51] 1--427 1--432 1--452 1--458 1--459 1--464 1--472 1--473 1--476 1--478
## [61] 1--495 1--496 1--497 1--501 1--502 1--503 1--510 1--518 2-- 8 2-- 13
## [71] 2-- 14 2-- 27 2-- 42 2-- 63 2-- 65 2-- 71 2-- 79 2-- 95 2-- 99 2--102
## [81] 2--116 2--131 2--132 2--159 2--161 2--164 2--172 2--179 2--194 2--197
## + ... omitted several edges
We apply the compare procedure to see which is the algorithm that better fits our network.
#Infomap
comp_I <- robinCompare(graph=graph, method1="louvain",
method2="infomap")
## [1] "Unweighted Network Parallel Function"
## Detected robin method type independent
## It can take time ... It depends on the size of the network.
#Walktrap
comp_W <- robinCompare(graph=graph, method1="louvain",
method2="walktrap")
## [1] "Unweighted Network Parallel Function"
## Detected robin method type independent
## It can take time ... It depends on the size of the network.
#LabelProp
comp_La <- robinCompare(graph=graph, method1="louvain",
method2="labelProp")
## [1] "Unweighted Network Parallel Function"
## Detected robin method type independent
## It can take time ... It depends on the size of the network.
#Fastgreedy
comp_F <- robinCompare(graph=graph, method1="louvain",
method2="fastGreedy")
## [1] "Unweighted Network Parallel Function"
## Detected robin method type independent
## It can take time ... It depends on the size of the network.
plotMultiCompare(comp_F,comp_La, comp_I, comp_W)
The lowest curve is the most stable algorithm. Louvain and Walktrap are the best algorithms in our example.
Due to the fact that Louvain is one of the best algorithm for our network we apply the robust procedure with the Louvain algorithm to see if the communities detected are statistically significant.
graphRandom <- random(graph=graph)
proc <- robinRobust(graph=graph, graphRandom=graphRandom, method="louvain")
## [1] "Unweighted Network Parallel Function"
## Detected robin method type independent
## It can take time ... It depends on the size of the network.
plot(proc)
robinFDATest(proc)
## [1] "First step: basis expansion"
## Swapping 'y' and 'argvals', because 'y' is simpler,
## and 'argvals' should be; now dim(argvals) = 13 ; dim(y) = 13 x 20
## [1] "Second step: joint univariate tests"
## [1] "Third step: interval-wise combination and correction"
## [1] "creating the p-value matrix: end of row 2 out of 9"
## [1] "creating the p-value matrix: end of row 3 out of 9"
## [1] "creating the p-value matrix: end of row 4 out of 9"
## [1] "creating the p-value matrix: end of row 5 out of 9"
## [1] "creating the p-value matrix: end of row 6 out of 9"
## [1] "creating the p-value matrix: end of row 7 out of 9"
## [1] "creating the p-value matrix: end of row 8 out of 9"
## [1] "creating the p-value matrix: end of row 9 out of 9"
## [1] "Interval Testing Procedure completed"
## TableGrob (1 x 2) "arrange": 2 grobs
## z cells name grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]
## $adj.pvalue
## [1] 0.0361 0.0023 0.0018 0.0018 0.0018 0.0018 0.0018 0.0018 0.0023
##
## $pvalues
## [1] 0.0361 0.0018 0.0018 0.0018 0.0018 0.0018 0.0018 0.0018 0.0018
robinGPTest(proc)
## Profile 1
## Profile 2
## [1] 69.41827
set.seed(100)
cluster_louvain(graph)
## IGRAPH clustering multi level, groups: 8, mod: 0.76
## + groups:
## $`1`
## [1] 1 2 8 13 14 17 27 42 63 65 71 79 95 99 102 116 131 132
## [19] 159 161 164 172 176 179 194 197 205 206 220 258 260 264 267 270 312 315
## [37] 325 327 328 338 351 368 378 386 391 393 395 397 406 407 409 420 421 424
## [55] 426 427 432 442 452 458 459 464 469 472 473 476 478 495 496 497 501 502
## [73] 503 510 518
##
## $`2`
## [1] 3 5 9 25 33 53 59 60 70 77 88 98 101 114 145 148 152 169
## [19] 170 214 215 241 244 262 275 282 283 289 296 304 305 308 309 346 367 373
## + ... omitted several groups/vertices
mc <- membershipCommunities(graph=graph, method="louvain")
The communities given by Louvain are statistically significant.
Once we found the best clustering algorithm, we can use the found communities for identifying marker genes, to subsequently find cell types.
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()