annotate Dotplot_Release/pheatmap_j.R @ 25:1c82a1d1ecd3 draft

Uploaded
author bornea
date Mon, 18 Apr 2016 11:34:37 -0400
parents bc752a05f16d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bc752a05f16d Uploaded
bornea
parents:
diff changeset
1 lo = function(rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheight_col, treeheight_row, legend, annotation, annotation_colors, annotation_legend, main, fontsize, fontsize_row, fontsize_col, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
2 # Get height of colnames and length of rownames
bc752a05f16d Uploaded
bornea
parents:
diff changeset
3 if(!is.null(coln[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
4 longest_coln = which.max(strwidth(coln, units = 'in'))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
5 gp = list(fontsize = fontsize_col, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
6 coln_height = unit(1, "grobheight", textGrob(coln[longest_coln], rot = 90, gp = do.call(gpar, gp))) + unit(5, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
7 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
8 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
9 coln_height = unit(5, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
10 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
11
bc752a05f16d Uploaded
bornea
parents:
diff changeset
12 if(!is.null(rown[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
13 longest_rown = which.max(strwidth(rown, units = 'in'))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
14 gp = list(fontsize = fontsize_row, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
15 rown_width = unit(1, "grobwidth", textGrob(rown[longest_rown], gp = do.call(gpar, gp))) + unit(10, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
16 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
17 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
18 rown_width = unit(5, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
19 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
20
bc752a05f16d Uploaded
bornea
parents:
diff changeset
21 gp = list(fontsize = fontsize, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
22 # Legend position
bc752a05f16d Uploaded
bornea
parents:
diff changeset
23 if(!is.na(legend[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
24 longest_break = which.max(nchar(names(legend)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
25 longest_break = unit(1.1, "grobwidth", textGrob(as.character(names(legend))[longest_break], gp = do.call(gpar, gp)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
26 title_length = unit(1.1, "grobwidth", textGrob("Scale", gp = gpar(fontface = "bold", ...)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
27 legend_width = unit(12, "bigpts") + longest_break * 1.2
bc752a05f16d Uploaded
bornea
parents:
diff changeset
28 legend_width = max(title_length, legend_width)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
29 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
30 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
31 legend_width = unit(0, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
32 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
33
bc752a05f16d Uploaded
bornea
parents:
diff changeset
34 # Set main title height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
35 if(is.na(main)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
36 main_height = unit(0, "npc")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
37 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
38 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
39 main_height = unit(1.5, "grobheight", textGrob(main, gp = gpar(fontsize = 1.3 * fontsize, ...)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
40 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
41
bc752a05f16d Uploaded
bornea
parents:
diff changeset
42 # Column annotations
bc752a05f16d Uploaded
bornea
parents:
diff changeset
43 if(!is.na(annotation[[1]][1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
44 # Column annotation height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
45 annot_height = unit(ncol(annotation) * (8 + 2) + 2, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
46 # Width of the correponding legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
47 longest_ann = which.max(nchar(as.matrix(annotation)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
48 annot_legend_width = unit(1.2, "grobwidth", textGrob(as.matrix(annotation)[longest_ann], gp = gpar(...))) + unit(12, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
49 if(!annotation_legend){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
50 annot_legend_width = unit(0, "npc")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
51 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
52 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
53 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
54 annot_height = unit(0, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
55 annot_legend_width = unit(0, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
56 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
57
bc752a05f16d Uploaded
bornea
parents:
diff changeset
58 # Tree height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
59 treeheight_col = unit(treeheight_col, "bigpts") + unit(5, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
60 treeheight_row = unit(treeheight_row, "bigpts") + unit(5, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
61
bc752a05f16d Uploaded
bornea
parents:
diff changeset
62 # Set cell sizes
bc752a05f16d Uploaded
bornea
parents:
diff changeset
63 if(is.na(cellwidth)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
64 matwidth = unit(1, "npc") - rown_width - legend_width - treeheight_row - annot_legend_width
bc752a05f16d Uploaded
bornea
parents:
diff changeset
65 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
66 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
67 matwidth = unit(cellwidth * ncol, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
68 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
69
bc752a05f16d Uploaded
bornea
parents:
diff changeset
70 if(is.na(cellheight)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
71 matheight = unit(1, "npc") - main_height - coln_height - treeheight_col - annot_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
72 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
73 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
74 matheight = unit(cellheight * nrow, "bigpts")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
75 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
76
bc752a05f16d Uploaded
bornea
parents:
diff changeset
77
bc752a05f16d Uploaded
bornea
parents:
diff changeset
78 # Produce layout()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
79 pushViewport(viewport(layout = grid.layout(nrow = 5, ncol = 5, widths = unit.c(treeheight_row, matwidth, rown_width, legend_width, annot_legend_width), heights = unit.c(main_height, treeheight_col, annot_height, matheight, coln_height)), gp = do.call(gpar, gp)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
80
bc752a05f16d Uploaded
bornea
parents:
diff changeset
81 # Get cell dimensions
bc752a05f16d Uploaded
bornea
parents:
diff changeset
82 pushViewport(vplayout(4, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
83 cellwidth = convertWidth(unit(0:1, "npc"), "bigpts", valueOnly = T)[2] / ncol
bc752a05f16d Uploaded
bornea
parents:
diff changeset
84 cellheight = convertHeight(unit(0:1, "npc"), "bigpts", valueOnly = T)[2] / nrow
bc752a05f16d Uploaded
bornea
parents:
diff changeset
85 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
86
bc752a05f16d Uploaded
bornea
parents:
diff changeset
87 # Return minimal cell dimension in bigpts to decide if borders are drawn
bc752a05f16d Uploaded
bornea
parents:
diff changeset
88 mindim = min(cellwidth, cellheight)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
89 return(mindim)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
90 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
91
bc752a05f16d Uploaded
bornea
parents:
diff changeset
92 draw_dendrogram = function(hc, horizontal = T){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
93 h = hc$height / max(hc$height) / 1.05
bc752a05f16d Uploaded
bornea
parents:
diff changeset
94 m = hc$merge
bc752a05f16d Uploaded
bornea
parents:
diff changeset
95 o = hc$order
bc752a05f16d Uploaded
bornea
parents:
diff changeset
96 n = length(o)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
97
bc752a05f16d Uploaded
bornea
parents:
diff changeset
98 m[m > 0] = n + m[m > 0]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
99 m[m < 0] = abs(m[m < 0])
bc752a05f16d Uploaded
bornea
parents:
diff changeset
100
bc752a05f16d Uploaded
bornea
parents:
diff changeset
101 dist = matrix(0, nrow = 2 * n - 1, ncol = 2, dimnames = list(NULL, c("x", "y")))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
102 dist[1:n, 1] = 1 / n / 2 + (1 / n) * (match(1:n, o) - 1)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
103
bc752a05f16d Uploaded
bornea
parents:
diff changeset
104 for(i in 1:nrow(m)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
105 dist[n + i, 1] = (dist[m[i, 1], 1] + dist[m[i, 2], 1]) / 2
bc752a05f16d Uploaded
bornea
parents:
diff changeset
106 dist[n + i, 2] = h[i]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
107 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
108
bc752a05f16d Uploaded
bornea
parents:
diff changeset
109 draw_connection = function(x1, x2, y1, y2, y){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
110 grid.lines(x = c(x1, x1), y = c(y1, y))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
111 grid.lines(x = c(x2, x2), y = c(y2, y))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
112 grid.lines(x = c(x1, x2), y = c(y, y))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
113 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
114
bc752a05f16d Uploaded
bornea
parents:
diff changeset
115 if(horizontal){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
116 for(i in 1:nrow(m)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
117 draw_connection(dist[m[i, 1], 1], dist[m[i, 2], 1], dist[m[i, 1], 2], dist[m[i, 2], 2], h[i])
bc752a05f16d Uploaded
bornea
parents:
diff changeset
118 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
119 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
120
bc752a05f16d Uploaded
bornea
parents:
diff changeset
121 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
122 gr = rectGrob()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
123 pushViewport(viewport(height = unit(1, "grobwidth", gr), width = unit(1, "grobheight", gr), angle = 90))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
124 dist[, 1] = 1 - dist[, 1]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
125 for(i in 1:nrow(m)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
126 draw_connection(dist[m[i, 1], 1], dist[m[i, 2], 1], dist[m[i, 1], 2], dist[m[i, 2], 2], h[i])
bc752a05f16d Uploaded
bornea
parents:
diff changeset
127 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
128 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
129 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
130 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
131
bc752a05f16d Uploaded
bornea
parents:
diff changeset
132 draw_matrix = function(matrix, border_color, border_width, fmat, fontsize_number){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
133 n = nrow(matrix)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
134 m = ncol(matrix)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
135 x = (1:m)/m - 1/2/m
bc752a05f16d Uploaded
bornea
parents:
diff changeset
136 y = 1 - ((1:n)/n - 1/2/n)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
137 for(i in 1:m){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
138 grid.rect(x = x[i], y = y[1:n], width = 1/m, height = 1/n, gp = gpar(fill = matrix[,i], col = border_color, lwd = border_width))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
139 if(attr(fmat, "draw")){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
140 grid.text(x = x[i], y = y[1:n], label = fmat[, i], gp = gpar(col = "grey30", fontsize = fontsize_number))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
141 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
142 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
143 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
144
bc752a05f16d Uploaded
bornea
parents:
diff changeset
145 draw_colnames = function(coln, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
146 m = length(coln)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
147 x = (1:m)/m - 1/2/m
bc752a05f16d Uploaded
bornea
parents:
diff changeset
148 grid.text(coln, x = x, y = unit(0.96, "npc"), just="right", rot = 90, gp = gpar(...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
149 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
150
bc752a05f16d Uploaded
bornea
parents:
diff changeset
151 draw_rownames = function(rown, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
152 n = length(rown)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
153 y = 1 - ((1:n)/n - 1/2/n)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
154 grid.text(rown, x = unit(0.04, "npc"), y = y, vjust = 0.5, hjust = 0, gp = gpar(...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
155 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
156
bc752a05f16d Uploaded
bornea
parents:
diff changeset
157 draw_legend = function(color, breaks, legend, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
158 height = min(unit(1, "npc"), unit(150, "bigpts"))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
159 pushViewport(viewport(x = 0, y = unit(1, "npc"), just = c(0, 1), height = height))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
160 legend_pos = (legend - min(breaks)) / (max(breaks) - min(breaks))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
161 breaks = (breaks - min(breaks)) / (max(breaks) - min(breaks))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
162 h = breaks[-1] - breaks[-length(breaks)]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
163 grid.rect(x = 0, y = breaks[-length(breaks)], width = unit(10, "bigpts"), height = h, hjust = 0, vjust = 0, gp = gpar(fill = color, col = "#FFFFFF00"))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
164 grid.text(names(legend), x = unit(12, "bigpts"), y = legend_pos, hjust = 0, gp = gpar(...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
165 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
166 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
167
bc752a05f16d Uploaded
bornea
parents:
diff changeset
168 convert_annotations = function(annotation, annotation_colors){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
169 new = annotation
bc752a05f16d Uploaded
bornea
parents:
diff changeset
170 for(i in 1:ncol(annotation)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
171 a = annotation[, i]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
172 b = annotation_colors[[colnames(annotation)[i]]]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
173 if(is.character(a) | is.factor(a)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
174 a = as.character(a)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
175 if(length(setdiff(a, names(b))) > 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
176 stop(sprintf("Factor levels on variable %s do not match with annotation_colors", colnames(annotation)[i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
177 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
178 new[, i] = b[a]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
179 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
180 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
181 a = cut(a, breaks = 100)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
182 new[, i] = colorRampPalette(b)(100)[a]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
183 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
184 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
185 return(as.matrix(new))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
186 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
187
bc752a05f16d Uploaded
bornea
parents:
diff changeset
188 draw_annotations = function(converted_annotations, border_color, border_width){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
189 n = ncol(converted_annotations)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
190 m = nrow(converted_annotations)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
191 x = (1:m)/m - 1/2/m
bc752a05f16d Uploaded
bornea
parents:
diff changeset
192 y = cumsum(rep(8, n)) - 4 + cumsum(rep(2, n))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
193 for(i in 1:m){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
194 grid.rect(x = x[i], unit(y[1:n], "bigpts"), width = 1/m, height = unit(8, "bigpts"), gp = gpar(fill = converted_annotations[i, ], col = border_color, lwd = border_width))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
195 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
196 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
197
bc752a05f16d Uploaded
bornea
parents:
diff changeset
198 draw_annotation_legend = function(annotation, annotation_colors, border_color, border_width, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
199 y = unit(1, "npc")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
200 text_height = unit(1, "grobheight", textGrob("FGH", gp = gpar(...)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
201 for(i in names(annotation_colors)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
202 grid.text(i, x = 0, y = y, vjust = 1, hjust = 0, gp = gpar(fontface = "bold", ...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
203 y = y - 1.5 * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
204 if(is.character(annotation[, i]) | is.factor(annotation[, i])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
205 for(j in 1:length(annotation_colors[[i]])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
206 grid.rect(x = unit(0, "npc"), y = y, hjust = 0, vjust = 1, height = text_height, width = text_height, gp = gpar(col = border_color, lwd = border_width, fill = annotation_colors[[i]][j]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
207 grid.text(names(annotation_colors[[i]])[j], x = text_height * 1.3, y = y, hjust = 0, vjust = 1, gp = gpar(...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
208 y = y - 1.5 * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
209 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
210 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
211 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
212 yy = y - 4 * text_height + seq(0, 1, 0.02) * 4 * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
213 h = 4 * text_height * 0.02
bc752a05f16d Uploaded
bornea
parents:
diff changeset
214 grid.rect(x = unit(0, "npc"), y = yy, hjust = 0, vjust = 1, height = h, width = text_height, gp = gpar(col = "#FFFFFF00", fill = colorRampPalette(annotation_colors[[i]])(50)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
215 txt = rev(range(grid.pretty(range(annotation[, i], na.rm = TRUE))))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
216 yy = y - c(0, 3) * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
217 grid.text(txt, x = text_height * 1.3, y = yy, hjust = 0, vjust = 1, gp = gpar(...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
218 y = y - 4.5 * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
219 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
220 y = y - 1.5 * text_height
bc752a05f16d Uploaded
bornea
parents:
diff changeset
221 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
222 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
223
bc752a05f16d Uploaded
bornea
parents:
diff changeset
224 draw_main = function(text, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
225 grid.text(text, gp = gpar(fontface = "bold", ...))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
226 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
227
bc752a05f16d Uploaded
bornea
parents:
diff changeset
228 vplayout = function(x, y){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
229 return(viewport(layout.pos.row = x, layout.pos.col = y))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
230 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
231
bc752a05f16d Uploaded
bornea
parents:
diff changeset
232 heatmap_motor = function(matrix, border_color, border_width, cellwidth, cellheight, tree_col, tree_row, treeheight_col, treeheight_row, filename, width, height, breaks, color, legend, annotation, annotation_colors, annotation_legend, main, fontsize, fontsize_row, fontsize_col, fmat, fontsize_number, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
233 grid.newpage()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
234
bc752a05f16d Uploaded
bornea
parents:
diff changeset
235 # Set layout
bc752a05f16d Uploaded
bornea
parents:
diff changeset
236 mindim = lo(coln = colnames(matrix), rown = rownames(matrix), nrow = nrow(matrix), ncol = ncol(matrix), cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, legend = legend, annotation = annotation, annotation_colors = annotation_colors, annotation_legend = annotation_legend, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
237
bc752a05f16d Uploaded
bornea
parents:
diff changeset
238 if(!is.na(filename)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
239 pushViewport(vplayout(1:5, 1:5))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
240
bc752a05f16d Uploaded
bornea
parents:
diff changeset
241 if(is.na(height)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
242 height = convertHeight(unit(0:1, "npc"), "inches", valueOnly = T)[2]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
243 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
244 if(is.na(width)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
245 width = convertWidth(unit(0:1, "npc"), "inches", valueOnly = T)[2]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
246 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
247
bc752a05f16d Uploaded
bornea
parents:
diff changeset
248 # Get file type
bc752a05f16d Uploaded
bornea
parents:
diff changeset
249 r = regexpr("\\.[a-zA-Z]*$", filename)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
250 if(r == -1) stop("Improper filename")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
251 ending = substr(filename, r + 1, r + attr(r, "match.length"))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
252
bc752a05f16d Uploaded
bornea
parents:
diff changeset
253 f = switch(ending,
bc752a05f16d Uploaded
bornea
parents:
diff changeset
254 pdf = function(x, ...) pdf(x, ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
255 png = function(x, ...) png(x, units = "in", res = 300, ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
256 jpeg = function(x, ...) jpeg(x, units = "in", res = 300, ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
257 jpg = function(x, ...) jpeg(x, units = "in", res = 300, ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
258 tiff = function(x, ...) tiff(x, units = "in", res = 300, compression = "lzw", ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
259 bmp = function(x, ...) bmp(x, units = "in", res = 300, ...),
bc752a05f16d Uploaded
bornea
parents:
diff changeset
260 stop("File type should be: pdf, png, bmp, jpg, tiff")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
261 )
bc752a05f16d Uploaded
bornea
parents:
diff changeset
262
bc752a05f16d Uploaded
bornea
parents:
diff changeset
263 # print(sprintf("height:%f width:%f", height, width))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
264 f(filename, height = height, width = width)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
265 heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, border_color = border_color, border_width = border_width, tree_col = tree_col, tree_row = tree_row, treeheight_col = treeheight_col, treeheight_row = treeheight_row, breaks = breaks, color = color, legend = legend, annotation = annotation, annotation_colors = annotation_colors, annotation_legend = annotation_legend, filename = NA, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, fmat = fmat, fontsize_number = fontsize_number, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
266 dev.off()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
267 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
268 return()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
269 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
270
bc752a05f16d Uploaded
bornea
parents:
diff changeset
271 # Omit border color if cell size is too small
bc752a05f16d Uploaded
bornea
parents:
diff changeset
272 if(mindim < 3) border_color = NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
273
bc752a05f16d Uploaded
bornea
parents:
diff changeset
274 # Draw title
bc752a05f16d Uploaded
bornea
parents:
diff changeset
275 if(!is.na(main)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
276 pushViewport(vplayout(1, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
277 draw_main(main, fontsize = 1.3 * fontsize, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
278 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
279 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
280
bc752a05f16d Uploaded
bornea
parents:
diff changeset
281 # Draw tree for the columns
bc752a05f16d Uploaded
bornea
parents:
diff changeset
282 if(!is.na(tree_col[[1]][1]) & treeheight_col != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
283 pushViewport(vplayout(2, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
284 draw_dendrogram(tree_col, horizontal = T)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
285 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
286 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
287
bc752a05f16d Uploaded
bornea
parents:
diff changeset
288 # Draw tree for the rows
bc752a05f16d Uploaded
bornea
parents:
diff changeset
289 if(!is.na(tree_row[[1]][1]) & treeheight_row != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
290 pushViewport(vplayout(4, 1))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
291 draw_dendrogram(tree_row, horizontal = F)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
292 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
293 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
294
bc752a05f16d Uploaded
bornea
parents:
diff changeset
295 # Draw matrix
bc752a05f16d Uploaded
bornea
parents:
diff changeset
296 pushViewport(vplayout(4, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
297 draw_matrix(matrix, border_color, border_width, fmat, fontsize_number)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
298 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
299
bc752a05f16d Uploaded
bornea
parents:
diff changeset
300 # Draw colnames
bc752a05f16d Uploaded
bornea
parents:
diff changeset
301 if(length(colnames(matrix)) != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
302 pushViewport(vplayout(5, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
303 pars = list(colnames(matrix), fontsize = fontsize_col, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
304 do.call(draw_colnames, pars)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
305 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
306 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
307
bc752a05f16d Uploaded
bornea
parents:
diff changeset
308 # Draw rownames
bc752a05f16d Uploaded
bornea
parents:
diff changeset
309 if(length(rownames(matrix)) != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
310 pushViewport(vplayout(4, 3))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
311 pars = list(rownames(matrix), fontsize = fontsize_row, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
312 do.call(draw_rownames, pars)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
313 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
314 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
315
bc752a05f16d Uploaded
bornea
parents:
diff changeset
316 # Draw annotation tracks
bc752a05f16d Uploaded
bornea
parents:
diff changeset
317 if(!is.na(annotation[[1]][1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
318 pushViewport(vplayout(3, 2))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
319 converted_annotation = convert_annotations(annotation, annotation_colors)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
320 draw_annotations(converted_annotation, border_color, border_width)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
321 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
322 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
323
bc752a05f16d Uploaded
bornea
parents:
diff changeset
324 # Draw annotation legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
325 if(!is.na(annotation[[1]][1]) & annotation_legend){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
326 if(length(rownames(matrix)) != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
327 pushViewport(vplayout(4:5, 5))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
328 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
329 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
330 pushViewport(vplayout(3:5, 5))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
331 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
332 draw_annotation_legend(annotation, annotation_colors, border_color, border_width, fontsize = fontsize, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
333 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
334 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
335
bc752a05f16d Uploaded
bornea
parents:
diff changeset
336 # Draw legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
337 if(!is.na(legend[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
338 length(colnames(matrix))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
339 if(length(rownames(matrix)) != 0){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
340 pushViewport(vplayout(4:5, 4))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
341 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
342 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
343 pushViewport(vplayout(3:5, 4))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
344 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
345 draw_legend(color, breaks, legend, fontsize = fontsize, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
346 upViewport()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
347 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
348
bc752a05f16d Uploaded
bornea
parents:
diff changeset
349
bc752a05f16d Uploaded
bornea
parents:
diff changeset
350 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
351
bc752a05f16d Uploaded
bornea
parents:
diff changeset
352 generate_breaks = function(x, n, center = F){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
353 if(center){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
354 m = max(abs(c(min(x, na.rm = T), max(x, na.rm = T))))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
355 res = seq(-m, m, length.out = n + 1)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
356 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
357 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
358 res = seq(min(x, na.rm = T), max(x, na.rm = T), length.out = n + 1)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
359 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
360
bc752a05f16d Uploaded
bornea
parents:
diff changeset
361 return(res)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
362 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
363
bc752a05f16d Uploaded
bornea
parents:
diff changeset
364 scale_vec_colours = function(x, col = rainbow(10), breaks = NA){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
365 return(col[as.numeric(cut(x, breaks = breaks, include.lowest = T))])
bc752a05f16d Uploaded
bornea
parents:
diff changeset
366 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
367
bc752a05f16d Uploaded
bornea
parents:
diff changeset
368 scale_colours = function(mat, col = rainbow(10), breaks = NA){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
369 mat = as.matrix(mat)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
370 return(matrix(scale_vec_colours(as.vector(mat), col = col, breaks = breaks), nrow(mat), ncol(mat), dimnames = list(rownames(mat), colnames(mat))))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
371 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
372
bc752a05f16d Uploaded
bornea
parents:
diff changeset
373 cluster_mat = function(mat, distance, method){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
374 if(!(method %in% c("ward", "single", "complete", "average", "mcquitty", "median", "centroid"))){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
375 stop("clustering method has to one form the list: 'ward', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'.")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
376 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
377 if(!(distance[1] %in% c("correlation", "euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")) & class(distance) != "dist"){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
378 print(!(distance[1] %in% c("correlation", "euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")) | class(distance) != "dist")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
379 stop("distance has to be a dissimilarity structure as produced by dist or one measure form the list: 'correlation', 'euclidean', 'maximum', 'manhattan', 'canberra', 'binary', 'minkowski'")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
380 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
381 if(distance[1] == "correlation"){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
382 d = as.dist(1 - cor(t(mat)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
383 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
384 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
385 if(class(distance) == "dist"){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
386 d = distance
bc752a05f16d Uploaded
bornea
parents:
diff changeset
387 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
388 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
389 d = dist(mat, method = distance)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
390 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
391 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
392
bc752a05f16d Uploaded
bornea
parents:
diff changeset
393 return(hclust(d, method = method))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
394 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
395
bc752a05f16d Uploaded
bornea
parents:
diff changeset
396 scale_rows = function(x){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
397 m = apply(x, 1, mean, na.rm = T)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
398 s = apply(x, 1, sd, na.rm = T)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
399 return((x - m) / s)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
400 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
401
bc752a05f16d Uploaded
bornea
parents:
diff changeset
402 scale_mat = function(mat, scale){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
403 if(!(scale %in% c("none", "row", "column"))){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
404 stop("scale argument shoud take values: 'none', 'row' or 'column'")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
405 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
406 mat = switch(scale, none = mat, row = scale_rows(mat), column = t(scale_rows(t(mat))))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
407 return(mat)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
408 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
409
bc752a05f16d Uploaded
bornea
parents:
diff changeset
410 generate_annotation_colours = function(annotation, annotation_colors, drop){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
411 if(is.na(annotation_colors)[[1]][1]){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
412 annotation_colors = list()
bc752a05f16d Uploaded
bornea
parents:
diff changeset
413 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
414 count = 0
bc752a05f16d Uploaded
bornea
parents:
diff changeset
415 for(i in 1:ncol(annotation)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
416 if(is.character(annotation[, i]) | is.factor(annotation[, i])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
417 if (is.factor(annotation[, i]) & !drop){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
418 count = count + length(levels(annotation[, i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
419 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
420 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
421 count = count + length(unique(annotation[, i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
422 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
423 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
424 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
425
bc752a05f16d Uploaded
bornea
parents:
diff changeset
426 factor_colors = hsv((seq(0, 1, length.out = count + 1)[-1] +
bc752a05f16d Uploaded
bornea
parents:
diff changeset
427 0.2)%%1, 0.7, 0.95)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
428
bc752a05f16d Uploaded
bornea
parents:
diff changeset
429 set.seed(3453)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
430
bc752a05f16d Uploaded
bornea
parents:
diff changeset
431 for(i in 1:ncol(annotation)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
432 if(!(colnames(annotation)[i] %in% names(annotation_colors))){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
433 if(is.character(annotation[, i]) | is.factor(annotation[, i])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
434 n = length(unique(annotation[, i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
435 if (is.factor(annotation[, i]) & !drop){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
436 n = length(levels(annotation[, i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
437 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
438 ind = sample(1:length(factor_colors), n)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
439 annotation_colors[[colnames(annotation)[i]]] = factor_colors[ind]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
440 l = levels(as.factor(annotation[, i]))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
441 l = l[l %in% unique(annotation[, i])]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
442 if (is.factor(annotation[, i]) & !drop){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
443 l = levels(annotation[, i])
bc752a05f16d Uploaded
bornea
parents:
diff changeset
444 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
445 names(annotation_colors[[colnames(annotation)[i]]]) = l
bc752a05f16d Uploaded
bornea
parents:
diff changeset
446 factor_colors = factor_colors[-ind]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
447 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
448 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
449 r = runif(1)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
450 annotation_colors[[colnames(annotation)[i]]] = hsv(r, c(0.1, 1), 1)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
451 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
452 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
453 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
454 return(annotation_colors)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
455 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
456
bc752a05f16d Uploaded
bornea
parents:
diff changeset
457 kmeans_pheatmap = function(mat, k = min(nrow(mat), 150), sd_limit = NA, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
458 # Filter data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
459 if(!is.na(sd_limit)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
460 s = apply(mat, 1, sd)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
461 mat = mat[s > sd_limit, ]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
462 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
463
bc752a05f16d Uploaded
bornea
parents:
diff changeset
464 # Cluster data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
465 set.seed(1245678)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
466 km = kmeans(mat, k, iter.max = 100)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
467 mat2 = km$centers
bc752a05f16d Uploaded
bornea
parents:
diff changeset
468
bc752a05f16d Uploaded
bornea
parents:
diff changeset
469 # Compose rownames
bc752a05f16d Uploaded
bornea
parents:
diff changeset
470 t = table(km$cluster)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
471 rownames(mat2) = sprintf("cl%s_size_%d", names(t), t)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
472
bc752a05f16d Uploaded
bornea
parents:
diff changeset
473 # Draw heatmap
bc752a05f16d Uploaded
bornea
parents:
diff changeset
474 pheatmap(mat2, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
475 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
476
bc752a05f16d Uploaded
bornea
parents:
diff changeset
477 #' A function to draw clustered heatmaps.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
478 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
479 #' A function to draw clustered heatmaps where one has better control over some graphical
bc752a05f16d Uploaded
bornea
parents:
diff changeset
480 #' parameters such as cell size, etc.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
481 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
482 #' The function also allows to aggregate the rows using kmeans clustering. This is
bc752a05f16d Uploaded
bornea
parents:
diff changeset
483 #' advisable if number of rows is so big that R cannot handle their hierarchical
bc752a05f16d Uploaded
bornea
parents:
diff changeset
484 #' clustering anymore, roughly more than 1000. Instead of showing all the rows
bc752a05f16d Uploaded
bornea
parents:
diff changeset
485 #' separately one can cluster the rows in advance and show only the cluster centers.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
486 #' The number of clusters can be tuned with parameter kmeans_k.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
487 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
488 #' @param mat numeric matrix of the values to be plotted.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
489 #' @param color vector of colors used in heatmap.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
490 #' @param kmeans_k the number of kmeans clusters to make, if we want to agggregate the
bc752a05f16d Uploaded
bornea
parents:
diff changeset
491 #' rows before drawing heatmap. If NA then the rows are not aggregated.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
492 #' @param breaks a sequence of numbers that covers the range of values in mat and is one
bc752a05f16d Uploaded
bornea
parents:
diff changeset
493 #' element longer than color vector. Used for mapping values to colors. Useful, if needed
bc752a05f16d Uploaded
bornea
parents:
diff changeset
494 #' to map certain values to certain colors, to certain values. If value is NA then the
bc752a05f16d Uploaded
bornea
parents:
diff changeset
495 #' breaks are calculated automatically.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
496 #' @param border_color color of cell borders on heatmap, use NA if no border should be
bc752a05f16d Uploaded
bornea
parents:
diff changeset
497 #' drawn.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
498 #' @param cellwidth individual cell width in points. If left as NA, then the values
bc752a05f16d Uploaded
bornea
parents:
diff changeset
499 #' depend on the size of plotting window.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
500 #' @param cellheight individual cell height in points. If left as NA,
bc752a05f16d Uploaded
bornea
parents:
diff changeset
501 #' then the values depend on the size of plotting window.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
502 #' @param scale character indicating if the values should be centered and scaled in
bc752a05f16d Uploaded
bornea
parents:
diff changeset
503 #' either the row direction or the column direction, or none. Corresponding values are
bc752a05f16d Uploaded
bornea
parents:
diff changeset
504 #' \code{"row"}, \code{"column"} and \code{"none"}
bc752a05f16d Uploaded
bornea
parents:
diff changeset
505 #' @param cluster_rows boolean values determining if rows should be clustered,
bc752a05f16d Uploaded
bornea
parents:
diff changeset
506 #' @param cluster_cols boolean values determining if columns should be clustered.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
507 #' @param clustering_distance_rows distance measure used in clustering rows. Possible
bc752a05f16d Uploaded
bornea
parents:
diff changeset
508 #' values are \code{"correlation"} for Pearson correlation and all the distances
bc752a05f16d Uploaded
bornea
parents:
diff changeset
509 #' supported by \code{\link{dist}}, such as \code{"euclidean"}, etc. If the value is none
bc752a05f16d Uploaded
bornea
parents:
diff changeset
510 #' of the above it is assumed that a distance matrix is provided.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
511 #' @param clustering_distance_cols distance measure used in clustering columns. Possible
bc752a05f16d Uploaded
bornea
parents:
diff changeset
512 #' values the same as for clustering_distance_rows.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
513 #' @param clustering_method clustering method used. Accepts the same values as
bc752a05f16d Uploaded
bornea
parents:
diff changeset
514 #' \code{\link{hclust}}.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
515 #' @param treeheight_row the height of a tree for rows, if these are clustered.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
516 #' Default value 50 points.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
517 #' @param treeheight_col the height of a tree for columns, if these are clustered.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
518 #' Default value 50 points.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
519 #' @param legend logical to determine if legend should be drawn or not.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
520 #' @param legend_breaks vector of breakpoints for the legend.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
521 #' @param legend_labels vector of labels for the \code{legend_breaks}.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
522 #' @param annotation data frame that specifies the annotations shown on top of the
bc752a05f16d Uploaded
bornea
parents:
diff changeset
523 #' columns. Each row defines the features for a specific column. The columns in the data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
524 #' and rows in the annotation are matched using corresponding row and column names. Note
bc752a05f16d Uploaded
bornea
parents:
diff changeset
525 #' that color schemes takes into account if variable is continuous or discrete.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
526 #' @param annotation_colors list for specifying annotation track colors manually. It is
bc752a05f16d Uploaded
bornea
parents:
diff changeset
527 #' possible to define the colors for only some of the features. Check examples for
bc752a05f16d Uploaded
bornea
parents:
diff changeset
528 #' details.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
529 #' @param annotation_legend boolean value showing if the legend for annotation tracks
bc752a05f16d Uploaded
bornea
parents:
diff changeset
530 #' should be drawn.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
531 #' @param drop_levels logical to determine if unused levels are also shown in the legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
532 #' @param show_rownames boolean specifying if column names are be shown.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
533 #' @param show_colnames boolean specifying if column names are be shown.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
534 #' @param main the title of the plot
bc752a05f16d Uploaded
bornea
parents:
diff changeset
535 #' @param fontsize base fontsize for the plot
bc752a05f16d Uploaded
bornea
parents:
diff changeset
536 #' @param fontsize_row fontsize for rownames (Default: fontsize)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
537 #' @param fontsize_col fontsize for colnames (Default: fontsize)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
538 #' @param display_numbers logical determining if the numeric values are also printed to
bc752a05f16d Uploaded
bornea
parents:
diff changeset
539 #' the cells.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
540 #' @param number_format format strings (C printf style) of the numbers shown in cells.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
541 #' For example "\code{\%.2f}" shows 2 decimal places and "\code{\%.1e}" shows exponential
bc752a05f16d Uploaded
bornea
parents:
diff changeset
542 #' notation (see more in \code{\link{sprintf}}).
bc752a05f16d Uploaded
bornea
parents:
diff changeset
543 #' @param fontsize_number fontsize of the numbers displayed in cells
bc752a05f16d Uploaded
bornea
parents:
diff changeset
544 #' @param filename file path where to save the picture. Filetype is decided by
bc752a05f16d Uploaded
bornea
parents:
diff changeset
545 #' the extension in the path. Currently following formats are supported: png, pdf, tiff,
bc752a05f16d Uploaded
bornea
parents:
diff changeset
546 #' bmp, jpeg. Even if the plot does not fit into the plotting window, the file size is
bc752a05f16d Uploaded
bornea
parents:
diff changeset
547 #' calculated so that the plot would fit there, unless specified otherwise.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
548 #' @param width manual option for determining the output file width in inches.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
549 #' @param height manual option for determining the output file height in inches.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
550 #' @param \dots graphical parameters for the text used in plot. Parameters passed to
bc752a05f16d Uploaded
bornea
parents:
diff changeset
551 #' \code{\link{grid.text}}, see \code{\link{gpar}}.
bc752a05f16d Uploaded
bornea
parents:
diff changeset
552 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
553 #' @return
bc752a05f16d Uploaded
bornea
parents:
diff changeset
554 #' Invisibly a list of components
bc752a05f16d Uploaded
bornea
parents:
diff changeset
555 #' \itemize{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
556 #' \item \code{tree_row} the clustering of rows as \code{\link{hclust}} object
bc752a05f16d Uploaded
bornea
parents:
diff changeset
557 #' \item \code{tree_col} the clustering of columns as \code{\link{hclust}} object
bc752a05f16d Uploaded
bornea
parents:
diff changeset
558 #' \item \code{kmeans} the kmeans clustering of rows if parameter \code{kmeans_k} was
bc752a05f16d Uploaded
bornea
parents:
diff changeset
559 #' specified
bc752a05f16d Uploaded
bornea
parents:
diff changeset
560 #' }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
561 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
562 #' @author Raivo Kolde <rkolde@@gmail.com>
bc752a05f16d Uploaded
bornea
parents:
diff changeset
563 #' @examples
bc752a05f16d Uploaded
bornea
parents:
diff changeset
564 #' # Generate some data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
565 #' test = matrix(rnorm(200), 20, 10)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
566 #' test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
bc752a05f16d Uploaded
bornea
parents:
diff changeset
567 #' test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
bc752a05f16d Uploaded
bornea
parents:
diff changeset
568 #' test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
bc752a05f16d Uploaded
bornea
parents:
diff changeset
569 #' colnames(test) = paste("Test", 1:10, sep = "")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
570 #' rownames(test) = paste("Gene", 1:20, sep = "")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
571 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
572 #' # Draw heatmaps
bc752a05f16d Uploaded
bornea
parents:
diff changeset
573 #' pheatmap(test)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
574 #' pheatmap(test, kmeans_k = 2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
575 #' pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
576 #' pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
577 #' pheatmap(test, cluster_row = FALSE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
578 #' pheatmap(test, legend = FALSE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
579 #' pheatmap(test, display_numbers = TRUE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
580 #' pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
581 #' pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
bc752a05f16d Uploaded
bornea
parents:
diff changeset
582 #' "1e-4", "1e-3", "1e-2", "1e-1", "1"))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
583 #' pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
584 #' pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
585 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
586 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
587 #' # Generate column annotations
bc752a05f16d Uploaded
bornea
parents:
diff changeset
588 #' annotation = data.frame(Var1 = factor(1:10 %% 2 == 0,
bc752a05f16d Uploaded
bornea
parents:
diff changeset
589 #' labels = c("Class1", "Class2")), Var2 = 1:10)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
590 #' annotation$Var1 = factor(annotation$Var1, levels = c("Class1", "Class2", "Class3"))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
591 #' rownames(annotation) = paste("Test", 1:10, sep = "")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
592 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
593 #' pheatmap(test, annotation = annotation)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
594 #' pheatmap(test, annotation = annotation, annotation_legend = FALSE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
595 #' pheatmap(test, annotation = annotation, annotation_legend = FALSE, drop_levels = FALSE)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
596 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
597 #' # Specify colors
bc752a05f16d Uploaded
bornea
parents:
diff changeset
598 #' Var1 = c("navy", "darkgreen")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
599 #' names(Var1) = c("Class1", "Class2")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
600 #' Var2 = c("lightgreen", "navy")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
601 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
602 #' ann_colors = list(Var1 = Var1, Var2 = Var2)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
603 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
604 #' pheatmap(test, annotation = annotation, annotation_colors = ann_colors, main = "Example")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
605 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
606 #' # Specifying clustering from distance matrix
bc752a05f16d Uploaded
bornea
parents:
diff changeset
607 #' drows = dist(test, method = "minkowski")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
608 #' dcols = dist(t(test), method = "minkowski")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
609 #' pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
610 #'
bc752a05f16d Uploaded
bornea
parents:
diff changeset
611 #' @export
bc752a05f16d Uploaded
bornea
parents:
diff changeset
612 pheatmap_j = function(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60", border_width = 1, cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE, cluster_cols = TRUE, clustering_distance_rows = "euclidean", clustering_distance_cols = "euclidean", clustering_method = "complete", treeheight_row = ifelse(cluster_rows, 50, 0), treeheight_col = ifelse(cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA, legend_labels = NA, annotation = NA, annotation_colors = NA, annotation_legend = TRUE, drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize, display_numbers = F, number_format = "%.2f", fontsize_number = 0.8 * fontsize, filename = NA, width = NA, height = NA, ...){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
613
bc752a05f16d Uploaded
bornea
parents:
diff changeset
614 # Preprocess matrix
bc752a05f16d Uploaded
bornea
parents:
diff changeset
615 mat = as.matrix(mat)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
616 if(scale != "none"){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
617 mat = scale_mat(mat, scale)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
618 if(is.na(breaks)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
619 breaks = generate_breaks(mat, length(color), center = T)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
620 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
621 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
622
bc752a05f16d Uploaded
bornea
parents:
diff changeset
623
bc752a05f16d Uploaded
bornea
parents:
diff changeset
624 # Kmeans
bc752a05f16d Uploaded
bornea
parents:
diff changeset
625 if(!is.na(kmeans_k)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
626 # Cluster data
bc752a05f16d Uploaded
bornea
parents:
diff changeset
627 km = kmeans(mat, kmeans_k, iter.max = 100)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
628 mat = km$centers
bc752a05f16d Uploaded
bornea
parents:
diff changeset
629
bc752a05f16d Uploaded
bornea
parents:
diff changeset
630 # Compose rownames
bc752a05f16d Uploaded
bornea
parents:
diff changeset
631 t = table(km$cluster)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
632 rownames(mat) = sprintf("cl%s_size_%d", names(t), t)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
633 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
634 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
635 km = NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
636 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
637
bc752a05f16d Uploaded
bornea
parents:
diff changeset
638 # Do clustering
bc752a05f16d Uploaded
bornea
parents:
diff changeset
639 if(cluster_rows){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
640 tree_row = cluster_mat(mat, distance = clustering_distance_rows, method = clustering_method)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
641 mat = mat[tree_row$order, , drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
642 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
643 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
644 tree_row = NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
645 treeheight_row = 0
bc752a05f16d Uploaded
bornea
parents:
diff changeset
646 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
647
bc752a05f16d Uploaded
bornea
parents:
diff changeset
648 if(cluster_cols){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
649 tree_col = cluster_mat(t(mat), distance = clustering_distance_cols, method = clustering_method)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
650 mat = mat[, tree_col$order, drop = FALSE]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
651 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
652 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
653 tree_col = NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
654 treeheight_col = 0
bc752a05f16d Uploaded
bornea
parents:
diff changeset
655 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
656
bc752a05f16d Uploaded
bornea
parents:
diff changeset
657 # Format numbers to be displayed in cells
bc752a05f16d Uploaded
bornea
parents:
diff changeset
658 if(display_numbers){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
659 fmat = matrix(sprintf(number_format, mat), nrow = nrow(mat), ncol = ncol(mat))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
660 attr(fmat, "draw") = TRUE
bc752a05f16d Uploaded
bornea
parents:
diff changeset
661 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
662 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
663 fmat = matrix(NA, nrow = nrow(mat), ncol = ncol(mat))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
664 attr(fmat, "draw") = FALSE
bc752a05f16d Uploaded
bornea
parents:
diff changeset
665 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
666
bc752a05f16d Uploaded
bornea
parents:
diff changeset
667
bc752a05f16d Uploaded
bornea
parents:
diff changeset
668 # Colors and scales
bc752a05f16d Uploaded
bornea
parents:
diff changeset
669 if(!is.na(legend_breaks[1]) & !is.na(legend_labels[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
670 if(length(legend_breaks) != length(legend_labels)){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
671 stop("Lengths of legend_breaks and legend_labels must be the same")
bc752a05f16d Uploaded
bornea
parents:
diff changeset
672 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
673 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
674
bc752a05f16d Uploaded
bornea
parents:
diff changeset
675
bc752a05f16d Uploaded
bornea
parents:
diff changeset
676 if(is.na(breaks[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
677 breaks = generate_breaks(as.vector(mat), length(color))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
678 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
679 if (legend & is.na(legend_breaks[1])) {
bc752a05f16d Uploaded
bornea
parents:
diff changeset
680 legend = grid.pretty(range(as.vector(breaks)))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
681 names(legend) = legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
682 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
683 else if(legend & !is.na(legend_breaks[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
684 legend = legend_breaks[legend_breaks >= min(breaks) & legend_breaks <= max(breaks)]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
685
bc752a05f16d Uploaded
bornea
parents:
diff changeset
686 if(!is.na(legend_labels[1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
687 legend_labels = legend_labels[legend_breaks >= min(breaks) & legend_breaks <= max(breaks)]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
688 names(legend) = legend_labels
bc752a05f16d Uploaded
bornea
parents:
diff changeset
689 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
690 else{
bc752a05f16d Uploaded
bornea
parents:
diff changeset
691 names(legend) = legend
bc752a05f16d Uploaded
bornea
parents:
diff changeset
692 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
693 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
694 else {
bc752a05f16d Uploaded
bornea
parents:
diff changeset
695 legend = NA
bc752a05f16d Uploaded
bornea
parents:
diff changeset
696 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
697 mat = scale_colours(mat, col = color, breaks = breaks)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
698
bc752a05f16d Uploaded
bornea
parents:
diff changeset
699 # Preparing annotation colors
bc752a05f16d Uploaded
bornea
parents:
diff changeset
700 if(!is.na(annotation[[1]][1])){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
701 annotation = annotation[colnames(mat), , drop = F]
bc752a05f16d Uploaded
bornea
parents:
diff changeset
702 annotation_colors = generate_annotation_colours(annotation, annotation_colors, drop = drop_levels)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
703 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
704
bc752a05f16d Uploaded
bornea
parents:
diff changeset
705 if(!show_rownames){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
706 rownames(mat) = NULL
bc752a05f16d Uploaded
bornea
parents:
diff changeset
707 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
708
bc752a05f16d Uploaded
bornea
parents:
diff changeset
709 if(!show_colnames){
bc752a05f16d Uploaded
bornea
parents:
diff changeset
710 colnames(mat) = NULL
bc752a05f16d Uploaded
bornea
parents:
diff changeset
711 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
712
bc752a05f16d Uploaded
bornea
parents:
diff changeset
713 # Draw heatmap
bc752a05f16d Uploaded
bornea
parents:
diff changeset
714 heatmap_motor(mat, border_color = border_color, border_width = border_width, cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, filename = filename, width = width, height = height, breaks = breaks, color = color, legend = legend, annotation = annotation, annotation_colors = annotation_colors, annotation_legend = annotation_legend, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, fmat = fmat, fontsize_number = fontsize_number, ...)
bc752a05f16d Uploaded
bornea
parents:
diff changeset
715
bc752a05f16d Uploaded
bornea
parents:
diff changeset
716 invisible(list(tree_row = tree_row, tree_col = tree_col, kmeans = km))
bc752a05f16d Uploaded
bornea
parents:
diff changeset
717 }
bc752a05f16d Uploaded
bornea
parents:
diff changeset
718
bc752a05f16d Uploaded
bornea
parents:
diff changeset
719