a b/c_VisualizationScript/Visulz_bulkRNA_heatmap.R
1
2
# MESSAGE -----------------------------------------------------------------
3
#
4
# author: Yulin Lyu
5
# email: lvyulin@pku.edu.cn
6
#
7
# require: R whatever
8
#
9
# ---
10
11
# 1. Load packages --------------------------------------------------------
12
13
setwd("exampleData/RNA")
14
15
# grammar
16
library(tidyverse)
17
library(magrittr)
18
library(glue)
19
library(data.table)
20
21
# analysis
22
library(DESeq2)
23
24
# graphics
25
library(ComplexHeatmap)
26
library(circlize)
27
library(RColorBrewer)
28
library(ggsci)
29
library(scales)
30
31
# 2. Load data ------------------------------------------------------------
32
33
vsd <- readRDS("mid/vsd.rds")
34
vsdMtx <- assay(vsd)
35
36
diffData <- fread("mid/ES_vs_Fib.DEG.csv")
37
colnames(diffData)[1] <- "gene"
38
39
diffData[is.na(padj), padj := 1][]
40
diffData[, p := -log10(padj)][]
41
42
# 3. Plot -----------------------------------------------------------------
43
44
diffData[, type := "ns"][]
45
diffData[log2FoldChange > 3 & padj < 0.05, type := "up"][log2FoldChange < -3 & padj < 0.05, type := "down"][]
46
47
upGene <- diffData[order(p, decreasing = T)][type == "up"][1:10, gene]
48
downGene <- diffData[order(p, decreasing = T)][type == "down"][1:10, gene]
49
50
# heatmap with many genes
51
52
diffGene <- diffData[type != "ns", gene]
53
geneType <- diffData[type != "ns", type]
54
markGene <- c(upGene, downGene)
55
56
heatData <- vsdMtx[diffGene, ]
57
heatData %<>% apply(1, scale) %>% t() %>% set_colnames(colnames(vsdMtx))
58
59
colorFun <- colorRamp2(seq(1.5, -1.5, len = 9), brewer.pal(9, "RdBu"))
60
61
pdf("plot/heatmapMany.pdf", width = 6, height = 8)
62
63
Heatmap(
64
  heatData, col = colorFun, border = T,
65
  cluster_rows = T, cluster_columns = T,
66
  show_row_names = F, show_column_names = T,
67
  show_row_dend = F, show_column_dend = T,
68
  column_names_rot = 45,
69
  row_split = geneType,
70
  column_split = rep(c("Fib", "CiPS", "ES"), each = 2),
71
  left_annotation = rowAnnotation(
72
    type = anno_block(
73
      width = unit(.1, "in"), gp = gpar(fill = pal_nejm()(2)[2:1]))),
74
  right_annotation = rowAnnotation(
75
    gene = anno_mark(
76
      match(markGene, diffGene), markGene,
77
      labels_gp = gpar(fontface = "italic", fontsize = 10))),
78
  heatmap_legend_param = list(
79
    title = "Scaled expression",
80
    title_position = "lefttop-rot",
81
    legend_height = unit(1.5, "in")),
82
  width = unit(3, "in"),
83
  height = unit(6, "in")
84
)
85
86
dev.off()
87
88
# heatmap with few genes
89
90
setkey(diffData, gene)
91
92
usedGene <- c(upGene, downGene)
93
geneType <- diffData[usedGene, type]
94
95
heatData <- vsdMtx[usedGene, ]
96
heatData %<>% apply(1, scale) %>% t() %>% set_colnames(colnames(vsdMtx))
97
98
colorFun <- colorRamp2(seq(1.5, -1.5, len = 9), brewer.pal(9, "RdBu"))
99
100
pdf("plot/heatmapFew.pdf", width = 6, height = 6)
101
102
Heatmap(
103
  heatData, col = colorFun, border = F,
104
  cluster_rows = T, cluster_columns = T,
105
  show_row_names = T, show_column_names = T,
106
  show_row_dend = F, show_column_dend = T,
107
  column_names_rot = 45,
108
  row_names_gp = gpar(fontface = "italic"),
109
  row_split = geneType,
110
  column_split = rep(c("Fib", "CiPS", "ES"), each = 2),
111
  cell_fun = function(j, i, x, y, width, height, fill){
112
    grid.rect(x = x, y = y, width = width, height = height, 
113
              gp = gpar(col = "white", fill = NA))},
114
  left_annotation = rowAnnotation(
115
    type = anno_block(
116
      width = unit(.1, "in"), gp = gpar(fill = pal_nejm()(2)[2:1]))),
117
  heatmap_legend_param = list(
118
    title = "Scaled expression",
119
    title_position = "lefttop-rot",
120
    legend_height = unit(1.5, "in")),
121
  width = unit(3, "in"),
122
  height = unit(4, "in")
123
)
124
125
dev.off()
126