YOU WANT TO FIND PATTERNS OF GENES! YOU ARE EXACT RIGHT PLACE!!!

🔹 Step 1: LET’S INSTALL OR LOAD THE PACKAGES

library(WGCNA)
library(Seurat)

# I will use my seurat_object, u can use any of your seurat object!

rds_function(8) #Ignore this!

🔹 Step 2: Pseudobulk by Cluster (or Sample)

🔹 Step 2a : Filter genes does not have at least 50 counts (We will filter cells. We do not want artefacts to be lselected as pattterns. You can increase the threshold)

# Step 1: Get raw counts
counts <- Endo_sickle@assays$RNA$counts  # or use @data if you're working with log-normalized

# Step 2: Count how many cells express each gene
gene_detected_counts <- Matrix::rowSums(counts > 0)

# Step 3: Filter genes expressed in at least 50 cells
genes_to_keep <- names(gene_detected_counts[gene_detected_counts >= 50])

# Step 4: Subset Seurat object
Endo_sickle_filtered <- subset(Endo_sickle, features = genes_to_keep)

# Step 5: Average expression per group
cluster_avg <- AverageExpression(Endo_sickle_filtered, return.seurat = FALSE)$RNA
## As of Seurat v5, we recommend using AggregateExpression to perform pseudo-bulk analysis.
## First group.by variable `ident` starts with a number, appending `g` to ensure valid variable names
## This message is displayed once per session.
# Step 6: Transpose for WGCNA
datExpr <- t(cluster_avg)

🔹 Step 4: Pick Soft-Thresholding Power

powers = c(1:20)
sft = pickSoftThreshold(datExpr, powerVector = powers, verbose = 5)
## pickSoftThreshold: will use block size 4032.
##  pickSoftThreshold: calculating connectivity for given powers...
##    ..working on genes 1 through 4032 of 11096
##    ..working on genes 4033 through 8064 of 11096
##    ..working on genes 8065 through 11096 of 11096
##    Power SFT.R.sq  slope truncated.R.sq mean.k. median.k. max.k.
## 1      1  0.06660  0.980          0.995  3610.0    3580.0   5350
## 2      2  0.00551 -0.158          0.990  1700.0    1660.0   3150
## 3      3  0.10600 -0.572          0.984   949.0     906.0   2060
## 4      4  0.24600 -0.802          0.987   590.0     552.0   1450
## 5      5  0.37800 -0.967          0.986   395.0     363.0   1070
## 6      6  0.49100 -1.120          0.983   279.0     249.0    819
## 7      7  0.55900 -1.210          0.975   205.0     178.0    647
## 8      8  0.60900 -1.250          0.968   156.0     132.0    523
## 9      9  0.66000 -1.270          0.961   122.0      99.2    432
## 10    10  0.71400 -1.260          0.955    97.6      75.9    363
## 11    11  0.78000 -1.230          0.950    79.6      59.0    310
## 12    12  0.85800 -1.230          0.971    66.0      46.7    274
## 13    13  0.91800 -1.220          0.981    55.6      37.3    244
## 14    14  0.92500 -1.310          0.971    47.4      30.3    230
## 15    15  0.94300 -1.380          0.975    40.9      24.9    220
## 16    16  0.95700 -1.430          0.979    35.6      20.6    211
## 17    17  0.97000 -1.450          0.984    31.3      17.1    204
## 18    18  0.97600 -1.470          0.983    27.8      14.4    197
## 19    19  0.97900 -1.480          0.981    24.8      12.2    192
## 20    20  0.98400 -1.480          0.984    22.3      10.3    186
plot(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
     xlab = "Soft Threshold (power)", ylab = "Scale Free Topology Model Fit, signed R^2", type = "n")
text(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2], labels = powers, col = "red")
abline(h = 0.90, col = "red")  # Look for R^2 ~ 0.9

🔹 Step 5: Build the Network

softPower <- 6  # Use the power where R^2 is ~0.9
net <- blockwiseModules(datExpr, power = softPower,
                        TOMType = "unsigned", minModuleSize = 30,
                        reassignThreshold = 0, mergeCutHeight = 0.25,
                        numericLabels = TRUE, pamRespectsDendro = FALSE,
                        saveTOMs = FALSE, verbose = 3)
