-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheatmap_plot.rtf
executable file
·106 lines (105 loc) · 3.42 KB
/
heatmap_plot.rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf600
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 \
### heatmap plot ###\
heatmap_plot <- function(m, groups, legend_colors, sample_names, type="normal", dendrogram="column", labrow="", bool_rowv=TRUE, bool_colv = TRUE, plot_path=NULL)\{\
\
# load package\
library(\'93gplots\'94)\
\
# create groups\
names(colors) <- levels(groups)\
\
# replaces NAs with 0\
m[is.na(m)] <- 0\
colnames(m) <- sample_names\
\
# should rows be reordered\
if (bool_rowv)\{\
rowv <- as.dendrogram(hclust(dist(m)))\
\} else \{\
rowv <- FALSE\
\}\
\
# should columns be reordered\
if (is.logical(bool_colv))\{\
if (bool_colv)\{\
colv <- as.dendrogram(hclust(dist(t(m))))\
\} else \{\
colv <- FALSE\
\}\
\} else \{\
colv <- bool_colv\
\}\
\
# specify colors\
if(is.null(legend_colors))\{\
sidecolors <- rep("white", times=ncol(m))\
\} else\{\
sidecolors <- legend_colors[groups]\
\}\
\
# create color palette\
heatmap_pal <- colorRampPalette(rev(brewer.pal(11, "RdBu")))\
\
# plot heatmap\
colors_pal <- colorRampPalette(pals::parula(40))\
par(mfrow=c(1,1))\
par(xpd=TRUE)\
if (type == "normal")\{\
heatmap.2(m, \
Rowv = rowv,\
Colv=colv, \
margins=c(8,8), cexCol = 1,labRow=labrow,col=heatmap_pal(50), ColSideColors = sidecolors, symkey = F,\
cex.lab=1.5, scale="none", trace="none", dendrogram=dendrogram)\
\}\
if (type == "centered")\{\
min_m <- min(m, na.rm=TRUE)\
max_m <- max(m, na.rm=TRUE)\
heatmap.2(m, \
Rowv = rowv,\
Colv=colv,\
labRow=labrow, margins=c(8,8), ColSideColors = sidecolors, trace="none",col=heatmap_pal(50),\
breaks = seq(from=-2,to=2, length.out=51), \
symkey = F,\
dendrogram=dendrogram)\
if(!is.null(plot_path))\{\
pdf(file=plot_path, width = 6, height = 6)\
heatmap.2(m, \
Rowv = rowv,\
Colv=colv,\
labRow=labrow, margins=c(8,8), ColSideColors = sidecolors, trace="none",col=heatmap_pal(50),\
breaks = seq(from=-2,to=2, length.out=51), \
symkey = F,\
dendrogram=dendrogram)\
dev.off()\
\}\
\} \
if (type == "standardized")\{\
min_m <- min(m, na.rm=TRUE)\
max_m <- max(m, na.rm=TRUE)\
heatmap.2(m, \
Rowv = rowv,\
Colv= colv,\
labRow=labrow, margins=c(8,8), ColSideColors = sidecolors, trace="none",col=heatmap_pal(50), symkey = F,\
breaks = seq(from=-2,to=2, length.out=51),\
dendrogram=dendrogram)\
if (!is.null(plot_path))\{\
pdf(file=plot_path, width = 6, height = 6)\
heatmap.2(m, \
Rowv = rowv,\
Colv= colv,\
labRow=labrow, margins=c(8,8), ColSideColors = sidecolors, trace="none",col=heatmap_pal(50), symkey = F,\
breaks = seq(from=-2,to=2, length.out=51),\
dendrogram=dendrogram)\
dev.off()\
\}\
\}\
\
# add legend\
par(xpd=TRUE)\
\}}