如何获取venn diagram中的基因列表

我们使用venn diagram工具在R中绘制venn diagram并不困难。那么,当我们希望得到每一组中的基因列表应该如何处理呢?可以参考下面的代码。

set.seed(1)
symbols <- list(A=sample(letters, 10), B=sample(letters, 5), C=sample(letters, 8))
library(Vennerable)
plot(Venn(symbols))

plot of chunk unnamed-chunk-2

symbols.all <- sort(unique(unlist(symbols)))
symbols.mat <- matrix(0, nrow=length(symbols.all), ncol=length(symbols), 
                      dimnames=list(genes=symbols.all, groups=names(symbols)))
for(i in 1:length(symbols)) symbols.mat[symbols[[i]], i] <- 1
head(symbols.mat)
##      groups
## genes A B C
##     b 1 0 0
##     e 1 1 1
##     f 0 1 0
##     g 1 0 0
##     i 0 1 1
##     j 1 0 0
library(UpSetR)
upset(as.data.frame(symbols.mat))

plot of chunk unnamed-chunk-2

symbols.venn <- split(rownames(symbols.mat), apply(symbols.mat, 1, paste, collapse=""))
names(symbols.venn)
## [1] "001" "010" "011" "100" "101" "111"
names(symbols.venn) <- 
  sapply(names(symbols.venn), function(.ele) 
    paste(colnames(symbols.mat)[as.logical(as.numeric(strsplit(.ele, "")[[1]]))], 
          collapse="__"))
symbols.venn
## $C
## [1] "r" "t" "y" "z"
## 
## $B
## [1] "f" "q"
## 
## $B__C
## [1] "i" "x"
## 
## $A
## [1] "b" "g" "j" "l" "n" "s" "u" "w"
## 
## $A__C
## [1] "m"
## 
## $A__B__C
## [1] "e"

Leave a Reply

  

  

  

%d 博主赞过: