-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathheatmap.r
64 lines (51 loc) · 1.87 KB
/
heatmap.r
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
library(dplyr)
b = import('base')
io = import('io')
ar = import('array')
INFILE = commandArgs(TRUE)[1] %or% "../../scores/speed/gsea_reactome.RData"
OUTFILE = commandArgs(TRUE)[2] %or% "speed_linear.pdf"
data = io$load(INFILE)
index = data$index %>%
select(id, pathway, effect) %>%
arrange(pathway, effect) %>%
as.data.frame()
# scale each pathway across samples, then pathways per sample
scores = data$scores[index$id,]
scores[index$effect == "inhibiting"] = - scores[index$effect == "inhibiting"]
scores = t(scores)
rownames(scores) = substr(rownames(scores), 0, 40)
# remove id column, add names for pheatmap to understand
rownames(index) = index$id
index$id = NULL
# this does the same as scale columns
scale_pheatmap = function(mat, n=100, center=TRUE) {
color = 1:n
# color = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(n)
# scale matrix
x = ar$map(mat, along=1, scale)
# generate breaks
m = max(abs(c(min(x, na.rm = T), max(x, na.rm = T))))
breaks = seq(-m, m, length.out = n + 1)
scaled = color[as.numeric(cut(c(x), breaks=breaks, include.lowest=TRUE))]
mat[] = scaled
mat
}
pdf(OUTFILE, paper="a4r", width=26, height=20)
pheatmap::pheatmap(scores,
annotation = index,
cluster_cols = FALSE,
show_colnames = FALSE,
annotation_legend = TRUE)
# this does the same as scale columns
#pheatmap::pheatmap(scale_pheatmap(scores),
# annotation = index,
# cluster_cols = FALSE,
# show_colnames = FALSE,
# annotation_legend = TRUE)
pheatmap::pheatmap(scores,
annotation = index,
scale = "column",
cluster_cols = FALSE,
show_colnames = FALSE,
annotation_legend = TRUE)
dev.off()