Make a Combination Matrix for UpSet Plot

make_comb_mat(..., mode = c("distinct", "intersect", "union"),
top_n_sets = Inf, min_set_size = -Inf, remove_empty_comb_set = TRUE,
remove_complement_set = FALSE, universal_set = NULL,
complement_size = NULL, value_fun = NULL)

## Arguments

... The input sets. If it is represented as a single variable, it should be a matrix/data frame or a list. If it is multiple variables, it should be name-value pairs, see Input section for explanation. The mode for forming the combination set, see Mode section. Number of sets with largest size. Ths minimal set size that is used for generating the combination matrix. Whether remove empty combination sets? Whether remove the complement set which has no intersection to any of the set. The universal set. If it is set, the size of the complement set of all sets is also calculated. It if is specified, complement_size is ignored. The size for the complement of all sets. If it is specified, the combination set name will be like "00...". 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. GRanges or IRanges object), the sum of widths of regions in the set is calculated as the size of the set.

## Input

To represent multiple sets, the variable can be represented as:

1. A list of sets where each set is a vector, e.g.:

    list(set1 = c("a", "b", "c"),
set2 = c("b", "c", "d", "e"),
...)  

2. A binary matrix/data frame where rows are elements and columns are sets, e.g.:

      a b c
h 1 1 1
t 1 0 1
j 1 0 0
u 1 0 1
w 1 0 0
...  

If the variable is a data frame, the binary columns (only contain 0 and 1) and the logical columns are only kept.

The set can be genomic regions, then it can only be represented as a list of GRanges objects.

## Mode

E.g. for three sets (A, B, C), the UpSet approach splits the combination of selecting elements in the set or not in the set and calculates the sizes of the combination sets. For three sets, all possible combinations are:

    A B C
1 1 1
1 1 0
1 0 1
0 1 1
1 0 0
0 1 0
0 0 1  

A value of 1 means to select that set and 0 means not to select that set. E.g., "1 1 0" means to select set A, B while not set C. Note there is no "0 0 0", because the background size is not of interest here. With the code of selecting and not selecting the sets, next we need to define how to calculate the size of that combination set. There are three modes:

1. distinct mode: 1 means in that set and 0 means not in that set, then "1 1 0" means a set of elements also in set A and B, while not in C (i.e. setdiff(intersect(A, B), C)). Under this mode, the seven combination sets are the seven partitions in the Venn diagram and they are mutually exclusive.

2. intersect mode: 1 means in that set and 0 is not taken into account, then, "1 1 0" means a set of elements in set A and B, and they can also in C or not in C (i.e. intersect(A, B)). Under this mode, the seven combination sets can overlap.

3. union mode: 1 means in that set and 0 is not taken into account. When there are multiple 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)). Under this mode, the seven combination sets can overlap.

## Value

A matrix also in a class of comb_mat.

Following functions can be applied to it: set_name, comb_name, set_size, comb_size, comb_degree, extract_comb and t.comb_mat.

## Examples

set.seed(123)
lt = list(a = sample(letters, 10),
b = sample(letters, 15),
c = sample(letters, 20))
m = make_comb_mat(lt)

mat = list_to_matrix(lt)
mat#>   a b c
#> a 0 1 1
#> b 0 0 1
#> c 1 1 0
#> d 0 1 0
#> e 1 1 1
#> f 0 0 1
#> g 0 1 1
#> h 0 1 1
#> i 0 1 1
#> j 1 1 1
#> k 1 1 0
#> l 0 1 1
#> m 0 0 1
#> n 1 1 0
#> o 1 0 1
#> q 0 0 1
#> r 1 0 1
#> s 1 1 0
#> t 0 0 1
#> u 0 1 1
#> v 0 0 1
#> w 0 0 1
#> x 1 1 1
#> y 1 1 1
#> z 0 0 1m = make_comb_mat(mat)

# \dontrun{
library(circlize)
#> Attaching package: ‘BiocGenerics’#> The following objects are masked from ‘package:parallel’:
#>
#>     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
#>     clusterExport, clusterMap, parApply, parCapply, parLapply,
#>     parLapplyLB, parRapply, parSapply, parSapplyLB#> The following objects are masked from ‘package:stats’:
#>
#>     IQR, mad, sd, var, xtabs#> The following objects are masked from ‘package:base’:
#>
#>     anyDuplicated, append, as.data.frame, basename, cbind, colnames,
#>     dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
#>     grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
#>     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
#>     rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
#>     union, unique, unsplit, which, which.max, which.min#> Loading required package: S4Vectors#>
#> Attaching package: ‘S4Vectors’#> The following object is masked from ‘package:base’:
#>
#>     expand.grid#> Loading required package: IRanges#> Loading required package: GenomeInfoDblt = lapply(1:4, function(i) generateRandomBed())
lt = lapply(lt, function(df) GRanges(seqnames = df[, 1],
ranges = IRanges(df[, 2], df[, 3])))
names(lt) = letters[1:4]
m = make_comb_mat(lt)
# }