##  Calculating module eigengenes block-wise from all genes
##    Flagging genes and samples with too many missing values...
##     ..step 1
##  ....pre-clustering genes to determine blocks..
##    Projective K-means:
##    ..k-means clustering..
##    ..merging smaller clusters...
## Block sizes:
## gBlocks
##    1    2    3 
## 4195 4115 2786 
##  ..Working on block 1 .
##     TOM calculation: adjacency..
##     ..will not use multithreading.
##      Fraction of slow calculations: 0.000000
##     ..connectivity..
##     ..matrix multiplication (system BLAS)..
##     ..normalization..
##     ..done.
##  ....clustering..
##  ....detecting modules..
##  ....calculating module eigengenes..
##  ....checking kME in modules..
##  ..Working on block 2 .
##     TOM calculation: adjacency..
##     ..will not use multithreading.
##      Fraction of slow calculations: 0.000000
##     ..connectivity..
##     ..matrix multiplication (system BLAS)..
##     ..normalization..
##     ..done.
##  ....clustering..
##  ....detecting modules..
##  ....calculating module eigengenes..
##  ....checking kME in modules..
##  ..Working on block 3 .
##     TOM calculation: adjacency..
##     ..will not use multithreading.
##      Fraction of slow calculations: 0.000000
##     ..connectivity..
##     ..matrix multiplication (system BLAS)..
##     ..normalization..
##     ..done.
##  ....clustering..
##  ....detecting modules..
##  ....calculating module eigengenes..
##  ....checking kME in modules..
##      ..removing 13 genes from module 2 because their KME is too low.
##  ..merging modules that are too close..
##      mergeCloseModules: Merging modules whose distance is less than 0.25
##        Calculating new MEs...

🔹 Step 6: Get Module Eigengenes and Hub Genes

moduleColors <- labels2colors(net$colors)
MEs <- net$MEs

# Identify hub genes in the turquoise module (example)
module <- 6
geneModuleMembership <- cor(datExpr, MEs, use = "p")
hubGenes <- names(sort(geneModuleMembership[, paste0("ME", module)], decreasing = TRUE))[1:33]

FeaturePlot(Endo_sickle, hubGenes, combine = FALSE)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

## 
## [[6]]

## 
## [[7]]

## 
## [[8]]

## 
## [[9]]

## 
## [[10]]

## 
## [[11]]

## 
## [[12]]

## 
## [[13]]

## 
## [[14]]

## 
## [[15]]

## 
## [[16]]

## 
## [[17]]

## 
## [[18]]

## 
## [[19]]

## 
## [[20]]

## 
## [[21]]

## 
## [[22]]

## 
## [[23]]

## 
## [[24]]

## 
## [[25]]

## 
## [[26]]

## 
## [[27]]

## 
## [[28]]

## 
## [[29]]

## 
## [[30]]

## 
## [[31]]

## 
## [[32]]

## 
## [[33]]

🔹 Step 6a:et hub genes for all modules automatically

hub_gene_list <- list()
for (mod in names(MEs)) {
  top_genes <- names(sort(geneModuleMembership[, mod], decreasing = TRUE))[1:33]
  hub_gene_list[[mod]] <- top_genes
}

