Browse code

add test for make_comb_mat()

Zuguang Gu authored on 27/03/2019 20:52:10
Showing 6 changed files

... ...
@@ -10,7 +10,8 @@ Imports: circlize (>= 0.4.5), GetoptLong, colorspace, clue,
10 10
     RColorBrewer, GlobalOptions (>= 0.1.0), parallel
11 11
 Suggests: testthat (>= 1.0.0), knitr, markdown, dendsort, 
12 12
     Cairo, png, jpeg, tiff, fastcluster,
13
-    dendextend (>= 1.0.1), grImport, grImport2, glue
13
+    dendextend (>= 1.0.1), grImport, grImport2, glue,
14
+    GenomicRanges
14 15
 VignetteBuilder: knitr
15 16
 Description: Complex heatmaps are efficient to visualize associations 
16 17
     between different sources of data sets and reveal potential patterns. 
... ...
@@ -1,6 +1,7 @@
1 1
 CHANGES in VERSION 1.99.7
2 2
 
3 3
 * `UpSet()` supports adding complement sets.
4
+* `make_comb_set()`: add `universal_set` and `complement_size` arguments.
4 5
 
5 6
 ========================
6 7
 
... ...
@@ -295,7 +295,8 @@ list_to_matrix = function(lt, universal_set = NULL) {
295 295
 # -mode The mode for forming the combination set, see Mode section.
296 296
 # -top_n_sets Number of sets with largest size.
297 297
 # -min_set_size Ths minimal set size that is used for generating the combination matrix.
298
-# -universal_set The universal set. It if is specified, ``complement_size`` is ignored.
298
+# -universal_set The universal set. If it is set, the size of the complement set of all sets is also calculated.
299
+#                It if is specified, ``complement_size`` is ignored.
299 300
 # -complement_size The size for the complement of all sets. If it is specified, the combination
300 301
 #                  set name will be like "00...".
301 302
 # -value_fun For each combination set, how to calculate the size? If it is a scalar set, 
... ...
@@ -7,21 +7,23 @@ Convert a List of Sets to a Binary Matrix
7 7
 Convert a List of Sets to a Binary Matrix
8 8
 }
9 9
 \usage{
10
-list_to_matrix(lt)
10
+list_to_matrix(lt, universal_set = NULL)
11 11
 }
12 12
 \arguments{
13 13
 
14 14
   \item{lt}{A list of vectors.}
15
+  \item{universal_set}{The universal set.}
15 16
 
16 17
 }
17 18
 \details{
18 19
 It converts the list which have m sets to a binary matrix with n rows and m columns
19
-where n is the number of union of all sets in the list.
20
+where n is the size of universal set.
20 21
 }
21 22
 \examples{
22 23
 set.seed(123)
23
-lt = list(a = sample(letters, 10),
24
-          b = sample(letters, 15),
25
-          c = sample(letters, 20))
24
+lt = list(a = sample(letters, 5),
25
+          b = sample(letters, 10),
26
+          c = sample(letters, 15))
26 27
 list_to_matrix(lt)
28
+list_to_matrix(lt, universal_set = letters)
27 29
 }
... ...
@@ -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, complement_size = NULL, value_fun)
11
+    top_n_sets = Inf, min_set_size = -Inf, universal_set = NULL, 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{universal_set}{The universal set. If it is set, the size of the complement set of all sets is also calculated. It if is specified, \code{complement_size} is ignored.}
19 20
   \item{complement_size}{The size for the complement of all sets. If it is specified, the combination set name will be like "00...".}
20 21
   \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.}
21 22
 
... ...
@@ -54,10 +54,10 @@ test_that("test default make_comb_mat", {
54 54
 test_that("test default make_comb_mat with universal_set", {
55 55
 	m = make_comb_mat(lt, universal_set = letters)
56 56
 
57
-	expect_that(length(comb_size(m)), is_identical_to(8))
57
+	expect_that(length(comb_size(m)), equals(8))
58 58
 	expect_that("000" %in% comb_name(m), is_identical_to(TRUE))
59 59
 	expect_that(0 %in% comb_degree(m), is_identical_to(TRUE))
60
-
60
+	expect_that(sort(extract_comb(m, "000")), is_identical_to(sort(setdiff(letters, unlist(lt)))))
61 61
 })
62 62
 
63 63
 
... ...
@@ -65,8 +65,43 @@ test_that("test default make_comb_mat with universal_set which is smaller than t
65 65
 	m = make_comb_mat(lt, universal_set = letters[1:10])
66 66
 
67 67
 	expect_that("000" %in% comb_name(m), is_identical_to(TRUE))
68
-	expect_that(0 %in% comb_degree(m), is_identical_to(TRUE))
69
-
68
+	expect_that(sort(extract_comb(m, "000")), is_identical_to(sort(setdiff(letters[1:10], unlist(lt)))))
70 69
 })
71 70
 
72
-# test GRanges
73 71
\ No newline at end of file
72
+# test GRanges
73
+library(GenomicRanges)
74
+
75
+#  1        0         0         0         0
76
+#  ++++++++++++++++++++++++++++++++++++++++
77
+#     -------      ---------     ---------
78
+#   ------     ------    ------      ----
79
+#      -----     -----       ----------
80
+#       ++++++    +++++++     +++++++
81
+
82
+gr1 = GRanges(seqnames = "chr1",
83
+	ranges = IRanges(start = c(4, 17, 31), end = c(10, 25, 39)))
84
+gr2 = GRanges(seqnames = "chr1",
85
+	ranges = IRanges(start = c(2, 13, 23, 35), end = c(7, 18, 28, 38)))
86
+gr3 = GRanges(seqnames = "chr1",
87
+	ranges = IRanges(start = c(5, 15, 27), end = c(9, 19, 36)))
88
+universal = GRanges(seqnames = "chr1",
89
+	ranges = IRanges(start = 1, end = 40))
90
+bg = GRanges(seqnames = "chr1",
91
+	ranges = IRanges(start = c(6, 16, 28), end = c(11, 22, 34)))
92
+
93
+lt = list(gr1 = gr1, gr2 = gr2, gr3 = gr3)
94
+
95
+test_that("test default make_comb_mat with GRanges", {
96
+	m = make_comb_mat(lt)
97
+
98
+	expect_that(unname(set_size(m)), equals(c(sum(GenomicRanges::width(gr1)), sum(GenomicRanges::width(gr2)), sum(GenomicRanges::width(gr3)))))
99
+	expect_that(comb_size(m)[["111"]], equals(sum(GenomicRanges::width( GenomicRanges::intersect(GenomicRanges::intersect(gr1, gr2), gr3) ))))
100
+
101
+	m = make_comb_mat(lt, universal_set = universal)
102
+	expect_that(length(comb_size(m)), equals(8))
103
+	expect_that(comb_size(m)[["000"]], equals(sum(GenomicRanges::width( GenomicRanges::setdiff(universal, GenomicRanges::union(GenomicRanges::union(gr1, gr2), gr3))))))
104
+
105
+	m = make_comb_mat(lt, universal_set = bg)
106
+	expect_that(length(comb_size(m)), equals(8))
107
+	expect_that(comb_size(m)[["000"]], equals(sum(GenomicRanges::width( GenomicRanges::setdiff(bg, GenomicRanges::union(GenomicRanges::union(gr1, gr2), gr3))))))
108
+})