Browse code

anno_points(): images can be point symbols

jokergoo authored on 09/12/2019 11:06:42
Showing4 changed files

... ...
@@ -1,3 +1,9 @@
1
+CHANGES in VERSION 2.3.1
2
+
3
+* `anno_points()`: allows images as symbols.
4
+
5
+========================
6
+
1 7
 CHANGES in VERSION 2.1.1
2 8
 
3 9
 * `Heatmap()`: give error when heatmap has empty string as its name.
... ...
@@ -663,6 +663,11 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
663 663
 			stop_wrap("`axis_direction` is not supported any more.")
664 664
 		}
665 665
 	}
666
+	if("pch_as_image" %in% names(other_args)) {
667
+		pch_as_image = other_args$pch_as_image
668
+	} else {
669
+		pch_as_image = FALSE
670
+	}
666 671
 
667 672
 	ef = function() NULL
668 673
 	if(is.null(.ENV$current_annotation_which)) {
... ...
@@ -732,8 +737,17 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
732 737
 					default.units = "native", pch = pch[i], size = size[i])
733 738
 			}
734 739
 		} else {
735
-			grid.points(value[index], n - seq_along(index) + 1, gp = subset_gp(gp, index), default.units = "native", 
736
-				pch = pch[index], size = size[index])
740
+			if(pch_as_image) {
741
+				for(ii in seq_along(index)) {
742
+					pch_image = png::readPNG(pch[ index[ii] ])
743
+					grid.raster(pch_image, y = n - ii + 1, x = value[ index[ii] ], 
744
+						default.units = "native", width = size[ index[ii] ], 
745
+						height = size[ index[ii] ]*(nrow(pch_image)/ncol(pch_image)))
746
+				}
747
+			} else {
748
+				grid.points(value[index], n - seq_along(index) + 1, gp = subset_gp(gp, index), default.units = "native", 
749
+					pch = pch[index], size = size[index])
750
+			}
737 751
 		}
738 752
 		if(axis_param$side == "top") {
739 753
 			if(k > 1) axis = FALSE
... ...
@@ -756,10 +770,21 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
756 770
 		pushViewport(viewport(yscale = data_scale, xscale = c(0.5, n+0.5)))
757 771
 		if(is.matrix(value)) {
758 772
 			for(i in seq_len(ncol(value))) {
759
-				grid.points(seq_along(index), value[index, i], gp = subset_gp(gp, i), default.units = "native", pch = pch[i], size = size[i])
773
+				grid.points(seq_along(index), value[index, i], gp = subset_gp(gp, i), 
774
+					default.units = "native", pch = pch[i], size = size[i])
760 775
 			}
761 776
 		} else {
762
-			grid.points(seq_along(index), value[index], gp = subset_gp(gp, index), default.units = "native", pch = pch[index], size = size[index])
777
+			if(pch_as_image) {
778
+				for(ii in seq_along(index)) {
779
+					pch_image = png::readPNG(pch[ index[ii] ])
780
+					grid.raster(pch_image, x = ii, value[ index[ii] ], 
781
+						default.units = "native", width = size[ index[ii] ], 
782
+						height = size[ index[ii] ]*(nrow(pch_image)/ncol(pch_image)))
783
+				}
784
+			} else {
785
+				grid.points(seq_along(index), value[index], gp = subset_gp(gp, index), 
786
+					default.units = "native", pch = pch[index], size = size[index])
787
+			}
763 788
 		}
764 789
 		if(axis_param$side == "left") {
765 790
 			if(k > 1) axis = FALSE
... ...
@@ -785,7 +810,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
785 810
 		height = anno_size$height,
786 811
 		n = n,
787 812
 		data_scale = data_scale,
788
-		var_import = list(value, gp, border, pch, size, axis, axis_param, axis_grob, data_scale)
813
+		var_import = list(value, gp, border, pch, size, axis, axis_param, axis_grob, data_scale, pch_as_image)
789 814
 	)
790 815
 
791 816
 	anno@subset_rule$gp = subset_vector
... ...
@@ -87,7 +87,7 @@ make_comb_mat_from_matrix = function(x, mode, top_n_sets = Inf, min_set_size = -
87 87
 	comb_mat = t(comb_mat)
88 88
 
89 89
 	nc = ncol(comb_mat)
90
-	comb_mat2 = Matrix::Matrix(0, nrow = nrow(comb_mat), ncol = nc*(nc-1)/2, sparse = TRUE)
90
+	comb_mat2 = matrix(0, nrow = nrow(comb_mat), ncol = nc*(nc-1)/2)
91 91
 	rownames(comb_mat2) = rownames(comb_mat)
92 92
 	if(mode == "intersect") {
93 93
 		if(nc > 1) {
... ...
@@ -223,7 +223,7 @@ make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = In
223 223
     	complement_size = value_fun(complement_set)
224 224
     }
225 225
     
226
-    comb_mat = Matrix::Matrix(FALSE, nrow = n, ncol = sum(choose(n, 1:n)), s)
226
+    comb_mat = matrix(FALSE, nrow = n, ncol = sum(choose(n, 1:n)))
227 227
     rownames(comb_mat) = nm
228 228
     j = 1
229 229
     for(k in 1:n) {
... ...
@@ -449,6 +449,18 @@ make_comb_mat = function(..., mode = c("distinct", "intersect", "union"),
449 449
 			value_fun = length
450 450
 		}
451 451
 	}
452
+
453
+	if(is.atomic(lt[[1]])) {
454
+		m = make_comb_mat_from_matrix(list_to_matrix(lt), mode = mode, top_n_sets = top_n_sets, 
455
+			min_set_size = min_set_size, universal_set = universal_set, complement_size = complement_size)
456
+		if(remove_empty_comb_set) {
457
+			m = m[comb_size(m) > 0]
458
+		}
459
+		if(remove_complement_set) {
460
+			m = m[comb_degree(m) > 0]
461
+		}
462
+		return(m)
463
+	}
452 464
 	
453 465
 	m = make_comb_mat_from_list(lt, value_fun, mode = mode, top_n_sets = top_n_sets, min_set_size = min_set_size, 
454 466
 		universal_set = universal_set, complement_size = complement_size)
... ...
@@ -579,8 +591,7 @@ comb_name = function(m, readable = FALSE) {
579 591
 		nm = sapply(strsplit(nm, ""), function(x) {
580 592
 			l = as.logical(as.numeric(x))
581 593
 			paste(sn[l], collapse = "&")
582
-		}
583
-		nm = unname(nm)
594
+		})
584 595
 	}
585 596
 
586 597
 	return(nm)
... ...
@@ -129,6 +129,14 @@ draw(anno, test = "anno_points")
129 129
 anno = anno_points(runif(10), axis_param = list(direction = "reverse"), ylim = c(0, 1), which = "row")
130 130
 draw(anno, test = "anno_points")
131 131
 
132
+# pch as image
133
+image1 = sample(dir("/desktop-home/guz/Downloads/IcoMoon-Free-master/PNG/64px", full.names = TRUE), 10)
134
+x = runif(10)
135
+anno1 = anno_points(x, pch = image1, pch_as_image = TRUE, size = unit(5, "mm"), height = unit(4, "cm"))
136
+anno2 = anno_points(x, height = unit(4, "cm"))
137
+draw(anno1, test = "anno_points")
138
+draw(anno2, test = "anno_points")
139
+
132 140
 ##### test anno_lines ###
133 141
 anno = anno_lines(runif(10))
134 142
 draw(anno, test = "anno_lines")