#wd <- "/Users/usri/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24"
library(WGCNA)
## Warning: package 'WGCNA' was built under R version 4.3.3
## Loading required package: dynamicTreeCut
## Loading required package: fastcluster
## 
## Attaching package: 'fastcluster'
## The following object is masked from 'package:stats':
## 
##     hclust
## 
## 
## Attaching package: 'WGCNA'
## The following object is masked from 'package:stats':
## 
##     cor
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4          ✔ readr     2.1.5     
## ✔ forcats   1.0.0.9000     ✔ stringr   1.5.1     
## ✔ ggplot2   3.5.1          ✔ tibble    3.2.1     
## ✔ lubridate 1.9.3          ✔ tidyr     1.3.1     
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(org.Hs.eg.db)
## Loading required package: AnnotationDbi
## Warning: package 'AnnotationDbi' was built under R version 4.3.2
## Loading required package: stats4
## Loading required package: BiocGenerics
## Warning: package 'BiocGenerics' was built under R version 4.3.1
## 
## Attaching package: 'BiocGenerics'
## 
## The following objects are masked from 'package:lubridate':
## 
##     intersect, setdiff, union
## 
## The following objects are masked from 'package:dplyr':
## 
##     combine, intersect, setdiff, union
## 
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## 
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, aperm, append, as.data.frame, basename, cbind,
##     colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
##     get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
##     match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
##     table, tapply, union, unique, unsplit, which.max, which.min
## 
## Loading required package: Biobase
## Warning: package 'Biobase' was built under R version 4.3.1
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.
## 
## Loading required package: IRanges
## Warning: package 'IRanges' was built under R version 4.3.1
## Loading required package: S4Vectors
## Warning: package 'S4Vectors' was built under R version 4.3.2
## 
## Attaching package: 'S4Vectors'
## 
## The following objects are masked from 'package:lubridate':
## 
##     second, second<-
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, rename
## 
## The following object is masked from 'package:tidyr':
## 
##     expand
## 
## The following object is masked from 'package:utils':
## 
##     findMatches
## 
## The following objects are masked from 'package:base':
## 
##     expand.grid, I, unname
## 
## 
## Attaching package: 'IRanges'
## 
## The following object is masked from 'package:lubridate':
## 
##     %within%
## 
## The following objects are masked from 'package:dplyr':
## 
##     collapse, desc, slice
## 
## The following object is masked from 'package:purrr':
## 
##     reduce
## 
## 
## Attaching package: 'AnnotationDbi'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(org.Mm.eg.db)
## 
library(pheatmap)
library(ggplot2)
library(ComplexHeatmap)
## Warning: package 'ComplexHeatmap' was built under R version 4.3.1
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.18.0
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
## 
## If you use it in published research, please cite either one:
## - Gu, Z. Complex Heatmap Visualization. iMeta 2022.
## - Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
##     genomic data. Bioinformatics 2016.
## 
## 
## The new InteractiveComplexHeatmap package can directly export static 
## complex heatmaps into an interactive Shiny app with zero effort. Have a try!
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
## ! pheatmap() has been masked by ComplexHeatmap::pheatmap(). Most of the arguments
##    in the original pheatmap() are identically supported in the new function. You 
##    can still use the original function by explicitly calling pheatmap::pheatmap().
## 
## 
## Attaching package: 'ComplexHeatmap'
## 
## The following object is masked from 'package:pheatmap':
## 
##     pheatmap
numericMetaMEGA <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/numericMetaMEGA.rds")
netMEGA <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/netMEGA.rds")
cleanDatMEGA <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/cleanDatMEGA.rds")
cleanDat <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/camk2a.cleanDat.after.outlier.removal.15samples.rds")
net <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/camk2a.wgcna.net.rds")
netCAMK2A <- net
MEGAsymbols <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/MEGAsymbols.rds")
#calculate and store the number of overlapping genes:
# Initialize a matrix to store the number of overlapping genes between each CAMK2A and MEGA module
camk2aModules <- unique(netCAMK2A$colors)
megaModules <- unique(netMEGA$colors)
camk2aSymbols <- readRDS("~/Desktop/Camk2a.Sep26.24.analysis/Camk2a.16.OCT24/camk2aSymbols.rds")
# Remove grey modules if applicable
camk2aModules <- camk2aModules[camk2aModules != "grey"]
megaModules <- megaModules[megaModules != "grey"]

# Initialize matrix to store overlap counts
overlapMatrix <- matrix(0, nrow = length(camk2aModules), ncol = length(megaModules))
rownames(overlapMatrix) <- camk2aModules
colnames(overlapMatrix) <- megaModules

# Calculate overlap for each CAMK2A and MEGA module pair
for (camk2aModule in camk2aModules) {
  camk2aGenes <- camk2aSymbols[which(netCAMK2A$colors == camk2aModule)]
  
  for (megaModule in megaModules) {
    megaGenes <- MEGAsymbols[which(netMEGA$colors == megaModule)]
    
    # Find overlapping genes between CAMK2A and MEGA module
    overlap <- intersect(camk2aGenes, megaGenes)
    
    # Store the number of overlapping genes
    overlapMatrix[camk2aModule, megaModule] <- length(overlap)
  }
}

# Print the overlap matrix
print(overlapMatrix)
##             yellow tan greenyellow orange turquoise green black darkgreen
## pink             2   2           2      3         4     3     1         5
## greenyellow      0   0           1      0         2     3     0         3
## blue            11   4           3      3        28    11     4         6
## green            8   1           3      4        10     7     5         4
## yellow           6   2           4      4        16     3     2         2
## turquoise        9   5           6      7        32    12     6         2
## black            6   0           0      2         7     2     2         3
## red              6   4           1      3        11     7     4         3
## magenta          5   1           0      2         7     0     0         0
## tan              0   0           0      0         1     1     1         0
## brown           13   2           1      1        15     3     4         3
## purple           3   0           1      1         5     2     3         0
## salmon           2   1           0      0         1     1     0         0
##             saddlebrown ivory brown steelblue darkred lightcyan1 pink salmon
## pink                  2     2     1         0       0          0    0      1
## greenyellow           0     0     0         2       0          0    2      1
## blue                  4     1     5         2       1          0   15      2
## green                 2     0     4         1       0          0    5      1
## yellow                1     1     2         1       0          0    3      2
## turquoise             1     1     3         2       0          0    6      5
## black                 0     1     2         1       0          0    2      0
## red                   0     1     1         3       2          0    6      2
## magenta               0     1     1         1       1          0    1      0
## tan                   0     0     3         0       0          0    0      0
## brown                 2     0     2         2       1          0    6      1
## purple                1     0     2         1       0          0    2      0
## salmon                1     0     0         0       0          0    0      0
##             white blue midnightblue darkorange cyan paleturquoise
## pink            0    3            0          0    6             2
## greenyellow     0    3            2          0    1             0
## blue            0   21            2          0    7             0
## green           0   14            2          1    5             0
## yellow          0   14            2          1    4             1
## turquoise       1   15            5          0    5             1
## black           0    2            1          0    1             0
## red             0    8            7          0    3             1
## magenta         0    4            4          0    1             0
## tan             0    2            1          0    3             0
## brown           0   12            7          1    3             1
## purple          0    4            1          0    4             1
## salmon          0    1            0          0    0             0
##             darkolivegreen red darkgrey darkturquoise violet plum1 magenta
## pink                     0   1        1             0      1     0       0
## greenyellow              0   1        0             1      0     0       1
## blue                     0   3        2             0      2     2       1
## green                    0   1        0             2      0     0       1
## yellow                   1   1        0             2      1     1       0
## turquoise                3   3        1             1      4     2       0
## black                    1   0        0             0      0     0       2
## red                      0   0        0             0      1     1       1
## magenta                  1   0        0             0      0     1       1
## tan                      0   0        1             0      0     0       1
## brown                    1   0        1             0      0     0       0
## purple                   0   0        1             0      0     1       1
## salmon                   0   0        0             0      0     0       0
##             yellowgreen purple skyblue3 royalblue lightcyan lightyellow
## pink                  1      0        1         0         0           0
## greenyellow           2      1        1         0         0           0
## blue                  1      3        2         1         2           1
## green                 0      3        2         0         1           0
## yellow                0      2        1         0         3           0
## turquoise             1      3        0         0         7           0
## black                 2      0        0         0         1           0
## red                   1      1        0         1         2           1
## magenta               1      0        1         0         0           0
## tan                   0      0        0         0         0           0
## brown                 1      1        1         0         2           1
## purple                0      1        0         0         1           1
## salmon                0      1        0         0         0           0
##             lightgreen orangered4 grey60 lightsteelblue1 skyblue darkmagenta
## pink                 1          0      0               1       1           0
## greenyellow          1          0      0               0       0           0
## blue                 2          0      1               0       3           0
## green                0          1      0               0       3           1
## yellow               0          0      0               0       3           0
## turquoise            2          3      0               0       0           0
## black                0          0      0               0       2           1
## red                  1          3      0               0       1           0
## magenta              0          0      1               0       0           0
## tan                  0          1      0               0       2           1
## brown                2          1      0               0       1           1
## purple               0          0      0               1       1           0
## salmon               1          0      0               1       0           0
##             sienna3 mediumpurple3 floralwhite
## pink              2             0           0
## greenyellow       0             0           0
## blue              1             0           3
## green             0             0           1
## yellow            1             0           2
## turquoise         1             0           6
## black             2             0           0
## red               0             0           0
## magenta           0             0           0
## tan               0             0           0
## brown             0             0           0
## purple            0             0           1
## salmon            0             0           0
#To determine which CAMK2A modules overlap the most with MEGA modules and then assess their statistical significance, you can use the following approach:
#Steps:
#1-Calculate the number of overlapping genes between each CAMK2A module and each MEGA module.
#2-Construct a contingency table for each pair of CAMK2A and MEGA modules.
#3-Perform a statistical test, such as Fisher’s exact test or a hypergeometric test, to evaluate the significance of the overlap between modules.
#4-Summarize the results to identify which CAMK2A modules have the most overlap with which MEGA modules and whether that overlap is statistically significant.
#1. Calculate Overlapping Genes Between CAMK2A and MEGA Modules
##Statistical Significance of the Overlap (Hypergeometric or Fisher’s Exact Test)
#2.1. Performing a Hypergeometric Test
#A hypergeometric test will test whether the overlap is greater than expected by chance, given the total number of genes in both datasets.

