R語言: 從pheatmap無縫遷移至ComplexHeatmap

pheatmap是一個(gè)非常受歡迎的繪制熱圖的R包。ComplexHeatmap包即是受之啟發(fā)而來。你可以發(fā)現(xiàn)Heatmap()函數(shù)中很多參數(shù)都與pheatmap()相同。在pheatmap的時(shí)代(請(qǐng)?jiān)试S我這么說),pheatmap意思是pretty heatmap,但是隨著時(shí)間推進(jìn),技術(shù)發(fā)展,各種新的數(shù)據(jù)出現(xiàn),pretty is no more pretty,我們需要更加復(fù)雜和更有效率的熱圖可視化方法對(duì)龐大的數(shù)據(jù)進(jìn)行快速并且有效的解讀,因此我開發(fā)并且一直維護(hù)和改進(jìn)著ComplexHeatmap包。為了使龐大并且“陳舊”的(對(duì)不起,我不應(yīng)該這么說。)pheatmap用戶群能夠迅速并且無痛的遷移至ComplexHeatmap,從2.5.2版本開始,我在ComplexHeatmap包中加入了一個(gè)pheatmap()函數(shù),它涵蓋了pheatmap::pheatmap()所有的功能,也就是說,它提供了和pheatmap::pheatmap()一模一樣的參數(shù),并且生成的熱圖的樣式也幾乎相同。同時(shí),ComplexHeatmap::pheatmap()函數(shù)也能使用ComplexHeatmap獨(dú)有的功能,比如對(duì)行和列進(jìn)行切分,加入自定義的annotation,多個(gè)熱圖和annotation的連接,或者創(chuàng)建一個(gè)互動(dòng)的熱圖(interactive heatmap, 通過ht_shiny()函數(shù))

ComplexHeatmap::pheatmap()包含了pheatmap::pheatmap()中所有的參數(shù),這意味著,當(dāng)你從pheatmap遷移至ComplexHeatmap時(shí),你無需添加任何額外的步驟,你只需要載入ComplexHeatmap而不是pheatmap包,然后重新運(yùn)行你原始的pheatmap代碼。剩下的你只是去見證奇跡的發(fā)生。

注意如下五個(gè)pheatmap::pheatmap()的參數(shù)在ComplexHeatmap::pheatmap()中被忽視:

  • kmeans_k:在pheatmap::pheatmap()中,如果這個(gè)參數(shù)被設(shè)定,輸入矩陣會(huì)進(jìn)行k均值聚類,然后每個(gè)cluster使用其均值向量表示。最終的熱圖是k個(gè)均值向量的熱圖。此操作改變了原始矩陣的大小,而且每個(gè)cluster的大小信息丟失了,直接解讀均值向量可能會(huì)造成對(duì)數(shù)據(jù)的誤解。我不贊成此操作,因此我沒有支持這個(gè)參數(shù)。在ComplexHeatmap中,row_kmcolumn_km參數(shù)可能是一個(gè)更好的選擇。
  • filename:如果這個(gè)參數(shù)被設(shè)定,熱圖直接保存至指定的文件中。我認(rèn)為這只是畫蛇添足(沒有貶低pheatmap的意思,只是最近在給小孩講成語故事,然后想在這里使用一下)的一步,ComplexHeatmap::pheatmap()不支持此參數(shù)。
  • width:filename的寬度。
  • height:filename的長度。
  • silent: 是否打印信息。

pheatmap::pheatmap()中,color參數(shù)需要設(shè)置為一個(gè)長長的顏色向量(如果你想用100種顏色的話),比如:

pheatmap::pheatmap(mat,     color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100))

ComplexHeatmap::pheatmap()中,你可以簡化無需使用colorRampPalette()去擴(kuò)展更多的顏色,你可以直接簡化為如下,顏色會(huì)被自動(dòng)插值和擴(kuò)展。

ComplexHeatmap::pheatmap(mat,     color = rev(brewer.pal(n = 7, name = "RdYlBu")))

例子

我們首先創(chuàng)建一個(gè)隨機(jī)數(shù)據(jù),這個(gè)來自于pheatmap包中提供的例子(https://rdrr.io/cran/pheatmap/man/pheatmap.html).

test = matrix(rnorm(200), 20, 10)test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4colnames(test) = paste("Test", 1:10, sep = "")rownames(test) = paste("Gene", 1:20, sep = "")

我們載入ComplexHeatmap包,然后執(zhí)行pheatmap()函數(shù),生成一副和pheatmap::pheatmap()非常類似的熱圖。

library(ComplexHeatmap)# 注意這是ComplexHeatmap::pheatmappheatmap(test)
image

ComplexHeatmap::pheatmap()中,按照pheatmap::pheatmap()的樣式進(jìn)行了相應(yīng)的配置,因此,大部分元素的樣式一模一樣。只有少部分不一致,比如說熱圖的legend。

下一個(gè)例子是在熱圖中加入annotation。以下代碼是在pheatmap()中添加annotation。如果你是pheatmap()用戶,你應(yīng)該對(duì)annotation的數(shù)據(jù)格式不太陌生。

annotation_col = data.frame(    CellType = factor(rep(c("CT1", "CT2"), 5)),     Time = 1:5)rownames(annotation_col) = paste("Test", 1:10, sep = "")annotation_row = data.frame(    GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))rownames(annotation_row) = paste("Gene", 1:20, sep = "")ann_colors = list(    Time = c("white", "firebrick"),    CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),    GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))pheatmap(test,     annotation_col = annotation_col,     annotation_row = annotation_row,     annotation_colors = ann_colors)
image