# View top hub genes per module
hub_gene_list
## $ME17
##  [1] "Tob2"     "Wrap53"   "Cyp2f2"   "Vcp"      "Cbr2"     "Pgam1"   
##  [7] "Ctsd"     "Spty2d1"  "Atp1b1"   "Pcbp1"    "Gclc"     "Cldn10"  
## [13] "Zdhhc5"   "Nabp1"    "Fasn"     "F7"       "Gcc1"     "Brd2"    
## [19] "Ppp1r15b" "Aox3"     "Nek1"     "Pum1"     "Mal2"     "Copz1"   
## [25] "Kcnj15"   "Kbtbd2"   "Scgb1a1"  "Ubqln1"   "Ndufaf3"  "Zmat3"   
## [31] "Kpna1"    "Selenbp1" "Cltc"    
## 
## $ME21
##  [1] "Atg2a"         "Gripap1"       "Fbrs"          "Kdelr2"       
##  [5] "Cnppd1"        "Egfl6"         "Aim2"          "Golga7"       
##  [9] "Ppm1b"         "C4b"           "Itgb2"         "Stk17b"       
## [13] "Uros"          "Slc1a5"        "Mir142hg"      "Tspan1"       
## [17] "Med13"         "Aff1"          "Timm13"        "Polh"         
## [21] "Fbxl14"        "Lcp1"          "Xpnpep3"       "Btg1"         
## [25] "Mrpl39"        "Polr3b"        "Whamm"         "1700022I11Rik"
## [29] "Runx1"         "Mpg"           "Cecr2"         "Larp1b"       
## [33] "Aplf"         
## 
## $ME23
##  [1] "Sesn1"         "Pdcd4"         "Letm2"         "Slc16a6"      
##  [5] "Rngtt"         "4931422A03Rik" "Ppil4"         "Stx5a"        
##  [9] "Atp5a1"        "Ep300"         "Ccdc88c"       "Zfp207"       
## [13] "Tmem131"       "Zbtb11"        "Bmp2k"         "Vrk3"         
## [17] "Tnrc6b"        "Wdr78"         "Zmynd8"        "Tfam"         
## [21] "Sirt1"         "Slc12a6"       "Rb1cc1"        "D030028A08Rik"
## [25] "Msh3"          "Sf3b1"         "Taf8"          "Chmp3"        
## [29] "Plcl2"         "Larp1b"        "Prdm2"         "Ythdc1"       
## [33] "Med13"        
## 
## $ME7
##  [1] "Rsrp1"         "Ypel4"         "Suco"          "Xpo7"         
##  [5] "Clcn3"         "Slc25a21"      "Hectd4"        "Evi5"         
##  [9] "Rfx2"          "Fbxo34"        "Gm36199"       "Kif24"        
## [13] "Tnfaip2"       "Hspa1b"        "Slfn14"        "Otud5"        
## [17] "Zfp874a"       "Ip6k1"         "Ubap1"         "Prkar2b"      
## [21] "Dyrk3"         "Abcb10"        "Gm46515"       "Fhdc1"        
## [25] "B230354K17Rik" "Trak2"         "Pim1"          "Gm20161"      
## [29] "Pnrc1"         "Tfrc"          "Cdkn1b"        "5430401H09Rik"
## [33] "Mtmr3"        
## 
## $ME19
##  [1] "4632427E13Rik" "Ubxn7"         "Crebrf"        "Btrc"         
##  [5] "Timm23"        "Atf7ip"        "Ccdc138"       "Polr2a"       
##  [9] "Xpo4"          "Ep400"         "Pot1a"         "Arid4b"       
## [13] "Rab5a"         "Lrrc29"        "Mier1"         "Zfp652"       
## [17] "Usp33"         "Ankrd28"       "Clk1"          "Ppp4r3b"      
## [21] "Ppp2r1b"       "Pcf11"         "Srcap"         "Rad18"        
## [25] "Wapl"          "Tnrc6b"        "Usp7"          "Acin1"        
## [29] "Cabin1"        "Hnrnpc"        "2700049A03Rik" "Dhx8"         
## [33] "2810402E24Rik"
## 
## $ME10
##  [1] "Taldo1"        "Eif1"          "1810058I24Rik" "Prxl2a"       
##  [5] "Isg15"         "Ccndbp1"       "Mpc2"          "Slc25a39"     
##  [9] "Apol11b"       "Sin3b"         "Atp5l"         "Ube2l6"       
## [13] "Hbb-bt"        "Alas2"         "Rplp0"         "Rplp1"        
## [17] "Fam220a"       "Bpgm"          "Rps9"          "Smox"         
## [21] "Ftl1"          "Isg20"         "Oaz1"          "Ubb"          
## [25] "Tpt1"          "Map1lc3b"      "Eef1d"         "Ube2c"        
## [29] "Ctsf"          "Bola3"         "Psmd4"         "Ftl1-ps1"     
## [33] "Ucp2"         
## 
## $ME25
##  [1] "Mdh1"          "H2-M3"         "Bloc1s1"       "Peg3"         
##  [5] "Zfp839"        "Exoc3l"        "Nsa2"          "Zfp763"       
##  [9] "Srrd"          "Xbp1"          "Tha1"          "Ufd1"         
## [13] "Ybey"          "Gm49602"       "Ogfr"          "Tm2d3"        
## [17] "Tmem223"       "Acad11"        "Laptm4a"       "Tatdn3"       
## [21] "Mrps21"        "Ccs"           "Rxrb"          "Cmc4"         
## [25] "2310009B15Rik" "Nudt2"         "Ergic3"        "Timm21"       
## [29] "Pcdhgb2"       "Leo1"          "Nagk"          "Prodh"        
## [33] "Mansc1"       
## 
## $ME26
##  [1] "Cisd1"         "Cptp"          "Selenof"       "Pomp"         
##  [5] "Ninj1"         "2310009A05Rik" "Tmem167b"      "Ino80c"       
##  [9] "Gm12655"       "Psmb1"         "Txn1"          "Nop53"        
## [13] "Psme2"         "Champ1"        "Cops2"         "Gm15663"      
## [17] "Uqcrc2"        "Higd2a"        "Raly"          "Scnn1g"       
## [21] "Tex264"        "Tomm7"         "Ipo13"         "Pcdhgb2"      
## [25] "Smarcd2"       "Mrpl20"        "Commd9"        "Tnfaip3"      
## [29] "Smim10l1"      "Zfp180"        "Lgals3bp"      "Pnrc2"        
## [33] "S100a13"      
## 
## $ME15
##  [1] "Samd5"         "Vegfc"         "Amigo2"        "Ptgs2"        
##  [5] "Gask1b"        "Il6st"         "Flvcr2"        "Ptgis"        
##  [9] "1700020G17Rik" "Lhx6"          "Pde1c"         "Kirrel3"      
## [13] "Cygb"          "Ptgs1"         "Csrp2"         "Chn2"         
## [17] "Lama1"         "Net1"          "Pla2g4a"       "Mmp16"        
## [21] "Bst1"          "Slc6a2"        "Gm48749"       "Lpcat2"       
## [25] "Tmem45a"       "Gm5127"        "Fos"           "Gm38304"      
## [29] "Exoc3l2"       "Ackr3"         "Slc16a2"       "Gm12703"      
## [33] "Cysltr1"      
## 
## $ME27
##  [1] "Adgra2"        "Crim1"         "2810403D21Rik" "Airn"         
##  [5] "Ssna1"         "Nkd2"          "Asb7"          "Cryl1"        
##  [9] "Znrf3"         "Usp3"          "Rere"          "Pbx1"         
## [13] "Extl3"         "Wdfy2"         "2010001A14Rik" "Spry2"        
## [17] "Med26"         "Pja2"          "Ldlrad3"       "Nhs"          
## [21] "Gstcd"         "Ext2"          "Trappc9"       "Bloc1s5"      
## [25] "Cyp7b1"        "Trim44"        "Il15"          "Wdr11"        
## [29] "Zfp930"        "Fez2"          "3830408C21Rik" "Apcdd1"       
## [33] "Gm4951"       
## 
## $ME13
##  [1] "Acacb"    "Sat1"     "Frk"      "Atp1b3"   "Tm4sf1"   "Itprid2" 
##  [7] "Enah"     "Gata6"    "Pitpnm3"  "Samd12"   "Rasgrf2"  "Ptprr"   
## [13] "Selenoi"  "Itpr3"    "Unc13b"   "Tead2"    "Fam107a"  "Ivns1abp"
## [19] "Gm28153"  "Crispld1" "Fam135a"  "Mrtfb"    "Kcnc2"    "Htra1"   
## [25] "Lrrc55"   "Gm15738"  "Cenpt"    "Bmp4"     "Cd2ap"    "Cdh13"   
## [31] "Slc12a7"  "Jam2"     "Gbp4"    
## 
## $ME11
##  [1] "Calcrl"        "Pcdh17"        "Gbp9"          "Cavin4"       
##  [5] "Tspan7"        "Sh3d19"        "Il17rd"        "Pltp"         
##  [9] "Garnl3"        "Pcnx"          "Hpgd"          "Cpd"          
## [13] "Bmpr2"         "Tek"           "Cyyr1"         "Arhgef26"     
## [17] "Kank4"         "Uaca"          "Erg"           "Rb1"          
## [21] "Nckap5"        "4430402I18Rik" "Adgrl4"        "Sox7"         
## [25] "Clec14a"       "Clec2d"        "Tmem100"       "Aldh3a2"      
## [29] "Nudt7"         "Lifr"          "Cav1"          "Por"          
## [33] "Syne2"        
## 
## $ME6
##  [1] "Supt20"        "Paxbp1"        "Ctdspl2"       "Phykpl"       
##  [5] "Hnrnpa3"       "Immp1l"        "Zdhhc6"        "Srsf11"       
##  [9] "Rufy1"         "Mtrex"         "Gm14966"       "1500015A07Rik"
## [13] "Rufy2"         "Itch"          "Ssbp1"         "Gps2"         
## [17] "Hnrnph3"       "Srek1"         "Usp50"         "Trmt13"       
## [21] "Ndfip1"        "Son"           "6720427I07Rik" "Rsbn1"        
## [25] "Sepsecs"       "Ciz1"          "Ktn1"          "Cbx1"         
## [29] "Cfl2"          "Luc7l2"        "Leng8"         "Ushbp1"       
## [33] "Pex3"         
## 
## $ME14
##  [1] "6030458C11Rik" "BC005561"      "Plscr4"        "Gm20544"      
##  [5] "Zfp326"        "Sf3a3"         "Slk"           "Rbms2"        
##  [9] "Arhgef15"      "Hint3"         "Rnpep"         "Timm17a"      
## [13] "Mrps10"        "Gm4673"        "Arhgef5"       "Fam192a"      
## [17] "Prdx4"         "Rnf125"        "Pelp1"         "9330020H09Rik"
## [21] "Thumpd2"       "8430429K09Rik" "Capn15"        "Uba3"         
## [25] "Gabbr1"        "Ccm2l"         "Ranbp17"       "Plod2"        
## [29] "Rsph3b"        "Pola2"         "Ankra2"        "Slc39a9"      
## [33] "Fam8a1"       
## 
## $ME22
##  [1] "1700061H18Rik" "Styx"          "Gm16064"       "Stpg1"        
##  [5] "Gm15541"       "Gm16152"       "Nsun6"         "Slc5a12"      
##  [9] "Jun"           "Gm47392"       "Adhfe1"        "Mettl24"      
## [13] "Gm26854"       "Trp53cor1"     "Vxn"           "Gm13582"      
## [17] "Cfap52"        "Mapk6"         "Gm29530"       "Lrrc36"       
## [21] "Lnx2"          "Ms4a8a"        "Ap3m1"         "Gm830"        
## [25] "Sema5a"        "Gm28410"       "Zfp711"        "Gm20616"      
## [29] "Gm12216"       "Dnah11"        "Pphln1"        "Gm15991"      
## [33] "Pmaip1"       
## 
## $ME8
##  [1] "Rnf25"         "Fam118a"       "Tlnrd1"        "Vegfb"        
##  [5] "Tctex1d2"      "Atp7a"         "Kat2a"         "Gnb4"         
##  [9] "Nfatc4"        "Xpc"           "Arhgap10"      "Clcc1"        
## [13] "Zfp260"        "Morn1"         "Adcy3"         "Dmd"          
## [17] "Kxd1"          "Pcdhga6"       "Elmod3"        "Rerg"         
## [21] "Klhdc8b"       "Slc39a11"      "1110046J04Rik" "Map4k3"       
## [25] "Pdzd2"         "Chmp5"         "Rapgef2"       "Slc9a6"       
## [29] "Daxx"          "Sart1"         "Myh11"         "Gtf2h1"       
## [33] "Nmnat2"       
## 
## $ME9
##  [1] "9530068E07Rik" "Zfp84"         "Rab2b"         "Trim41"       
##  [5] "Dazap1"        "Baz1b"         "Nbeal1"        "Arl13b"       
##  [9] "Scaf4"         "5730455P16Rik" "Rbm27"         "Fam222b"      
## [13] "Appl2"         "Ppfia1"        "Klhl20"        "Rwdd1"        
## [17] "Ercc5"         "Rspry1"        "Paip1"         "Zfp142"       
## [21] "Purg"          "Rabl6"         "Cep290"        "Cep112"       
## [25] "Opa1"          "Trmu"          "Vhl"           "AW549877"     
## [29] "Dvl1"          "Sdf2"          "Grk5"          "Cbx5"         
## [33] "Prpf6"        
## 
## $ME4
##  [1] "Cep170"  "Pacs1"   "Arhgef3" "Fkbp1a"  "Clic4"   "Klf7"    "Inpp5a" 
##  [8] "Mertk"   "Abi1"    "Vcl"     "Clic5"   "Psd3"    "Rab31"   "Prkce"  
## [15] "Trib2"   "Pcdh1"   "Tspan12" "Ttyh3"   "Cltb"    "Dusp16"  "Mpped2" 
## [22] "Zfp710"  "Dcbld1"  "Gm47644" "Midn"    "Kcnq1"   "Rgs12"   "Frmd6"  
## [29] "Fam219a" "Nrp1"    "Tspan13" "Acap2"   "Nectin3"
## 
## $ME16
##  [1] "Gpat4"         "Tex261"        "Ercc6"         "Lzts2"        
##  [5] "Ltbr"          "Cacfd1"        "Traf3ip1"      "Prkci"        
##  [9] "Stard3"        "Tspan4"        "Cdkl2"         "Opa3"         
## [13] "Mapre3"        "Ccnd2"         "Dnaaf5"        "Bvht"         
## [17] "Shroom3"       "Def8"          "Ccdc85c"       "Rgl3"         
## [21] "Camk2g"        "Snx16"         "Hs1bp3"        "Soga1"        
## [25] "Tnfaip1"       "2510009E07Rik" "Cdo1"          "Ubox5"        
## [29] "Rara"          "Dync1h1"       "Rell1"         "Zfp72"        
## [33] "Epb41l2"      
## 
## $ME5
##  [1] "Ltn1"     "Cdc42bpb" "Rad23b"   "Mdfic"    "Babam2"   "Pdcd6ip" 
##  [7] "Nhlrc3"   "Mpdz"     "Ppt2"     "Kif16b"   "Cradd"    "Shank3"  
## [13] "Naa50"    "Cavin2"   "Sec23ip"  "Sec61a1"  "Eprs"     "Bzw1"    
## [19] "Gfpt1"    "Zfp426"   "Asap1"    "Nckap1"   "Copb2"    "Cnn3"    
## [25] "Tcf4"     "Kifap3"   "Emp1"     "Tns2"     "Tanc1"    "Ccdc43"  
## [31] "Mob2"     "Stat5b"   "Gm47469" 
## 
## $ME3
##  [1] "Ahcyl1"  "Fbxw11"  "Rnf38"   "Traf7"   "Fam193b" "Rnf213"  "Snapc3" 
##  [8] "Pds5b"   "Eea1"    "Rnf111"  "Gmeb1"   "Sbno1"   "Mkln1"   "Abl2"   
## [15] "Ccser2"  "Vps8"    "Ptk2"    "Zfp322a" "Atp6v1h" "Gsk3b"   "Cacul1" 
## [22] "Senp6"   "March6"  "Klhdc3"  "Arid2"   "Rhot1"   "Pank2"   "Ccar1"  
## [29] "Ubr5"    "Fam193a" "Dnajc13" "Pdpk1"   "St7l"   
## 
## $ME24
##  [1] "Psme2b"   "Vps33b"   "Wdr36"    "Ifih1"    "Tnpo2"    "Slc38a7" 
##  [7] "Rabac1"   "Gmeb2"    "Blmh"     "Manea"    "BC002059" "Plod2"   
## [13] "Pde6d"    "Mocs2"    "Cdc42bpa" "E2f6"     "Sec14l1"  "Mink1"   
## [19] "Stard3nl" "Bcor"     "Wdr70"    "Dnmt3a"   "Gm43259"  "Nras"    
## [25] "Acadl"    "Fut10"    "Cntrob"   "Ipo8"     "Magt1"    "Prmt5"   
## [31] "Rnf217"   "Fnip2"    "Unc45bos"
## 
## $ME18
##  [1] "Uri1"     "Zfp141"   "Prpf38a"  "Pter"     "Fam114a1" "Ascc1"   
##  [7] "Stx16"    "Bcs1l"    "Zdhhc13"  "Pdcd2l"   "Ddb2"     "Ralgps1" 
## [13] "Nab1"     "Znhit6"   "Mthfs"    "Kat7"     "Ddx18"    "Aimp1"   
## [19] "Osbpl7"   "Zfp37"    "Mterf1a"  "Nudt5"    "Rexo1"    "Snrnp200"
## [25] "Gemin5"   "Cct2"     "Pbxip1"   "Arl2"     "Sdha"     "Cdk10"   
## [31] "Kiz"      "Arf4"     "Cul9"    
## 
## $ME1
##  [1] "Lrba"          "Bicral"        "Smchd1"        "Zfp639"       
##  [5] "4921524J17Rik" "Nup35"         "Alkbh1"        "Cab39l"       
##  [9] "Morc3"         "Seh1l"         "Atr"           "Pbrm1"        
## [13] "Fam149b"       "Jarid2"        "2810004N23Rik" "Ddx39b"       
## [17] "Rp9"           "Rfx7"          "Kdm3b"         "Trub1"        
## [21] "Prkdc"         "Pisd"          "Ilkap"         "Taf2"         
## [25] "Zfp809"        "Orc4"          "Cul5"          "Prmt9"        
## [29] "Scai"          "Fbxo32"        "Capn7"         "Ndufaf2"      
## [33] "Gm46218"      
## 
## $ME12
##  [1] "Rbm10"    "C9orf72"  "Dcun1d4"  "Ppp2r2d"  "Zbtb41"   "Zscan22" 
##  [7] "Fam168b"  "Ppt1"     "Cog8"     "Det1"     "Arhgap18" "Atg16l1" 
## [13] "Arfip2"   "Mettl25"  "Ldah"     "Rfx3"     "Cops3"    "Stat6"   
## [19] "AW146154" "Ano10"    "Filip1l"  "Dcaf17"   "Cnbd2"    "Pa2g4"   
## [25] "Clcn4"    "Zfp429"   "Hdac2"    "Golga1"   "Gm4221"   "Slbp"    
## [31] "Ginm1"    "Bace1"    "Lemd2"   
## 
## $ME2
##  [1] "Strn"     "Dnmbp"    "Nr3c2"    "Plekha5"  "Trp53bp1" "Sntb2"   
##  [7] "Gls"      "Cpne2"    "Hjurp"    "Cul7"     "Ccdc157"  "Dlg1"    
## [13] "Smyd2"    "Ndrg1"    "Wdr73"    "Spsb1"    "Atp9a"    "Zfp512"  
## [19] "Ubtd1"    "Magi1"    "Tshz3"    "Smad1"    "Palm"     "Gm11266" 
## [25] "Anxa11"   "Micall1"  "Mpp7"     "Aplp2"    "Chrnb1"   "Maged1"  
## [31] "Ntn1"     "Dab2"     "Zfp521"  
## 
## $ME20
##  [1] "Tinf2"         "Mrps31"        "Gemin8"        "Wdr53"        
##  [5] "Smim19"        "Atf1"          "Copa"          "Ecd"          
##  [9] "Ndufs1"        "Jrkl"          "Pex6"          "Prickle3"     
## [13] "Prmt7"         "Phtf1"         "Golgb1"        "Sap30l"       
## [17] "9430015G10Rik" "Hps4"          "Ccnt1"         "Ccdc125"      
## [21] "Sec22a"        "Gtpbp8"        "Pex1"          "Sbf1"         
## [25] "Gpr107"        "Zfp932"        "2610037D02Rik" "Desi1"        
## [29] "Alkbh3"        "Mcm9"          "Dcun1d2"       "Yeats2"       
## [33] "Ccdc47"       
## 
## $ME0
##  [1] "Crebl2"  "Acaa1a"  "Plekhm1" "Pi4k2b"  "Fam53b"  "Micu1"   "Tsc22d2"
##  [8] "Man1a2"  "Rock1"   "Alg9"    "Eif4a1"  "Herpud2" "Add1"    "Nrip1"  
## [15] "Eif3b"   "Dapk3"   "Ezh1"    "C2cd5"   "Rad21"   "Uqcc2"   "Kbtbd2" 
## [22] "Ubac1"   "Rnf139"  "Pum1"    "Rbm15b"  "Adam10"  "Aaas"    "Trim26" 
## [29] "Imp4"    "Rbfa"    "Stag2"   "Zmynd8"  "Scaf11"

🔹 Step 7:Plot MEs for every ME with ggplot2

# Assume: correlation_matrix is the matrix you showed (traits in rows, MEs in columns)

# Transpose so MEs become rows if needed:
cor_df <- as.data.frame(MEs)

# Loop through each module and plot its correlation with traits
for (module in colnames(cor_df)) {
  barplot(
    cor_df[[module]],
    names.arg = rownames(cor_df),
    las = 2,
    col = "steelblue",
    ylim = c(-1, 1),
    main = paste("Module-Trait Correlation:", module),
    ylab = "Correlation"
  )
  abline(h = 0, lty = 2)
  Sys.sleep(1)  # pause between plots
}

🔹 Step 7b:Plot MEs for every ME with heatmap

library(pheatmap)
pheatmap(MEs, cluster_rows = TRUE, cluster_cols = TRUE) #that is cool

dim(geneModuleMembership) #genes versus ME types 
## [1] 11096    28
dim(MEs) #Your metadata colum vs ME_types
## [1] 12 28
geneModuleMembership1 <- as.data.frame(geneModuleMembership)