# Perform hypergeometric test for each CAMK2A-MEGA module pair
pvalMatrix <- matrix(NA, nrow = length(camk2aModules), ncol = length(megaModules))
rownames(pvalMatrix) <- camk2aModules
colnames(pvalMatrix) <- megaModules

# Total number of genes in the universe (total genes in both datasets combined)
totalGenes <- length(unique(c(camk2aSymbols, MEGAsymbols)))

for (camk2aModule in camk2aModules) {
  camk2aGenes <- camk2aSymbols[which(netCAMK2A$colors == camk2aModule)]
  numCamk2aGenes <- length(camk2aGenes)
  
  for (megaModule in megaModules) {
    megaGenes <- MEGAsymbols[which(netMEGA$colors == megaModule)]
    numMegaGenes <- length(megaGenes)
    
    overlap <- intersect(camk2aGenes, megaGenes)
    numOverlap <- length(overlap)
    
    # Hypergeometric test
    pvalMatrix[camk2aModule, megaModule] <- phyper(numOverlap - 1, numCamk2aGenes, totalGenes - numCamk2aGenes, numMegaGenes, lower.tail = FALSE)
  }
}

# Print p-value matrix
print(pvalMatrix)
##                   yellow       tan greenyellow     orange    turquoise
## pink        0.6238139690 0.3300336   0.4178549 0.03836807 5.755144e-01
## greenyellow 1.0000000000 1.0000000   0.6178317 1.00000000 7.578316e-01
## blue        0.1352080087 0.6200609   0.8926047 0.50929486 5.358546e-04
## green       0.0463243339 0.8988086   0.5142032 0.05443058 2.351269e-01
## yellow      0.3154680127 0.7387716   0.3851620 0.08306062 1.487673e-02
## turquoise   0.3637003570 0.4383047   0.4265218 0.02002751 2.339155e-05
## black       0.0188426230 1.0000000   1.0000000 0.17589386 1.102920e-01
## red         0.2041586347 0.1840244   0.9358243 0.16809075 1.362418e-01
## magenta     0.0468616497 0.6772456   1.0000000 0.15729852 8.218141e-02
## tan         1.0000000000 1.0000000   1.0000000 1.00000000 7.839499e-01
## brown       0.0009190771 0.7543711   0.9634922 0.82239477 3.870414e-02
## purple      0.2669977146 1.0000000   0.7109662 0.47692364 2.596132e-01
## salmon      0.1653875025 0.3415915   1.0000000 1.00000000 7.675744e-01
##                  green     black   darkgreen saddlebrown      ivory     brown
## pink        0.26486485 0.8193571 0.001505317   0.1146342 0.01936605 0.9472364
## greenyellow 0.11545912 1.0000000 0.018498763   1.0000000 1.00000000 1.0000000
## blue        0.05908699 0.8623733 0.086030668   0.1584804 0.54618942 0.9799500
## green       0.05491744 0.2184302 0.078780434   0.3058112 1.00000000 0.8033498
## yellow      0.76166333 0.8905824 0.562269303   0.7281007 0.38169535 0.9887054
## turquoise   0.03238808 0.5893865 0.824250095   0.8872204 0.55319119 0.9986475
## black       0.54747716 0.5137101 0.054742679   1.0000000 0.19776367 0.7953914
## red         0.05273287 0.3944990 0.216433251   1.0000000 0.33742298 0.9962753
## magenta     1.00000000 1.0000000 1.000000000   1.0000000 0.18543682 0.9383667
## tan         0.48827580 0.4668749 1.000000000   1.0000000 1.00000000 0.0851671
## brown       0.77998077 0.5367798 0.304585826   0.3839383 1.00000000 0.9905792
## purple      0.45771533 0.1747175 1.000000000   0.3958750 1.00000000 0.7123845
## salmon      0.47166527 1.0000000 1.000000000   0.1856175 1.00000000 1.0000000
##              steelblue   darkred lightcyan1         pink    salmon     white
## pink        1.00000000 1.0000000          1 1.0000000000 0.6924775 1.0000000
## greenyellow 0.04738233 1.0000000          1 0.3054007241 0.5439640 1.0000000
## blue        0.56903819 0.9645232          1 0.0009992517 0.9268487 1.0000000
## green       0.63713294 1.0000000          1 0.2134766911 0.8959115 1.0000000
## yellow      0.69059320 1.0000000          1 0.7179700973 0.7325502 1.0000000
## turquoise   0.57959984 1.0000000          1 0.5809242941 0.4278411 0.9185335
## black       0.41588481 1.0000000          1 0.5090883217 1.0000000 1.0000000
## red         0.07688446 0.5142971          1 0.0959509620 0.6512940 1.0000000
## magenta     0.39374214 0.5796714          1 0.7994704711 1.0000000 1.0000000
## tan         1.00000000 1.0000000          1 1.0000000000 1.0000000 1.0000000
## brown       0.33652054 0.8771430          1 0.1800472248 0.9329331 1.0000000
## purple      0.36489664 1.0000000          1 0.4216919289 1.0000000 1.0000000
## salmon      1.00000000 1.0000000          1 1.0000000000 1.0000000 1.0000000
##                    blue midnightblue darkorange        cyan paleturquoise
## pink        0.558556174  1.000000000  1.0000000 0.001009651     0.0941319
## greenyellow 0.303383570  0.174429797  1.0000000 0.543963979     1.0000000
## blue        0.001733351  0.920233375  1.0000000 0.128008747     1.0000000
## green       0.001164893  0.642711245  0.7405175 0.071732522     1.0000000
## yellow      0.004580315  0.719733655  0.7901102 0.255890782     0.6855577
## turquoise   0.113582474  0.406813279  1.0000000 0.427841133     0.8561115
## black       0.801172653  0.689646042  1.0000000 0.698797253     1.0000000
## red         0.181981121  0.005625638  1.0000000 0.379125452     0.6286212
## magenta     0.293113622  0.022096755  1.0000000 0.672716665     1.0000000
## tan         0.289023518  0.344682769  1.0000000 0.008464912     1.0000000
## brown       0.032072102  0.014861309  0.8003416 0.497617348     0.6969900
## purple      0.236331898  0.627647650  1.0000000 0.017355802     0.3609166
## salmon      0.647626174  1.000000000  1.0000000 1.000000000     1.0000000
##             darkolivegreen       red  darkgrey darkturquoise    violet
## pink             1.0000000 0.8372047 0.5530629     1.0000000 0.4061227
## greenyellow      1.0000000 0.7014139 1.0000000     0.4150576 1.0000000
## blue             1.0000000 0.9589752 0.7882699     1.0000000 0.5615052
## green            1.0000000 0.9692837 1.0000000     0.4525123 1.0000000
## yellow           0.6479582 0.9822366 1.0000000     0.5290441 0.6855577
## turquoise        0.2464761 0.9627447 0.9500322     0.9500322 0.1226121
## black            0.3802828 1.0000000 1.0000000     1.0000000 1.0000000
## red              1.0000000 1.0000000 1.0000000     1.0000000 0.6286212
## magenta          0.3594187 1.0000000 1.0000000     1.0000000 1.0000000
## tan              1.0000000 1.0000000 0.2562303     1.0000000 1.0000000
## brown            0.6595288 1.0000000 0.8420287     1.0000000 1.0000000
## purple           1.0000000 1.0000000 0.4994033     1.0000000 1.0000000
## salmon           1.0000000 1.0000000 1.0000000     1.0000000 1.0000000
##                 plum1   magenta yellowgreen    purple  skyblue3 royalblue
## pink        1.0000000 1.0000000  0.35202555 1.0000000 0.3329392 1.0000000
## greenyellow 1.0000000 0.6687376  0.03316919 0.6215743 0.2363079 1.0000000
## blue        0.4016486 0.9976537  0.79468149 0.8964967 0.4288283 0.9762457
## green       1.0000000 0.9585672  1.00000000 0.5210016 0.1791352 1.0000000
## yellow      0.5728473 1.0000000  1.00000000 0.8312943 0.5929967 1.0000000
## turquoise   0.4110693 1.0000000  0.80098035 0.9036974 1.0000000 1.0000000
## black       1.0000000 0.4974138  0.07090508 1.0000000 1.0000000 1.0000000
## red         0.5172510 0.9573304  0.56168250 0.9376024 1.0000000 0.8574974
## magenta     0.3043290 0.7922990  0.33700548 1.0000000 0.3185223 1.0000000
## tan         1.0000000 0.4565986  1.00000000 1.0000000 1.0000000 1.0000000
## brown       1.0000000 1.0000000  0.62999105 0.9647080 0.6045414 1.0000000
## purple      0.2804822 0.7596536  1.00000000 0.7146135 1.0000000 1.0000000
## salmon      1.0000000 1.0000000  1.00000000 0.4000153 1.0000000 1.0000000
##             lightcyan lightyellow lightgreen orangered4    grey60
## pink        1.0000000   1.0000000  0.6591564 1.00000000 1.0000000
## greenyellow 1.0000000   1.0000000  0.5116310 1.00000000 1.0000000
## blue        0.9130610   0.9768731  0.9011669 1.00000000 0.9827759
## green       0.8834780   1.0000000  1.0000000 0.48597956 1.0000000
## yellow      0.4437126   1.0000000  1.0000000 1.00000000 1.0000000
## turquoise   0.1135607   1.0000000  0.9070573 0.13315666 1.0000000
## black       0.6802217   1.0000000  1.0000000 1.00000000 1.0000000
## red         0.6237032   0.8594708  0.8707550 0.02719222 1.0000000
## magenta     1.0000000   1.0000000  1.0000000 1.00000000 0.6515525
## tan         1.0000000   1.0000000  1.0000000 0.11970532 1.0000000
## brown       0.7226075   0.9060903  0.7019486 0.54831635 1.0000000
## purple      0.6181231   0.5880946  1.0000000 1.00000000 1.0000000
## salmon      1.0000000   1.0000000  0.3139258 1.00000000 1.0000000
##             lightsteelblue1    skyblue darkmagenta    sienna3 mediumpurple3
## pink             0.23446742 0.46364539   1.0000000 0.06878518             1
## greenyellow      1.00000000 1.00000000   1.0000000 1.00000000             1
## blue             1.00000000 0.38675442   1.0000000 0.79468149             1
## green            1.00000000 0.11413954   0.5710570 1.00000000             1
## yellow           1.00000000 0.15597545   1.0000000 0.61840274             1
## turquoise        1.00000000 1.00000000   1.0000000 0.80098035             1
## black            1.00000000 0.13011623   0.3616905 0.07090508             1
## red              1.00000000 0.69400784   1.0000000 1.00000000             1
## magenta          1.00000000 1.00000000   1.0000000 1.00000000             1
## tan              1.00000000 0.02130863   0.1496975 1.00000000             1
## brown            1.00000000 0.76007598   0.6360953 1.00000000             1
## purple           0.20512267 0.41447951   1.0000000 1.00000000             1
## salmon           0.08928843 1.00000000   1.0000000 1.00000000             1
##              floralwhite
## pink        1.000000e+00
## greenyellow 1.000000e+00
## blue        3.501014e-02
## green       3.213908e-01
## yellow      7.224877e-02
## turquoise   7.377017e-05
## black       1.000000e+00
## red         1.000000e+00
## magenta     1.000000e+00
## tan         1.000000e+00
## brown       1.000000e+00
## purple      1.593902e-01
## salmon      1.000000e+00
#2.2. Fisher’s Exact Test
#Alternatively, you can use Fisher’s exact test to compare the contingency table of overlapping and non-overlapping genes between modules:
# Perform Fisher's exact test for each CAMK2A-MEGA module pair
pvalMatrixFisher <- matrix(NA, nrow = length(camk2aModules), ncol = length(megaModules))
rownames(pvalMatrixFisher) <- camk2aModules
colnames(pvalMatrixFisher) <- megaModules

