Browse code

hidden temporary file when use_raster is on

Zuguang Gu authored on 16/09/2016 16:02:42
Showing 4 changed files

... ...
@@ -4,6 +4,7 @@ CHANGES in VERSION 1.11.7
4 4
   as `level(value)` or `unique(value)`
5 5
 * HeatmapAnnotation: give warnings if color is defined while with no annotations
6 6
 * HeatmapAnnotation: check `col`, if it is not valid, give warnings
7
+* catch error when making annotation graphics
7 8
 
8 9
 ==========================
9 10
 
... ...
@@ -1306,7 +1306,7 @@ setMethod(f = "draw_heatmap_body",
1306 1306
         if(heatmap_width <= 0 || heatmap_height <= 0) {
1307 1307
             stop("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.")
1308 1308
         }
1309
-        temp_image = tempfile(pattern = paste0("heatmap_body_", object@name, "_", k, "_"), tmpdir = ".", fileext = paste0(".", device_info[2]))
1309
+        temp_image = tempfile(pattern = paste0(".heatmap_body_", object@name, "_", k, "_"), tmpdir = ".", fileext = paste0(".", device_info[2]))
1310 1310
         #getFromNamespace(raster_device, ns = device_info[1])(temp_image, width = heatmap_width*raster_quality, height = heatmap_height*raster_quality)
1311 1311
         device_fun = getFromNamespace(raster_device, ns = device_info[1])
1312 1312
         do.call("device_fun", c(list(filename = temp_image, width = heatmap_width*raster_quality, height = heatmap_height*raster_quality), raster_device_param))
... ...
@@ -474,7 +474,11 @@ setMethod(f = "draw",
474 474
 			for(i in seq_len(n_anno)) {
475 475
 				pushViewport(viewport(y = sum(anno_size[seq(i, n_anno)]) + sum(gap[seq(i, n_anno)]) - gap[n_anno], 
476 476
 					height = anno_size[i], just = c("center", "top")))
477
-				draw(object@anno_list[[i]], index, k, n)
477
+				oe = try(draw(object@anno_list[[i]], index, k, n))
478
+				if(class(oe) == "try-error") {
479
+					cat("Error when drawing annotation '", object@anno_list[[i]]@name, "'\n", sep = "")
480
+					stop(oe)
481
+				}
478 482
 				upViewport()
479 483
 			}
480 484
 		} else { # put on bottom of the heatmap
... ...
@@ -482,14 +486,22 @@ setMethod(f = "draw",
482 486
 			for(i in seq_len(n_anno)) {
483 487
 				pushViewport(viewport(y = unit(1, "npc") - (sum(anno_size[seq_len(i)]) + sum(gap[seq_len(i)]) - gap[i]), 
484 488
 					height = anno_size[i], just = c("center", "bottom")))
485
-				draw(object@anno_list[[i]], index, k, n)
489
+				oe = try(draw(object@anno_list[[i]], index, k, n))
490
+				if(class(oe) == "try-error") {
491
+					cat("Error when drawing annotation '", object@anno_list[[i]]@name, "'\n", sep = "")
492
+					stop(oe)
493
+				}
486 494
 				upViewport()
487 495
 			}
488 496
 		}
489 497
 	} else if(which == "row") {
490 498
 		for(i in seq_len(n_anno)) {
491 499
 			pushViewport(viewport(x = sum(anno_size[seq_len(i)]) + sum(gap[seq_len(i)]) - gap[i], width = anno_size[i], just = c("right", "center")))
492
-			draw(object@anno_list[[i]], index, k, n)
500
+			oe = try(draw(object@anno_list[[i]], index, k, n))
501
+			if(class(oe) == "try-error") {
502
+				cat("Error when drawing annotation '", object@anno_list[[i]]@name, "'\n", sep = "")
503
+				stop(oe)
504
+			}
493 505
 			upViewport()
494 506
 		}
495 507
 	}
... ...
@@ -149,6 +149,39 @@ oncoPrint(mat_list,
149 149
     }, col = col)
150 150
 ```
151 151
 
152
+With a single function for `alter_fun`, you can define different graphics for different alterations.
153
+In following plot, you need to adjust the height of the whole plot to make sure the height for each cell
154
+is more than double of the width.
155
+
156
+```{r, width = 6, height = 7}
157
+snv_fun = function(x, y, w, h) {
158
+	grid.rect(x, y, w, h, gp = gpar(fill = col["snv"], col = NA))
159
+}
160
+
161
+indel_fun = function(x, y, r) {
162
+	grid.circle(x, y, r, gp = gpar(fill = col["indel"], col = NA))
163
+}
164
+
165
+oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]],
166
+    alter_fun = function(x, y, w, h, v) {
167
+        n = sum(v)
168
+        w = convertWidth(w, "cm")*0.9
169
+        h = convertHeight(h, "cm")*0.9
170
+        l = min(unit.c(w, h))
171
+
172
+        grid.rect(x, y, w, h, gp = gpar(fill = "grey", col = NA))
173
+
174
+        if(n == 0) return(NULL)
175
+        if(n == 1) {
176
+        	if(names(which(v)) == "snv") snv_fun(x, y, l, l)
177
+        	if(names(which(v)) == "indel") indel_fun(x, y, l*0.5)
178
+        } else if(n == 2) {
179
+        	snv_fun(x, y-0.25*h, l, l)
180
+        	indel_fun(x, y+0.25*h, l*0.5)
181
+        }
182
+    }, col = col)
183
+```
184
+
152 185
 If `alter_fun` is specified as a list, the order of the elements controls the order of adding graphics. 
153 186
 There is a special element named `background` which defines how to draw background and it should be always put
154 187
 as the first element in the `alter_fun` list. In following example, backgrond color is changed to light green with borders.
... ...
@@ -173,6 +206,27 @@ oncoPrint(mat_list,
173 206
 	), col = col)
174 207
 ```
175 208
 
209
+You can customize the oncoprot by self-defining `alter_fun`. But be careful you must
210
+convert `w` and `h` to absolute units.
211
+
212
+```{r}
213
+oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]],
214
+    alter_fun = list(
215
+        snv = function(x, y, w, h) {
216
+        	w = convertWidth(w, "cm")
217
+        	h = convertHeight(h, "cm")
218
+        	l = min(unit.c(w, h))
219
+        	grid.rect(x, y, l*0.9, l*0.9, gp = gpar(fill = col["snv"], col = NA))
220
+        },
221
+        indel = function(x, y, w, h) {
222
+        	w = convertWidth(w, "cm")
223
+        	h = convertHeight(h, "cm")
224
+        	r = min(unit.c(w, h))*0.5
225
+        	grid.circle(x, y, r*0.9, gp = gpar(fill = col["indel"], col = NA))
226
+        }
227
+    ), col = col)
228
+```
229
+
176 230
 ## Apply to cBioPortal dataset
177 231
 
178 232
 Now we make an oncoPrint with a real-world data. The data is retrieved from [cBioPortal](http://www.cbioportal.org/).