Question: How to get the list of common genes from a VennDiagram?
1
Wox340 wrote:

`**Package ‘![VennDiagram]’.**` How can I get the list of common objects in each combination?

For e.g. what are the common 44 Genes in interset (similar image got from online)? I have used below scripts

``````Overlap <- calculate.overlap(x = list(BB, BB, BRR))

Overlap[]
``````

But in this method, I can not find the list of 1761, 466 genes

how these numbers are given?

Even when I tried `Overlap\$a5` , `Overlap\$a7`, etc this also not given some of the values correctly?

Could someone help me?

rna-seq R • 1.1k views
modified 8 months ago by manuel.belmadani1.2k • written 8 months ago by Wox340
1

Provide example data, `calculate.overlap` does exactly what you need, try the examples from the manuals. Also, why `BB` is repeated twice?

If you need just the overlap of genes from 3 sets, try:

``````intersect(intersect(1:4, 2:5), 3:7)
#  3 4
``````

Or more generic solution:

``````Reduce(intersect, list(1:4, 2:5, 3:7))
#  3 4
``````
4

From what I can tell, the `a`s are distributed from top left to right, to bottom. i.e. ``` a1 = 1761 a2=126 a3=466 a4=64 a5=44 a6=27 a7=366 ```

This is based on: ``````> library("VennDiagram")
>
> gene_list = paste0("GENE", 1:1000)
>
> studies = list( S1=sample(gene_list, 700, replace = FALSE),
+                 S2=sample(gene_list, 700, replace = FALSE),
+                 S3=sample(gene_list, 700, replace = FALSE) )
>
> ol = calculate.overlap(x = studies)
> ol_size=sapply(ol, length)
>
>
> venn.diagram(
+   x = studies,
+   euler.d = TRUE,
+   filename = "Euler_3set_scaled.tiff",
+   cex = 2.5,
+   cat.cex = 2.5,
+   cat.pos = 0
+ );
 1
> # Get the 63
> length( setdiff(studies\$S1, union(studies\$S2, studies\$S3)) )
 63
> # Confirm order
> ol_size
a5  a2  a4  a6  a1  a3  a7
351 143 143 134  63  72  72
``````

And it matches the order from `a1` to `a7` going left-right, top-bottom.

If you want to be sure of what you're getting, you can use set operators like I did with `setdiff()` and `union()`.

1

Probably, this is another way:

``````t=get.venn.partitions(studies, keep.elements = T, force.unique = T)
t\$..values.. # To get all common genes
t\$..set..
 "S1∩S2∩S3"     "(S2∩S3)∖(S1)" "(S1∩S3)∖(S2)" "(S3)∖(S1∪S2)"
 "(S1∩S2)∖(S3)" "(S2)∖(S1∪S3)" "(S1)∖(S2∪S3)"

ol_size
a5  a2  a4  a6  a1  a3  a7
338 148 154 145  60  69  63
lengths(t\$..values..)
1
338
``````

OP can edit this code for better. Remove lengths to print the gene list:

``````> for (length in (1:length(t\$..set..))) {
+     print (paste(t\$..set..[length], lengths(t\$..values..[length])), sep="\t")
+ }
 "S1∩S2∩S3 338"
 "(S2∩S3)∖(S1) 145"
 "(S1∩S3)∖(S2) 154"
 "(S3)∖(S1∪S2) 63"
 "(S1∩S2)∖(S3) 148"
 "(S2)∖(S1∪S3) 69"
 "(S1)∖(S2∪S3) 60"
``````