for (camk2aModule in camk2aModules) {
  camk2aGenes <- camk2aSymbols[which(netCAMK2A$colors == camk2aModule)]
  numCamk2aGenes <- length(camk2aGenes)
  
  for (megaModule in megaModules) {
    megaGenes <- MEGAsymbols[which(netMEGA$colors == megaModule)]
    numMegaGenes <- length(megaGenes)
    
    overlap <- intersect(camk2aGenes, megaGenes)
    numOverlap <- length(overlap)
    
    # Create a contingency table
    contingencyTable <- matrix(c(numOverlap, numCamk2aGenes - numOverlap, 
                                 numMegaGenes - numOverlap, 
                                 totalGenes - (numCamk2aGenes + numMegaGenes - numOverlap)),
                               nrow = 2)
    
    # Fisher's exact test
    fisherResult <- fisher.test(contingencyTable, alternative = "greater")
    pvalMatrixFisher[camk2aModule, megaModule] <- fisherResult$p.value
  }
}

# Print p-value matrix from Fisher's test
print(pvalMatrixFisher)
##                   yellow       tan greenyellow     orange    turquoise
## pink        0.6238139690 0.3300336   0.4178549 0.03836807 5.755144e-01
## greenyellow 1.0000000000 1.0000000   0.6178317 1.00000000 7.578316e-01
## blue        0.1352080087 0.6200609   0.8926047 0.50929486 5.358546e-04
## green       0.0463243339 0.8988086   0.5142032 0.05443058 2.351269e-01
## yellow      0.3154680127 0.7387716   0.3851620 0.08306062 1.487673e-02
## turquoise   0.3637003570 0.4383047   0.4265218 0.02002751 2.339155e-05
## black       0.0188426230 1.0000000   1.0000000 0.17589386 1.102920e-01
## red         0.2041586347 0.1840244   0.9358243 0.16809075 1.362418e-01
## magenta     0.0468616497 0.6772456   1.0000000 0.15729852 8.218141e-02
## tan         1.0000000000 1.0000000   1.0000000 1.00000000 7.839499e-01
## brown       0.0009190771 0.7543711   0.9634922 0.82239477 3.870414e-02
## purple      0.2669977146 1.0000000   0.7109662 0.47692364 2.596132e-01
## salmon      0.1653875025 0.3415915   1.0000000 1.00000000 7.675744e-01
##                  green     black   darkgreen saddlebrown      ivory     brown
## pink        0.26486485 0.8193571 0.001505317   0.1146342 0.01936605 0.9472364
## greenyellow 0.11545912 1.0000000 0.018498763   1.0000000 1.00000000 1.0000000
## blue        0.05908699 0.8623733 0.086030668   0.1584804 0.54618942 0.9799500
## green       0.05491744 0.2184302 0.078780434   0.3058112 1.00000000 0.8033498
## yellow      0.76166333 0.8905824 0.562269303   0.7281007 0.38169535 0.9887054
## turquoise   0.03238808 0.5893865 0.824250095   0.8872204 0.55319119 0.9986475
## black       0.54747716 0.5137101 0.054742679   1.0000000 0.19776367 0.7953914
## red         0.05273287 0.3944990 0.216433251   1.0000000 0.33742298 0.9962753
## magenta     1.00000000 1.0000000 1.000000000   1.0000000 0.18543682 0.9383667
## tan         0.48827580 0.4668749 1.000000000   1.0000000 1.00000000 0.0851671
## brown       0.77998077 0.5367798 0.304585826   0.3839383 1.00000000 0.9905792
## purple      0.45771533 0.1747175 1.000000000   0.3958750 1.00000000 0.7123845
## salmon      0.47166527 1.0000000 1.000000000   0.1856175 1.00000000 1.0000000
##              steelblue   darkred lightcyan1         pink    salmon     white
## pink        1.00000000 1.0000000          1 1.0000000000 0.6924775 1.0000000
## greenyellow 0.04738233 1.0000000          1 0.3054007241 0.5439640 1.0000000
## blue        0.56903819 0.9645232          1 0.0009992517 0.9268487 1.0000000
## green       0.63713294 1.0000000          1 0.2134766911 0.8959115 1.0000000
## yellow      0.69059320 1.0000000          1 0.7179700973 0.7325502 1.0000000
## turquoise   0.57959984 1.0000000          1 0.5809242941 0.4278411 0.9185335
## black       0.41588481 1.0000000          1 0.5090883217 1.0000000 1.0000000
## red         0.07688446 0.5142971          1 0.0959509620 0.6512940 1.0000000
## magenta     0.39374214 0.5796714          1 0.7994704711 1.0000000 1.0000000
## tan         1.00000000 1.0000000          1 1.0000000000 1.0000000 1.0000000
## brown       0.33652054 0.8771430          1 0.1800472248 0.9329331 1.0000000
## purple      0.36489664 1.0000000          1 0.4216919289 1.0000000 1.0000000
## salmon      1.00000000 1.0000000          1 1.0000000000 1.0000000 1.0000000
##                    blue midnightblue darkorange        cyan paleturquoise
## pink        0.558556174  1.000000000  1.0000000 0.001009651     0.0941319
## greenyellow 0.303383570  0.174429797  1.0000000 0.543963979     1.0000000
## blue        0.001733351  0.920233375  1.0000000 0.128008747     1.0000000
## green       0.001164893  0.642711245  0.7405175 0.071732522     1.0000000
## yellow      0.004580315  0.719733655  0.7901102 0.255890782     0.6855577
## turquoise   0.113582474  0.406813279  1.0000000 0.427841133     0.8561115
## black       0.801172653  0.689646042  1.0000000 0.698797253     1.0000000
## red         0.181981121  0.005625638  1.0000000 0.379125452     0.6286212
## magenta     0.293113622  0.022096755  1.0000000 0.672716665     1.0000000
## tan         0.289023518  0.344682769  1.0000000 0.008464912     1.0000000
## brown       0.032072102  0.014861309  0.8003416 0.497617348     0.6969900
## purple      0.236331898  0.627647650  1.0000000 0.017355802     0.3609166
## salmon      0.647626174  1.000000000  1.0000000 1.000000000     1.0000000
##             darkolivegreen       red  darkgrey darkturquoise    violet
## pink             1.0000000 0.8372047 0.5530629     1.0000000 0.4061227
## greenyellow      1.0000000 0.7014139 1.0000000     0.4150576 1.0000000
## blue             1.0000000 0.9589752 0.7882699     1.0000000 0.5615052
## green            1.0000000 0.9692837 1.0000000     0.4525123 1.0000000
## yellow           0.6479582 0.9822366 1.0000000     0.5290441 0.6855577
## turquoise        0.2464761 0.9627447 0.9500322     0.9500322 0.1226121
## black            0.3802828 1.0000000 1.0000000     1.0000000 1.0000000
## red              1.0000000 1.0000000 1.0000000     1.0000000 0.6286212
## magenta          0.3594187 1.0000000 1.0000000     1.0000000 1.0000000
## tan              1.0000000 1.0000000 0.2562303     1.0000000 1.0000000
## brown            0.6595288 1.0000000 0.8420287     1.0000000 1.0000000
## purple           1.0000000 1.0000000 0.4994033     1.0000000 1.0000000
## salmon           1.0000000 1.0000000 1.0000000     1.0000000 1.0000000
##                 plum1   magenta yellowgreen    purple  skyblue3 royalblue
## pink        1.0000000 1.0000000  0.35202555 1.0000000 0.3329392 1.0000000
## greenyellow 1.0000000 0.6687376  0.03316919 0.6215743 0.2363079 1.0000000
## blue        0.4016486 0.9976537  0.79468149 0.8964967 0.4288283 0.9762457
## green       1.0000000 0.9585672  1.00000000 0.5210016 0.1791352 1.0000000
## yellow      0.5728473 1.0000000  1.00000000 0.8312943 0.5929967 1.0000000
## turquoise   0.4110693 1.0000000  0.80098035 0.9036974 1.0000000 1.0000000
## black       1.0000000 0.4974138  0.07090508 1.0000000 1.0000000 1.0000000
## red         0.5172510 0.9573304  0.56168250 0.9376024 1.0000000 0.8574974
## magenta     0.3043290 0.7922990  0.33700548 1.0000000 0.3185223 1.0000000
## tan         1.0000000 0.4565986  1.00000000 1.0000000 1.0000000 1.0000000
## brown       1.0000000 1.0000000  0.62999105 0.9647080 0.6045414 1.0000000
## purple      0.2804822 0.7596536  1.00000000 0.7146135 1.0000000 1.0000000
## salmon      1.0000000 1.0000000  1.00000000 0.4000153 1.0000000 1.0000000
##             lightcyan lightyellow lightgreen orangered4    grey60
## pink        1.0000000   1.0000000  0.6591564 1.00000000 1.0000000
## greenyellow 1.0000000   1.0000000  0.5116310 1.00000000 1.0000000
## blue        0.9130610   0.9768731  0.9011669 1.00000000 0.9827759
## green       0.8834780   1.0000000  1.0000000 0.48597956 1.0000000
## yellow      0.4437126   1.0000000  1.0000000 1.00000000 1.0000000
## turquoise   0.1135607   1.0000000  0.9070573 0.13315666 1.0000000
## black       0.6802217   1.0000000  1.0000000 1.00000000 1.0000000
## red         0.6237032   0.8594708  0.8707550 0.02719222 1.0000000
## magenta     1.0000000   1.0000000  1.0000000 1.00000000 0.6515525
## tan         1.0000000   1.0000000  1.0000000 0.11970532 1.0000000
## brown       0.7226075   0.9060903  0.7019486 0.54831635 1.0000000
## purple      0.6181231   0.5880946  1.0000000 1.00000000 1.0000000
## salmon      1.0000000   1.0000000  0.3139258 1.00000000 1.0000000
##             lightsteelblue1    skyblue darkmagenta    sienna3 mediumpurple3
## pink             0.23446742 0.46364539   1.0000000 0.06878518             1
## greenyellow      1.00000000 1.00000000   1.0000000 1.00000000             1
## blue             1.00000000 0.38675442   1.0000000 0.79468149             1
## green            1.00000000 0.11413954   0.5710570 1.00000000             1
## yellow           1.00000000 0.15597545   1.0000000 0.61840274             1
## turquoise        1.00000000 1.00000000   1.0000000 0.80098035             1
## black            1.00000000 0.13011623   0.3616905 0.07090508             1
## red              1.00000000 0.69400784   1.0000000 1.00000000             1
## magenta          1.00000000 1.00000000   1.0000000 1.00000000             1
## tan              1.00000000 0.02130863   0.1496975 1.00000000             1
## brown            1.00000000 0.76007598   0.6360953 1.00000000             1
## purple           0.20512267 0.41447951   1.0000000 1.00000000             1
## salmon           0.08928843 1.00000000   1.0000000 1.00000000             1
##              floralwhite
## pink        1.000000e+00
## greenyellow 1.000000e+00
## blue        3.501014e-02
## green       3.213908e-01
## yellow      7.224877e-02
## turquoise   7.377017e-05
## black       1.000000e+00
## red         1.000000e+00
## magenta     1.000000e+00
## tan         1.000000e+00
## brown       1.000000e+00
## purple      1.593902e-01
## salmon      1.000000e+00
############################
#3. Adjusting for Multiple Comparisons
#Because you are performing multiple tests (one for each CAMK2A-MEGA module pair), it’s important to adjust the p-values to control for false discovery rate (FDR) using methods like Benjamini-Hochberg correction:
# Adjust p-values for multiple comparisons using the Benjamini-Hochberg method
adjustedPvalMatrix <- p.adjust(pvalMatrix, method = "BH")

