使用R包ComplexHeatmap绘制复杂热图_2020-04-07

## 1.设置当前工作目录

setwd("./ComplexHeatmap")

## 2.安装和导入R包:ComplexHeatmap

### 2.1 Bioconductor安装

if (!requireNamespace("BiocManager", quietly = TRUE))

  install.packages("BiocManager")

BiocManager::install("ComplexHeatmap")

### 2.2 导入R包

library(ComplexHeatmap)

library(openxlsx)

## 3.R包简要信息

### 3.1 Description

# Package: ComplexHeatmap

# Type: Package

# Title: Make Complex Heatmaps

# Version: 2.2.0

# Date: 2019-10-22

# Author: Zuguang Gu

# Maintainer: Zuguang Gu <z.gu@dkfz.de>

#  Depends: R (>= 3.1.2), methods, grid, graphics, stats, grDevices

# Imports: circlize (>= 0.4.5), GetoptLong, colorspace, clue,

# RColorBrewer, GlobalOptions (>= 0.1.0), parallel, png

# Suggests: testthat (>= 1.0.0), knitr, markdown, dendsort, Cairo, jpeg,

# tiff, fastcluster, dendextend (>= 1.0.1), grImport, grImport2,

# glue, GenomicRanges

# VignetteBuilder: knitr

# Description: Complex heatmaps are efficient to visualize associations

# between different sources of data sets and reveal potential patterns.

# Here the ComplexHeatmap package provides a highly flexible way to arrange

# multiple heatmaps and supports various annotation graphics.

# biocViews: Software, Visualization, Sequencing

# URL: https://github.com/jokergoo/ComplexHeatmap,

# https://jokergoo.github.io/ComplexHeatmap-reference/book/

#  License: MIT + file LICENSE

# git_url: https://git.bioconductor.org/packages/ComplexHeatmap

# git_branch: RELEASE_3_10

# git_last_commit: 8ef3ec8

# git_last_commit_date: 2019-10-29

# Date/Publication: 2019-10-29

# NeedsCompilation: no

# Packaged: 2019-10-30 03:00:31 UTC; biocbuild

# Built: R 3.6.1; ; 2019-10-30 12:58:59 UTC; windows

### 3.2 Main function

ls(package:ComplexHeatmap)

# [1] "%v%"

# [2] "[.AnnotationFunction"

# [3] "[.comb_mat"

# [4] "[.Heatmap"

# [5] "[.HeatmapAnnotation"

# [6] "[.HeatmapList"

# [7] "[.SingleAnnotation"

# [8] "+.AdditiveUnit"

# [9] "add_heatmap"

# [10] "AdditiveUnit"

# [11] "adjust_dend_by_x"

# [12] "adjust_heatmap_list"

# [13] "anno_barplot"

# [14] "anno_block"

# [15] "anno_boxplot"

# [16] "anno_density"

# [17] "anno_empty"

# [18] "anno_histogram"

# [19] "anno_horizon"

# [20] "anno_image"

# [21] "anno_joyplot"

# [22] "anno_lines"

# [23] "anno_link"

# [24] "anno_mark"

# [25] "anno_oncoprint_barplot"

# [26] "anno_points"

# [27] "anno_simple"

# [28] "anno_summary"

# [29] "anno_text"

# [30] "anno_zoom"

# [31] "annotation_axis_grob"

# [32] "annotation_legend_size"

# [33] "AnnotationFunction"

# [34] "c.ColorMapping"

# [35] "c.HeatmapAnnotation"

# [36] "cluster_within_group"

# [37] "color_mapping_legend"

# [38] "ColorMapping"

# [39] "column_dend"

# [40] "column_order"

# [41] "columnAnnotation"

# [42] "comb_degree"

# [43] "comb_name"

# [44] "comb_size"

# [45] "component_height"

# [46] "component_width"

# [47] "copy_all"

# [48] "decorate_annotation"

# [49] "decorate_column_dend"

# [50] "decorate_column_names"

# [51] "decorate_column_title"

# [52] "decorate_dend"

# [53] "decorate_dimnames"

# [54] "decorate_heatmap_body"

# [55] "decorate_row_dend"

# [56] "decorate_row_names"

# [57] "decorate_row_title"

# [58] "decorate_title"

# [59] "default_axis_param"

# [60] "default_get_type"

# [61] "dend_heights"

# [62] "dend_xy"

# [63] "dendrogramGrob"

# [64] "densityHeatmap"

# [65] "dim.Heatmap"

# [66] "dist2"

# [67] "draw"

# [68] "draw_annotation"

# [69] "draw_annotation_legend"

# [70] "draw_dend"

# [71] "draw_dimnames"

# [72] "draw_heatmap_body"

# [73] "draw_heatmap_legend"

# [74] "draw_heatmap_list"

# [75] "draw_title"

# [76] "extract_comb"

# [77] "get_color_mapping_list"

# [78] "get_legend_param_list"

# [79] "getXY_in_parent_vp"

# [80] "grid.annotation_axis"

# [81] "grid.boxplot"

# [82] "grid.dendrogram"

# [83] "grid.draw.Legends"

# [84] "Heatmap"

# [85] "heatmap_legend_size"

# [86] "HeatmapAnnotation"

# [87] "HeatmapList"

# [88] "height.AnnotationFunction"

# [89] "height.Heatmap"

# [90] "height.HeatmapAnnotation"

# [91] "height.HeatmapList"

# [92] "height.Legends"

# [93] "height.SingleAnnotation"

# [94] "height<-.AnnotationFunction"

# [95] "height<-.HeatmapAnnotation"

# [96] "height<-.SingleAnnotation"

# [97] "heightDetails.annotation_axis"

# [98] "heightDetails.legend"

# [99] "heightDetails.legend_body"

# [100] "heightDetails.packed_legends"

# [101] "ht_global_opt"

# [102] "ht_opt"

# [103] "is_abs_unit"

# [104] "Legend"

# [105] "Legends"

# [106] "length.HeatmapAnnotation"

# [107] "length.HeatmapList"

# [108] "list_components"

# [109] "list_to_matrix"

# [110] "make_column_cluster"

# [111] "make_comb_mat"

# [112] "make_layout"

# [113] "make_row_cluster"

# [114] "map_to_colors"

# [115] "max_text_height"

# [116] "max_text_width"

# [117] "merge_dendrogram"

# [118] "names.HeatmapAnnotation"

# [119] "names.HeatmapList"

# [120] "names<-.HeatmapAnnotation"

# [121] "ncol.Heatmap"

# [122] "nobs.AnnotationFunction"

# [123] "nobs.HeatmapAnnotation"

# [124] "nobs.SingleAnnotation"

# [125] "normalize_comb_mat"

# [126] "nrow.Heatmap"

# [127] "oncoPrint"

# [128] "order.comb_mat"

# [129] "packLegend"

# [130] "pindex"

# [131] "prepare"

# [132] "print.comb_mat"

# [133] "re_size"

# [134] "restore_matrix"

# [135] "row_anno_barplot"

# [136] "row_anno_boxplot"

# [137] "row_anno_density"

# [138] "row_anno_histogram"

# [139] "row_anno_link"

# [140] "row_anno_points"

# [141] "row_anno_text"

# [142] "row_dend"

# [143] "row_order"

# [144] "rowAnnotation"

# [145] "set_component_height"

# [146] "set_component_width"

# [147] "set_name"

# [148] "set_size"

# [149] "SingleAnnotation"

# [150] "size.AnnotationFunction"

# [151] "size.HeatmapAnnotation"

# [152] "size.SingleAnnotation"

# [153] "size<-.AnnotationFunction"

# [154] "size<-.HeatmapAnnotation"

# [155] "size<-.SingleAnnotation"

# [156] "smartAlign2"

# [157] "str.comb_mat"

# [158] "subset_gp"

# [159] "subset_matrix_by_row"

# [160] "subset_vector"

# [161] "summary.Heatmap"

# [162] "summary.HeatmapList"

# [163] "t.comb_mat"

# [164] "test_alter_fun"

# [165] "unify_mat_list"

# [166] "UpSet"

# [167] "upset_right_annotation"

# [168] "upset_top_annotation"

# [169] "width.AnnotationFunction"

# [170] "width.Heatmap"

# [171] "width.HeatmapAnnotation"

# [172] "width.HeatmapList"

# [173] "width.Legends"

# [174] "width.SingleAnnotation"

# [175] "width<-.AnnotationFunction"

# [176] "width<-.HeatmapAnnotation"

# [177] "width<-.SingleAnnotation"

# [178] "widthDetails.annotation_axis"

# [179] "widthDetails.legend"

# [180] "widthDetails.legend_body"

# [181] "widthDetails.packed_legends"

## 4.测试

### 4.1 读取测试数据

data <- read.xlsx("Protein_Expression_Data.xlsx",

                  sheet = "data",

                  startRow = 1,

                  colNames = T,

                  rowNames = F,

                  detectDates = F)

dim(data)

# [1] 591  19

colnames(data)

# [1] "Accession"                 

# [2] "Gene.Name"                 

# [3] "Description"               

# [4] "Coverage.[%]"               

# [5] "#.Peptides"                 

# [6] "#.PSMs"                     

# [7] "#.Unique.Peptides"         

# [8] "#.AAs"                     

# [9] "MW.[kDa]"                   

# [10] "calc..pI"                   

# [11] "Score.Sequest.HT:.Sequest.HT"

# [12] "C1"                         

# [13] "T2"                         

# [14] "C3"                         

# [15] "T4"                         

# [16] "C5"                         

# [17] "T6"                         

# [18] "C7"                         

# [19] "T8" 

#@ 提取所需要的列

data_clean <- data[,12:19]

dim(data_clean)

# [1] 591  8

#@ 合并新的蛋白表达矩阵数据

data_clean_new <- data.frame(data$Accession, data_clean)

#@ 修改列名

colnames(data_clean_new)[1] <- "Accession"

#@ 查看前六行数据

head(data_clean_new)

# Accession    C1  T2    C3    T4    C5    T6    C7    T8

# 1    P04114 105.2 93.8 125.2 139.6  82.8  76.9  74.2 102.2

# 2    P0C0L5  68.9 74.7 121.7 122.0  85.3  98.4 114.2 114.8

# 3    P0C0L4  88.4 95.4 111.9 133.2  81.3 101.6 103.9  84.3

# 4    P02768  90.9 84.0 110.1  89.3 120.8 100.5 101.1 103.2

# 5    P01024  89.5 83.9 105.0  94.8  97.6  70.5 121.2 137.5

# 6    P00734  93.2 92.9 101.1 130.4  76.5 110.0 107.9  88.1

rownames(data_clean_new) <- data_clean_new$Accession

data_clean_new$Accession <- NULL

right_annotation <- colMeans(data_clean_new)

class(right_annotation)

# [1] "numeric"

data_clean_new <- as.matrix(data_clean_new)

##********************************************************##

### 4.2 绘制barplot_heatmap ###

#### 4.2.1 colMean

ha = HeatmapAnnotation(barplot = anno_barplot(right_annotation, height = unit(3, "cm"),

                                          box_width = 0.9, outline = FALSE, gp = gpar(fill = 1:8)))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

#### 4.2.2 colSum

right_annotation <- colSums(data_clean_new)

ha = HeatmapAnnotation(barplot = anno_barplot(right_annotation, height = unit(3, "cm"),

                                              box_width = 0.9, outline = FALSE, gp = gpar(fill = 1:8)))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

### 4.3 boxplot_heatmap ###

#### 4.3.1 default color

ha = HeatmapAnnotation(boxplot = anno_boxplot(data_clean_new, height = unit(3, "cm"), box_width = 0.9, outline = FALSE))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

#### 4.3.2 custom color

ha = HeatmapAnnotation(boxplot = anno_boxplot(data_clean_new, height = unit(3, "cm"), box_width = 0.9, outline = FALSE, gp = gpar(fill = 1:8)))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

