眾所周知,seurat在降維之后主要依據兩個函數來進行細胞分類,這里我們來深入了解一下seurat如何進行細胞分類的。
首先我們來看有關分類的兩個函數
library(Seurat)
?FindNeighbors
Description:
Constructs a Shared Nearest Neighbor (SNN) Graph for a given
dataset. We first determine the k-nearest neighbors of each cell.
We use this knn graph to construct the SNN graph by calculating
the neighborhood overlap (Jaccard index) between every cell and
its k.param nearest neighbors.
這個地方說明,這個函數首先是計算每個細胞的KNN,也就是計算每個細胞之間的相互距離,依據細胞之間距離的graph來構建snn graph(依據細胞之間“鄰居”的overlop)
這里有三個問題:1、knn是什么,2、Jaccard index又是什么 3、鄰居的判定
我們來看看參數(主要參數):
distance.matrix: Boolean value of whether the provided matrix is a
distance matrix; note, for objects of class ‘dist’, this
parameter will be set automatically
這個參數我們通常不會設置,但是默認是TRUE。
k.param: Defines k for the k-nearest neighbor algorithm
這個參數就是用來定義最相近的幾個細胞作為鄰居,默認是20
compute.SNN: also compute the shared nearest neighbor graph
計算共享鄰居的數量,一般不設置
prune.SNN: Sets the cutoff for acceptable Jaccard index when computing
the neighborhood overlap for the SNN construction. Any edges
with values less than or equal to this will be set to 0 and
removed from the SNN graph. Essentially sets the strigency of
pruning (0 - no pruning, 1 - prune everything).
在計算SNN構造的鄰域重疊時,為可接受的Jaccard index設置截止值。 值小于或等于此值的任何邊將被設置為0并從SNN圖中刪除。 本質上設置修剪的嚴格程度(0-不修剪,1-修剪所有內容)。**這個我們在后面討論**。
nn.method: Method for nearest neighbor finding. Options include: rann,
annoy
這個參數提供了如何判斷鄰居的方法,提供的可選是rann和annoy,**這個我們在后面討論**。
annoy.metric: Distance metric for annoy. Options include: euclidean,
cosine, manhattan, and hamming
(annoy距離的方式)
force.recalc: Force recalculation of SNN
SNN強制重新計算,一般不設置
我們來一一解決其中的問題
1、什么是Jaccard index
Jaccard index , 又稱為Jaccard相似系數(Jaccard similarity coefficient)用于比較有限樣本集之間的相似性與差異性。Jaccard系數值越大,樣本相似度越高。
給定兩個集合A,B,Jaccard 系數定義為A與B交集的大小與A與B并集的大小的比值,定義如下:
Jaccard.png
這個函數用在這里就是說,兩個細胞共有"鄰居"數量和所有"鄰居"數量的比值,值越大,共享的”鄰居“越多,兩個細胞越相似。
還有Jaccard 距離的概念,公式是:
Jaccard 距離.png
2、KNN是什么,KNN是最簡單的機器學習算法,這里不多介紹了,大家可以參考百度KNN。
3、鄰居判定方法的rann和annoy
首先來說annoy,annoy全稱“Approximate Nearest Neighbors Oh Yeah”,是一種適合實際應用的快速相似查找算法。Annoy 同樣通過建立一個二叉樹來使得每個點查找時間復雜度是O(log n),和kd樹不同的是,annoy沒有對k維特征進行切分。annoy的每一次空間劃分,可以看作聚類數為2的KMeans過程。收斂后在產生的兩個聚類中心連線之間建立一條垂線(圖中的黑線),把數據空間劃分為兩部分。在劃分的子空間內不停的遞歸迭代繼續劃分,直到每個子空間最多只剩下K個數據節點,劃分結束。最終生成的二叉樹具有如下類似結構,二叉樹底層是葉子節點記錄原始數據節點,其他中間節點記錄的是分割超平面的信息。
annoy.png
這聽起來不像人話,主要的思想類似于層次聚類,不同的是annoy采用的是二分法,一直分割直到剩下k個數據點,這是一種計算相似性的算法,細胞之間距離的計算提供了euclidean, cosine, manhattan, and hamming等幾種。
rann暫時沒查到,以后更新
接下來查看第二個函數
library(Seurat)
?FindClusters
Description:
Identify clusters of cells by a shared nearest neighbor (SNN)
modularity optimization based clustering algorithm. First
calculate k-nearest neighbors and construct the SNN graph. Then
optimize the modularity function to determine clusters. For a full
description of the algorithms, see Waltman and van Eck (2013) _The
European Physical Journal B_. Thanks to Nigel Delaney
(evolvedmicrobe@github) for the rewrite of the Java modularity
optimizer code in Rcpp!
這個說明,依據SNN來識別類群,當然算法很復雜,我們可以參考給的網址。
我們來看看主要參數
resolution: Value of the resolution parameter, use a value above
(below) 1.0 if you want to obtain a larger (smaller) number
of communities.
這個參數可以理解為清晰度,值越低,可以容納更少的共享鄰居數量,聚類數也會變少。
modularity.fxn: 計算模塊系數函數,1為標準函數;2為備選函數,這里沒有具體說明是什么函數,我認為1是上面提到的Kronecker delta函數。
method: Method for running leiden (defaults to matrix which is fast
for small datasets). Enable method = "igraph" to avoid
casting large data to a dense matrix.
這個參數表示leiden算法的計算方式,(我對算法是小白~,求大神告知)
algorithm: 模塊系數優化算法,1使用原始Louvain算法;2使用Louvain algorithm with multilevel refinement;3使用SLM算法;4使用Leiden算法(注:4需要額外安裝插件)
n.start: 隨機開始的數量,默認是10
random.seed: 隨機數種子,默認是0
里面有很多算法方面的內容,對于數學而言,不會就是不會,沒什么炫耀的,但是要知道其原理。
,這里分享給大家一些鏈接,大家可以多多研究研究。
Kronecker delta函數
leiden
Louvain
SLM
請保持憤怒,讓王多魚傾家蕩產