# Or for Fisher's exact test:
adjustedPvalMatrixFisher <- p.adjust(pvalMatrixFisher, method = "BH")

# Print adjusted p-values
print(adjustedPvalMatrix)
##   [1] 1.00000000 1.00000000 1.00000000 0.87428045 1.00000000 1.00000000
##   [7] 0.60187351 1.00000000 0.87428045 1.00000000 0.09518842 1.00000000
##  [13] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [19] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [25] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [31] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [37] 1.00000000 1.00000000 1.00000000 0.79067025 1.00000000 1.00000000
##  [43] 0.89750794 1.00000000 0.60187351 1.00000000 1.00000000 1.00000000
##  [49] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [55] 0.09518842 1.00000000 0.60187351 0.01337997 1.00000000 1.00000000
##  [61] 1.00000000 1.00000000 0.79067025 1.00000000 1.00000000 1.00000000
##  [67] 1.00000000 0.93882659 0.89750794 1.00000000 0.75891096 1.00000000
##  [73] 0.89750794 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [79] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [85] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [91] 1.00000000 0.10763020 0.60187351 1.00000000 1.00000000 1.00000000
##  [97] 1.00000000 0.89750794 1.00000000 1.00000000 1.00000000 1.00000000
## [103] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [109] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [115] 1.00000000 1.00000000 1.00000000 0.60187351 1.00000000 1.00000000
## [121] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [127] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [133] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [139] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [145] 0.87428045 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [151] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [157] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [163] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [169] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [175] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [181] 1.00000000 1.00000000 1.00000000 1.00000000 0.09518842 1.00000000
## [187] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [193] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [199] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [205] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [211] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [217] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [223] 1.00000000 0.11016406 0.09518842 0.26199399 1.00000000 1.00000000
## [229] 1.00000000 1.00000000 1.00000000 0.75891096 1.00000000 1.00000000
## [235] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [241] 1.00000000 0.29253316 0.60187351 1.00000000 0.60187351 1.00000000
## [247] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [253] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [259] 1.00000000 1.00000000 0.09518842 1.00000000 1.00000000 1.00000000
## [265] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 0.40349416
## [271] 1.00000000 0.60187351 1.00000000 1.00000000 1.00000000 1.00000000
## [277] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [283] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [289] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [295] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [301] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [307] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [313] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [319] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [325] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [331] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [337] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [343] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [349] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [355] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [361] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [367] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [373] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [379] 0.75891096 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [385] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [391] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [397] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [403] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [409] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [415] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [421] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [427] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [433] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [439] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [445] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [451] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [457] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [463] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [469] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [475] 1.00000000 0.70699779 1.00000000 1.00000000 1.00000000 1.00000000
## [481] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [487] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [493] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [499] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [505] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [511] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [517] 0.60187351 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [523] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [529] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [535] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [541] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [547] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [553] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [559] 1.00000000 1.00000000 1.00000000 0.77022318 1.00000000 1.00000000
## [565] 0.02109827 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [571] 1.00000000 1.00000000
print(adjustedPvalMatrixFisher)
##   [1] 1.00000000 1.00000000 1.00000000 0.87428045 1.00000000 1.00000000
##   [7] 0.60187351 1.00000000 0.87428045 1.00000000 0.09518842 1.00000000
##  [13] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [19] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [25] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [31] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [37] 1.00000000 1.00000000 1.00000000 0.79067025 1.00000000 1.00000000
##  [43] 0.89750794 1.00000000 0.60187351 1.00000000 1.00000000 1.00000000
##  [49] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [55] 0.09518842 1.00000000 0.60187351 0.01337997 1.00000000 1.00000000
##  [61] 1.00000000 1.00000000 0.79067025 1.00000000 1.00000000 1.00000000
##  [67] 1.00000000 0.93882659 0.89750794 1.00000000 0.75891096 1.00000000
##  [73] 0.89750794 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [79] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [85] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##  [91] 1.00000000 0.10763020 0.60187351 1.00000000 1.00000000 1.00000000
##  [97] 1.00000000 0.89750794 1.00000000 1.00000000 1.00000000 1.00000000
## [103] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [109] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [115] 1.00000000 1.00000000 1.00000000 0.60187351 1.00000000 1.00000000
## [121] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [127] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [133] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [139] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [145] 0.87428045 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [151] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [157] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [163] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [169] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [175] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [181] 1.00000000 1.00000000 1.00000000 1.00000000 0.09518842 1.00000000
## [187] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [193] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [199] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [205] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [211] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [217] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [223] 1.00000000 0.11016406 0.09518842 0.26199399 1.00000000 1.00000000
## [229] 1.00000000 1.00000000 1.00000000 0.75891096 1.00000000 1.00000000
## [235] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [241] 1.00000000 0.29253316 0.60187351 1.00000000 0.60187351 1.00000000
## [247] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [253] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [259] 1.00000000 1.00000000 0.09518842 1.00000000 1.00000000 1.00000000
## [265] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 0.40349416
## [271] 1.00000000 0.60187351 1.00000000 1.00000000 1.00000000 1.00000000
## [277] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [283] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [289] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [295] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [301] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [307] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [313] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [319] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [325] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [331] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [337] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [343] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [349] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [355] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [361] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [367] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [373] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [379] 0.75891096 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [385] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [391] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [397] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [403] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [409] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [415] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [421] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [427] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [433] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [439] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [445] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [451] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [457] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [463] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [469] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [475] 1.00000000 0.70699779 1.00000000 1.00000000 1.00000000 1.00000000
## [481] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [487] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [493] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [499] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [505] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [511] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [517] 0.60187351 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [523] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [529] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [535] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [541] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [547] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [553] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [559] 1.00000000 1.00000000 1.00000000 0.77022318 1.00000000 1.00000000
## [565] 0.02109827 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## [571] 1.00000000 1.00000000
#4. Identify Significant Overlaps
#You can now identify which module pairs have a statistically significant overlap by applying a threshold (e.g., p-value < 0.05):
# Identify significant overlaps (e.g., p-value < 0.05)
significantOverlaps <- which(adjustedPvalMatrix < 0.05, arr.ind = TRUE)
print(significantOverlaps)
## [1]  58 565
# For Fisher's exact test:
significantOverlapsFisher <- which(adjustedPvalMatrixFisher < 0.05, arr.ind = TRUE)
print(significantOverlapsFisher)
## [1]  58 565
# Example datasets: camk2aSymbols and megaSymbols are lists of gene symbols for each module