看起來和pheatmap::pheatmap()還是很一致。

ComplexHeatmap::pheatmap()內(nèi)部其實(shí)使用了Heatmap()函數(shù),因此更多的參數(shù)都最終傳遞給了Heatmap()。我們可以在pheatmap()中使用一些Heatmap()特有的參數(shù),比如row_splitcolumn_split來對(duì)行和列進(jìn)行切分。

pheatmap(test,     annotation_col = annotation_col,     annotation_row = annotation_row,     annotation_colors = ann_colors,     row_split = annotation_row$GeneClass,    column_split = annotation_col$CellType)
image

ComplexHeatmap::pheatmap()返回一個(gè)Heatmap對(duì)象,因此它可以與其他Heatmap/HeatmapAnnotation對(duì)象連接。換句話說,你可以使用炫酷的+或者%v%對(duì)多個(gè)pheatmap水平連接或者垂直連接。

p1 = pheatmap(test, name = "mat1")p2 = rowAnnotation(foo = anno_barplot(1:nrow(test)))p3 = pheatmap(test, name = "mat2",     col = c("navy", "white", "firebrick3"))p1 + p2 + p3
image

ComplexHeatmap支持將一個(gè)熱圖導(dǎo)出為一個(gè)shiny app,這也同樣適用于pheatmap(),因此你可以這樣做:

ht = pheatmap(...)ht_shiny(ht) # 強(qiáng)烈建議試一試

還有一件重要的小事是,因?yàn)?code>ComplexHeatmap::pheatmap()返回一個(gè)Heatmap對(duì)象,如果pheatmap()并沒有在一個(gè)interactive的環(huán)境執(zhí)行,比如說在一個(gè)R腳本中,或者在一個(gè)函數(shù)/for loop中,你應(yīng)該顯式的調(diào)用draw()函數(shù)進(jìn)行畫圖。

for(...) {    p = pheatmap(...)    draw(p)}

最后我想說的事,這篇文章的主旨并不是鼓勵(lì)用戶直接使用ComplexHeatmap::pheatmap(),我只是在此展示了pheatmap完全可以用ComplexHeatmap來代替,而且ComplexHeatmap提供了工具讓用戶無需任何額外的操作(zero effort)就可以遷移以前舊的代碼。但是我還是強(qiáng)烈建議用戶直接使用ComplexHeatmap中的“正經(jīng)函數(shù)”。

從pheatmap到ComplexHeatmap的翻譯

在“閱讀原文”中,你可以找到一個(gè)表格,其中詳細(xì)的列出了如何將pheatmap::pheatmap()中的參數(shù)對(duì)應(yīng)到Heatmap()中。

比較

這一小節(jié)我比較了相同參數(shù)下pheatmap::pheatmap()生成的熱圖和ComplexHeatmap::pheatmap()的相似度。我使用了pheatmap包中所有的例子(https://rdrr.io/cran/pheatmap/man/pheatmap.html)。同時(shí)我也使用了ComplexHeatmap中提供的一個(gè)簡單的幫助函數(shù)ComplexHeatmap::compare_pheatmap()。它的功能就是把參數(shù)同時(shí)傳遞給pheatmap::pheatmap()ComplexHeatmap::pheatmap(),然后生成兩幅熱圖,這樣可以直接進(jìn)行比較。因此如下代碼

compare_pheatmap(test)

其實(shí)等同于:

pheatmap::pheatmap(test)ComplexHeatmap::pheatmap(test)

在往下閱讀之前,我先告訴你結(jié)論:pheatmap::pheatmap()ComplexHeatmap::pheatmap()產(chǎn)生的熱圖幾乎完全相同。

只提供一個(gè)矩陣:

compare_pheatmap(test)
image

對(duì)列進(jìn)行z-score歸一化,行聚類距離使用相關(guān)性距離:

compare_pheatmap(test,     scale = "row",     clustering_distance_rows = "correlation")
image

設(shè)定顏色:

compare_pheatmap(test,     color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
image

不對(duì)行聚類:

compare_pheatmap(test,     cluster_row = FALSE)
image

不顯示legend:

compare_pheatmap(test,     legend = FALSE)
image

在矩陣格子上顯示數(shù)值:

compare_pheatmap(test,     display_numbers = TRUE)
image

對(duì)矩陣格子上的數(shù)值進(jìn)行格式化:

compare_pheatmap(test,     display_numbers = TRUE,     number_format = "%.1e")
image

自定義矩陣格子上的文字:

compare_pheatmap(test,     display_numbers = matrix(ifelse(test > 5, "*", ""),                              nrow(test)))
image

定義legend上的label:

compare_pheatmap(test,     cluster_row = FALSE,     legend_breaks = -1:4,     legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))
image

熱圖的標(biāo)題:

compare_pheatmap(test,     cellwidth = 15,     cellheight = 12,     main = "Example heatmap")
image

添加列的annotation:

annotation_col = data.frame(    CellType = factor(rep(c("CT1", "CT2"), 5)),     Time = 1:5)rownames(annotation_col) = paste("Test", 1:10, sep = "")annotation_row = data.frame(    GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))rownames(annotation_row) = paste("Gene", 1:20, sep = "")compare_pheatmap(test,     annotation_col = annotation_col)
image

