Browse code

UpSet() supports adding complement sets

Zuguang Gu authored on 26/03/2019 22:10:06
Showing 3 changed files

... ...
@@ -1,3 +1,9 @@
1
+CHANGES in VERSION 1.99.7
2
+
3
+* `UpSet()` supports adding complement sets.
4
+
5
+========================
6
+
1 7
 CHANGES in VERSION 1.99.6
2 8
 
3 9
 * adjust the size of heatmap annotations and add testing scripts.
... ...
@@ -1,5 +1,5 @@
1 1
 
2
-make_comb_mat_from_matrix = function(x, mode, top_n_sets = Inf, min_set_size = -Inf) {
2
+make_comb_mat_from_matrix = function(x, mode, top_n_sets = Inf, min_set_size = -Inf, complement_size = NULL) {
3 3
 	# check whether x is a binary matrix
4 4
 	if(is.data.frame(x)) {
5 5
 		lc = sapply(x, function(x) {
... ...
@@ -93,6 +93,11 @@ make_comb_mat_from_matrix = function(x, mode, top_n_sets = Inf, min_set_size = -
93 93
 		})
94 94
 	}
95 95
 
96
+	if(!is.null(complement_size)) {
97
+		comb_mat = cbind(rep(0, nrow(comb_mat)), comb_mat)
98
+		comb_size = c(complement_size, comb_size)
99
+	}
100
+
96 101
 	attr(comb_mat, "set_size") = set_size
97 102
 	attr(comb_mat, "comb_size") = comb_size
98 103
 	attr(comb_mat, "mode") = mode
... ...
@@ -103,7 +108,7 @@ make_comb_mat_from_matrix = function(x, mode, top_n_sets = Inf, min_set_size = -
103 108
 
104 109
 }
105 110
 
106
-make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = Inf, min_set_size = -Inf) {
111
+make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = Inf, min_set_size = -Inf, complement_size = NULL) {
107 112
 	n = length(lt)
108 113
     nm = names(lt)
109 114
     if(is.null(nm)) {
... ...
@@ -183,6 +188,12 @@ make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = In
183 188
     }
184 189
 
185 190
     comb_mat = comb_mat + 0
191
+
192
+    if(!is.null(complement_size)) {
193
+		comb_mat = cbind(rep(0, nrow(comb_mat)), comb_mat)
194
+		comb_size = c(complement_size, comb_size)
195
+	}
196
+
186 197
     attr(comb_mat, "set_size") = set_size
187 198
 	attr(comb_mat, "comb_size") = comb_size
188 199
 	attr(comb_mat, "mode") = mode
... ...
@@ -228,6 +239,8 @@ list_to_matrix = function(lt) {
228 239
 # -mode The mode for forming the combination set, see Mode section.
229 240
 # -top_n_sets Number of sets with largest size.
230 241
 # -min_set_size Ths minimal set size that is used for generating the combination matrix.
242
+# -complement_size The size for the complement of all sets. If it is specified, the combination
243
+#                  set name will be like "00...".
231 244
 # -value_fun For each combination set, how to calculate the size? If it is a scalar set, 
232 245
 #      the length of the vector is the size of the set, while if it is a region-based set,
233 246
 #      (i.e. ``GRanges`` or ``IRanges`` object), the sum of widths of regions in the set is
... ...
@@ -316,7 +329,7 @@ list_to_matrix = function(lt) {
316 329
 # m = make_comb_mat(lt)
317 330
 # }
318 331
 make_comb_mat = function(..., mode = c("distinct", "intersect", "union"),
319
-	top_n_sets = Inf, min_set_size = -Inf, value_fun) {
332
+	top_n_sets = Inf, min_set_size = -Inf, complement_size = NULL, value_fun) {
320 333
 
321 334
 	lt = list(...)
322 335
 
... ...
@@ -324,7 +337,7 @@ make_comb_mat = function(..., mode = c("distinct", "intersect", "union"),
324 337
 	if(length(lt) == 1) {
325 338
 		lt = lt[[1]]
326 339
 		if(!is.null(dim(lt))) {
327
-			return(make_comb_mat_from_matrix(lt, mode = mode, top_n_sets = top_n_sets, min_set_size = min_set_size))
340
+			return(make_comb_mat_from_matrix(lt, mode = mode, top_n_sets = top_n_sets, min_set_size = min_set_size, complement_size = complement_size))
328 341
 		}
329 342
 	}
330 343
 
... ...
@@ -337,7 +350,7 @@ make_comb_mat = function(..., mode = c("distinct", "intersect", "union"),
337 350
 			value_fun = length
338 351
 		}
339 352
 	}
340
-	make_comb_mat_from_list(lt, value_fun, mode = mode, top_n_sets = top_n_sets, min_set_size = min_set_size)
353
+	make_comb_mat_from_list(lt, value_fun, mode = mode, top_n_sets = top_n_sets, min_set_size = min_set_size, complement_size = complement_size)
341 354
 }
342 355
 
343 356
 
... ...
@@ -492,6 +505,11 @@ comb_degree = function(m) {
492 505
 # m = make_comb_mat(lt)
493 506
 # extract_comb(m, "110")
494 507
 extract_comb = function(m, comb_name) {
508
+
509
+	if(grepl("^0+$", comb_name)) {
510
+		stop_wrap(qq("Cannot extract elements for the complement set '@{comb_name}'."))
511
+	}
512
+
495 513
 	all_comb_names = comb_name(m)
496 514
 	if(!comb_name %in% all_comb_names) {
497 515
 		stop_wrap(paste0("Cannot find a combination name:	", comb_name, ", valid combination name should be in `comb_name(m)`."))
... ...
@@ -8,7 +8,7 @@ Make a Combination Matrix for UpSet Plot
8 8
 }
9 9
 \usage{
10 10
 make_comb_mat(..., mode = c("distinct", "intersect", "union"),
11
-    top_n_sets = Inf, min_set_size = -Inf, value_fun)
11
+    top_n_sets = Inf, min_set_size = -Inf, complement_size = NULL, value_fun)
12 12
 }
13 13
 \arguments{
14 14
 
... ...
@@ -16,6 +16,7 @@ make_comb_mat(..., mode = c("distinct", "intersect", "union"),
16 16
   \item{mode}{The mode for forming the combination set, see Mode section.}
17 17
   \item{top_n_sets}{Number of sets with largest size.}
18 18
   \item{min_set_size}{Ths minimal set size that is used for generating the combination matrix.}
19
+  \item{complement_size}{The size for the complement of all sets. If it is specified, the combination set name will be like "00...".}
19 20
   \item{value_fun}{For each combination set, how to calculate the size? If it is a scalar set,  the length of the vector is the size of the set, while if it is a region-based set, (i.e. \code{GRanges} or \code{IRanges} object), the sum of widths of regions in the set is calculated as the size of the set.}
20 21
 
21 22
 }