# Initialize a matrix to store the overlap counts between CAMK2A and MEGA modules
camk2aModules <- unique(netCAMK2A$colors)  # List of CAMK2A module colors
megaModules <- unique(netMEGA$colors)      # List of MEGA module colors

# Initialize matrices to store overlap counts and p-values
overlapMatrix <- matrix(0, nrow = length(camk2aModules), ncol = length(megaModules))
rownames(overlapMatrix) <- camk2aModules
colnames(overlapMatrix) <- megaModules

pvalMatrix <- matrix(NA, nrow = length(camk2aModules), ncol = length(megaModules))
rownames(pvalMatrix) <- camk2aModules
colnames(pvalMatrix) <- megaModules

# Perform overlap calculation and Fisher's exact test
totalGenes <- length(unique(c(rownames(cleanDat), rownames(cleanDatMEGA))))  # Total number of unique genes

for (camkMod in camk2aModules) {
  camkGenes <- rownames(cleanDat)[netCAMK2A$colors == camkMod]  # Genes in CAMK2A module
  
  for (megaMod in megaModules) {
    megaGenes <- rownames(cleanDatMEGA)[netMEGA$colors == megaMod]  # Genes in MEGA module
    
    # Calculate overlap
    overlapGenes <- intersect(camkGenes, megaGenes)
    overlapCount <- length(overlapGenes)
    overlapMatrix[camkMod, megaMod] <- overlapCount  # Store overlap count
    
    # Create contingency table for Fisher's Exact Test
    a <- overlapCount  # Number of overlapping genes
    b <- length(camkGenes) - a  # Genes in CAMK2A module but not in MEGA
    c <- length(megaGenes) - a  # Genes in MEGA module but not in CAMK2A
    d <- totalGenes - (a + b + c)  # Genes not in either module
    
    contingencyTable <- matrix(c(a, b, c, d), nrow = 2)
    
    # Fisher's Exact Test to check significance of the overlap
    testResult <- fisher.test(contingencyTable, alternative = "greater")
    
    # Store p-value
    pvalMatrix[camkMod, megaMod] <- testResult$p.value
  }
}

