誠然,小編的上一篇(如何讓你的圖變得高大上之ComplexHeatmap())結局得太匆匆,因此,本篇奉上作圖代碼實(shí)例。
本次,我們將展示一個(gè)甲基化與表達譜聯(lián)合分析的熱圖。本著(zhù)先學(xué)習再創(chuàng )造的態(tài)度,小編做了一下知識的搬運工,本篇所有代碼均引用自:Zuguang Gu, Roland Eils and Matthias Schlesner,?Complex heatmaps reveal patterns and correlations in multidimensional genomic data,?Bioinformatics, 2016。
我們先上效果圖:
其實(shí)代碼并不長(cháng),關(guān)鍵在于如何準備作圖所用數據以及對代碼的理解上,所以下面將著(zhù)重對這兩點(diǎn)進(jìn)行解釋說(shuō)明。
>library(ComplexHeatmap)
>library(circlize)
#為了測試方便,用setwd設置工作路徑
setwd(“/media/bmk/***/Meth_Exp”)
#rds格式是一種保存了數據的數據類(lèi)型和結構的格式,可以用函數saveRDS()來(lái)生成這種格式的文件。meth.rds文件來(lái)自R包示例文件,如需查看這個(gè)文件中都是什么,請參考(看不清熱圖的聚類(lèi)結果怎么辦)的方法。另外,文件中只包含了DMR關(guān)聯(lián)基因的甲基化和表達量呈現負相關(guān)的DMR
>res_list?=?readRDS(“data/meth.rds”)

#顯示樣本是腫瘤樣本還是正常樣本的標簽
>type?=?res_list$type
(20個(gè)正常樣本和20個(gè)腫瘤樣本)
#矩陣,其中行對應差異甲基化區域(DMR),矩陣中的值是每個(gè)樣品中DMR中的平均甲基化水平。
>mat_meth?=?res_list$mat_meth

#矩陣,其中行對應于與DMR相關(guān)的基因(即與DMR最近的基因,也可以叫做DMR關(guān)聯(lián)基因),矩陣中的值是每個(gè)樣品中每個(gè)基因的表達水平(對樣品中的每個(gè)基因的表達進(jìn)行了標準化)。
>mat_expr?=?res_list$mat_expr

#甲基化變化的方向(hyper表示腫瘤樣本中高甲基化,hypo表示腫瘤樣本中低甲基化)
>direction?=?res_list$direction
#甲基化與甲基化關(guān)聯(lián)基因相關(guān)性p值
>cor_pvalue?=?res_list$cor_pvalue
#基因類(lèi)型(如蛋白編碼基因或lincRNA等)
>gene_type?=?res_list$gene_type
#DMR注釋到基因的功能區間(如intergenic/intragenic或者TSS
>anno_gene?=?res_list$anno_gene
#DMR到關(guān)聯(lián)基因TSS的距離
>dist?=?res_list$dist
#與增強子重疊的DMR的部分
>anno_enhancer?=?res_list$anno_enhancer

#顏色定義參見(jiàn)上一篇(如何讓你的圖變得高大上之ComplexHeatmap()) ##我們首先定義兩個(gè)列注釋?zhuān)缓笾谱鲝碗s的熱圖。 >ht_global_opt( #Heatmap()實(shí)際上是單一熱圖的類(lèi)構造函數。如果需要組合超過(guò)一個(gè)熱圖,用戶(hù)可以通過(guò)+操作符添加熱圖。默認情況下,將兩個(gè)熱圖通過(guò)+連接后,第二個(gè)熱圖的行聚類(lèi)樹(shù)會(huì )去掉,行的順序會(huì )與是第一個(gè)熱圖的順序保持一致。
##首先計算甲基化矩陣的列聚類(lèi),以便可以將表達矩陣中的列調整為具有與甲基化矩陣中相同的列順序。
>column_tree?=?hclust(dist(t(mat_meth)))
>column_order?=?column_tree$order
>library(RColorBrewer)
#定義甲基化表達水平顏色,從0/blue-0.5/white-1/red漸變
>meth_col_fun?=?colorRamp2(c(0,?0.5,?1),?c(“blue”,?“white”,?“red”))
#定義甲基化變化方向對應顏色
>direction_col?=?c(“hyper”?=?“red”,?“hypo”?=?“blue”)
#定義表達水平顏色
>expr_col_fun?=?colorRamp2(c(-2,?0,?2),?c(“green”,?“white”,?“red”))
#定義相關(guān)性p值顏色
>pvalue_col_fun?=?colorRamp2(c(0,?2,?4),?c(“white”,?“white”,?“red”))
#定義基因類(lèi)型顏色
>gene_type_col?=?structure(brewer.pal(length(unique(gene_type)),?“Set3”),
names?=?unique(gene_type))
#定義注釋model顏色
>anno_gene_col?=?structure(brewer.pal(length(unique(anno_gene)),?“Set1”),
names?=?unique(anno_gene))
#定義距離顏色
>dist_col_fun?=?colorRamp2(c(0,?10000),?c(“black”,?“white”))
#定義增強子相關(guān)顏色
>enhancer_col_fun?=?colorRamp2(c(0,?1),?c(“white”,?“orange”))
#ht_global_opt()是一個(gè)可選函數,它會(huì )全局控制一些參數。我們可以通過(guò)此全局函數同時(shí)為所有熱圖/注釋設置一些參數。需要注意的是,一定將它放在熱圖代碼(也就是Heatmap())之前,并在繪制熱圖后重置所有選項值以消除對下一個(gè)熱圖的影響。
#可以通過(guò)?ComplexHeatmap::ht_global_opt查看此函數的幫助
>?names(ht_global_opt())????????#可查看該函數可定義的參數
[1]?“heatmap_row_names_gp”
[2]?“heatmap_column_names_gp”
[3]?“heatmap_row_title_gp”
[4]?“heatmap_column_title_gp”
[5]?“heatmap_legend_title_gp”
[6]?“heatmap_legend_title_position”
[7]?“heatmap_legend_labels_gp”
[8]?“heatmap_legend_grid_height”
[9]?“heatmap_legend_grid_width”
[10]?“heatmap_legend_grid_border”
[11]?“annotation_legend_title_gp”
[12]?“annotation_legend_title_position”
[13]?“annotation_legend_labels_gp”
[14]?“annotation_legend_grid_height”
[15]?“annotation_legend_grid_width”????[16]?“annotation_legend_grid_border”
[17]?“fast_hclust”
heatmap_legend_title_gp?=?gpar(fontsize?=?8,?fontface?=?“bold”),
heatmap_legend_labels_gp?=?gpar(fontsize?=?8),
heatmap_column_names_gp?=?gpar(fontsize?=?8),
heatmap_column_title_gp?=?gpar(fontsize?=?10),
heatmap_row_title_gp?=?gpar(fontsize?=?8)
)
#利用HeatmapAnnotation()對行或列注釋。HeatmapAnnotation()函數會(huì )生成一個(gè)注釋用的list對象。該函數的主要格式是HeatmapAnnotation(df/數據框, name/注釋名稱(chēng), col/注釋顏色列表, show_legend/是否顯示數據框中每一列的圖例)
#樣本類(lèi)型注釋?zhuān)琓umor樣本為pink,Control樣本為royalbule,名稱(chēng)在左邊
>ha?=?HeatmapAnnotation(type?=?type,
col?=?list(type?=?c(“Tumor”?=?“pink”,?“Control”?=?“royalblue”)),
annotation_name_side?=?“left”)
#不顯示圖例名稱(chēng)
>ha2?=?HeatmapAnnotation(type?=?type,
col?=?list(type?=?c(“Tumor”?=?“pink”,?“Control”?=?“royalblue”)),
show_legend?=?FALSE)
>ht_list?=?Heatmap(mat_meth,?name?=?“methylation”,?col?=?meth_col_fun,
column_order=?column_order,
top_annotation?=?ha,?column_title?=?“Methylation”)?+









復雜的熱圖顯示高度甲基化的DMR富含基因間和基因內區域,很少與增強子重疊。相反,低甲基化的DMR富含轉錄起始位點(diǎn)(TSS)和增強子。
知識點(diǎn)總結
1.ComplexHeatmap可實(shí)現單個(gè)熱圖的相加以實(shí)現數據之間的聯(lián)合。
2.ht_global_opt()函數可實(shí)現整個(gè)熱圖的全局控制,但要注意使用結束后進(jìn)行重置。
3.draw()函數在返回值是HeatmapList對象可以實(shí)現更多的控制。
]]>