'How can I apply fisher.test in R to a large matrix, and extract p-values to a new matrix?

I have a large matrix (12 rows, 53 columns) with counts of how many times genes in my clusters "A", "B", "C", etc. overlap with clusters created by someone else "0", "1", "2", etc. I am only providing a preview of the data to not overcrowd the post, but can provide the dput() info for the full data if needed.

  1. What I want, is to apply fisher.test() in R to identify which of "0", "1", "2", etc. clusters are significantly enriched by which of my own clusters "A", "B", "C", etc. I'm imagining this may require some kind of loop, but I'm not sure where to begin. Would my matrix of overlap counts be the correct input for fisher.test()?

  2. Also, I would like to output each result to a different table/matrix so that I can later create some sort of heat map like this:

overlap_heatmap

Thank you in advance for any of your help.

      ["0"] ["1"] ["2"]["3"]["4"]
["A"] 2370  1261  229  103  737
["B"] 414   81    9    21   148
["C"] 110   30    50   19   24
["D"] 55    5     4    0    10
["E"] 864   193   138  45   345
structure(c(2370, 414, 110, 55, 864, 1261, 81, 30, 5, 193, 229, 
9, 50, 4, 138, 103, 21, 19, 0, 45, 737, 148, 24, 10, 345), .Dim = c(5L, 
5L), .Dimnames = list(c("A", "B", "C", "D", "E"), c("0", "1", 
"2", "3", "4")))


Solution 1:[1]

I was able to conduct a pairwise fisher test on the two overlapping datasets using an R package called 'GeneOverlap'. I had to feed in the raw data for gene set 1, gene set 2, and the number of genes to be considered for the background. The package created its own contingency table, performed the pairwise fisher tests, and had an option for me to extract various results including p-values.

Although I got the result I was looking for, it feels bittersweet in that it feels more like a workaround to my problem. However, I no longer need my question answered.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J22