不繪制annotation的legend:

compare_pheatmap(test,     annotation_col = annotation_col,     annotation_legend = FALSE)
image

同時(shí)添加行和列的annotation:

compare_pheatmap(test,     annotation_col = annotation_col,     annotation_row = annotation_row)
image

調(diào)整列名的旋轉(zhuǎn):

compare_pheatmap(test,     annotation_col = annotation_col,     annotation_row = annotation_row,     angle_col = "45")
image

調(diào)整列名的旋轉(zhuǎn)至水平方向:

compare_pheatmap(test,     annotation_col = annotation_col,     angle_col = "0")
image

控制annotation的顏色:

ann_colors = list(    Time = c("white", "firebrick"),    CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),    GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))compare_pheatmap(test,     annotation_col = annotation_col,     annotation_colors = ann_colors,     main = "Title")
image

同時(shí)控制行和列annotation的顏色:

compare_pheatmap(test,     annotation_col = annotation_col,    annotation_row = annotation_row,     annotation_colors = ann_colors)
image

只提供部分annotation的顏色,未提供顏色的annotation使用隨機(jī)顏色:

compare_pheatmap(test,     annotation_col = annotation_col,     annotation_colors = ann_colors[2]) 
image

將熱圖分為兩部分,我建議直接使用Heatmap()中的row_split或者row_km參數(shù)。

compare_pheatmap(test,     annotation_col = annotation_col,     cluster_rows = FALSE,     gaps_row = c(10, 14))
image

使用cutree()對(duì)列的dendrogram切分:

compare_pheatmap(test,     annotation_col = annotation_col,     cluster_rows = FALSE,     gaps_row = c(10, 14),     cutree_col = 2)
image

自定義行名:

labels_row = c("", "", "", "", "", "",     "", "", "", "", "", "", "", "", "",     "", "", "Il10", "Il15", "Il1b")compare_pheatmap(test,     annotation_col = annotation_col,     labels_row = labels_row)
image

自定義聚類的距離:

drows = dist(test, method = "minkowski")dcols = dist(t(test), method = "minkowski")compare_pheatmap(test,     clustering_distance_rows = drows,     clustering_distance_cols = dcols)
image

對(duì)聚類的回調(diào)處理:

library(dendsort)callback = function(hc, ...){dendsort(hc)}compare_pheatmap(test,     clustering_callback = callback)
image

文章轉(zhuǎn)載自:【https://mp.weixin.qq.com/s?__biz=MzUzMzMwNjgzNA==&mid=2247488608&idx=1&sn=34e4b3fbb7584016976a2762ec249005&chksm=faa758ddcdd0d1cba062c1619e69d60bb9ff35e0d5086e2e7e08b30593bbaaf1c0344c650e4f&mpshare=1&scene=1&srcid=1129QpMCXENZ5MGFhgJp0bb2&sharer_sharetime=1606639806017&sharer_shareid=5e26caa3a2ed75e9d7c29e213ad5207a&key=41897554b8f6847e25c08aa02e9b63c319268bb40a0e7fdc3c06ca0da729b7d1e2bfc8aff56e1b0f07660939647bfc3e52ca7f14db0a45435025627cba4aaeee66e9050af3adb28fc9c4ad7531bbb03057e348b12ec5882c68d569eba055923fe3bdb30db82dd40972d3f7ccfd5c3f22c31fb5ffb2d811a9715a6ce440e7f36e&ascene=1&uin=MjY2MjY3OTUzMw%3D%3D&devicetype=Windows+8+x64&version=6300002f&lang=zh_CN&exportkey=A0bW8nRpQ9CIxwAto8jHEsk%3D&pass_ticket=9O5%2FRZux9VSyxlHx2C5QWqp5tCm4%2F7SLjOJEiKH56nP9PVovBrBu7uHVgb%2F9fBh%2B&wx_header=0

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容