Browse code

add i/j to alter_fun

Zuguang Gu authored on 22/03/2018 11:04:26
Showing2 changed files

... ...
@@ -10,6 +10,7 @@ CHANGES in VERSIONS 1.17.1
10 10
   ordered by number of samples that have mutations
11 11
 * correctly reorder rows
12 12
 * add `row_gap` argument for list of heatmaps
13
+* `oncoPrint()`: add `j` and `i` as optional argument for `alter_fun`
13 14
 
14 15
 =======================
15 16
 
... ...
@@ -156,13 +156,13 @@ oncoPrint = function(mat, get_type = function(x) x,
156 156
 
157 157
 	if(missing(alter_fun) && missing(col)) {
158 158
 		if(length(mat_list) == 1) {
159
-			af = function(x, y, w, h, v) {
159
+			af = function(x, y, w, h, v, j, i) {
160 160
 				grid.rect(x, y, w, h, gp = gpar(fill = "#CCCCCC", col = NA))
161 161
 				if(v[1]) grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "red", col = NA))
162 162
 			}
163 163
 			col = "red"
164 164
 		} else if(length(mat_list) == 2) {
165
-			af = function(x, y, w, h, v) {
165
+			af = function(x, y, w, h, v, j, i) {
166 166
 				grid.rect(x, y, w, h, gp = gpar(fill = "#CCCCCC", col = NA))
167 167
 				if(v[1]) grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "red", col = NA))
168 168
 		        if(v[2]) grid.rect(x, y, w*0.9, h*0.4, gp = gpar(fill = "blue", col = NA))
... ...
@@ -183,19 +183,33 @@ oncoPrint = function(mat, get_type = function(x) x,
183 183
 
184 184
 		alter_fun = alter_fun[unique(c("background", intersect(names(alter_fun), all_type)))]
185 185
 
186
-		af = function(x, y, w, h, v) {
186
+		af = function(x, y, w, h, v, j, i) {
187 187
 			if(!is.null(alter_fun$background)) alter_fun$background(x, y, w, h)
188 188
 
189 189
 			alter_fun = alter_fun[names(alter_fun) != "background"]
190 190
 
191 191
 			if(sum(v)) {
192 192
 				for(nm in names(alter_fun)) {
193
-					if(v[nm]) alter_fun[[nm]](x, y, w, h)
193
+					if(v[nm]) {
194
+						if(length(formals(alter_fun[[nm]])) == 6) {
195
+							alter_fun[[nm]](x, y, w, h, j, i)
196
+						} else {
197
+							alter_fun[[nm]](x, y, w, h)
198
+						}
199
+					}
194 200
 				}
195 201
 			}
196 202
 		}
197 203
 	} else {
198
-		af = alter_fun
204
+		if(length(formals(alter_fun)) == 7) {
205
+			af = function(x, y, w, h, v, j, i) {
206
+				alter_fun(x, y, w, h, v, j, i)
207
+			}
208
+		} else {
209
+			af = function(x, y, w, h, v, j, i) {
210
+				alter_fun(x, y, w, h, v)
211
+			}
212
+		}
199 213
 	}
200 214
 
201 215
 	col = col[intersect(names(col), all_type)]
... ...
@@ -323,7 +337,7 @@ oncoPrint = function(mat, get_type = function(x) x,
323 337
 		cell_fun = function(j, i, x, y, width, height, fill) {
324 338
 			z = arr[i, j, ]
325 339
 			names(z) = dimnames(arr)[[3]]
326
-			af(x, y, width, height, z)
340
+			af(x, y, width, height, z, j, i)
327 341
 		},
328 342
 		top_annotation = top_annotation,
329 343
 		top_annotation_height = top_annotation_height,