單細胞數據整合-1:Harmony原理介紹和官網教程

<meta charset="utf-8">

<article class="_2rhmJa">

1. 原理介紹

官網:https://github.com/immunogenomics/harmony
(Harmony必須在R版本3.4以上運行,支持 Linux, OS X, and Windows 平臺。)
文章:https://www.biorxiv.org/content/early/2018/11/04/461954
harmony算法與其他整合算法相比的優勢:

(1)整合數據的同時對稀有細胞的敏感性依然很好;
(2)省內存;
(3)適合于更復雜的單細胞分析實驗設計,可以比較來自不同供體,組織和技術平臺的細胞。

基本原理:我們用不同顏色表示不同數據集,用形狀表示不同的細胞類型。首先,Harmony應用主成分分析(一文看懂PCA主成分分析)將轉錄組表達譜嵌入到低維空間中,然后應用迭代過程去除數據集特有的影響。

(A)Harmony概率性地將細胞分配給cluster,從而使每個cluster內數據集的多樣性最大化。
(B)Harmony計算每個cluster的所有數據集的全局中心,以及特定數據集的中心。
(C)在每個cluster中,Harmony基于中心為每個數據集計算校正因子。
(D)最后,Harmony使用基于C的特定于細胞的因子校正每個細胞。由于Harmony使用軟聚類,因此可以通過多個因子的線性組合對其A中進行的軟聚類分配進行線性校正,來修正每個單細胞。
重復步驟A到D,直到收斂為止。聚類分配和數據集之間的依賴性隨著每一輪的減少而減小。

image

2. 操作演示

R包安裝

library(devtools)
install_github("immunogenomics/harmony")

安裝過程可能包括從源代碼編譯C++代碼,因此可能需要幾分鐘。

下載稀疏矩陣示例(https://www.dropbox.com/s/t06tptwbyn7arb6/pbmc_stim.RData?dl=1)

library(Seurat)
library(cowplot)
library(harmony)
load('data/pbmc_stim.RData') #加載矩陣數據
#在運行Harmony之前,創建一個Seurat對象并按照標準PCA進行分析。
pbmc <- CreateSeuratObject(counts = cbind(stim.sparse, ctrl.sparse), project = "PBMC", min.cells = 5) %>%
    Seurat::NormalizeData(verbose = FALSE) %>%
    FindVariableFeatures(selection.method = "vst", nfeatures = 2000) %>%
    ScaleData(verbose = FALSE) %>%
    RunPCA(pc.genes = pbmc@var.genes, npcs = 20, verbose = FALSE) #R語言中%>%的含義是什么呢,管道函數啦,就是把左件的值發送給右件的表達式,并作為右件表達式函數的第一個參數。
pbmc@meta.data$stim <- c(rep("STIM", ncol(stim.sparse)), rep("CTRL", ncol(ctrl.sparse)))#賦值條件變量

未經校正的PC中的數據集之間存在明顯差異:

options(repr.plot.height = 5, repr.plot.width = 12)
p1 <- DimPlot(object = pbmc, reduction = "pca", pt.size = .1, group.by = "stim")
p2 <- VlnPlot(object = pbmc, features = "PC_1", group.by = "stim", pt.size = .1)
plot_grid(p1,p2)

image

Run Harmony

運行Harmony的最簡單方法是傳遞Seurat對象并指定要集成的變量。RunHarmony返回Seurat對象,并使用更正后的Harmony坐標(使用Harmony代替PCA)。將plot_convergence設置為TRUE,這樣我們就可以確保Harmony目標函數在每一輪中都變得更好。

RunHarmony函數中主要參數:

  • group.by.vars參數是設置按哪個分組來整合
  • max.iter.harmony設置迭代次數,默認是10。運行RunHarmony結果會提示在迭代多少次后完成了收斂。
  • ??lambda參數,默認值是1,決定了Harmony整合的力度。lambda值調小,整合力度變大,反之。(只有這個參數影響整合力度,調整范圍一般在0.5-2之間)
  • ??theta參數:Diversity clustering penalty parameter. Specify for each variable in group.by.vars. Default theta=2. theta=0 does not encourage any diversity. Larger values of theta result in more diverse clusters. 這個參數我常用默認值,但在不同文獻中這個參數往往不同。
  • ??dims.use參數:Which PCA dimensions to use for Harmony. By default, use all.
  • sigma參數:Width of soft kmeans clusters. Default sigma=0.1. Sigma scales the distance from a cell to cluster centroids. Larger values of sigma result in cells assigned to more clusters. Smaller values of sigma make soft kmeans cluster approach hard clustering.
options(repr.plot.height = 2.5, repr.plot.width = 6)
pbmc <- pbmc %>%
RunHarmony("stim", plot_convergence = TRUE) #Harmony converged after 8 iterations

image

Harmory運行后的結果儲存在:

pbmc@reductions$harmony

使用Embeddings命令訪問新的Harmony embeddings。

harmony_embeddings <- Embeddings(pbmc, 'harmony')
harmony_embeddings[1:5, 1:5]

image

讓我們查看確認數據集在Harmony運行之后的前兩個維度中得到很好的整合。

options(repr.plot.height = 5, repr.plot.width = 12)
p1 <- DimPlot(object = pbmc, reduction = "harmony", pt.size = .1, group.by = "stim")
p2 <- VlnPlot(object = pbmc, features = "harmony_1", group.by = "stim", pt.size = .1)
plot_grid(p1,p2)

image

Downstream analysis

許多下游分析是在低維嵌入而不是基因表達上進行的。要使用校正后的Harmony embeddings而不是PC,設置reduction ='harmony'

pbmc <- pbmc %>%
    RunUMAP(reduction = "harmony", dims = 1:20) %>%
    FindNeighbors(reduction = "harmony", dims = 1:20) %>%
    FindClusters(resolution = 0.5) %>%
    identity()

在UMAP embedding中,我們可以看到更復雜的結構。由于我們使用harmony embeddings,因此UMAP embeddings混合得很好。

options(repr.plot.height = 4, repr.plot.width = 10)
DimPlot(pbmc, reduction = "umap", group.by = "stim", pt.size = .1, split.by = 'stim')

image

TSNE分析

pbmc=RunTSNE(pbmc,reduction = "harmony", dims = 1:20)
TSNEPlot(object = pbmc, pt.size = 0.5, label = TRUE,split.by='stim') 

image

兩樣本合并的TSNE和UMAP圖

DimPlot(pbmc, reduction = "umap",pt.size = .1,  label = TRUE)
TSNEPlot(pbmc, pt.size = .1, label = TRUE) 

隨后就可以尋找差異表達基因并對細胞進行注釋。

3. 關于Harmony操作是否會對差異分析產生影響

Harmony輸入的是scRNA@reductionspca的數據,得出的結果儲存在scRNA@reductionsharmony中。

image

而差異分析使用的是scRNA@assays$RNA@counts數據,互不影響。

4. 多樣本批次矯正方法匯總

工具 Batch-effect-corrected output 方法
Seurat2 R Normalized canonical components Canonical correlation analysis and dynamic time warping
Seurat3 R Normalized gene expression matrix CCA and mutural nearest neighbors-anchors
Harmony R Normalized feature reduction vectors Iterative clustering in dimensionally reduced space
MNN Correct R Normalized gene expression matrix Mutual nearest neighbor in gene expression space
fastMNN R Normalized principal components MNN in dimensionally reduced space
ComBat R Normalized gene expression matrix Adjusts for known batches using an empirical Bayesian framework
limma R Normalized gene expression matrix Linear model/empirical Bayes model
scGen R Normalized gene expression matrix Variational auto-encoders neural network model and latent space
Scanorama R/P Normalized gene expression matrix Mutual nearest neighbor and panoramic stitching
MND-ResNet P Normalized principal components Residual neural network for calibration
ZINB-WaVE R Normalized gene expression matrix Zero-inflated negative binomial model, extension of RUV model
scMerge R Normalized gene expression matrix Stably expressed genes (scSEGs) and RUVIII model
LIGER R Normalized feature reduction vectors Integrative non-negative matrix factorization (iNMF) and joint clustering + quantile alignment
BBKNN P Connectivity graph and normalized dimension reduction vectors (UMAP) Batch balanced k-nearest neighbors

</article>

7人點贊

單細胞測序學習記錄

作者:Hayley筆記
鏈接:http://www.lxweimin.com/p/7c43dc99c4b1
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容