# View the overlap matrix and p-value matrix
print("Overlap Matrix (Counts of Overlapping Genes):")
## [1] "Overlap Matrix (Counts of Overlapping Genes):"
print(overlapMatrix)
##             yellow tan greenyellow orange turquoise green grey black darkgreen
## pink             3   2           1      0        13     8    1     0         5
## greenyellow      1   0           0      0         8     2    1     0         6
## blue            19   2           3      2        25    10   20     1         0
## green            6   0           1      0        13     9   11     1         1
## grey            23   5           9      8        47    13   33    10         7
## yellow           3   1           1      5        10     4    9     6         5
## turquoise       12   1           7     15        25     2    7    16         1
## black            3   1           0      3         7     1    3     4         1
## red              7   2           1      0        26    14    8     0         3
## magenta          4   1           1      4         7     2    6     1         0
## tan              0   0           1      1         2     1    2     1         1
## brown            6   3           7      2         7     4    5     3         2
## purple           4   0           1      3        12     3    5     1         2
## salmon           1   0           0      0         2     0    1     1         1
##             saddlebrown ivory brown steelblue darkred lightcyan1 pink salmon
## pink                  0     0     1         0       1          0    2      0
## greenyellow           0     0     1         0       0          0    0      0
## blue                  0     0     2         1       1          0    6      0
## green                 1     0     2         0       1          0    5      0
## grey                  5     2    15         8       1          0   16      7
## yellow                3     2     4         5       0          0    3      9
## turquoise             0     3    11         5       1          0   17      2
## black                 1     0     2         5       0          0    1      1
## red                   0     0     0         2       0          0    4      1
## magenta               1     1     0         0       0          0    3      0
## tan                   1     1     0         0       0          0    0      1
## brown                 7     0     2         0       2          1    3      4
## purple                0     0     1         1       0          0    2      0
## salmon                1     0     0         1       0          0    0      0
##             white blue midnightblue darkorange cyan paleturquoise
## pink            0    3            2          0    3             1
## greenyellow     0    1            3          1    3             0
## blue            0   56            8          1    1             0
## green           0   22            4          0    1             2
## grey            1   33           11          0   12             5
## yellow          0    0            3          0    9             1
## turquoise       1    4            9          1   23             0
## black           0    1            2          0   10             1
## red             0   11            2          1    3             2
## magenta         0    3            1          0    0             0
## tan             0    1            0          0    1             0
## brown           0    9            1          0    2             0
## purple          0    5            1          0    0             0
## salmon          0    1            0          0    2             0
##             darkolivegreen red darkgrey darkturquoise violet plum1 magenta
## pink                     0   1        0             0      0     1       0
## greenyellow              0   0        0             1      0     1       0
## blue                     1   6        0             2      2     0       1
## green                    1   2        0             0      1     2       1
## grey                     3   1        2             4      4     4       2
## yellow                   0   1        2             0      1     1       0
## turquoise                3   1        5             2      1     3       0
## black                    1   0        0             0      1     1       1
## red                      0   2        0             1      0     0       1
## magenta                  0   1        0             1      0     0       1
## tan                      0   0        0             1      0     1       0
## brown                    0   0        0             1      1     0       4
## purple                   0   0        0             0      0     0       0
## salmon                   0   0        0             0      0     0       0
##             yellowgreen purple skyblue3 royalblue lightcyan lightyellow
## pink                  1      0        0         0         0           1
## greenyellow           0      1        0         1         2           0
## blue                  4      2        1         0         1           0
## green                 1      0        0         0         1           0
## grey                  6      4        2         3         3           1
## yellow                1      4        1         1         1           0
## turquoise             2      3        5         2         5           1
## black                 0      1        0         0         2           0
## red                   1      2        0         0         1           0
## magenta               0      0        1         0         1           0
## tan                   0      0        0         1         0           0
## brown                 1      3        0         0         3           0
## purple                0      1        0         0         0           0
## salmon                1      0        0         0         2           1
##             lightgreen orangered4 grey60 lightsteelblue1 skyblue darkmagenta
## pink                 0          0      0               0       0           0
## greenyellow          0          0      0               0       1           0
## blue                 0          0      1               0       0           4
## green                3          0      0               0       3           0
## grey                 0          3      1               1       6           2
## yellow               4          2      1               0       2           1
## turquoise            2          1      0               0       0           0
## black                0          1      0               0       0           0
## red                  0          0      0               0       0           0
## magenta              0          3      0               0       1           0
## tan                  0          0      0               0       0           0
## brown                3          1      0               0      18           0
## purple               0          0      0               0       0           0
## salmon               1          2      0               0       1           0
##             sienna3 mediumpurple3 floralwhite
## pink              0             0           0
## greenyellow       0             0           0
## blue              0             0           0
## green             2             0           2
## grey              1             0           1
## yellow            5             0           0
## turquoise         0             0           0
## black             0             0           0
## red               0             0           0
## magenta           1             0           0
## tan               0             0           0
## brown             1             0          17
## purple            0             0           0
## salmon            0             0           0
print("P-Value Matrix (Fisher's Exact Test Results):")
## [1] "P-Value Matrix (Fisher's Exact Test Results):"
print(pvalMatrix)
##                   yellow       tan greenyellow       orange    turquoise
## pink        2.935239e-01 0.2889259  0.72922394 1.000000e+00 4.462012e-05
## greenyellow 7.235263e-01 1.0000000  1.00000000 1.000000e+00 2.285833e-03
## blue        5.202014e-05 0.9020261  0.84923810 7.064645e-01 1.346248e-03
## green       1.539242e-01 1.0000000  0.91839714 1.000000e+00 2.066343e-02
## grey        1.813541e-03 0.8060052  0.41979125 6.204639e-02 1.367195e-06
## yellow      7.944547e-01 0.9090016  0.94495437 1.682257e-02 2.808405e-01
## turquoise   4.696051e-02 0.9819488  0.19717159 2.168282e-08 1.765740e-03
## black       3.026700e-01 0.6667930  1.00000000 3.126596e-02 7.376711e-02
## red         6.751066e-02 0.6037842  0.91648728 1.000000e+00 2.218654e-09
## magenta     1.036203e-01 0.6404716  0.70988752 3.751742e-03 5.382505e-02
## tan         1.000000e+00 1.0000000  0.38141838 2.219171e-01 3.901104e-01
## brown       2.630534e-01 0.4405291  0.02771937 4.586216e-01 7.294933e-01
## purple      7.868240e-02 1.0000000  0.67456060 2.014453e-02 4.120353e-05
## salmon      4.913242e-01 1.0000000  1.00000000 1.000000e+00 3.668533e-01
##                    green      grey        black    darkgreen  saddlebrown
## pink        1.876545e-04 1.0000000 1.0000000000 9.725258e-04 1.0000000000
## greenyellow 2.938602e-01 0.9999897 1.0000000000 1.017053e-05 1.0000000000
## blue        6.883319e-02 1.0000000 0.9964500763 1.000000e+00 1.0000000000
## green       3.752884e-03 0.9999918 0.9485817632 7.738289e-01 0.6387495366
## grey        2.182641e-01 1.0000000 0.4879270191 2.071768e-01 0.2355469863
## yellow      4.829662e-01 1.0000000 0.1206185786 2.735218e-02 0.1107328791
## turquoise   9.840581e-01 1.0000000 0.0001515875 9.439071e-01 1.0000000000
## black       8.130457e-01 0.9999945 0.0686125792 5.455333e-01 0.4173637280
## red         1.998035e-06 0.9999999 1.0000000000 1.778605e-01 1.0000000000
## magenta     4.565180e-01 0.9980805 0.7690520672 1.000000e+00 0.3951783531
## tan         4.543877e-01 0.9851683 0.4338243401 2.479334e-01 0.1773060549
## brown       5.074568e-01 1.0000000 0.6793775703 5.253359e-01 0.0001875714
## purple      1.619571e-01 0.9984217 0.7353842305 1.407503e-01 1.0000000000
## salmon      1.000000e+00 0.9976149 0.4182598307 2.376478e-01 0.1696155518
##                  ivory     brown   steelblue   darkred lightcyan1         pink
## pink        1.00000000 0.9298586 1.000000000 0.5630401  1.0000000 4.478410e-01
## greenyellow 1.00000000 0.8295856 1.000000000 1.0000000  1.0000000 1.000000e+00
## blue        1.00000000 0.9993153 0.825044787 0.9511090  1.0000000 4.647534e-01
## green       1.00000000 0.9617671 1.000000000 0.7956617  1.0000000 1.621675e-01
## grey        0.34823602 0.6647005 0.009494185 0.9949706  1.0000000 2.804776e-02
## yellow      0.06866473 0.8304869 0.003901403 1.0000000  1.0000000 6.529395e-01
## turquoise   0.03421933 0.3567740 0.030360554 0.9539252  1.0000000 3.955094e-05
## black       1.00000000 0.7471479 0.000113349 1.0000000  1.0000000 7.900423e-01
## red         1.00000000 1.0000000 0.226881709 1.0000000  1.0000000 3.203808e-01
## magenta     0.16946464 1.0000000 1.000000000 1.0000000  1.0000000 1.712322e-01
## tan         0.06953818 1.0000000 1.000000000 1.0000000  1.0000000 1.000000e+00
## brown       1.00000000 0.9834502 1.000000000 0.5609250  0.3805781 6.737096e-01
## purple      1.00000000 0.8980450 0.336942231 1.0000000  1.0000000 3.737286e-01
## salmon      1.00000000 1.0000000 0.154166074 1.0000000  1.0000000 1.000000e+00
##                   salmon     white         blue midnightblue darkorange
## pink        1.0000000000 1.0000000 4.908225e-01   0.27440409  1.0000000
## greenyellow 1.0000000000 1.0000000 8.334208e-01   0.03058301  0.3454007
## blue        1.0000000000 1.0000000 8.990532e-29   0.03265950  0.9016849
## green       1.0000000000 1.0000000 3.222851e-09   0.13369920  1.0000000
## grey        0.4945159268 0.9797327 9.927915e-05   0.05852170  1.0000000
## yellow      0.0005233923 1.0000000 1.000000e+00   0.39741159  1.0000000
## turquoise   0.9041771270 0.8963889 9.888483e-01   0.01358827  0.9060668
## black       0.6622545845 1.0000000 9.353524e-01   0.28111348  1.0000000
## red         0.8682804321 1.0000000 1.060537e-02   0.58335780  0.7015550
## magenta     1.0000000000 1.0000000 4.549719e-01   0.62663285  1.0000000
## tan         0.3244127535 1.0000000 6.282328e-01   1.00000000  1.0000000
## brown       0.2196381941 1.0000000 1.438321e-01   0.90765757  1.0000000
## purple      1.0000000000 1.0000000 7.309750e-02   0.59086616  1.0000000
## salmon      1.0000000000 1.0000000 6.102750e-01   1.00000000  1.0000000
##                     cyan paleturquoise darkolivegreen       red  darkgrey
## pink        8.805020e-02     0.3760095      1.0000000 0.8062466 1.0000000
## greenyellow 3.259499e-02     1.0000000      1.0000000 1.0000000 1.0000000
## blue        9.795276e-01     1.0000000      0.7880629 0.5298633 1.0000000
## green       8.707450e-01     0.2253640      0.5579383 0.8190316 1.0000000
## grey        3.326953e-02     0.1703829      0.4991435 0.9999723 0.9451053
## yellow      5.233923e-04     0.6489031      1.0000000 0.9738129 0.4762111
## turquoise   4.306614e-12     1.0000000      0.2037852 0.9977591 0.1274130
## black       8.559859e-08     0.3811682      0.3514966 1.0000000 1.0000000
## red         3.221520e-01     0.2223334      1.0000000 0.8150155 1.0000000
## magenta     1.000000e+00     1.0000000      1.0000000 0.7887093 1.0000000
## tan         3.244128e-01     1.0000000      1.0000000 1.0000000 1.0000000
## brown       6.971981e-01     1.0000000      1.0000000 1.0000000 1.0000000
## purple      1.000000e+00     1.0000000      1.0000000 1.0000000 1.0000000
## salmon      5.188273e-02     1.0000000      1.0000000 1.0000000 1.0000000
##             darkturquoise    violet     plum1   magenta yellowgreen    purple
## pink            1.0000000 1.0000000 0.2930429 1.0000000  0.32479927 1.0000000
## greenyellow     0.3844759 1.0000000 0.2062112 1.0000000  1.00000000 0.5847636
## blue            0.7398683 0.5076978 1.0000000 0.9957880  0.05207892 0.9517781
## green           1.0000000 0.5952887 0.1405893 0.9437416  0.52918759 1.0000000
## grey            0.6702257 0.3390194 0.1697452 0.9992537  0.03491013 0.9663344
## yellow          1.0000000 0.6489031 0.5368187 1.0000000  0.58173466 0.3241482
## turquoise       0.7495156 0.8267533 0.1334115 1.0000000  0.42329802 0.8628594
## black           1.0000000 0.3811682 0.2973452 0.7827624  1.00000000 0.7389508
## red             0.7496292 1.0000000 1.0000000 0.9422269  0.52590209 0.7106728
## magenta         0.4985476 1.0000000 1.0000000 0.7585612  1.00000000 1.0000000
## tan             0.2350306 1.0000000 0.1197058 1.0000000  1.00000000 1.0000000
## brown           0.8115710 0.6604628 1.0000000 0.4359013  0.59323465 0.5732938
## purple          1.0000000 1.0000000 1.0000000 1.0000000  1.00000000 0.6782725
## salmon          1.0000000 1.0000000 1.0000000 1.0000000  0.12847679 1.0000000
##               skyblue3 royalblue  lightcyan lightyellow lightgreen  orangered4
## pink        1.00000000 1.0000000 1.00000000   0.6069865  1.0000000 1.000000000
## greenyellow 1.00000000 0.4607139 0.14332838   1.0000000  1.0000000 1.000000000
## blue        0.73709144 1.0000000 0.97515039   1.0000000  1.0000000 1.000000000
## green       1.00000000 1.0000000 0.85687349   1.0000000  0.2795127 1.000000000
## grey        0.67330856 0.9319063 0.95430224   0.9974461  1.0000000 0.312251609
## yellow      0.55662617 0.8722908 0.89454254   1.0000000  0.1638758 0.151215923
## turquoise   0.01067327 0.8561154 0.30937929   0.9689305  0.8741626 0.688720742
## black       1.00000000 1.0000000 0.27126011   1.0000000  1.0000000 0.273492306
## red         1.00000000 1.0000000 0.85428160   1.0000000  1.0000000 1.000000000
## magenta     0.29328632 1.0000000 0.61711799   1.0000000  1.0000000 0.003142396
## tan         1.00000000 0.2888847 1.00000000   1.0000000  1.0000000 1.000000000
## brown       1.00000000 1.0000000 0.40086340   1.0000000  0.3767010 0.512817463
## purple      1.00000000 1.0000000 1.00000000   1.0000000  1.0000000 1.000000000
## salmon      1.00000000 1.0000000 0.04737891   0.2789085  0.2888871 0.005238662
##                grey60 lightsteelblue1      skyblue darkmagenta     sienna3
## pink        1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## greenyellow 1.0000000       1.0000000 3.130142e-01  1.00000000 1.000000000
## blue        0.9745415       1.0000000 1.000000e+00  0.05475108 1.000000000
## green       1.0000000       1.0000000 9.143002e-02  1.00000000 0.171067893
## grey        0.9983985       0.7869081 1.388321e-01  0.71834984 0.918768757
## yellow      0.8929769       1.0000000 3.513988e-01  0.58778812 0.001644944
## turquoise   1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## black       1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## red         1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## magenta     1.0000000       1.0000000 4.137530e-01  1.00000000 0.310652817
## tan         1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## brown       1.0000000       1.0000000 2.369272e-16  1.00000000 0.593234649
## purple      1.0000000       1.0000000 1.000000e+00  1.00000000 1.000000000
## salmon      1.0000000       1.0000000 1.791349e-01  1.00000000 1.000000000
##             mediumpurple3  floralwhite
## pink                    1 1.000000e+00
## greenyellow             1 1.000000e+00
## blue                    1 1.000000e+00
## green                   1 4.694767e-02
## grey                    1 6.894328e-01
## yellow                  1 1.000000e+00
## turquoise               1 1.000000e+00
## black                   1 1.000000e+00
## red                     1 1.000000e+00
## magenta                 1 1.000000e+00
## tan                     1 1.000000e+00
## brown                   1 5.182473e-25
## purple                  1 1.000000e+00
## salmon                  1 1.000000e+00
# Adjust p-values for multiple testing (optional, to control false discovery rate)
pvalMatrixAdjusted <- p.adjust(pvalMatrix, method = "BH")
print("Adjusted P-Value Matrix (Fisher's Exact Test with BH correction):")
## [1] "Adjusted P-Value Matrix (Fisher's Exact Test with BH correction):"
print(pvalMatrixAdjusted)
##   [1] 1.000000e+00 1.000000e+00 2.184846e-03 1.000000e+00 4.231595e-02
##   [6] 1.000000e+00 5.527540e-01 1.000000e+00 6.739855e-01 9.194478e-01
##  [11] 1.000000e+00 1.000000e+00 7.289693e-01 1.000000e+00 1.000000e+00
##  [16] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [21] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [26] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [31] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [36] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 4.109323e-01
##  [41] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [46] 1.000000e+00 6.514871e-01 2.789005e-01 1.951454e-06 4.277511e-01
##  [51] 1.000000e+00 7.626829e-02 1.000000e+00 1.000000e+00 3.254116e-01
##  [56] 1.000000e+00 2.007905e-03 5.143125e-02 3.533901e-02 3.254489e-01
##  [61] 9.570363e-05 1.000000e+00 4.231595e-02 6.936310e-01 2.795504e-07
##  [66] 5.947100e-01 1.000000e+00 1.000000e+00 1.996787e-03 1.000000e+00
##  [71] 5.911118e-03 1.000000e+00 6.739855e-01 7.626829e-02 1.000000e+00
##  [76] 1.000000e+00 1.000000e+00 1.000000e+00 1.258762e-04 1.000000e+00
##  [81] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [86] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [91] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
##  [96] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [101] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 5.305562e-03
## [106] 6.739855e-01 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [111] 1.000000e+00 1.000000e+00 2.663875e-02 5.824939e-04 1.000000e+00
## [116] 1.000000e+00 1.000000e+00 4.109323e-01 1.000000e+00 1.000000e+00
## [121] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [126] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [131] 1.000000e+00 9.689127e-01 1.000000e+00 1.000000e+00 1.000000e+00
## [136] 1.000000e+00 1.000000e+00 5.911118e-03 1.000000e+00 1.000000e+00
## [141] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [146] 6.739855e-01 4.311635e-01 1.000000e+00 1.000000e+00 1.000000e+00
## [151] 6.739855e-01 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [156] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [161] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [166] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [171] 1.000000e+00 1.000000e+00 1.759217e-01 7.680886e-02 4.277511e-01
## [176] 4.200581e-03 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [181] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [186] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [191] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [196] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [201] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [206] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [211] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 4.109323e-01
## [216] 1.000000e+00 1.996787e-03 1.000000e+00 1.000000e+00 1.000000e+00
## [221] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [226] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.498805e-02
## [231] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [236] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [241] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [246] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [251] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 5.664035e-26
## [256] 3.383994e-07 3.909116e-03 1.000000e+00 1.000000e+00 1.000000e+00
## [261] 1.867823e-01 1.000000e+00 1.000000e+00 1.000000e+00 6.936310e-01
## [266] 1.000000e+00 1.000000e+00 4.277511e-01 4.277511e-01 1.000000e+00
## [271] 6.248927e-01 1.000000e+00 2.313678e-01 1.000000e+00 1.000000e+00
## [276] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [281] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [286] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [291] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 8.039366e-01
## [296] 4.277511e-01 1.000000e+00 1.000000e+00 4.277511e-01 1.498805e-02
## [301] 6.782917e-10 6.740889e-06 1.000000e+00 1.000000e+00 1.000000e+00
## [306] 1.000000e+00 1.000000e+00 5.858878e-01 1.000000e+00 1.000000e+00
## [311] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [316] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [321] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [326] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [331] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [336] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [341] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [346] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [351] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [356] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [361] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [366] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [371] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [376] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [381] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [386] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [391] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [396] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [401] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [406] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [411] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [416] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [421] 1.000000e+00 1.000000e+00 5.858878e-01 1.000000e+00 4.312428e-01
## [426] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [431] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [436] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [441] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [446] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [451] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.867823e-01
## [456] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [461] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [466] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [471] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [476] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [481] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [486] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 5.527540e-01
## [491] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [496] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [501] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [506] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [511] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [516] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [521] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [526] 1.000000e+00 1.000000e+00 6.826585e-02 1.000000e+00 1.000000e+00
## [531] 1.000000e+00 1.000108e-01 1.000000e+00 1.000000e+00 1.000000e+00
## [536] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [541] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [546] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [551] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [556] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [561] 1.000000e+00 1.000000e+00 1.000000e+00 8.228702e-01 1.000000e+00
## [566] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [571] 1.000000e+00 4.975471e-14 1.000000e+00 1.000000e+00 1.000000e+00
## [576] 1.000000e+00 5.947100e-01 1.000000e+00 1.000000e+00 1.000000e+00
## [581] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [586] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [591] 1.000000e+00 1.000000e+00 1.000000e+00 4.145259e-02 1.000000e+00
## [596] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [601] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [606] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [611] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [616] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 5.527540e-01
## [621] 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00
## [626] 1.000000e+00 1.000000e+00 1.632479e-22 1.000000e+00 1.000000e+00
# Example: Visualize the overlap matrix with a heatmap
heatmap(overlapMatrix, main = "Gene Overlap Between CAMK2A and MEGA Modules",
        xlab = "MEGA Modules", ylab = "CAMK2A Modules", col = heat.colors(100))

