Browse code

add 'show_pct' in oncoPrint()

Zuguang Gu authored on 08/03/2016 10:36:50
Showing 4 changed files

... ...
@@ -6,6 +6,7 @@ CHANGES in VERSION 1.9.3
6 6
 * when `gap` is set for the main heatmap, other heatmps also adjust their `gap` values to it
7 7
 * fixed a bug that when rownames/colnames are not complete, dendrograms are corrupted
8 8
 * `alter_fun` now supports adding graphics grid by grid
9
+* add `show_pct` option in `oncoPrint()`
9 10
 
10 11
 ==============================
11 12
 
... ...
@@ -17,6 +17,7 @@
17 17
 # -column_order order of samples. By default the order is calculated by the 'memo sort' method which can visualize
18 18
 #                                 the mutual exclusivity across genes. Set it to ``NULL`` if you don't want to set the order
19 19
 # -show_column_names whether show column names
20
+# -show_pct whether show percent values on the left of the oncoprint
20 21
 # -pct_gp graphic paramters for percent row annotation
21 22
 # -pct_digits digits for percent values
22 23
 # -axis_gp graphic paramters for axes
... ...
@@ -50,7 +51,7 @@ oncoPrint = function(mat, get_type = function(x) x,
50 51
 	row_order = oncoprint_row_order(),
51 52
 	column_order = oncoprint_column_order(),
52 53
 	show_column_names = FALSE,
53
-	pct_gp = gpar(), pct_digits = 0,
54
+	show_pct = TRUE, pct_gp = gpar(), pct_digits = 0,
54 55
 	axis_gp = gpar(fontsize = 8), 
55 56
 	show_row_barplot = TRUE, 
56 57
 	row_barplot_width = unit(2, "cm"),
... ...
@@ -72,6 +73,9 @@ oncoPrint = function(mat, get_type = function(x) x,
72 73
 	}
73 74
 	
74 75
 	# convert mat to mat_list
76
+	if(inherits(mat, "data.frame")) {
77
+		mat = as.matrix(mat)
78
+	}
75 79
 	if(inherits(mat, "matrix")) {
76 80
 
77 81
 		all_type = unique(unlist(lapply(mat, get_type)))
... ...
@@ -84,6 +88,7 @@ oncoPrint = function(mat, get_type = function(x) x,
84 88
 			dimnames(m) = dimnames(mat)
85 89
 			m
86 90
 		})
91
+		names(mat_list) = all_type
87 92
 	} else if(inherits(mat, "list")) {
88 93
 		mat_list = mat
89 94
 
... ...
@@ -195,7 +200,7 @@ oncoPrint = function(mat, get_type = function(x) x,
195 200
 	# for each gene, percent of samples that have alterations
196 201
 	pct = rowSums(apply(arr, 1:2, any)) / ncol(mat_list[[1]])
197 202
 	pct = paste0(round(pct * 100, digits = pct_digits), "%")
198
-	ha_pct = rowAnnotation(pct = row_anno_text(pct, just = "right", offset = unit(1, "npc"), gp = pct_gp), width = grobWidth(textGrob("100%", gp = pct_gp)))
203
+	ha_pct = rowAnnotation(pct = row_anno_text(pct, just = "right", offset = unit(1, "npc"), gp = pct_gp), width = max_text_width(pct, gp = pct_gp))
199 204
 
200 205
 	#####################################################################
201 206
 	# row annotation which is a barplot
... ...
@@ -272,10 +277,14 @@ oncoPrint = function(mat, get_type = function(x) x,
272 277
 		top_annotation = top_annotation,
273 278
 		heatmap_legend_param = heatmap_legend_param, ...)
274 279
 
275
-	if(show_row_barplot) {
276
-		ht_list = ha_pct + ht + ha_row_bar
277
-	} else {
280
+	if(show_pct) {
278 281
 		ht_list = ha_pct + ht
282
+	} else {
283
+		ht_list = ht
284
+	}
285
+
286
+	if(show_row_barplot) {
287
+		ht_list = ht_list + ha_row_bar
279 288
 	}
280 289
 
281 290
 	return(ht_list)
... ...
@@ -12,7 +12,7 @@ oncoPrint(mat, get_type = function(x) x,
12 12
     row_order = oncoprint_row_order(),
13 13
     column_order = oncoprint_column_order(),
14 14
     show_column_names = FALSE,
15
-    pct_gp = gpar(), pct_digits = 0,
15
+    show_pct = TRUE, pct_gp = gpar(), pct_digits = 0,
16 16
     axis_gp = gpar(fontsize = 8),
17 17
     show_row_barplot = TRUE,
18 18
     row_barplot_width = unit(2, "cm"),
... ...
@@ -33,6 +33,7 @@ oncoPrint(mat, get_type = function(x) x,
33 33
   \item{row_order}{order of genes. By default it is sorted by frequency of alterations decreasingly. Set it to \code{NULL} if you don't want to set the order}
34 34
   \item{column_order}{order of samples. By default the order is calculated by the 'memo sort' method which can visualize the mutual exclusivity across genes. Set it to \code{NULL} if you don't want to set the order}
35 35
   \item{show_column_names}{whether show column names}
36
+  \item{show_pct}{whether show percent values on the left of the oncoprint}
36 37
   \item{pct_gp}{graphic paramters for percent row annotation}
37 38
   \item{pct_digits}{digits for percent values}
38 39
   \item{axis_gp}{graphic paramters for axes}
... ...
@@ -140,7 +140,9 @@ oncoPrint(mat_list,
140 140
     alter_fun = function(x, y, w, h, v) {
141 141
 		n = sum(v)
142 142
 		h = h*0.9
143
-		if(n) grid.rect(x, y - h*0.5 + 1:n/n*h, w*0.9, 1/n*h, gp = gpar(fill = col[which(v)], col = NA), just = "top")
143
+		# use `names(which(v))` to correctly map between `v` and `col`
144
+		if(n) grid.rect(x, y - h*0.5 + 1:n/n*h, w*0.9, 1/n*h, 
145
+			gp = gpar(fill = col[names(which(v))], col = NA), just = "top")
144 146
     }, col = col)
145 147
 ```
146 148