Browse code

also resize the image when using raster image

Zuguang Gu authored on 17/11/2018 16:02:08
Showing3 changed files

... ...
@@ -55,8 +55,9 @@ setMethod(f = "draw_heatmap_body",
55 55
     if(!is.null(cell_fun)) {
56 56
         use_raster = FALSE
57 57
     }
58
-        
58
+   
59 59
     if(use_raster) {
60
+
60 61
         # write the image into a temporary file and read it back
61 62
         device_info = switch(raster_device,
62 63
             png = c("grDevices", "png", "readPNG"),
... ...
@@ -78,7 +79,19 @@ setMethod(f = "draw_heatmap_body",
78 79
         if(heatmap_width <= 0 || heatmap_height <= 0) {
79 80
             stop_wrap("The width or height of the raster image is zero, maybe you forget to turn off the previous graphic device or it was corrupted. Run `dev.off()` to close it.")
80 81
         }
81
-        
82
+
83
+        matrix_is_resized = FALSE
84
+        if(heatmap_width < nc && heatmap_height < nr) {
85
+            mat2 = resize_matrix(mat, nr = heatmap_height, nc = heatmap_width)
86
+            matrix_is_resized = TRUE
87
+        } else if(heatmap_width < nc) {
88
+            mat2 = resize_matrix(mat, nr = nr, nc = heatmap_width)
89
+            matrix_is_resized = TRUE
90
+        } else if(heatmap_height < nr) {
91
+            mat2 = resize_matrix(mat, nr = heatmap_height, nc = nc)
92
+            matrix_is_resized = TRUE
93
+        }
94
+
82 95
         temp_dir = tempdir()
83 96
                 # dir.create(tmp_dir, showWarnings = FALSE)
84 97
         temp_image = tempfile(pattern = paste0(".heatmap_body_", object@name, "_", kr, "_", kc), tmpdir = temp_dir, fileext = paste0(".", device_info[2]))
... ...
@@ -86,12 +99,32 @@ setMethod(f = "draw_heatmap_body",
86 99
         device_fun = getFromNamespace(raster_device, ns = device_info[1])
87 100
 
88 101
         do.call(device_fun, c(list(filename = temp_image, width = max(c(heatmap_width*raster_quality, 1)), height = max(c(heatmap_height*raster_quality, 1))), raster_device_param))
89
-        grid.rect(x[expand_index[[2]]], y[expand_index[[1]]], width = unit(1/nc, 'npc'), height = unit(1/nr, 'npc'), gp = do.call('gpar', c(list(fill = col_matrix), gp)))
102
+        if(matrix_is_resized) {
103
+            if(object@heatmap_param$verbose) {
104
+                qqcat("resize the matrix from (@{nrow(mat)} x @{ncol(mat)}) to (@{nrow(mat2)} x @{ncol(mat2)}).\n")
105
+            }
106
+            col_matrix2 = map_to_colors(object@matrix_color_mapping, mat2)
107
+            nc2 = ncol(mat2)
108
+            nr2 = nrow(mat2)
109
+            x2 = (seq_len(nc2) - 0.5) / nc2
110
+            y2 = (rev(seq_len(nr2)) - 0.5) / nr2
111
+            expand_index2 = expand.grid(seq_len(nr2), seq_len(nc2))
112
+            grid.rect(x2[expand_index2[[2]]], y2[expand_index2[[1]]], width = unit(1/nc2, 'npc'), height = unit(1/nr2, 'npc'), gp = do.call('gpar', c(list(fill = col_matrix2), gp)))
113
+        } else {
114
+            grid.rect(x[expand_index[[2]]], y[expand_index[[1]]], width = unit(1/nc, 'npc'), height = unit(1/nr, 'npc'), gp = do.call('gpar', c(list(fill = col_matrix), gp)))
115
+        }
90 116
         if(is.function(layer_fun)) {
91
-            layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
92
-                x[expand_index[[2]]], y[expand_index[[1]]],
93
-                unit(rep(1/nc, nrow(expand_index)), "npc"), unit(rep(1/nr, nrow(expand_index)), "npc"),
94
-                as.vector(col_matrix))
117
+            if(length(as.list(formals(fun))) == 7) {
118
+                layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
119
+                    x[expand_index[[2]]], y[expand_index[[1]]],
120
+                    unit(rep(1/nc, nrow(expand_index)), "npc"), unit(rep(1/nr, nrow(expand_index)), "npc"),
121
+                    as.vector(col_matrix))
122
+            } else {
123
+                layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
124
+                    x[expand_index[[2]]], y[expand_index[[1]]],
125
+                    unit(rep(1/nc, nrow(expand_index)), "npc"), unit(rep(1/nr, nrow(expand_index)), "npc"),
126
+                    as.vector(col_matrix), kr, kc)
127
+            }
95 128
         }
96 129
         dev.off2()
97 130
         
... ...
@@ -142,6 +175,7 @@ setMethod(f = "draw_heatmap_body",
142 175
         file.remove(temp_image)
143 176
 
144 177
     } else {
178
+
145 179
         if(any(names(gp) %in% c("type"))) {
146 180
             if(gp$type == "none") {
147 181
             } else {
... ...
@@ -161,10 +195,17 @@ setMethod(f = "draw_heatmap_body",
161 195
             }
162 196
         }
163 197
         if(is.function(layer_fun)) {
164
-            layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
198
+            if(length(as.list(formals(fun))) == 7) {
199
+                layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
200
+                    x[expand_index[[2]]], y[expand_index[[1]]],
201
+                    unit(rep(1/nc, nrow(expand_index)), "npc"), unit(rep(1/nr, nrow(expand_index)), "npc"),
202
+                    as.vector(col_matrix))
203
+            } else {
204
+                layer_fun(column_order[ expand_index[[2]] ], row_order[ expand_index[[1]] ], 
165 205
                 x[expand_index[[2]]], y[expand_index[[1]]],
166 206
                 unit(rep(1/nc, nrow(expand_index)), "npc"), unit(rep(1/nr, nrow(expand_index)), "npc"),
167
-                as.vector(col_matrix))
207
+                as.vector(col_matrix), kr, kc)
208
+            }
168 209
         }
169 210
     }
170 211
 
... ...
@@ -165,6 +165,7 @@ densityHeatmap = function(data,
165 165
 		cluster_columns = cluster_columns,
166 166
 		clustering_distance_columns = clustering_distance_columns,
167 167
 		clustering_method_columns = clustering_method_columns,
168
+		column_dend_reorder = mean_value,
168 169
 		column_names_side = column_names_side,
169 170
 		show_column_names = show_column_names,
170 171
 		column_names_max_height = column_names_max_height,
... ...
@@ -820,3 +820,9 @@ to_unit = function(str) {
820 820
     unit(as.numeric(d), u)
821 821
 }
822 822
 
823
+
824
+resize_matrix = function(mat, nr, nc) {
825
+    w_ratio = nc/ncol(mat)
826
+    h_ratio = nr/nrow(mat)
827
+    mat[ floor(1:nr / h_ratio), floor(1:nc / w_ratio)]
828
+}