Browse code

minor fix

Zuguang Gu authored on 29/12/2018 15:24:39
Showing24 changed files

... ...
@@ -159,8 +159,6 @@ export("decorate_row_title")
159 159
 export("decorate_title")
160 160
 export("default_axis_param")
161 161
 export("default_get_type")
162
-export("default_upset_right_annotation")
163
-export("default_upset_top_annotation")
164 162
 export("dend_heights")
165 163
 export("dend_xy")
166 164
 export("dendrogramGrob")
... ...
@@ -180,7 +178,9 @@ export("make_comb_mat")
180 178
 export("max_text_height")
181 179
 export("max_text_width")
182 180
 export("merge_dendrogram")
181
+export("normalize_comb_mat")
183 182
 export("oncoPrint")
183
+export("order.comb_mat")
184 184
 export("packLegend")
185 185
 export("pindex")
186 186
 export("rowAnnotation")
... ...
@@ -198,6 +198,8 @@ export("subset_gp")
198 198
 export("subset_matrix_by_row")
199 199
 export("subset_vector")
200 200
 export("unify_mat_list")
201
+export("upset_right_annotation")
202
+export("upset_top_annotation")
201 203
 exportClasses("AdditiveUnit")
202 204
 export("AdditiveUnit")
203 205
 exportClasses("AnnotationFunction")
... ...
@@ -58,7 +58,7 @@ HeatmapAnnotation = setClass("HeatmapAnnotation",
58 58
 # -show_annotation_name Whether show annotation names? For column annotation, annotation names are drawn either on the left
59 59
 #   or the right, and for row annotations, names are draw either on top or at the bottom. The value can be a vector.
60 60
 # -annotation_name_gp Graphic parameters for anntation names. Graphic paramters can be vectors.
61
-# -annotation_name_offset Offset to the annotations, `grid::unit` object. The value can be a vector.
61
+# -annotation_name_offset Offset to the annotation names, a `grid::unit` object. The value can be a vector.
62 62
 # -annotation_name_side Side of the annotation names.
63 63
 # -annotation_name_rot Rotation of the annotation names, it can only take values in ``c(00, 90, 180, 270)``. The value can be a vector.
64 64
 # -annotation_height Height of each annotation if annotations are column annotations.
... ...
@@ -110,6 +110,16 @@ make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = In
110 110
     	stop_wrap("The list must have names.")
111 111
     }
112 112
 
