Browse code

add raster_resize argument

Zuguang Gu authored on 17/11/2018 21:11:19
Showing3 changed files

... ...
@@ -168,6 +168,7 @@ Heatmap = setClass("Heatmap",
168 168
 # -raster_device Graphic device which is used to generate the raster image.
169 169
 # -raster_quality A value set to larger than 1 will improve the quality of the raster image.
170 170
 # -raster_device_param A list of further parameters for the selected graphic device. For raster image support, please check https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html#heatmap-as-raster-image .
171
+# -raster_resize Whether resize the matrix to let the dimension of the matrix the same as the dimension of the raster image?
171 172
 # -post_fun A function which will be executed after the heatmap list is drawn.
172 173
 #
173 174
 # == details
... ...
@@ -269,6 +270,7 @@ Heatmap = function(matrix, col, name,
269 270
     raster_device = c("png", "jpeg", "tiff", "CairoPNG", "CairoJPEG", "CairoTIFF"),
270 271
     raster_quality = 2,
271 272
     raster_device_param = list(),
273
+    raster_resize = FALSE,
272 274
 
273 275
     post_fun = NULL) {
274 276
 
... ...
@@ -787,6 +789,7 @@ Heatmap = function(matrix, col, name,
787 789
     .Object@heatmap_param$raster_device = match.arg(raster_device)[1]
788 790
     .Object@heatmap_param$raster_quality = raster_quality
789 791
     .Object@heatmap_param$raster_device_param = raster_device_param
792
+    .Object@heatmap_param$raster_resize = raster_resize
790 793
     .Object@heatmap_param$verbose = verbose
791 794
     .Object@heatmap_param$post_fun = post_fun
792 795
     .Object@heatmap_param$calling_env = parent.frame()
... ...
@@ -81,15 +81,17 @@ setMethod(f = "draw_heatmap_body",
81 81
         }
82 82
 
83 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
84
+        if(object@heatmap_param$raster_resize) {
85
+            if(heatmap_width < nc && heatmap_height < nr) {
86
+                mat2 = resize_matrix(mat, nr = heatmap_height, nc = heatmap_width)
87
+                matrix_is_resized = TRUE
88
+            } else if(heatmap_width < nc) {
89
+                mat2 = resize_matrix(mat, nr = nr, nc = heatmap_width)
90
+                matrix_is_resized = TRUE
91
+            } else if(heatmap_height < nr) {
92
+                mat2 = resize_matrix(mat, nr = heatmap_height, nc = nc)
93
+                matrix_is_resized = TRUE
94
+            }
93 95
         }
94 96
 
95 97
         temp_dir = tempdir()
... ...
@@ -824,5 +824,5 @@ to_unit = function(str) {
824 824
 resize_matrix = function(mat, nr, nc) {
825 825
     w_ratio = nc/ncol(mat)
826 826
     h_ratio = nr/nrow(mat)
827
-    mat[ floor(1:nr / h_ratio), floor(1:nc / w_ratio)]
827
+    mat[ floor(1:nr / h_ratio), floor(1:nc / w_ratio), drop = FALSE]
828 828
 }