# Load necessary library for better heatmap visualization
library(pheatmap)
# Adjust p-values for multiple testing using Benjamini-Hochberg (FDR correction)
pvalMatrixAdjusted <- matrix(p.adjust(pvalMatrix, method = "BH"), nrow = length(camk2aModules), ncol = length(megaModules))
rownames(pvalMatrixAdjusted) <- camk2aModules
colnames(pvalMatrixAdjusted) <- megaModules

# Set a significance threshold (e.g., p < 0.05)
significantThreshold <- 0.05

# Create a binary significance matrix (1 = significant, 0 = not significant)
significantMatrix <- ifelse(pvalMatrixAdjusted < significantThreshold, 1, 0)

# Visualize the significant overlaps with a heatmap
pheatmap(significantMatrix, 
         main = "Significant Overlaps Between CAMK2A and MEGA Modules (p < 0.05)", 
         display_numbers = pvalMatrixAdjusted,  # Show adjusted p-values on heatmap
         color = c("white", "red"),  # Red for significant overlaps, white for non-significant
         cluster_rows = FALSE,  # Don't cluster rows (CAMK2A modules)
         cluster_cols = FALSE,  # Don't cluster columns (MEGA modules)
         fontsize_number = 10,  # Font size for displayed p-values
         legend_breaks = c(0, 1), 
         legend_labels = c("Not Significant", "Significant"),
         fontsize = 12)

