Browse code

Update AnnotationFunction-function.R

Fixed pch matrix reordering by:
For column, move reording out of for loop, so matrix is only reordered once.
For row, added reordering in before for loop.

Also changed some indentation.

mbdabrowska1 authored on 13/08/2019 10:20:26 • GitHub committed on 13/08/2019 10:20:26
Showing1 changed files

... ...
@@ -222,33 +222,38 @@ anno_simple = function(x, col, na_col = "grey",
222 222
 
223 223
 	row_fun = function(index) {
224 224
 		
225
-		n = length(index)
226
-		y = (n - seq_len(n) + 0.5) / n
227
-        if(is.matrix(value)) {
228
-            nc = ncol(value)
229
-            for(i in seq_len(nc)) {
230
-                fill = map_to_colors(color_mapping, value[index, i])
231
-                grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, 
232
-                	gp = do.call("gpar", c(list(fill = fill), gp)))
233
-                if(!is.null(pch)) {
234
-					l = !is.na(pch[, i])
235
-					grid.points(x = rep((i-0.5)/nc, sum(l)), y = y[l], pch = pch[l, i], 
236
-						size = {if(length(pt_size) == 1) pt_size else pt_size[i]}, 
237
-						gp = subset_gp(pt_gp, i))
238
-				}
239
-            }
240
-        } else {
241
-			fill = map_to_colors(color_mapping, value[index])
242
-			grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
243
-			if(!is.null(pch)) {
244
-				pch = pch[index]
245
-				pt_size = pt_size[index]
246
-				pt_gp = subset_gp(pt_gp, index)
247
-				l = !is.na(pch)
248
-				grid.points(x = rep(0.5, sum(l)), y = y[l], pch = pch[l], size = pt_size[l], 
249
-					gp = subset_gp(pt_gp, which(l)))
250
-			}
251
-        }
225
+	    n = length(index)
226
+	    y = (n - seq_len(n) + 0.5) / n
227
+	    if(is.matrix(value)) {
228
+		nc = ncol(value)
229
+		if(is.matrix(pch)){
230
+	            pch = pch[index, , drop = FALSE]
231
+	        } else if(is.vector(pch)) {
232
+		    stop_wrap(paste0("Since annotation is a matrix, pch should also be a matrix")
233
+	        }
234
+		for(i in seq_len(nc)) {
235
+		    fill = map_to_colors(color_mapping, value[index, i])
236
+		    grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, 
237
+			gp = do.call("gpar", c(list(fill = fill), gp)))
238
+		    if(!is.null(pch)) {
239
+			l = !is.na(pch[, i])
240
+			grid.points(x = rep((i-0.5)/nc, sum(l)), y = y[l], pch = pch[l, i], 
241
+			    size = {if(length(pt_size) == 1) pt_size else pt_size[i]}, 
242
+			    gp = subset_gp(pt_gp, i))
243
+		    }
244
+		}
245
+	    } else {
246
+		fill = map_to_colors(color_mapping, value[index])
247
+		grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
248
+		if(!is.null(pch)) {
249
+		    pch = pch[index]
250
+		    pt_size = pt_size[index]
251
+		    pt_gp = subset_gp(pt_gp, index)
252
+		    l = !is.na(pch)
253
+		    grid.points(x = rep(0.5, sum(l)), y = y[l], pch = pch[l], size = pt_size[l], 
254
+			gp = subset_gp(pt_gp, which(l)))
255
+		}
256
+	    }
252 257
         if(border) grid.rect(gp = gpar(fill = "transparent"))
253 258
 	}
254 259
 
... ...
@@ -258,28 +263,34 @@ anno_simple = function(x, col, na_col = "grey",
258 263
 		x = (seq_len(n) - 0.5) / n
259 264
         if(is.matrix(value)) {
260 265
             nc = ncol(value)
266
+		
267
+	    if(is.matrix(pch)){
268
+	        pch = pch[index, , drop = FALSE]
269
+	    } else if(is.vector(pch)) {
270
+		stop_wrap(paste0("Since annotation is a matrix, pch should also be a matrix")
271
+	    }
272
+			  
261 273
             for(i in seq_len(nc)) {
262 274
                 fill = map_to_colors(color_mapping, value[index, i])
263 275
                 grid.rect(x, y = (nc-i +0.5)/nc, width = 1/n, height = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
264
-                if(!is.null(pch)) {
265
-                	pch = pch[index, , drop = FALSE]
266
-					l = !is.na(pch[, i])
267
-					grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i], 
268
-						size = {if(length(pt_size) == 1) pt_size else pt_size[i]}, 
269
-						gp = subset_gp(pt_gp, i))
270
-				}
271
-            }
276
+		if(!is.null(pch)){
277
+		    l = !is.na(pch[, i])
278
+		    grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i], 
279
+			size = {if(length(pt_size) == 1) pt_size else pt_size[i]}, 
280
+			gp = subset_gp(pt_gp, i))
281
+		}
282
+	    }
272 283
         } else {
273
-			fill = map_to_colors(color_mapping, value[index])
274
-			grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
275
-			if(!is.null(pch)) {
276
-				pch = pch[index]
277
-				pt_size = pt_size[index]
278
-				pt_gp = subset_gp(pt_gp, index)
279
-				l = !is.na(pch)
280
-				grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l], 
281
-					gp = subset_gp(pt_gp, which(l)))
282
-			}
284
+		fill = map_to_colors(color_mapping, value[index])
285
+		grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
286
+		if(!is.null(pch)) {
287
+			pch = pch[index]
288
+			pt_size = pt_size[index]
289
+			pt_gp = subset_gp(pt_gp, index)
290
+			l = !is.na(pch)
291
+			grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l], 
292
+				gp = subset_gp(pt_gp, which(l)))
293
+		}
283 294
         }
284 295
         if(border) grid.rect(gp = gpar(fill = "transparent"))
285 296
 	}