A related question was asked the other day.
gene_lists = list(letters[1:3], letters[3:7], letters[6:8])
There's loads of ways to do this
The solution that @lessismore generated in the comments was effectively:
make_bipartite_adjacency_from_sets <- function(list_of_sets){
universe <- sort(unique(unlist(list_of_sets)))
adjacency_df <- lapply(list_of_sets, function(x) as.numeric(universe %in% x)) %>% as.data.frame()
rownames(adjacency_df) <- universe
adjacency_df
}
make_bipartite_adjacency_from_sets(gene_lists)
G1 G2 G3
a 1 0 0
b 1 0 0
c 1 1 0
d 0 1 0
e 0 1 0
f 0 1 1
g 0 1 1
h 0 0 1
You could also do a tidyverse version (but this disallows row names):
make_bipartite_adjacency_from_sets2 <- function(list_of_sets){
list_of_sets %>%
purrr::map(function(x) tibble::data_frame(gene_id = x, adj = 1)) %>%
dplyr::bind_rows(.id = "set_id") %>%
tidyr::spread(key = set_id, value = adj, fill = 0)
}
make_bipartite_adjacency_from_sets2(gene_lists)
# A tibble: 8 x 4
gene_id G1 G2 G3
* <chr> <dbl> <dbl> <dbl>
1 a 1 0 0
2 b 1 0 0
3 c 1 1 0
4 d 0 1 0
5 e 0 1 0
6 f 0 1 1
7 g 0 1 1
8 h 0 0 1