Browse code

add an internal argument for anno_zoom()

Zuguang Gu authored on 11/04/2020 18:04:59
Showing2 changed files

... ...
@@ -3305,6 +3305,7 @@ anno_block = function(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot
3305 3305
 #         The value can be a proportion number or  a `grid::unit` object. The length can be either one or two.
3306 3306
 # -width Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.
3307 3307
 # -height Height of the annotation. The value should be an absolute unit. Height is not allowed to be set for row annotation.
3308
+# -internal_line Internally used.
3308 3309
 #
3309 3310
 # == details
3310 3311
 # `anno_zoom` creates several plotting regions (boxes) which can be corresponded to subsets of rows/columns in the
... ...
@@ -3338,7 +3339,7 @@ anno_zoom = function(align_to, panel_fun = function(index, nm = NULL) { grid.rec
3338 3339
 	which = c("column", "row"), side = ifelse(which == "column", "top", "right"),
3339 3340
 	size = NULL, gap = unit(1, "mm"), 
3340 3341
 	link_width = unit(5, "mm"), link_height = link_width, link_gp = gpar(),
3341
-	extend = unit(0, "mm"), width = NULL, height = NULL) {
3342
+	extend = unit(0, "mm"), width = NULL, height = NULL, internal_line = TRUE) {
3342 3343
 	
3343 3344
 	if(is.null(.ENV$current_annotation_which)) {
3344 3345
 		which = match.arg(which)[1]
... ...
@@ -3507,14 +3508,39 @@ anno_zoom = function(align_to, panel_fun = function(index, nm = NULL) { grid.rec
3507 3508
 			df = align_to_df[[i]]
3508 3509
 			for(j in 1:nrow(df)) {
3509 3510
 				# draw each polygon
3510
-				if(side == "right") {
3511
-					grid.polygon(unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3512
-						c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3513
-						default.units = "native", gp = subset_gp(link_gp, i))
3511
+				if(internal_line && "col" %in% names(link_gp)) {
3512
+					link_gp3 = link_gp2 = link_gp
3513
+					link_gp2$col = link_gp$fill
3514
+					link_gp2$lty = NULL
3515
+					link_gp3$fill = NA
3516
+					if(side == "right") {
3517
+						grid.polygon(unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3518
+							c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3519
+							default.units = "native", gp = subset_gp(link_gp2, i))
3520
+
3521
+						grid.lines(unit.c(link_width, unit(c(0, 0), "npc"), link_width),
3522
+							c(h[i, "bottom"], pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"]),
3523
+							default.units = "native", gp = subset_gp(link_gp3, i))
3524
+					} else {
3525
+						grid.polygon(unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3526
+							c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3527
+							default.units = "native", gp = subset_gp(link_gp2, i))
3528
+
3529
+						grid.lines(unit.c(unit(0, "npc"), rep(link_width, 2), unit(0, "npc")),
3530
+							c(h[i, "bottom"], pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"]),
3531
+							default.units = "native", gp = subset_gp(link_gp3, i))
3532
+					}
3533
+
3514 3534
 				} else {
3515
-					grid.polygon(unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3516
-						c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3517
-						default.units = "native", gp = subset_gp(link_gp, i))
3535
+					if(side == "right") {
3536
+						grid.polygon(unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3537
+							c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3538
+							default.units = "native", gp = subset_gp(link_gp, i))
3539
+					} else {
3540
+						grid.polygon(unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3541
+							c(pos[df[j, 2]] - 0.5/n, pos[df[j, 1]] + 0.5/n, h[i, "top"], h[i, "bottom"]),
3542
+							default.units = "native", gp = subset_gp(link_gp, i))
3543
+					}
3518 3544
 				}
3519 3545
 			}
3520 3546
 		}
... ...
@@ -3654,16 +3680,45 @@ anno_zoom = function(align_to, panel_fun = function(index, nm = NULL) { grid.rec
3654 3680
 			df = align_to_df[[i]]
3655 3681
 			for(j in 1:nrow(df)) {
3656 3682
 				# draw each polygon
3657
-				if(side == "top") {
3658
-					grid.polygon(
3659
-						c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3660
-						unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3661
-						default.units = "native", gp = subset_gp(link_gp, i))
3683
+				if(internal_line && "col" %in% names(link_gp)) {
3684
+					link_gp3 = link_gp2 = link_gp
3685
+					link_gp2$col = link_gp$fill
3686
+					link_gp2$lty = NULL
3687
+					link_gp3$fill = NA
3688
+
3689
+					if(side == "top") {
3690
+						grid.polygon(
3691
+							c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3692
+							unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3693
+							default.units = "native", gp = subset_gp(link_gp2, i))
3694
+
3695
+						grid.lines(
3696
+							c(h[i, "right"], pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"]),
3697
+							unit.c(link_width,unit(c(0, 0), "npc"), link_width),
3698
+							default.units = "native", gp = subset_gp(link_gp3, i))
3699
+					} else {
3700
+						grid.polygon(
3701
+							c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3702
+							unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3703
+							default.units = "native", gp = subset_gp(link_gp2, i))
3704
+
3705
+						grid.lines(
3706
+							c(h[i, "right"], pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"]),
3707
+							unit.c(unit(0, "npc"), rep(link_width, 2), unit(0, "npc")),
3708
+							default.units = "native", gp = subset_gp(link_gp3, i))
3709
+					}
3662 3710
 				} else {
3663
-					grid.polygon(
3664
-						c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3665
-						unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3666
-						default.units = "native", gp = subset_gp(link_gp, i))
3711
+					if(side == "top") {
3712
+						grid.polygon(
3713
+							c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3714
+							unit.c(unit(c(0, 0), "npc"), rep(link_width, 2)),
3715
+							default.units = "native", gp = subset_gp(link_gp, i))
3716
+					} else {
3717
+						grid.polygon(
3718
+							c(pos[df[j, 2]] + 0.5/n, pos[df[j, 1]] - 0.5/n, h[i, "left"], h[i, "right"]),
3719
+							unit.c(rep(link_width, 2), unit(c(0, 0), "npc")),
3720
+							default.units = "native", gp = subset_gp(link_gp, i))
3721
+					}
3667 3722
 				}
3668 3723
 			}
3669 3724
 		}
... ...
@@ -3685,7 +3740,7 @@ anno_zoom = function(align_to, panel_fun = function(index, nm = NULL) { grid.rec
3685 3740
 		width = anno_size$width,
3686 3741
 		n = -1,
3687 3742
 		var_import = list(align_to, .pos, gap, size, panel_fun, side, anno_size, extend,
3688
-			link_width, link_height, link_gp),
3743
+			link_width, link_height, link_gp, internal_line),
3689 3744
 		show_name = FALSE
3690 3745
 	)
3691 3746
 
... ...
@@ -486,6 +486,15 @@ panel_fun = function(index, nm) {
486 486
 anno = anno_zoom(align_to = fa, which = "row", panel_fun = panel_fun)
487 487
 draw(anno, index = 1:100, test = "anno_zoom")
488 488
 
489
+panel_fun = function(index, nm) {
490
+	grid.rect(gp = gpar(fill = "grey", col = NA))
491
+	grid.text(nm)
492
+}
493
+
494
+anno = anno_zoom(align_to = fa, which = "row", panel_fun = panel_fun, link_gp = gpar(fill = "grey", col = "black"), internal_line = TRUE)
495
+draw(anno, index = 1:100, test = "anno_zoom")
496
+
497
+
489 498
 anno = anno_zoom(align_to = fa, which = "row", panel_fun = panel_fun,
490 499
 	gap = unit(1, "cm"))
491 500
 draw(anno, index = 1:100, test = "anno_zoom, set gap")