第四節(jié):細(xì)胞聚類分析
在本節(jié)教程中咒钟,我們將基于批次矯正后的整合數(shù)據(jù)集進(jìn)行細(xì)胞聚類分析唉韭,我們使用PCA線性降維的結(jié)果分別執(zhí)行k-最近鄰圖聚類纤控,層次聚類和k-均值聚類。
加載所需的R包和數(shù)據(jù)集
if (!require(clustree)) {
install.packages("clustree", dependencies = FALSE)
}
## Loading required package: clustree
## Loading required package: ggraph
suppressPackageStartupMessages({
library(Seurat)
library(cowplot)
library(ggplot2)
library(pheatmap)
library(rafalib)
library(clustree)
})
alldata <- readRDS("data/results/covid_qc_dr_int.rds")
執(zhí)行k-最近鄰圖聚類
在執(zhí)行圖聚類的過(guò)程中主要包括以下3個(gè)步驟:
- Build a kNN graph from the data
- Prune spurious connections from kNN graph (optional step). This is a SNN graph.
- Find groups of cells that maximizes the connections within the group compared other groups.
構(gòu)建kNN/SNN圖
執(zhí)行圖聚類的第一步是構(gòu)建一個(gè)kNN圖吃挑,我們使用PCA降維的前N個(gè)PC用于計(jì)算。
我們可以使用Seurat包中的FindNeighbors
函數(shù)計(jì)算構(gòu)建KNN和SNN圖街立。
# check that CCA is still the active assay
alldata@active.assay
## [1] "CCA"
# 使用FindNeighbors函數(shù)構(gòu)建SNN圖
alldata <- FindNeighbors(alldata, dims = 1:30, k.param = 60, prune.SNN = 1/15)
## Computing nearest neighbor graph
## Computing SNN
# check the names for graphs in the object.
names(alldata@graphs)
## [1] "CCA_nn" "CCA_snn"
我們可以看一下kNN圖舶衬,它是一個(gè)連接矩陣,其中不同細(xì)胞之間的每個(gè)連接都表示為1個(gè)s赎离,這稱之為未加權(quán)圖(Seurat中的默認(rèn)值)逛犹。但是,某些細(xì)胞之間的連接可能比其他細(xì)胞的更重要梁剔,在這種情況下虽画,圖的尺度會(huì)從0到最大距離。通常荣病,距離越小码撰,兩點(diǎn)越接近,它們之間的連接也越牢固个盆,這稱之為加權(quán)圖脖岛。加權(quán)圖和未加權(quán)圖均適用于圖聚類朵栖,但是對(duì)于大型數(shù)據(jù)集(>100k細(xì)胞),使用非加權(quán)圖在聚類上的速度會(huì)更快柴梆。
pheatmap(alldata@graphs$CCA_nn[1:200, 1:200],
col = c("white", "black"), border_color = "grey90",
legend = F, cluster_rows = F, cluster_cols = F, fontsize = 2)
基于SNN圖進(jìn)行細(xì)胞聚類
在構(gòu)建好SNN圖后陨溅,我們可以基于其執(zhí)行圖聚類。選用不同的分辨率(resolution)進(jìn)行細(xì)胞聚類轩性,分辨率越大声登,聚類出來(lái)的細(xì)胞簇?cái)?shù)越多。
在Seurat中揣苏,我們使用FindClusters
函數(shù)進(jìn)行細(xì)胞聚類悯嗓,默認(rèn)情況下(algorithm = 1
),該函數(shù)將使用“ Louvain”算法進(jìn)行基于圖的聚類卸察。要使用leiden算法脯厨,我們需要將其設(shè)置為algorithm = 4
。
# Clustering with louvain (algorithm 1)
for (res in c(0.1, 0.25, 0.5, 1, 1.5, 2)) {
alldata <- FindClusters(alldata, graph.name = "CCA_snn", resolution = res, algorithm = 1)
}
# each time you run clustering, the data is stored in meta data columns:
# seurat_clusters - lastest results only CCA_snn_res.XX - for each different
# resolution you test.
plot_grid(ncol = 3, DimPlot(alldata, reduction = "umap", group.by = "CCA_snn_res.0.5") + ggtitle("louvain_0.5"),
DimPlot(alldata, reduction = "umap", group.by = "CCA_snn_res.1") + ggtitle("louvain_1"),
DimPlot(alldata, reduction = "umap", group.by = "CCA_snn_res.2") + ggtitle("louvain_2"))
現(xiàn)在坑质,我們可以使用clustree
包來(lái)可視化不同分辨率下細(xì)胞在聚類群之間的分配合武。
# install.packages('clustree')
suppressPackageStartupMessages(library(clustree))
clustree(alldata@meta.data, prefix = "CCA_snn_res.")
K均值聚類
K-means是一種常用的聚類算法,已在許多應(yīng)用領(lǐng)域中使用涡扼。在R中稼跳,可以通過(guò)kmeans
函數(shù)進(jìn)行調(diào)用。通常吃沪,它應(yīng)用于表達(dá)數(shù)據(jù)的降維表示(由于低維距離的可解釋性汤善,因此通常用于PCA)。
我們需要預(yù)先設(shè)定聚類群的數(shù)量票彪。由于聚類的結(jié)果取決于群集中心的初始化红淡,因此通常建議使用多個(gè)啟動(dòng)配置(通過(guò)nstart參數(shù))運(yùn)行K-means。
for (k in c(5, 7, 10, 12, 15, 17, 20)) {
alldata@meta.data[, paste0("kmeans_", k)] <- kmeans(x = alldata@reductions[["pca"]]@cell.embeddings, centers = k, nstart = 100)$cluster
}
plot_grid(ncol = 3, DimPlot(alldata, reduction = "umap", group.by = "kmeans_5") + ggtitle("kmeans_5"),
DimPlot(alldata, reduction = "umap", group.by = "kmeans_10") + ggtitle("kmeans_10"),
DimPlot(alldata, reduction = "umap", group.by = "kmeans_15") + ggtitle("kmeans_15"))
使用clustree函數(shù)查看不同聚類群的結(jié)果
clustree(alldata@meta.data, prefix = "kmeans_")
層次聚類
定義細(xì)胞之間的距離
基本的Rstats
包中包含一個(gè)dist
函數(shù)降铸,可以用于計(jì)算所有成對(duì)樣本之間的距離在旱。由于我們要計(jì)算樣本之間的距離,而不是基因之間的距離推掸,因此我們需要先對(duì)表達(dá)數(shù)據(jù)進(jìn)行轉(zhuǎn)置桶蝎,然后再將其應(yīng)用于dist
函數(shù)中。dist
函數(shù)中可用的距離計(jì)算方法有:“euclidean”, “maximum”, “manhattan”, “canberra”, “binary” or “minkowski”.
d <- dist(alldata@reductions[["pca"]]@cell.embeddings, method = "euclidean")
可以看到终佛,dist
函數(shù)不能實(shí)現(xiàn)correlation的方法俊嗽。但是,我們可以創(chuàng)建自己的距離并將其轉(zhuǎn)換為距離對(duì)象铃彰。我們首先可以使用cor
函數(shù)計(jì)算樣本之間的相關(guān)性绍豁。如您所知,相關(guān)性的范圍是從-1到1的牙捉,其中1表示兩個(gè)樣本最接近竹揍,-1表示兩個(gè)樣本最遠(yuǎn)敬飒,0介于兩者之間。但是芬位,這在定義距離時(shí)會(huì)產(chǎn)生問(wèn)題无拗,因?yàn)榫嚯x0表示兩個(gè)樣本最接近,距離1表示兩個(gè)樣本最遠(yuǎn)昧碉,而距離-1沒(méi)有意義英染。因此,我們需要將相關(guān)性轉(zhuǎn)換為正尺度(又稱adjacency):
將相關(guān)性轉(zhuǎn)換為0-1比例后被饿,我們可以簡(jiǎn)單地使用as.dist
函數(shù)將其轉(zhuǎn)換為距離對(duì)象四康。
# Compute sample correlations
# 計(jì)算細(xì)胞之間的相關(guān)性
sample_cor <- cor(Matrix::t(alldata@reductions[["pca"]]@cell.embeddings))
# Transform the scale from correlations
sample_cor <- (1 - sample_cor)/2
# Convert it to a distance object
d2 <- as.dist(sample_cor)
基于細(xì)胞之間的距離進(jìn)行層次聚類
在計(jì)算出所有樣本之間的距離之后,我們可以對(duì)其進(jìn)行層次聚類狭握。我們將使用hclust
函數(shù)實(shí)現(xiàn)該功能闪金,在該函數(shù)中,我們可以簡(jiǎn)單地使用上面創(chuàng)建的距離對(duì)象來(lái)運(yùn)行它论颅“タ眩可用的方法有:“ward.D”, “ward.D2”, “single”, “complete”, “average”, “mcquitty”, “median” or “centroid”。
# euclidean
h_euclidean <- hclust(d, method = "ward.D2")
# correlation
h_correlation <- hclust(d2, method = "ward.D2")
創(chuàng)建好分層聚類樹(shù)后恃疯,下一步就是定義哪些樣本屬于特定簇漏设。我們可以使用cutree
函數(shù)根據(jù)特定k值切割聚類樹(shù),以定義聚類群今妄。我們還可以定義簇的數(shù)量或確定高度愿题。
#euclidean distance
alldata$hc_euclidean_5 <- cutree(h_euclidean,k = 5)
alldata$hc_euclidean_10 <- cutree(h_euclidean,k = 10)
alldata$hc_euclidean_15 <- cutree(h_euclidean,k = 15)
#correlation distance
alldata$hc_corelation_5 <- cutree(h_correlation,k = 5)
alldata$hc_corelation_10 <- cutree(h_correlation,k = 10)
alldata$hc_corelation_15 <- cutree(h_correlation,k = 15)
plot_grid(ncol = 3,
DimPlot(alldata, reduction = "umap", group.by = "hc_euclidean_5")+ggtitle("hc_euc_5"),
DimPlot(alldata, reduction = "umap", group.by = "hc_euclidean_10")+ggtitle("hc_euc_10"),
DimPlot(alldata, reduction = "umap", group.by = "hc_euclidean_15")+ggtitle("hc_euc_15"),
DimPlot(alldata, reduction = "umap", group.by = "hc_corelation_5")+ggtitle("hc_cor_5"),
DimPlot(alldata, reduction = "umap", group.by = "hc_corelation_10")+ggtitle("hc_cor_10"),
DimPlot(alldata, reduction = "umap", group.by = "hc_corelation_15")+ggtitle("hc_cor_15")
)
保存細(xì)胞聚類的結(jié)果
saveRDS(alldata, "data/results/covid_qc_dr_int_cl.rds")
sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Catalina 10.15.5
##
## Matrix products: default
## BLAS/LAPACK: /Users/paulo.czarnewski/.conda/envs/scRNAseq2021/lib/libopenblasp-r0.3.12.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] parallel stats4 grid stats graphics grDevices utils
## [8] datasets methods base
##
## other attached packages:
## [1] rafalib_1.0.0 pheatmap_1.0.12
## [3] clustree_0.4.3 ggraph_2.0.4
## [5] reticulate_1.18 harmony_1.0
## [7] Rcpp_1.0.6 scran_1.18.0
## [9] SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0
## [11] Biobase_2.50.0 GenomicRanges_1.42.0
## [13] GenomeInfoDb_1.26.0 IRanges_2.24.0
## [15] S4Vectors_0.28.0 BiocGenerics_0.36.0
## [17] MatrixGenerics_1.2.0 matrixStats_0.57.0
## [19] ggplot2_3.3.3 cowplot_1.1.1
## [21] KernSmooth_2.23-18 fields_11.6
## [23] spam_2.6-0 dotCall64_1.0-0
## [25] DoubletFinder_2.0.3 Matrix_1.3-2
## [27] Seurat_3.2.3 RJSONIO_1.3-1.4
## [29] optparse_1.6.6
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.1.0 htmlwidgets_1.5.3
## [3] BiocParallel_1.24.0 Rtsne_0.15
## [5] munsell_0.5.0 codetools_0.2-18
## [7] ica_1.0-2 statmod_1.4.35
## [9] future_1.21.0 miniUI_0.1.1.1
## [11] withr_2.4.0 colorspace_2.0-0
## [13] knitr_1.30 ROCR_1.0-11
## [15] tensor_1.5 listenv_0.8.0
## [17] labeling_0.4.2 GenomeInfoDbData_1.2.4
## [19] polyclip_1.10-0 bit64_4.0.5
## [21] farver_2.0.3 parallelly_1.23.0
## [23] vctrs_0.3.6 generics_0.1.0
## [25] xfun_0.20 R6_2.5.0
## [27] graphlayouts_0.7.1 rsvd_1.0.3
## [29] locfit_1.5-9.4 hdf5r_1.3.3
## [31] bitops_1.0-6 spatstat.utils_1.20-2
## [33] DelayedArray_0.16.0 assertthat_0.2.1
## [35] promises_1.1.1 scales_1.1.1
## [37] gtable_0.3.0 beachmat_2.6.0
## [39] globals_0.14.0 goftest_1.2-2
## [41] tidygraph_1.2.0 rlang_0.4.10
## [43] splines_4.0.3 lazyeval_0.2.2
## [45] checkmate_2.0.0 yaml_2.2.1
## [47] reshape2_1.4.4 abind_1.4-5
## [49] backports_1.2.1 httpuv_1.5.5
## [51] tools_4.0.3 ellipsis_0.3.1
## [53] RColorBrewer_1.1-2 ggridges_0.5.3
## [55] plyr_1.8.6 sparseMatrixStats_1.2.0
## [57] zlibbioc_1.36.0 purrr_0.3.4
## [59] RCurl_1.98-1.2 rpart_4.1-15
## [61] deldir_0.2-9 pbapply_1.4-3
## [63] viridis_0.5.1 zoo_1.8-8
## [65] ggrepel_0.9.1 cluster_2.1.0
## [67] magrittr_2.0.1 data.table_1.13.6
## [69] RSpectra_0.16-0 scattermore_0.7
## [71] lmtest_0.9-38 RANN_2.6.1
## [73] fitdistrplus_1.1-3 patchwork_1.1.1
## [75] mime_0.9 evaluate_0.14
## [77] xtable_1.8-4 gridExtra_2.3
## [79] compiler_4.0.3 tibble_3.0.5
## [81] maps_3.3.0 crayon_1.3.4
## [83] htmltools_0.5.1 mgcv_1.8-33
## [85] venn_1.9 later_1.1.0.1
## [87] tidyr_1.1.2 DBI_1.1.1
## [89] tweenr_1.0.1 formatR_1.7
## [91] MASS_7.3-53 getopt_1.20.3
## [93] igraph_1.2.6 pkgconfig_2.0.3
## [95] plotly_4.9.3 scuttle_1.0.0
## [97] admisc_0.11 dqrng_0.2.1
## [99] XVector_0.30.0 stringr_1.4.0
## [101] digest_0.6.27 sctransform_0.3.2
## [103] RcppAnnoy_0.0.18 spatstat.data_1.7-0
## [105] rmarkdown_2.6 leiden_0.3.6
## [107] uwot_0.1.10 edgeR_3.32.0
## [109] DelayedMatrixStats_1.12.0 curl_4.3
## [111] shiny_1.5.0 lifecycle_0.2.0
## [113] nlme_3.1-151 jsonlite_1.7.2
## [115] BiocNeighbors_1.8.0 viridisLite_0.3.0
## [117] limma_3.46.0 pillar_1.4.7
## [119] lattice_0.20-41 fastmap_1.0.1
## [121] httr_1.4.2 survival_3.2-7
## [123] glue_1.4.2 remotes_2.2.0
## [125] spatstat_1.64-1 png_0.1-7
## [127] bluster_1.0.0 bit_4.0.4
## [129] ggforce_0.3.2 stringi_1.5.3
## [131] BiocSingular_1.6.0 dplyr_1.0.3
## [133] irlba_2.3.3 future.apply_1.7.0
參考來(lái)源:https://nbisweden.github.io/workshop-scRNAseq/labs/compiled/seurat/seurat_04_clustering.html