### 4.4 Block annotation Heatmap ###

#### 4.4.1 top_annotation

ha <- HeatmapAnnotation(block = anno_block(gp = gpar(2:4),

                                          labels = c("sample1", "sample2", "sample3"), labels_gp = gpar(col = c("green", "blue", "orange"))))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16),

        column_km = 3)

#### 4.4.2 left_annotation

ha <- rowAnnotation(block = anno_block(gp = gpar(2:4),

                                          labels = c("group1", "group2", "group3", "group4", "group5"), labels_gp = gpar(col = c("green", "blue", "orange", "red", "purple"))))

Heatmap(data_clean_new,

        name = "protein exp",

        left_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16),

        row_km = 5)

#### 4.4.3 Combine top and left annotation

top_annotation <- HeatmapAnnotation(block = anno_block(gp = gpar(2:4),

                                          labels = c("sample1", "sample2", "sample3"), labels_gp = gpar(col = c("green", "blue", "orange"))))

left_annotation <- rowAnnotation(block = anno_block(gp = gpar(2:4),

                                      labels = c("group1", "group2", "group3", "group4", "group5"), labels_gp = gpar(col = c("green", "blue", "orange", "red", "purple"))))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = top_annotation,

        left_annotation = left_annotation,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16),

        column_km = 3,

        row_km = 5)

### 4.5 anno_density

ha <- HeatmapAnnotation(density = anno_density(data_clean_new, gp = gpar(fill = 1:8)))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

### 4.6 histogram annotation

ha <- HeatmapAnnotation(histogram = anno_histogram(data_clean_new, gp = gpar(fill = 1:8)))

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

### 4.7 joyplot annotation

ha <- HeatmapAnnotation(joyplot = anno_joyplot(data_clean_new, gp = gpar(fill = 1:8)))

draw(ha, test = "joyplot")

Heatmap(data_clean_new,

        name = "protein exp",

        top_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

### 4.8 lines annotation

ha <- rowAnnotation(lines = anno_lines(data_clean_new, add_points = TRUE, pt_gp = gpar(col = 5:6), pch = c(1, 16)))

draw(ha, test = "lines")

Heatmap(data_clean_new,

        name = "protein exp",

        left_annotation = ha,

        na_col = "grey",

        row_names_gp = gpar(fontsize = 0.5),

        column_names_gp = gpar(fontsize = 16))

## 5.结束

sessionInfo()

# R version 3.6.3 (2020-02-29)

# Platform: x86_64-w64-mingw32/x64 (64-bit)

# Running under: Windows 10 x64 (build 18363)

#

# Matrix products: default

#

# locale:

#  [1] LC_COLLATE=Chinese (Simplified)_China.936

# [2] LC_CTYPE=Chinese (Simplified)_China.936 

# [3] LC_MONETARY=Chinese (Simplified)_China.936

# [4] LC_NUMERIC=C                             

# [5] LC_TIME=Chinese (Simplified)_China.936   

#

# attached base packages:

#  [1] grid      stats    graphics  grDevices utils    datasets

# [7] methods  base   

#

# other attached packages:

#  [1] openxlsx_4.1.4      ComplexHeatmap_2.2.0

#

# loaded via a namespace (and not attached):

#  [1] Rcpp_1.0.4          rstudioapi_0.11    cluster_2.1.0     

# [4] hms_0.5.3          progress_1.2.2      colorspace_1.4-1 

# [7] clue_0.3-57        rjson_0.2.20        R6_2.4.1         

# [10] rlang_0.4.5        tools_3.6.3        parallel_3.6.3   

# [13] circlize_0.4.8      png_0.1-7          crayon_1.3.4     

# [16] zip_2.0.4          BiocManager_1.30.10 RColorBrewer_1.1-2

# [19] vctrs_0.2.4        GlobalOptions_0.1.1 shape_1.4.4       

# [22] stringi_1.4.6      compiler_3.6.3      prettyunits_1.1.1 

# [25] pkgconfig_2.0.3    GetoptLong_0.1.8

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值