Created
August 23, 2018 15:45
-
-
Save seb-mueller/9d07aa7f0ebf1caf2219cf52fbf518c2 to your computer and use it in GitHub Desktop.
since UpsetR doesn't report back the elements for the ploted groups this function was created to to just that. See (https://github.com/hms-dbmi/UpSetR/issues/85)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source of this function: https://github.com/hms-dbmi/UpSetR/issues/85#issuecomment-327900647 | |
fromList <- function (input) { | |
# Same as original fromList()... | |
elements <- unique(unlist(input)) | |
data <- unlist(lapply(input, function(x) { | |
x <- as.vector(match(elements, x)) | |
})) | |
data[is.na(data)] <- as.integer(0) | |
data[data != 0] <- as.integer(1) | |
data <- data.frame(matrix(data, ncol = length(input), byrow = F)) | |
data <- data[which(rowSums(data) != 0), ] | |
names(data) <- names(input) | |
# ... Except now it conserves your original value names! | |
row.names(data) <- elements | |
return(data) | |
} | |
overlapGroups <- function (listInput, sort = TRUE) { | |
# listInput could look like this: | |
# $one | |
# [1] "a" "b" "c" "e" "g" "h" "k" "l" "m" | |
# $two | |
# [1] "a" "b" "d" "e" "j" | |
# $three | |
# [1] "a" "e" "f" "g" "h" "i" "j" "l" "m" | |
listInputmat <- fromList(listInput) == 1 | |
# one two three | |
# a TRUE TRUE TRUE | |
# b TRUE TRUE FALSE | |
#... | |
# condensing matrix to unique combinations elements | |
listInputunique <- unique(listInputmat) | |
grouplist <- list() | |
# going through all unique combinations and collect elements for each in a list | |
for (i in 1:nrow(listInputunique)) { | |
currentRow <- listInputunique[i,] | |
myelements <- which(apply(listInputmat,1,function(x) all(x == currentRow))) | |
attr(myelements, "groups") <- currentRow | |
grouplist[[paste(colnames(listInputunique)[currentRow], collapse = ":")]] <- myelements | |
myelements | |
# attr(,"groups") | |
# one two three | |
# FALSE FALSE TRUE | |
# f i | |
# 12 13 | |
} | |
if (sort) { | |
grouplist <- grouplist[order(sapply(grouplist, function(x) length(x)), decreasing = TRUE)] | |
} | |
attr(grouplist, "elements") <- unique(unlist(listInput)) | |
return(grouplist) | |
# save element list to facilitate access using an index in case rownames are not named | |
} | |
# How to use: | |
# library(UpSetR) | |
# example of list input (list of named vectors) | |
#listInput <- list(one = letters[ c(1, 2, 3, 5, 7, 8, 11, 12, 13) ], | |
# two = letters[ c(1, 2, 4, 5, 10) ], | |
# three = letters[ c(1, 5, 6, 7, 8, 9, 10, 12, 13) ]) | |
# li <- overlapGroups(listInput) | |
# list of all elements: | |
# attr(li, "elements") | |
# [1] "a" "b" "c" "e" "g" "h" "k" "l" "m" "d" "j" "f" "i" | |
# which elements are in the biggest group? | |
# li[1] | |
# $`one:three` | |
# g h l m | |
# 5 6 8 9 | |
# attr(,"groups") | |
# one two three | |
# TRUE FALSE TRUE | |
# names(li[[1]]) | |
# [1] "g" "h" "l" "m" | |
# attr(li, "elements")[li[[1]]] | |
# [1] "g" "h" "l" "m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment