Browse code

restrict the size of the temp raster image

Zuguang Gu authored on 28/09/2021 09:20:28
Showing16 changed files

... ...
@@ -1,6 +1,8 @@
1 1
 CHANGES in VERSION 2.9.4
2 2
 
3 3
 * fixed a bug of missing right annotation legends for vertically concatenated heatmaps.
4
+* `Legend()`: support `border` to be set to `asis`.
5
+* Rasterization: the default maximal size for temporary image is set to 30000 px (both for width and height).
4 6
 
5 7
 ========================
6 8
 
... ...
@@ -1,32 +1,32 @@
1 1
 setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
2
-setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
3
-setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
4
-setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
5
-setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
6
-setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
7
-setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
8
-setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
9 2
 setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
10
-setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
11
-setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
12
-setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
13
-setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
14
-setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
15
-setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
16
-setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
17
-setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
18
-setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
19
-setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
20
-setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
21
-setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
3
+setGeneric('draw', function(object, ...) standardGeneric('draw'))
22 4
 setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
23
-setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
24 5
 setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
25
-setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
6
+setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
26 7
 setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
27
-setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
28
-setGeneric('draw', function(object, ...) standardGeneric('draw'))
8
+setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
9
+setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
10
+setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
11
+setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
29 12
 setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
13
+setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
14
+setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
15
+setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
16
+setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
17
+setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
18
+setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
19
+setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
20
+setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
21
+setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
30 22
 setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
31 23
 setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
24
+setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
25
+setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
32 26
 setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
27
+setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
28
+setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
29
+setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
30
+setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
31
+setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
32
+setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
... ...
@@ -1190,7 +1190,7 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
1190 1190
 # -which Whether it is a column annotation or a row annotation?
1191 1191
 # -border Wether draw borders of the annotation region?
1192 1192
 # -bar_width Relative width of the bars. The value should be smaller than one.
1193
-# -gp Graphic parameters for points. The length of each graphic parameter can be 1, length of ``x`` if ``x``
1193
+# -gp Graphic parameters for bars. The length of each graphic parameter can be 1, length of ``x`` if ``x``
1194 1194
 #     is a vector, or number of columns of ``x`` is ``x`` is a matrix.
1195 1195
 # -ylim Data ranges. By default it is ``range(x)`` if ``x`` is a vector, or ``range(rowSums(x))`` if ``x`` is a matrix.
1196 1196
 # -extend The extension to both side of ``ylim``. The value is a percent value corresponding to ``ylim[2] - ylim[1]``.
... ...
@@ -293,7 +293,7 @@ Heatmap = function(matrix, col, name,
293 293
     heatmap_legend_param = list(title = name),
294 294
 
295 295
     use_raster = NULL, 
296
-    raster_device = c("CairoPNG", "CairoJPEG", "CairoTIFF", "png", "jpeg", "tiff", "agg_png"),
296
+    raster_device = c("png", "jpeg", "tiff", "CairoPNG", "CairoJPEG", "CairoTIFF", "agg_png"),
297 297
     raster_quality = 1,
298 298
     raster_device_param = list(),
299 299
     raster_resize_mat = FALSE,
... ...
@@ -139,8 +139,22 @@ setMethod(f = "draw_heatmap_body",
139 139
             temp_image_width = ceiling(max(heatmap_width_pt, 1))
140 140
             temp_image_height = ceiling(max(heatmap_height_pt, 1))
141 141
         }
142
-        do.call(device_fun, c(list(filename = temp_image, 
143
-            width = temp_image_width, height = temp_image_height), raster_device_param))
142
+        temp_image_width = as.integer(temp_image_width)
143
+        temp_image_height = as.integer(temp_image_height)
144
+
145
+        if(!is.na(ht_opt$raster_temp_image_max_width)) {
146
+            temp_image_width = min(temp_image_width, ht_opt$raster_temp_image_max_width)
147
+        }
148
+        if(!is.na(ht_opt$raster_temp_image_max_height)) {
149
+            temp_image_height = min(temp_image_height, ht_opt$raster_temp_image_max_height)
150
+        }
151
+
152
+        oe = try(do.call(device_fun, c(list(filename = temp_image, 
153
+            width = temp_image_width, height = temp_image_height), raster_device_param)))
154
+        if(inherits(oe, "try-error")) {
155
+            stop_wrap(qq("The size of the temporary image for rasterization is too huge (@{temp_image_width} x @{temp_image_height} px) that it is cannot be handled by the device function `@{device_info[1]}:@{raster_device}()`. Please reduce the maximal size of temporary image by setting proper values for `ht_opt$raster_temp_image_max_width` and `ht_opt$raster_temp_image_max_height`."))
156
+        }
157
+
144 158
         if(object@heatmap_param$verbose) {
145 159
             qqcat("saving into a temp image (.@{device_info[2]}) with size @{temp_image_width}x@{temp_image_height}px.\n")
146 160
         }
... ...
@@ -205,7 +205,9 @@ ht_opt = setGlobalOptions(
205 205
 	save_last = list(
206 206
 		.value = FALSE
207 207
 	),
208
-	"validate_names" = TRUE
208
+	"validate_names" = TRUE,
209
+	raster_temp_image_max_width = 30000,
210
+	raster_temp_image_max_height = 30000
209 211
 )
210 212
 
211 213
 
... ...
@@ -516,7 +516,6 @@ discrete_legend_body = function(at, labels = at, nrow = NULL, ncol = 1, by_row =
516 516
 			textGrob(labels[index], x = labels_x, y = labels_y, just = "left", gp = labels_gp)
517 517
 		))