# Load necessary library
library(pheatmap)

# Set the significance threshold (e.g., p < 0.05)
significantThreshold <- 0.05

# Adjust p-values for multiple testing using Benjamini-Hochberg correction
pvalMatrixAdjusted <- matrix(p.adjust(pvalMatrix, method = "BH"), nrow = length(camk2aModules), ncol = length(megaModules))
rownames(pvalMatrixAdjusted) <- camk2aModules
colnames(pvalMatrixAdjusted) <- megaModules

# Replace non-significant p-values with NA for clearer visualization
pvalMatrixSignificant <- pvalMatrixAdjusted
pvalMatrixSignificant[pvalMatrixSignificant >= significantThreshold] <- NA

# Create a color palette that emphasizes significant values
colorPalette <- colorRampPalette(c("red", "red1", "red2"))(158)

# Plot the heatmap with only significant p-values shown
pheatmap(pvalMatrixSignificant,
         main = "Significant Overlaps Between CAMK2A and MEGA Modules (p < 0.05)",
         display_numbers = ifelse(!is.na(pvalMatrixSignificant), sprintf("%.3f", pvalMatrixSignificant), ""),  # Show p-values
         color = colorPalette,  # Use a gradient from white to blue
         cluster_rows = FALSE,  # Don't cluster rows (CAMK2A modules)
         cluster_cols = FALSE,  # Don't cluster columns (MEGA modules)
         fontsize_number = 10,  # Font size for displayed p-values
         fontsize = 14,  # General font size for module labels
         na_col = "white"  # Color for non-significant cells (light gray)
)