113
+    if(inherits(lt[[1]], "GRanges")) {
114
+    	union = getFromNamespace("union", ns = "BiocGenerics")
115
+    	intersect = getFromNamespace("intersect", ns = "BiocGenerics")
116
+    	setdiff = getFromNamespace("setdiff", ns = "BiocGenerics")
117
+    } else if(inherits(lt[[1]], "IRanges")) {
118
+    	union = getFromNamespace("union", ns = "BiocGenerics")
119
+    	intersect = getFromNamespace("intersect", ns = "BiocGenerics")
120
+    	setdiff = getFromNamespace("setdiff", ns = "BiocGenerics")
121
+    }
122
+
113 123
     if(inherits(lt[[1]], "GRanges")) {
114 124
     	set_size = sapply(lt, function(x) {
115 125
 	    	value_fun(union(x, x[NULL]))
... ...
@@ -195,8 +205,8 @@ make_comb_mat_from_list = function(lt, mode, value_fun = length, top_n_sets = In
195 205
 # == example
196 206
 # set.seed(123)
197 207
 # lt = list(a = sample(letters, 10),
198
-# 	      b = sample(letters, 15),
199
-# 	      c = sample(letters, 20))
208
+#           b = sample(letters, 15),
209
+#           c = sample(letters, 20))
200 210
 # list_to_matrix(lt)
201 211
 list_to_matrix = function(lt) {
202 212
 	cn = unique(unlist(lt))
... ...
@@ -210,7 +220,7 @@ list_to_matrix = function(lt) {
210 220
 }
211 221
 
212 222
 # == title
213
-# Make a Combination matrix for UpSet Plot
223
+# Make a Combination Matrix for UpSet Plot
214 224
 #
215 225
 # == param
216 226
 # -... The input sets. If it is represented as a single variable, it should be a matrix/data frame
... ...
@@ -218,7 +228,7 @@ list_to_matrix = function(lt) {
218 228
 # -mode The mode for forming the combination set, see Mode section.
219 229
 # -top_n_sets Number of sets with largest size.
220 230
 # -min_set_size Ths minimal set size that is used for generating the combination matrix.
221
-# -value_fun For each combination set, how to calculate the size. If it is a scalar set, 
231
+# -value_fun For each combination set, how to calculate the size? If it is a scalar set, 
222 232
 #      the length of the vector is the size of the set, while if it is a region-based set,
223 233
 #      (i.e. ``GRanges`` or ``IRanges`` object), the sum of widths of regions in the set is
224 234
 #      calculated as the size of the set.
... ...
@@ -240,6 +250,7 @@ list_to_matrix = function(lt) {
240 250
 #     j 1 0 0
241 251
 #     u 1 0 1
242 252
 #     w 1 0 0
253
+#     ...
243 254
 #
244 255
 # If the variable is a data frame, the binary columns (only contain 0 and 1) and the logical
245 256
 # columns are only kept.
... ...
@@ -266,26 +277,29 @@ list_to_matrix = function(lt) {
266 277
 # we need to define how to calculate the size of that combination set. There are three modes:
267 278
 #
268 279
 # 1. ``distinct`` mode: 1 means in that set and 0 means not in that set, then "1 1 0" means a
269
-# set of elements also in set A and B, while not in C (``setdiff(intersect(A, B), C)``). Under
280
+# set of elements also in set A and B, while not in C (i.e. ``setdiff(intersect(A, B), C)``). Under
270 281
 # this mode, the seven combination sets are the seven partitions in the Venn diagram and they
271 282
 # are mutually exclusive.
272 283
 #
273 284
 # 2. ``intersect`` mode: 1 means in that set and 0 is not taken into account, then, "1 1 0" means
274
-# a set of elements in set A and B, and they can also in C or not in C (``intersect(A, B)``).
275
-# Under this mode, the seven combinatio sets can overlap.
285
+# a set of elements in set A and B, and they can also in C or not in C (i.e. ``intersect(A, B)``).
286
+# Under this mode, the seven combination sets can overlap.
276 287
 #
277 288
 # 3. ``union`` mode: 1 means in that set and 0 is not taken into account. When there are multiple
278
-# 1, the relationship is OR. Then, "1 1 0" means a set of elements in set A or B, and they can also in C or not in C (``union(A, B)``).
279
-# Under this mode, the seven combinatio sets can overlap.
289
+# 1, the relationship is OR. Then, "1 1 0" means a set of elements in set A or B, and they can also in C or not in C (i.e. ``union(A, B)``).
290
+# Under this mode, the seven combination sets can overlap.
280 291
 #
281 292
 # == value
282 293
 # A matrix also in a class of ``comb_mat``.
283 294
 #
295
+# Following functions can be applied to it: `set_name`, `comb_name`, `set_size`, `comb_size`, `comb_degree`,
296
+# `extract_comb` and `t.comb_mat`.
297
+#
284 298
 # == example
285 299
 # set.seed(123)
286 300
 # lt = list(a = sample(letters, 10),
287
-# 	      b = sample(letters, 15),
288
-# 	      c = sample(letters, 20))
301
+#           b = sample(letters, 15),
302
+#           c = sample(letters, 20))
289 303
 # m = make_comb_mat(lt)
290 304
 #
291 305
 # mat = list_to_matrix(lt)
... ...
@@ -297,7 +311,7 @@ list_to_matrix = function(lt) {
297 311
 # library(GenomicRanges)
298 312
 # lt = lapply(1:4, function(i) generateRandomBed())
299 313
 # lt = lapply(lt, function(df) GRanges(seqnames = df[, 1], 
300
-# 	ranges = IRanges(df[, 2], df[, 3])))
314
+#     ranges = IRanges(df[, 2], df[, 3])))
301 315
 # names(lt) = letters[1:4]
302 316
 # m = make_comb_mat(lt)
303 317
 # }
... ...
@@ -316,9 +330,9 @@ make_comb_mat = function(..., mode = c("distinct", "intersect", "union"),
316 330
 
317 331
 	if(missing(value_fun)) {
318 332
 		if(inherits(lt[[1]], "GRanges")) {
319
-			value_fun = function(x) sum(as.numeric(end(x) - start(x) + 1))
333
+			value_fun = function(x) sum(as.numeric(getFromNamespace("width", ns = "BiocGenerics")(x)))
320 334
 		} else if(inherits(lt[[1]], "IRanges")) {
321
-			value_fun = function(x) sum(as.numeric(end(x) - start(x) + 1))
335
+			value_fun = function(x) sum(as.numeric(getFromNamespace("width", ns = "BiocGenerics")(x)))
322 336
 		} else {
323 337
 			value_fun = length
324 338
 		}
... ...
@@ -331,6 +345,10 @@ binaryToInt = function(x) {
331 345
 	sum(x * 2^(rev(seq_along(x)) - 1))
332 346
 }
333 347
 
348
+intToBinary = function(x, len) {
349
+	rev(as.integer(intToBits(x))[1:len])
350
+}
351
+
334 352
 # == title
335 353
 # Set Names
336 354
 #
... ...
@@ -343,8 +361,8 @@ binaryToInt = function(x) {
343 361
 # == example
344 362
 # set.seed(123)
345 363
 # lt = list(a = sample(letters, 10),
346
-# 	      b = sample(letters, 15),
347
-# 	      c = sample(letters, 20))
364
+#           b = sample(letters, 15),
365
+#           c = sample(letters, 20))
348 366
 # m = make_comb_mat(lt)
349 367
 # set_name(m)
350 368
 set_name = function(m) {
... ...
@@ -368,8 +386,8 @@ set_name = function(m) {
368 386
 # == example
369 387
 # set.seed(123)
370 388
 # lt = list(a = sample(letters, 10),
371
-# 	      b = sample(letters, 15),
372
-# 	      c = sample(letters, 20))
389
+#           b = sample(letters, 15),
390
+#           c = sample(letters, 20))
373 391
 # m = make_comb_mat(lt)
374 392
 # set_size(m)
375 393
 set_size = function(m) {
... ...
@@ -388,8 +406,8 @@ set_size = function(m) {
388 406
 # == example
389 407
 # set.seed(123)
390 408
 # lt = list(a = sample(letters, 10),
391
-# 	      b = sample(letters, 15),
392
-# 	      c = sample(letters, 20))
409
+#           b = sample(letters, 15),
410
+#           c = sample(letters, 20))
393 411
 # m = make_comb_mat(lt)
394 412
 # comb_size(m)
395 413
 comb_size = function(m) {
... ...
@@ -415,8 +433,8 @@ comb_size = function(m) {
415 433
 # == example
416 434
 # set.seed(123)
417 435
 # lt = list(a = sample(letters, 10),
418
-# 	      b = sample(letters, 15),
419
-# 	      c = sample(letters, 20))
436
+#           b = sample(letters, 15),
437
+#           c = sample(letters, 20))
420 438
 # m = make_comb_mat(lt)
421 439
 # comb_name(m)
422 440
 comb_name = function(m) {
... ...
@@ -443,8 +461,8 @@ comb_name = function(m) {
443 461
 # == example
444 462
 # set.seed(123)
445 463
 # lt = list(a = sample(letters, 10),
446
-# 	      b = sample(letters, 15),
447
-# 	      c = sample(letters, 20))
464
+#           b = sample(letters, 15),
465
+#           c = sample(letters, 20))
448 466
 # m = make_comb_mat(lt)
449 467
 # comb_degree(m)
450 468
 comb_degree = function(m) {
... ...
@@ -469,9 +487,8 @@ comb_degree = function(m) {
469 487
 # == example
470 488
 # set.seed(123)
471 489
 # lt = list(a = sample(letters, 10),
472
-# 	      b = sample(letters, 15),
473
-# 	      c = sample(letters, 20))
474
-#
490
+#           b = sample(letters, 15),
491
+#           c = sample(letters, 20))
475 492
 # m = make_comb_mat(lt)
476 493
 # extract_comb(m, "110")
477 494
 extract_comb = function(m, comb_name) {
... ...
@@ -510,6 +527,17 @@ extract_comb = function(m, comb_name) {
510 527
 		}
511 528
 	}
512 529
 	if(!is.null(lt)) {
530
+
531
+		if(inherits(lt[[1]], "GRanges")) {
532
+	    	union = getFromNamespace("union", ns = "BiocGenerics")
533
+	    	intersect = getFromNamespace("intersect", ns = "BiocGenerics")
534
+	    	setdiff = getFromNamespace("setdiff", ns = "BiocGenerics")
535
+	    } else if(inherits(lt[[1]], "IRanges")) {
536
+	    	union = getFromNamespace("union", ns = "BiocGenerics")
537
+	    	intersect = getFromNamespace("intersect", ns = "BiocGenerics")
538
+	    	setdiff = getFromNamespace("setdiff", ns = "BiocGenerics")
539
+	    }
540
+
513 541
 		do_comb = function(lt, mode, do = rep(TRUE, length(lt))) {
514 542
 	        set1_index = which(do)
515 543
 	        set2_index = which(!do)
... ...
@@ -550,8 +578,8 @@ extract_comb = function(m, comb_name) {
550 578
 # == example
551 579
 # set.seed(123)
552 580
 # lt = list(a = sample(letters, 10),
553
-# 	      b = sample(letters, 15),
554
-# 	      c = sample(letters, 20))
581
+#           b = sample(letters, 15),
582
+#           c = sample(letters, 20))
555 583
 # m = make_comb_mat(lt)
556 584
 # t(m)
557 585
 t.comb_mat = function(x) {
... ...
@@ -566,16 +594,18 @@ t.comb_mat = function(x) {
566 594
 # == param
567 595
 # -x A combination matrix returned by `make_comb_mat`.
568 596
 # -i Indices on rows.
569
-# -j Indices on columns
597
+# -j Indices on columns.
570 598
 # -drop It is always reset to ``FALSE`` internally.
571 599
 #
572 600
 # == details
573 601
 # If sets are on rows of the combination matrix, the row indices correspond
574
-# to sets and column indices correspond to combination sets and if sets are
602
+# to sets and column indices correspond to combination sets, and if sets are
575 603
 # on columns of the combination matrix, rows correspond to the combination sets.
576 604
 #
577
-# You should not subset by the sets. It will give you wrong set size. The subsetting
578
-# on rows are only used internally.
605
+# If the index is one-dimension, e.g. ``x[i]``, the index always corresponds to the combination sets.
606
+#
607
+# You should not subset by the sets. It will give you wrong combination set size. The subsetting
608
+# on sets are only used internally.
579 609
 #
580 610
 # This subsetting method is mainly for subsetting combination sets, i.e., users
581 611
 # can first use `comb_size` to get the size of each combination set, and filter them
... ...
@@ -584,11 +614,12 @@ t.comb_mat = function(x) {
584 614
 # == example
585 615
 # set.seed(123)
586 616
 # lt = list(a = sample(letters, 10),
587
-# 	      b = sample(letters, 15),
588
-# 	      c = sample(letters, 20))
617
+#           b = sample(letters, 15),
618
+#           c = sample(letters, 20))
589 619
 # m = make_comb_mat(lt)
590 620
 # m2 = m[, comb_size(m) >= 3]
591 621
 # comb_size(m2)
622
+# m[comb_size(m) >= 3]
592 623
 "[.comb_mat" = function(x, i, j, drop = FALSE) {
593 624
 	set_size = attr(x, "set_size")
594 625
 	comb_size = attr(x, "comb_size")
... ...
@@ -596,12 +627,11 @@ t.comb_mat = function(x) {
596 627
 	mode = attr(x, "mode")
597 628
 
598 629
 	class(x) = "matrix"
599
-
600 630
 	if(set_on_rows) {
601 631
 		if(nargs() == 2) {
602
-			return(x[i])
603
-		}
604
-		if(missing(i)) {
632
+			x2 = x[, i, drop = FALSE]
633
+			comb_size = comb_size[i]
634
+		} else if(missing(i)) {
605 635
 			x2 = x[, j, drop = FALSE]
606 636
 			comb_size = comb_size[j]
607 637
 		} else if(missing(j)) {
... ...
@@ -614,9 +644,9 @@ t.comb_mat = function(x) {
614 644
 		}
615 645
 	} else {
616 646
 		if(nargs() == 2) {
617
-			return(x[i])
618
-		}
619
-		if(missing(i)) {
647
+			x2 = x[i, , drop = FALSE]
648
+			comb_size = comb_size[i]
649
+		} else if(missing(i)) {
620 650
 			x2 = x[, j, drop = FALSE]
621 651
 			set_size = set_size[j]
622 652
 		} else if(missing(j)) {
... ...
@@ -633,6 +663,8 @@ t.comb_mat = function(x) {
633 663
 	attr(x2, "comb_size") = comb_size
634 664
 	attr(x2, "mode") = mode
635 665
 	attr(x2, "set_on_rows") = set_on_rows
666
+	attr(x2, "x") = attr(x, "x")
667
+	attr(x2, "lt") = attr(x, "lt")
636 668
 	class(x2) = c("comb_mat", "matrix")
637 669
 	return(x2)
638 670
 }
... ...
@@ -666,21 +698,25 @@ print.comb_mat = function(x, ...) {
666 698
 # == param
667 699
 # -m A combination matrix returned by `make_comb_mat`. The matrix can be transposed to switch
668 700
 #    the position of sets and combination sets.
701
+# -comb_col The color for the dots representing combination sets.
702
+# -pt_size The point size for the dots representing combination sets.
703
+# -lwd The line width for the combination sets.
669 704
 # -set_order The order of sets.
670 705
 # -comb_order The order of combination sets.
671 706
 # -top_annotation A `HeatmapAnnotation` object on top of the combination matrix.
672 707
 # -right_annotation A `HeatmapAnnotation` object on the right of the combination matrix.
708
+# -row_names_side The side of row names.
673 709
 # -... Other arguments passed to `Heatmap`.
674 710
 #
675 711
 # == details
676
-# BY default, the sets are on rows and combination sets are on columns. The positions of the
712
+# By default, the sets are on rows and combination sets are on columns. The positions of the
677 713
 # two types of sets can be switched by transposing the matrix.
678 714
 #
679 715
 # When sets are on rows, the default top annotation is the barplot showing the size of each
680 716
 # combination sets and the default right annotation is the barplot showing the size of the sets.
681 717
 # The annotations are simply constructed by `HeatmapAnnotation` and `anno_barplot` with some
682
-# parameters pre-set. Users can check the source code of `default_upset_top_annotation` and
683
-# `default_upset_right_annotation` to find out how the annotations are defined.
718
+# parameters pre-set. Users can check the source code of `upset_top_annotation` and
719
+# `upset_right_annotation` to find out how the annotations are defined.
684 720
 #
685 721
 # To change or to add annotations, users just need to define a new `HeatmapAnnotation` object.
686 722
 # E.g. if we want to change the side of the axis and name on top annotation:
... ...
@@ -723,19 +759,51 @@ print.comb_mat = function(x, ...) {
723 759
 # == example
724 760
 # set.seed(123)
725 761
 # lt = list(a = sample(letters, 10),
726
-# 	      b = sample(letters, 15),
727
-# 	      c = sample(letters, 20))
762
+#           b = sample(letters, 15),
763
+#           c = sample(letters, 20))
728 764
 # m = make_comb_mat(lt)
729 765
 # UpSet(m)
730 766
 # UpSet(t(m))
731 767
 # 
732 768
 # m = make_comb_mat(lt, mode = "union")
733 769
 # UpSet(m)
770
+# UpSet(m, comb_col = c(rep(2, 3), rep(3, 3), 1))
734 771
 #
735
-UpSet = function(m, set_order = order(set_size(m), decreasing = TRUE), 
736
-	comb_order = order(comb_size(m), decreasing = TRUE), 
737
-	top_annotation = default_upset_top_annotation(m),
738
-	right_annotation = default_upset_right_annotation(m),
772
+#
773
+# # compare two UpSet plots
774
+# set.seed(123)
775
+# lt1 = list(a = sample(letters, 10),
776
+#           b = sample(letters, 15),
777
+#           c = sample(letters, 20))
778
+# m1 = make_comb_mat(lt1)
779
+# set.seed(456)
780
+# lt2 = list(a = sample(letters, 10),
781
+#           b = sample(letters, 15),
782
+#           c = sample(letters, 20))
783
+# m2 = make_comb_mat(lt2)
784
+#
785
+# max1 = max(c(set_size(m1), set_size(m2)))
786
+# max2 = max(c(comb_size(m1), comb_size(m2)))
787
+#
788
+# UpSet(m1, top_annotation = upset_top_annotation(m1, ylim = c(0, max2)),
789
+#     right_annotation = upset_right_annotation(m1, ylim = c(0, max1)),
790
+#     column_title = "UpSet1") +
791
+# UpSet(m2, top_annotation = upset_top_annotation(m2, ylim = c(0, max2)),
792
+#     right_annotation = upset_right_annotation(m2, ylim = c(0, max1)),
793
+#     column_title = "UpSet2")
794
+#
795
+UpSet = function(m, 
796
+	comb_col = "black",
797
+	pt_size = unit(3, "mm"), lwd = 2,
798
+	set_order = order(set_size(m), decreasing = TRUE), 
799
+	comb_order = if(attr(m, "set_on_rows")) {
800
+			order.comb_mat(m[set_order, ], decreasing = TRUE)
801
+		} else {
802
+			order.comb_mat(m[, set_order], decreasing = TRUE)
803
+		},
804
+	top_annotation = upset_top_annotation(m),
805
+	right_annotation = upset_right_annotation(m),
806
+	row_names_side = "left",
739 807
 	...) {
740 808
 
741 809
 	set_on_rows = attr(m, "set_on_rows")
... ...
@@ -745,7 +813,13 @@ UpSet = function(m, set_order = order(set_size(m), decreasing = TRUE),
745 813
 	
746 814
 	class(m2) = "matrix"
747 815
 
816
+	pt_size = pt_size
817
+	lwd = lwd
818
+
748 819
 	if(set_on_rows) {
820
+		n_comb = ncol(m)
821
+		if(length(comb_col == 1)) comb_col = rep(comb_col, n_comb)
822
+
749 823
 		layer_fun = function(j, i, x, y, w, h, fill) {
750 824
 			nr = round(1/as.numeric(h[1]))
751 825
 			nc = round(1/as.numeric(w[1]))
... ...
@@ -755,22 +829,41 @@ UpSet = function(m, set_order = order(set_size(m), decreasing = TRUE),
755 829
 					grid.rect(y = k/nr, height = 1/nr, just = "top", gp = gpar(fill = "#F0F0F0", col = NA))
756 830
 				}
757 831
 			}
758
-			grid.points(x, y, size = unit(3, "mm"), pch = 16, gp = gpar(col = ifelse(pindex(m2, i, j), "black", "#CCCCCC")))
832
+			grid.points(x, y, size = pt_size, pch = 16, gp = gpar(col = ifelse(pindex(m2, i, j), comb_col[j], "#CCCCCC")))
833
+			jj = unique(j)
759 834
 			for(k in seq_len(nc)) {
760 835
 		        if(sum(subm[, k]) >= 2) {
761 836
 		            i_min = min(which(subm[, k] > 0))
762 837
 		            i_max = max(which(subm[, k] > 0))
763
-		            grid.lines(c(k - 0.5, k - 0.5)/nc, (nr - c(i_min, i_max) + 0.5)/nr, gp = gpar(col = "black", lwd = 2))
838
+		            grid.lines(c(k - 0.5, k - 0.5)/nc, (nr - c(i_min, i_max) + 0.5)/nr, gp = gpar(col = comb_col[jj[k]], lwd = lwd))
764 839
 		        }
765 840
 		    }
766 841
 		}
842
+
843
+		# check top annotation
844
+		# if it is specified by upset_top_annotation and gp(col) is not set
845
+		ra = top_annotation
846
+		if(length(ra) == 1) {
847
+			ta_call = substitute(top_annotation)
848
+			ta_call = as.list(ta_call)
849
+			if(as.character(ta_call[[1]]) == "upset_top_annotation") {
850
+				if(!"gp" %in% names(as.list(ta_call))) {
851
+					ra@anno_list[[1]]@fun@var_env$gp$fill = comb_col
852
+					ra@anno_list[[1]]@fun@var_env$gp$col = comb_col
853
+				}
854
+			}
855
+		}
856
+
767 857
 		ht = Heatmap(m2, cluster_rows = FALSE, cluster_columns = FALSE, rect_gp = gpar(type = "none"),
768 858
 			layer_fun = layer_fun, show_heatmap_legend = FALSE,
769
-			top_annotation = top_annotation,
859
+			top_annotation = ra,
770 860
 			right_annotation = right_annotation,
771
-			row_names_side = "left",
861
+			row_names_side = row_names_side,
772 862
 			row_order = set_order, column_order = comb_order, ...)
773 863
 	} else {
864
+		n_comb = nrow(m)
865
+		if(length(comb_col == 1)) comb_col = rep(comb_col, n_comb)
866
+
774 867
 		layer_fun = function(j, i, x, y, w, h, fill) {
775 868
 			nr = round(1/as.numeric(h[1]))
776 869
 			nc = round(1/as.numeric(w[1]))
... ...
@@ -780,53 +873,121 @@ UpSet = function(m, set_order = order(set_size(m), decreasing = TRUE),
780 873
 					grid.rect(x = k/nc, width = 1/nc, just = "right", gp = gpar(fill = "#F0F0F0", col = NA))
781 874
 				}
782 875
 			}
783
-			grid.points(x, y, size = unit(3, "mm"), pch = 16, gp = gpar(col = ifelse(pindex(m2, i, j), "black", "#CCCCCC")))
876
+			grid.points(x, y, size = pt_size, pch = 16, gp = gpar(col = ifelse(pindex(m2, i, j), comb_col[i], "#CCCCCC")))
877
+			ii = unique(i)
784 878
 			for(k in seq_len(nr)) {
785 879
 		        if(sum(subm[k, ]) >= 2) {
786 880
 		            i_min = min(which(subm[k, ] > 0))
787 881
 		            i_max = max(which(subm[k, ] > 0))
788
-		            grid.lines((c(i_min, i_max) - 0.5)/nc, (nr - c(k ,k) + 0.5)/nr, gp = gpar(col = "black", lwd = 2))
882
+		            grid.lines((c(i_min, i_max) - 0.5)/nc, (nr - c(k ,k) + 0.5)/nr, gp = gpar(col = comb_col[ii[k]], lwd = lwd))
789 883
 		        }
790 884
 		    }
791 885
 		}
886
+
887
+		ra = right_annotation
888
+		if(length(ra) == 1) {
889
+			ta_call = substitute(top_annotation)
890
+			ta_call = as.list(ta_call)
891
+			if(as.character(ta_call[[1]]) == "upset_right_annotation") {
892
+				if(!"gp" %in% names(as.list(ta_call))) {
893
+					ra@anno_list[[1]]@fun@var_env$gp$fill = comb_col
894
+					ra@anno_list[[1]]@fun@var_env$gp$col = comb_col
895
+				}
896
+			}
897
+		}
792 898
 		ht = Heatmap(m2, cluster_rows = FALSE, cluster_columns = FALSE, rect_gp = gpar(type = "none"),
793 899
 			layer_fun = layer_fun, show_heatmap_legend = FALSE,
794 900
 			top_annotation = top_annotation,
795
-			right_annotation = right_annotation,
901
+			right_annotation = ra,
796 902
 			row_order = comb_order, column_order = set_order, ...)
797 903
 	}
798 904
 	ht
799 905
 }
800 906
 
907
+# == title
908
+# Order of the Combination Sets
909
+#
910
+# == param
911
+# -m A combination matrix returned by `make_comb_mat`.
912
+# -on On sets or on combination sets?
913
+# -decreasing Whether the ordering is applied decreasingly.
914
+#
915
+# == details
916
+# It first sorts by the degree of the combination sets then
917
+# by the combination matrix.
918
+#
919
+order.comb_mat = function(m, decreasing = TRUE, on = "comb_set") {
920
+	if(on == "set") {
921
+		return(order(set_size(m), decreasing = decreasing))
922
+	} else {
923
+		set_on_rows = attr(m, "set_on_rows")
924
+		if(set_on_rows) {
925
+			lt = list(comb_degree(m))
926
+			lt = c(lt, as.list(as.data.frame(t(m))))
927
+			lt$decreasing = decreasing
928
+			do.call(order, lt)
929
+		} else {
930
+			lt = list(comb_degree(m))
931
+			lt = c(lt, as.list(as.data.frame(m)))
932
+			lt$decreasing = decreasing
933
+			do.call(order, lt)
934
+		}
935
+	}
936
+}
937
+
801 938
 # == title
802 939
 # Default UpSet Top Annotation
803 940
 #
804 941
 # == param
805 942
 # -m A combination matrix which is as same as the one for `UpSet`.
943
+# -gp Graphic parameters for bars.
944
+# -height The height of the top annotation.
945
+# -show_annotation_name Whether show annotation names?
946
+# -annotation_name_gp Graphic parameters for anntation names.
947
+# -annotation_name_offset Offset to the annotation name, a `grid::unit` object.
948
+# -annotation_name_side Side of the annotation name.
949
+# -annotation_name_rot Rotation of the annotation name, it can only take values in ``c(00, 90, 180, 270)``.
950
+# -... Passed to `anno_barplot`.
806 951
 #
807 952
 # == details
808 953
 # The default top annotation is actually barplot implemented by `anno_barplot`. For
809 954
 # how to set the top annotation or bottom annotation in `UpSet`, please refer to `UpSet`.
810 955
 #
811
-default_upset_top_annotation = function(m) {
956
+upset_top_annotation = function(m, 
957
+	gp = gpar(fill = "black"), 
958
+	height = unit(ifelse(set_on_rows, 2, 3), "cm"),
959
+	show_annotation_name = TRUE,
960
+	annotation_name_gp = gpar(),
961
+	annotation_name_offset = NULL,
962
+	annotation_name_side = "left",
963
+	annotation_name_rot = 0,
964
+	...) {
812 965
 	set_on_rows = attr(m, "set_on_rows")
813 966
 	
814 967
 	if(set_on_rows) {
815
-		ha = HeatmapAnnotation("Intersection size" = anno_barplot(comb_size(m), 
816
-				border = FALSE, gp = gpar(fill = "black"), height = unit(2, "cm")), 
817
-			annotation_name_side = "left", annotation_name_rot = 0)
968
+		ha = HeatmapAnnotation("Intersection\nsize" = anno_barplot(comb_size(m), 
969
+					border = FALSE, gp = gp, height = height, ...), 
970
+				show_annotation_name = show_annotation_name,
971
+				annotation_name_gp = annotation_name_gp,
972
+				annotation_name_offset = annotation_name_offset,
973
+				annotation_name_side = annotation_name_side,
974
+				annotation_name_rot = annotation_name_rot)
818 975
 	} else {
819
-		ha = HeatmapAnnotation("Set size" = anno_barplot(set_size(m), border = FALSE, 
820
-				gp = gpar(fill = "black"), height = unit(3, "cm")),
821
-			annotation_name_side = "left", annotation_name_rot = 0)
976
+		ha = HeatmapAnnotation("Set\nsize" = anno_barplot(set_size(m), border = FALSE, 
977
+					gp = gp, height = height, ...),
978
+				show_annotation_name = show_annotation_name,
979
+				annotation_name_gp = annotation_name_gp,
980
+				annotation_name_offset = annotation_name_offset,
981
+				annotation_name_side = annotation_name_side,
982
+				annotation_name_rot = annotation_name_rot)
822 983
 	}
823 984
 
824 985
 	mode = attr(m, "mode")
825 986
 	if(set_on_rows) {
826 987
 		if(mode %in% c("distinct", "intersect")) {
827
-			names(ha) = "Intersection size"
988
+			names(ha) = "Intersection\nsize"
828 989
 		} else {
829
-			names(ha) = "Union size"
990
+			names(ha) = "Union\nsize"
830 991
 		}
831 992
 	}
832 993
 	return(ha)
... ...
@@ -837,29 +998,131 @@ default_upset_top_annotation = function(m) {
837 998
 #
838 999
 # == param
839 1000
 # -m A combination matrix which is as same as the one for `UpSet`.
1001
+# -gp Graphic parameters for bars.
1002
+# -width Width of the right annotation.
1003
+# -show_annotation_name Whether show annotation names?
1004
+# -annotation_name_gp Graphic parameters for anntation names.
1005
+# -annotation_name_offset Offset to the annotation name, a `grid::unit` object.
1006
+# -annotation_name_side Side of the annotation name.
1007
+# -annotation_name_rot Rotation of the annotation name, it can only take values in ``c(00, 90, 180, 270)``.
1008
+# -... Passed to `anno_barplot`.
840 1009
 #
841 1010
 # == details
842 1011
 # The default right annotation is actually barplot implemented by `anno_barplot`. For
843 1012
 # how to set the right annotation or left annotation in `UpSet`, please refer to `UpSet`.
844 1013
 #
845
-default_upset_right_annotation = function(m) {
1014
+upset_right_annotation = function(m,
1015
+	gp = gpar(fill = "black"),  
1016
+	width = unit(ifelse(set_on_rows, 3, 2), "cm"),
1017
+	show_annotation_name = TRUE,
1018
+	annotation_name_gp = gpar(),
1019
+	annotation_name_offset = NULL,
1020
+	annotation_name_side = "bottom",
1021
+	annotation_name_rot = NULL,
1022
+	...) {
846 1023
 	set_on_rows = attr(m, "set_on_rows")
847 1024
 
848 1025
 	if(set_on_rows) {
849 1026
 		ha = rowAnnotation("Set size" = anno_barplot(set_size(m), border = FALSE, 
850
-					gp = gpar(fill = "black"), width = unit(3, "cm")))
1027
+					gp = gp, width = width, ...),
1028
+				show_annotation_name = show_annotation_name,
1029
+				annotation_name_gp = annotation_name_gp,
1030
+				annotation_name_offset = annotation_name_offset,
1031
+				annotation_name_side = annotation_name_side,
1032
+				annotation_name_rot = annotation_name_rot)
851 1033
 	} else {
852
-		ha = rowAnnotation("Intersection size" = anno_barplot(comb_size(m), 
853
-				border = FALSE, gp = gpar(fill = "black"), width = unit(2, "cm")))
1034
+		ha = rowAnnotation("Intersection\nsize" = anno_barplot(comb_size(m), 
1035
+					border = FALSE, gp = gp, width = width, ...),
1036
+				show_annotation_name = show_annotation_name,
1037
+				annotation_name_gp = annotation_name_gp,
1038
+				annotation_name_offset = annotation_name_offset,
1039
+				annotation_name_side = annotation_name_side,
1040
+				annotation_name_rot = annotation_name_rot)
854 1041
 	}
855 1042
 
856 1043
 	mode = attr(m, "mode")
857 1044
 	if(!set_on_rows) {
858 1045
 		if(mode %in% c("distinct", "intersect")) {
859
-			names(ha) = "Intersection size"
1046
+			names(ha) = "Intersection\nsize"
860 1047
 		} else {
861 1048
 			names(ha) = "Union size"
862 1049
 		}
863 1050
 	}
864 1051
 	return(ha)
865 1052
 }
1053
+
1054
+# == title
1055
+# Normalize a list of combination matrice
1056
+#
1057
+# == param
1058
+# -... If it is a single argument, the value should be a list of combination matrices.
1059
+#
1060
+# == details
1061
+# It normalizes a list of combination matrice to make them have same number and order of sets and combination sets.
1062
+#
1063
+# The sets (by `set_name`) from all combination matrice should be the same.
1064
+#
1065
+normalize_comb_mat = function(...) {
1066
+	lt = list(...)
1067
+	if(length(lt) == 1) {
1068
+		if(!is.list(lt)) {
1069
+			stop_wrap("If you only specify one argument, it must be a list of comb_mat objects.")
1070
+		}
1071
+		lt = lt[[1]]
1072
+	}
1073
+
1074
+	n = length(lt)
1075
+	if(n == 1) {
1076
+		stop_wrap("There should be at least two combination matrices.")
1077
+	}
1078
+
1079
+	set1 = set_name(lt[[1]])
1080
+	for(i in 2:n) {
1081
+		if(!setequal(set1, set_name(lt[[i]]))) {
1082
+			stop_wrap("The sets of all combination matrices should be identical.")
1083
+		}
1084
+	}
1085
+	all_set_name = set_name(lt[[1]])
1086
+	n_set = length(all_set_name)
1087
+
1088
+	all_comb_size = lapply(lt, function(x) {
1089
+		set_on_rows = attr(x, "set_on_rows")
1090
+		if(set_on_rows) {
1091
+			code = apply(x, 2, binaryToInt)
1092
+		} else {
1093
+			code = apply(x, 1, binaryToInt)
1094
+		}
1095
+		structure(comb_size(x), names = code)
1096
+	})
1097
+
1098
+	all_code = unique(unlist(lapply(all_comb_size, names)))
1099
+	comb_mat = do.call(cbind, lapply(as.numeric(all_code), intToBinary, len = n_set))
1100
+	rownames(comb_mat) = all_set_name
1101
+
1102
+	all_comb_size = lapply(all_comb_size, function(x) {
1103
+		x2 = structure(rep(0, length(all_code)), names = all_code)
1104
+		x2[names(x)] = x
1105
+		x2
1106
+	})
1107
+
1108
+	for(i in seq_along(lt)) {
1109
+		x = lt[[i]]
1110
+		set_on_rows = attr(x, "set_on_rows")
1111
+		attr = attributes(x)
1112
+		attr = attr[!names(attr) %in% c("dim", "dimname")]
1113
+
1114
+		if(set_on_rows) {
1115
+			x2 = comb_mat
1116
+		} else {
1117
+			x2 = t(comb_mat)
1118
+		}
1119
+		for(nm in names(attr)) {
1120
+			attr(x2, nm) = attr[[nm]]
1121
+		}
1122
+		attr(x2, "set_size") = attr$set_size[all_set_name]
1123
+		attr(x2, "comb_size") = all_comb_size[[i]]
1124
+		
1125
+		lt[[i]] = x2
1126
+	}
1127
+	return(lt)
1128
+}
... ...
@@ -14,17 +14,19 @@ Subset the Combination Matrix
14 14
 
15 15
   \item{x}{A combination matrix returned by \code{\link{make_comb_mat}}.}
16 16
   \item{i}{Indices on rows.}
17
-  \item{j}{Indices on columns}
17
+  \item{j}{Indices on columns.}
18 18
   \item{drop}{It is always reset to \code{FALSE} internally.}
19 19
 
20 20
 }
21 21
 \details{
22 22
 If sets are on rows of the combination matrix, the row indices correspond
23
-to sets and column indices correspond to combination sets and if sets are
23
+to sets and column indices correspond to combination sets, and if sets are
24 24
 on columns of the combination matrix, rows correspond to the combination sets.
25 25
 
26
-You should not subset by the sets. It will give you wrong set size. The subsetting
27
-on rows are only used internally.
26
+If the index is one-dimension, e.g. \code{x[i]}, the index always corresponds to the combination sets.
27
+
28
+You should not subset by the sets. It will give you wrong combination set size. The subsetting
29
+on sets are only used internally.
28 30
 
29 31
 This subsetting method is mainly for subsetting combination sets, i.e., users
30 32
 can first use \code{\link{comb_size}} to get the size of each combination set, and filter them
... ...
@@ -33,9 +35,10 @@ by the size.
33 35
 \examples{
34 36
 set.seed(123)
35 37
 lt = list(a = sample(letters, 10),
36
-	      b = sample(letters, 15),
37
-	      c = sample(letters, 20))
38
+          b = sample(letters, 15),
39
+          c = sample(letters, 20))
38 40
 m = make_comb_mat(lt)
39 41
 m2 = m[, comb_size(m) >= 3]
40 42
 comb_size(m2)
43
+m[comb_size(m) >= 3]
41 44
 }
... ...
@@ -44,7 +44,7 @@ HeatmapAnnotation(...,
44 44
   \item{gap}{Gap between annotations. It can be a single value or a vector of \code{\link[grid]{unit}} objects.}
45 45
   \item{show_annotation_name}{Whether show annotation names? For column annotation, annotation names are drawn either on the left or the right, and for row annotations, names are draw either on top or at the bottom. The value can be a vector.}
46 46
   \item{annotation_name_gp}{Graphic parameters for anntation names. Graphic paramters can be vectors.}
47
-  \item{annotation_name_offset}{Offset to the annotations, \code{\link[grid]{unit}} object. The value can be a vector.}
47
+  \item{annotation_name_offset}{Offset to the annotation names, a \code{\link[grid]{unit}} object. The value can be a vector.}
48 48
   \item{annotation_name_side}{Side of the annotation names.}
49 49
   \item{annotation_name_rot}{Rotation of the annotation names, it can only take values in \code{c(00, 90, 180, 270)}. The value can be a vector.}
50 50
   \item{annotation_height}{Height of each annotation if annotations are column annotations.}
... ...
@@ -7,31 +7,43 @@ Make the UpSet plot
7 7
 Make the UpSet plot
8 8
 }
9 9
 \usage{
10
-UpSet(m, set_order = order(set_size(m), decreasing = TRUE),
11
-    comb_order = order(comb_size(m), decreasing = TRUE),
12
-    top_annotation = default_upset_top_annotation(m),
13
-    right_annotation = default_upset_right_annotation(m),
10
+UpSet(m,
11
+    comb_col = "black",
12
+    pt_size = unit(3, "mm"), lwd = 2,
13
+    set_order = order(set_size(m), decreasing = TRUE),
14
+    comb_order = if(attr(m, "set_on_rows")) {
15
+    order.comb_mat(m[set_order, ], decreasing = TRUE)
16
+    } else {
17
+    order.comb_mat(m[, set_order], decreasing = TRUE)
18
+    },
19
+    top_annotation = upset_top_annotation(m),
20
+    right_annotation = upset_right_annotation(m),
21
+    row_names_side = "left",
14 22
     ...)
15 23
 }
16 24
 \arguments{
17 25
 
18 26
   \item{m}{A combination matrix returned by \code{\link{make_comb_mat}}. The matrix can be transposed to switch the position of sets and combination sets.}
27
+  \item{comb_col}{The color for the dots representing combination sets.}
28
+  \item{pt_size}{The point size for the dots representing combination sets.}
29
+  \item{lwd}{The line width for the combination sets.}
19 30
   \item{set_order}{The order of sets.}
20 31
   \item{comb_order}{The order of combination sets.}
21 32
   \item{top_annotation}{A \code{\link{HeatmapAnnotation}} object on top of the combination matrix.}
22 33
   \item{right_annotation}{A \code{\link{HeatmapAnnotation}} object on the right of the combination matrix.}
34
+  \item{row_names_side}{The side of row names.}
23 35
   \item{...}{Other arguments passed to \code{\link{Heatmap}}.}
24 36
 
25 37
 }
26 38
 \details{
27
-BY default, the sets are on rows and combination sets are on columns. The positions of the
39
+By default, the sets are on rows and combination sets are on columns. The positions of the
28 40
 two types of sets can be switched by transposing the matrix.
29 41
 
30 42
 When sets are on rows, the default top annotation is the barplot showing the size of each
31 43
 combination sets and the default right annotation is the barplot showing the size of the sets.
32 44
 The annotations are simply constructed by \code{\link{HeatmapAnnotation}} and \code{\link{anno_barplot}} with some
33
-parameters pre-set. Users can check the source code of \code{\link{default_upset_top_annotation}} and
34
-\code{\link{default_upset_right_annotation}} to find out how the annotations are defined.
45
+parameters pre-set. Users can check the source code of \code{\link{upset_top_annotation}} and
46
+\code{\link{upset_right_annotation}} to find out how the annotations are defined.
35 47
 
36 48
 To change or to add annotations, users just need to define a new \code{\link{HeatmapAnnotation}} object.
37 49
 E.g. if we want to change the side of the axis and name on top annotation:
... ...
@@ -76,12 +88,35 @@ by \code{+} or \code{\link[=pct_v_pct]{\%v\%}}.
76 88
 \examples{
77 89
 set.seed(123)
78 90
 lt = list(a = sample(letters, 10),
79
-	      b = sample(letters, 15),
80
-	      c = sample(letters, 20))
91
+          b = sample(letters, 15),
92
+          c = sample(letters, 20))
81 93
 m = make_comb_mat(lt)
82 94
 UpSet(m)
83 95
 UpSet(t(m))
84 96
 
85 97
 m = make_comb_mat(lt, mode = "union")
86 98
 UpSet(m)
99
+UpSet(m, comb_col = c(rep(2, 3), rep(3, 3), 1))
100
+
101
+# compare two UpSet plots
102
+set.seed(123)
103
+lt1 = list(a = sample(letters, 10),
104
+          b = sample(letters, 15),
105
+          c = sample(letters, 20))
106
+m1 = make_comb_mat(lt1)
107
+set.seed(456)
108
+lt2 = list(a = sample(letters, 10),
109
+          b = sample(letters, 15),
110
+          c = sample(letters, 20))
111
+m2 = make_comb_mat(lt2)
112
+
113
+max1 = max(c(set_size(m1), set_size(m2)))
114
+max2 = max(c(comb_size(m1), comb_size(m2)))
115
+
116
+UpSet(m1, top_annotation = upset_top_annotation(m1, ylim = c(0, max2)),
117
+    right_annotation = upset_right_annotation(m1, ylim = c(0, max1)),
118
+    column_title = "UpSet1") +
119
+UpSet(m2, top_annotation = upset_top_annotation(m2, ylim = c(0, max2)),
120
+    right_annotation = upset_right_annotation(m2, ylim = c(0, max1)),
121
+    column_title = "UpSet2")
87 122
 }
... ...
@@ -23,8 +23,8 @@ A vector of degrees of the combination sets.
23 23
 \examples{
24 24
 set.seed(123)
25 25
 lt = list(a = sample(letters, 10),
26
-	      b = sample(letters, 15),
27
-	      c = sample(letters, 20))
26
+          b = sample(letters, 15),
27
+          c = sample(letters, 20))
28 28
 m = make_comb_mat(lt)
29 29
 comb_degree(m)
30 30
 }
... ...
@@ -27,8 +27,8 @@ A vector of names of the combination sets.
27 27
 \examples{
28 28
 set.seed(123)
29 29
 lt = list(a = sample(letters, 10),
30
-	      b = sample(letters, 15),
31
-	      c = sample(letters, 20))
30
+          b = sample(letters, 15),
31
+          c = sample(letters, 20))
32 32
 m = make_comb_mat(lt)
33 33
 comb_name(m)
34 34
 }
... ...
@@ -20,8 +20,8 @@ A vector of sizes of the combination sets.
20 20
 \examples{
21 21
 set.seed(123)
22 22
 lt = list(a = sample(letters, 10),
23
-	      b = sample(letters, 15),
24
-	      c = sample(letters, 20))
23
+          b = sample(letters, 15),
24
+          c = sample(letters, 20))
25 25
 m = make_comb_mat(lt)
26 26
 comb_size(m)
27 27
 }
28 28
deleted file mode 100644
... ...
@@ -1,25 +0,0 @@
1
-\name{default_upset_right_annotation}
2
-\alias{default_upset_right_annotation}
3
-\title{
4
-Default UpSet Right Annotation
5
-}
6
-\description{
7
-Default UpSet Right Annotation
8
-}
9
-\usage{
10
-default_upset_right_annotation(m)
11
-}
12
-\arguments{
13
-
14
-  \item{m}{A combination matrix which is as same as the one for \code{\link{UpSet}}.}
15
-
16
-}
17
-\details{
18
-The default right annotation is actually barplot implemented by \code{\link{anno_barplot}}. For
19
-how to set the right annotation or left annotation in \code{\link{UpSet}}, please refer to \code{\link{UpSet}}.
20
-}
21
-\examples{
22
-# There is no example
23
-NULL
24
-
25
-}
26 0
deleted file mode 100644
... ...
@@ -1,25 +0,0 @@
1
-\name{default_upset_top_annotation}
2
-\alias{default_upset_top_annotation}
3
-\title{
4
-Default UpSet Top Annotation
5
-}
6
-\description{
7
-Default UpSet Top Annotation
8
-}
9
-\usage{
10
-default_upset_top_annotation(m)
11
-}
12
-\arguments{
13
-
14
-  \item{m}{A combination matrix which is as same as the one for \code{\link{UpSet}}.}
15
-
16
-}
17
-\details{
18
-The default top annotation is actually barplot implemented by \code{\link{anno_barplot}}. For
19
-how to set the top annotation or bottom annotation in \code{\link{UpSet}}, please refer to \code{\link{UpSet}}.
20
-}
21
-\examples{
22
-# There is no example
23
-NULL
24
-
25
-}
... ...
@@ -21,9 +21,8 @@ It returns the combination set.
21 21
 \examples{
22 22
 set.seed(123)
23 23
 lt = list(a = sample(letters, 10),
24
-	      b = sample(letters, 15),
25
-	      c = sample(letters, 20))
26
-
24
+          b = sample(letters, 15),
25
+          c = sample(letters, 20))
27 26
 m = make_comb_mat(lt)
28 27
 extract_comb(m, "110")
29 28
 }
... ...
@@ -21,7 +21,7 @@ where n is the number of union of all sets in the list.
21 21
 \examples{
22 22
 set.seed(123)
23 23
 lt = list(a = sample(letters, 10),
24
-	      b = sample(letters, 15),
25
-	      c = sample(letters, 20))
24
+          b = sample(letters, 15),
25
+          c = sample(letters, 20))
26 26
 list_to_matrix(lt)
27 27
 }
... ...
@@ -1,10 +1,10 @@
1 1
 \name{make_comb_mat}
2 2
 \alias{make_comb_mat}
3 3
 \title{
4
-Make a Combination matrix for UpSet Plot
4
+Make a Combination Matrix for UpSet Plot
5 5
 }
6 6
 \description{
7
-Make a Combination matrix for UpSet Plot
7
+Make a Combination Matrix for UpSet Plot
8 8
 }
9 9
 \usage{
10 10
 make_comb_mat(..., mode = c("distinct", "intersect", "union"),
... ...
@@ -16,7 +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{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.}
19
+  \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 20
 
21 21
 }
22 22
 \section{Input}{
... ...
@@ -37,7 +37,8 @@ To represent multiple sets, the variable can be represented as:
37 37
     t 1 0 1
38 38
     j 1 0 0
39 39
     u 1 0 1
40
-    w 1 0 0  }
40
+    w 1 0 0
41
+    ...  }
41 42
 
42 43
 If the variable is a data frame, the binary columns (only contain 0 and 1) and the logical
43 44
 columns are only kept.
... ...
@@ -64,25 +65,28 @@ size is not of interest here. With the code of selecting and not selecting the s
64 65
 we need to define how to calculate the size of that combination set. There are three modes:
65 66
 
66 67
 1. \code{distinct} mode: 1 means in that set and 0 means not in that set, then "1 1 0" means a
67
-set of elements also in set A and B, while not in C (\code{setdiff(intersect(A, B), C)}). Under
68
+set of elements also in set A and B, while not in C (i.e. \code{setdiff(intersect(A, B), C)}). Under
68 69
 this mode, the seven combination sets are the seven partitions in the Venn diagram and they
69 70
 are mutually exclusive.
70 71
 
71 72
 2. \code{intersect} mode: 1 means in that set and 0 is not taken into account, then, "1 1 0" means
72
-a set of elements in set A and B, and they can also in C or not in C (\code{intersect(A, B)}).
73
-Under this mode, the seven combinatio sets can overlap.
73
+a set of elements in set A and B, and they can also in C or not in C (i.e. \code{intersect(A, B)}).
74
+Under this mode, the seven combination sets can overlap.
74 75
 
75 76
 3. \code{union} mode: 1 means in that set and 0 is not taken into account. When there are multiple
76
-1, the relationship is OR. Then, "1 1 0" means a set of elements in set A or B, and they can also in C or not in C (\code{union(A, B)}).
77
-Under this mode, the seven combinatio sets can overlap.}
77
+1, the relationship is OR. Then, "1 1 0" means a set of elements in set A or B, and they can also in C or not in C (i.e. \code{union(A, B)}).
78
+Under this mode, the seven combination sets can overlap.}
78 79
 \value{
79 80
 A matrix also in a class of \code{comb_mat}.
81
+
82
+Following functions can be applied to it: \code{\link{set_name}}, \code{\link{comb_name}}, \code{\link{set_size}}, \code{\link{comb_size}}, \code{\link{comb_degree}},
83
+\code{\link{extract_comb}} and \code{\link{t.comb_mat}}.
80 84
 }
81 85
 \examples{
82 86
 set.seed(123)
83 87
 lt = list(a = sample(letters, 10),
84
-	      b = sample(letters, 15),
85
-	      c = sample(letters, 20))
88
+          b = sample(letters, 15),
89
+          c = sample(letters, 20))
86 90
 m = make_comb_mat(lt)
87 91
 
88 92
 mat = list_to_matrix(lt)
... ...
@@ -94,7 +98,7 @@ library(circlize)
94 98
 library(GenomicRanges)
95 99
 lt = lapply(1:4, function(i) generateRandomBed())
96 100
 lt = lapply(lt, function(df) GRanges(seqnames = df[, 1], 
97
-	ranges = IRanges(df[, 2], df[, 3])))
101
+    ranges = IRanges(df[, 2], df[, 3])))
98 102
 names(lt) = letters[1:4]
99 103
 m = make_comb_mat(lt)
100 104
 }
101 105
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+\name{normalize_comb_mat}
2
+\alias{normalize_comb_mat}
3
+\title{
4
+Normalize a list of combination matrice
5
+}
6
+\description{
7
+Normalize a list of combination matrice
8
+}
9
+\usage{
10
+normalize_comb_mat(...)
11
+}
12
+\arguments{
13
+
14
+  \item{...}{If it is a single argument, the value should be a list of combination matrices.}
15
+
16
+}
17
+\details{
18
+It normalizes a list of combination matrice to make them have same number and order of sets and combination sets.
19
+
20
+The sets (by \code{\link{set_name}}) from all combination matrice should be the same.
21
+}
22
+\examples{
23
+# There is no example
24
+NULL
25
+
26
+}
0 27
new file mode 100644
... ...
@@ -0,0 +1,27 @@
1
+\name{order.comb_mat}
2
+\alias{order.comb_mat}
3
+\title{
4
+Order of the Combination Sets
5
+}
6
+\description{
7
+Order of the Combination Sets
8
+}
9
+\usage{
10
+order.comb_mat(m, decreasing = TRUE, on = "comb_set")
11
+}
12
+\arguments{
13
+
14
+  \item{m}{A combination matrix returned by \code{\link{make_comb_mat}}.}
15
+  \item{on}{On sets or on combination sets?}
16
+  \item{decreasing}{Whether the ordering is applied decreasingly.}
17
+
18
+}
19
+\details{
20
+It first sorts by the degree of the combination sets then
21
+by the combination matrix.
22
+}
23
+\examples{
24
+# There is no example
25
+NULL
26
+
27
+}
... ...
@@ -20,8 +20,8 @@ A vector of set names.
20 20
 \examples{
21 21
 set.seed(123)
22 22
 lt = list(a = sample(letters, 10),
23
-	      b = sample(letters, 15),
24
-	      c = sample(letters, 20))
23
+          b = sample(letters, 15),
24
+          c = sample(letters, 20))
25 25
 m = make_comb_mat(lt)
26 26
 set_name(m)
27 27
 }
... ...
@@ -20,8 +20,8 @@ A vector of set sizes.
20 20
 \examples{
21 21
 set.seed(123)
22 22
 lt = list(a = sample(letters, 10),
23
-	      b = sample(letters, 15),
24
-	      c = sample(letters, 20))
23
+          b = sample(letters, 15),
24
+          c = sample(letters, 20))
25 25
 m = make_comb_mat(lt)
26 26
 set_size(m)
27 27
 }
... ...
@@ -17,8 +17,8 @@ Transpost the Combination Matrix
17 17
 \examples{
18 18
 set.seed(123)
19 19
 lt = list(a = sample(letters, 10),
20
-	      b = sample(letters, 15),
21
-	      c = sample(letters, 20))
20
+          b = sample(letters, 15),
21
+          c = sample(letters, 20))
22 22
 m = make_comb_mat(lt)
23 23
 t(m)
24 24
 }
25 25
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+\name{upset_right_annotation}
2
+\alias{upset_right_annotation}
3
+\title{
4
+Default UpSet Right Annotation
5
+}
6
+\description{
7
+Default UpSet Right Annotation
8
+}
9
+\usage{
10
+upset_right_annotation(m,
11
+    gp = gpar(fill = "black"),
12
+    width = unit(ifelse(set_on_rows, 3, 2), "cm"),
13
+    show_annotation_name = TRUE,
14
+    annotation_name_gp = gpar(),
15
+    annotation_name_offset = NULL,
16
+    annotation_name_side = "bottom",
17
+    annotation_name_rot = NULL,
18
+    ...)
19
+}
20
+\arguments{
21
+
22
+  \item{m}{A combination matrix which is as same as the one for \code{\link{UpSet}}.}
23
+  \item{gp}{Graphic parameters for bars.}
24
+  \item{width}{Width of the right annotation.}
25
+  \item{show_annotation_name}{Whether show annotation names?}
26
+  \item{annotation_name_gp}{Graphic parameters for anntation names.}
27
+  \item{annotation_name_offset}{Offset to the annotation name, a \code{\link[grid]{unit}} object.}
28
+  \item{annotation_name_side}{Side of the annotation name.}
29
+  \item{annotation_name_rot}{Rotation of the annotation name, it can only take values in \code{c(00, 90, 180, 270)}.}
30
+  \item{...}{Passed to \code{\link{anno_barplot}}.}
31
+
32
+}
33
+\details{
34
+The default right annotation is actually barplot implemented by \code{\link{anno_barplot}}. For
35
+how to set the right annotation or left annotation in \code{\link{UpSet}}, please refer to \code{\link{UpSet}}.
36
+}
37
+\examples{
38
+# There is no example
39
+NULL
40
+
41
+}
0 42
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+\name{upset_top_annotation}
2
+\alias{upset_top_annotation}
3
+\title{
4
+Default UpSet Top Annotation
5
+}
6
+\description{
7
+Default UpSet Top Annotation
8
+}
9
+\usage{
10
+upset_top_annotation(m,
11
+    gp = gpar(fill = "black"),
12
+    height = unit(ifelse(set_on_rows, 2, 3), "cm"),
13
+    show_annotation_name = TRUE,
14
+    annotation_name_gp = gpar(),
15
+    annotation_name_offset = NULL,
16
+    annotation_name_side = "left",
17
+    annotation_name_rot = 0,
18
+    ...)
19
+}
20
+\arguments{
21
+
22
+  \item{m}{A combination matrix which is as same as the one for \code{\link{UpSet}}.}
23
+  \item{gp}{Graphic parameters for bars.}
24
+  \item{height}{The height of the top annotation.}
25
+  \item{show_annotation_name}{Whether show annotation names?}
26
+  \item{annotation_name_gp}{Graphic parameters for anntation names.}
27
+  \item{annotation_name_offset}{Offset to the annotation name, a \code{\link[grid]{unit}} object.}
28
+  \item{annotation_name_side}{Side of the annotation name.}
29
+  \item{annotation_name_rot}{Rotation of the annotation name, it can only take values in \code{c(00, 90, 180, 270)}.}
30
+  \item{...}{Passed to \code{\link{anno_barplot}}.}
31
+
32
+}
33
+\details{
34
+The default top annotation is actually barplot implemented by \code{\link{anno_barplot}}. For
35
+how to set the top annotation or bottom annotation in \code{\link{UpSet}}, please refer to \code{\link{UpSet}}.
36
+}
37
+\examples{
38
+# There is no example
39
+NULL
40
+
41
+}
... ...
@@ -11,6 +11,7 @@ set_size(m)
11 11
 comb_size(m)
12 12
 lapply(comb_name(m), function(x) extract_comb(m, x))
13 13
 UpSet(m)
14
+UpSet(m, comb_col = c(rep(2, 3), rep(3, 3), 1))
14 15
 UpSet(t(m))
15 16
 
16 17
 set_name(t(m))
... ...
@@ -109,3 +110,29 @@ test_that("test comb_size and extract_comb", {
109 110
 	expect_that(cs, equals(unname(sapply(comb_name(m), function(nm) length(extract_comb(m, nm))))))
110 111
 })
111 112
 
113
+
114
+
115
+movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
116
+    header = T, sep = ";")
117
+genre = c("Action", "Romance", "Horror", "Children", "SciFi", "Documentary")
118
+rate = cut(movies$AvgRating, c(0, 1, 2, 3, 4, 5))
119
+m_list = tapply(seq_len(nrow(movies)), rate, function(ind) {
120
+	make_comb_mat(movies[ind, genre, drop = FALSE])
121
+})
122
+m_list2 = normalize_comb_mat(m_list)
123
+
124
+lapply(m_list2, set_name)
125
+lapply(m_list2, set_size)
126
+lapply(m_list2, comb_name)
127
+lapply(m_list2, comb_size)
128
+
129
+lapply(1:length(m_list), function(i) {
130
+	n1 = comb_name(m_list[[i]])
131
+	x1 = comb_size(m_list[[i]])
132
+	n2 = comb_name(m_list2[[i]])
133
+	x2 = comb_size(m_list2[[i]])
134
+	l = n2 %in% n1
135
+	x2[!l]
136
+})
137
+
138
+
... ...
@@ -21,7 +21,7 @@ h1, h2, h3, h4, h5 {
21 21
 }
22 22
 </style>
23 23
 
24
-From version 2.0.0 of **ComplexHeatmap**, there are big changes to the older versions. The major
24
+From version 1.99.0 of **ComplexHeatmap**, there are big changes to the older versions. The major
25 25
 functionalities are still the same, but it is not 100% compatible. This vignette lists the
26 26
 changes between the new and old versions.
27 27
 
... ...
@@ -64,7 +64,7 @@ changes between the new and old versions.
64 64
 - All annotations have default width/height now. E.g. the column simple annotation has height of 5mm
65 65
   and the column barplot annotation has height of 1cm.
66 66
 - Provide more annotation functions: `anno_empty()`, `anno_simple`, `anno_image()`, `anno_block()`,
67
-  `anno_lines()`, `anno_joyplot()`, `anno_horizon()`, `anno_summary()`.
67
+  `anno_lines()`, `anno_joyplot()`, `anno_horizon()`, `anno_summary()`, `anno_zoom()`.
68 68
   (https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html)
69 69
 - The size of the annotations are better to be set in the annotation functions, e.g.
70 70
   `anno_points(..., height = )`, but you can still set `annotation_height`.
... ...
@@ -117,3 +117,4 @@ Heatmap(m, cluster_rows = cluster_within_group(t(m), group),
117 117
 Heatmap is used to visualize the global patterns of your matrix which not every single row or column. I would suggest
118 118
 to random sample rows or columns into a reasonable small number, and the final heatmap should be the same as if you
119 119
 still insist to use the full matrix.
120
+