518 518
 
519
-
520 519
 		######### graphics ############
521 520
 		# grid
522 521
 		sgd = subset_gp(legend_gp, index)
... ...
@@ -526,8 +525,12 @@ discrete_legend_body = function(at, labels = at, nrow = NULL, ncol = 1, by_row =
526 525
 		} else {
527 526
 			sgd2$fill = background
528 527
 		}
529
-		sgd2$col = border
530
-
528
+		if(identical(border, "asis")) {
529
+			sgd2$col = sgd2$fill	
530
+		} else {
531
+			sgd2$col = border
532
+		}
533
+		
531 534
 		grid_x = previous_x
532 535
 		grid_y = y - (row_height[1:ni] - row_gap)*0.5
533 536
 		if(ni == nrow) grid_y[nrow] = y[nrow] - row_height[nrow]*0.5
... ...
@@ -90,7 +90,7 @@ Heatmap(matrix, col, name,
90 90
     heatmap_legend_param = list(title = name),
91 91
     
92 92
     use_raster = NULL,
93
-    raster_device = c("CairoPNG", "CairoJPEG", "CairoTIFF", "png", "jpeg", "tiff", "agg_png"),
93
+    raster_device = c("png", "jpeg", "tiff", "CairoPNG", "CairoJPEG", "CairoTIFF", "agg_png"),
94 94
     raster_quality = 1,
95 95
     raster_device_param = list(),
96 96
     raster_resize_mat = FALSE,
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{add_heatmap}.
10 10
 \code{add_heatmap} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
14 13
 \item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
15 15
 \item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
16 16
 }
17 17
 }
... ...
@@ -21,7 +21,7 @@ anno_barplot(x, baseline = 0, which = c("column", "row"), border = TRUE, bar_wid
21 21
   \item{which}{Whether it is a column annotation or a row annotation?}
22 22
   \item{border}{Wether draw borders of the annotation region?}
23 23
   \item{bar_width}{Relative width of the bars. The value should be smaller than one.}
24
-  \item{gp}{Graphic parameters for points. The length of each graphic parameter can be 1, length of \code{x} if \code{x} is a vector, or number of columns of \code{x} is \code{x} is a matrix.}
24
+  \item{gp}{Graphic parameters for bars. The length of each graphic parameter can be 1, length of \code{x} if \code{x} is a vector, or number of columns of \code{x} is \code{x} is a matrix.}
25 25
   \item{ylim}{Data ranges. By default it is \code{range(x)} if \code{x} is a vector, or \code{range(rowSums(x))} if \code{x} is a matrix.}
26 26
   \item{extend}{The extension to both side of \code{ylim}. The value is a percent value corresponding to \code{ylim[2] - ylim[1]}.}
27 27
   \item{axis}{Whether to add axis?}
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{component_height}.
10 10
 \code{component_height} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14 13
 \item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14
+\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{copy_all}.
10 10
 \code{copy_all} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
14 13
 \item \code{\link{copy_all,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
14
+\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -11,11 +11,11 @@ Method dispatch page for \code{draw}.
11 11
 
12 12
 \itemize{
13 13
 \item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
14
-\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
15
-\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14
+\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
16 15
 \item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
17 16
 \item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
18
-\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
17
+\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
18
+\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
19 19
 }
20 20
 }
21 21
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{draw_title}.
10 10
 \code{draw_title} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{draw_title,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{row_order}.
10 10
 \code{row_order} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{row_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14 13
 \item \code{\link{row_order,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14
+\item \code{\link{row_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,12 +10,12 @@ Method dispatch page for \code{show}.
10 10
 \code{show} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
+\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
14
+\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
13 15
 \item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
14
-\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 16
 \item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
16 17
 \item \code{\link{show,Heatmap-method}}, \code{\link{Heatmap-class}} class method
17
-\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
18
-\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
18
+\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
19 19
 }
20 20
 }
21 21
 \examples{