jamesbang

V1

2023/02/07阅读：52主题：雁栖湖

# 🤩 WGCNA | 值得你深入学习的生信分析方法！~（网状分析-第二步-网络构建与模块识别）

## 2用到的包

rm(list = ls())
library(WGCNA)
library(tidyverse)

## 4软阈值

### 4.1 topology analysis

powers <-  c(c(1:10), seq(from = 12, to=20, by=2))

sft <- pickSoftThreshold(datExpr, powerVector = powers, verbose = 5)

### 4.2 可视化

sizeGrWindow(9, 5)
par(mfrow = c(1,2))
cex1 = 0.9;

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",
main = paste("Scale independence"))
text(sft\$fitIndices[,1], -sign(sft\$fitIndices[,3])*sft\$fitIndices[,2],
labels=powers,cex=cex1,col="red")

abline(h=0.90,col="red")

plot(sft\$fitIndices[,1], sft\$fitIndices[,5],
xlab="Soft Threshold (power)",
ylab="Mean Connectivity",
type="n",
main = paste("Mean connectivity"))
text(sft\$fitIndices[,1], sft\$fitIndices[,5], labels=powers, cex=cex1,col="red")

## 5构建网络与模块识别（一步法）

### 5.1 构建网络

net <-  blockwiseModules(datExpr, power = 6,
TOMType = "unsigned",
minModuleSize = 30,
reassignThreshold = 0, mergeCutHeight = 0.25,
numericLabels = T, pamRespectsDendro = F,
saveTOMs = T,
saveTOMFileBase = "femaleMouseTOM",
verbose = 3)

### 5.2 查看模块数

table(net\$colors)

### 5.3 可视化

sizeGrWindow(12,9)
mergedColors <- labels2colors(net\$colors)

plotDendroAndColors(net\$dendrograms[[1]], mergedColors[net\$blockGenes[[1]]],
"Module colors", dendroLabels = F, hang = 0.03, addGuide = T,
guideHang = 0.05)

### 5.4 save一下data

moduleLabels <-  net\$colors

moduleColors <- labels2colors(net\$colors)

MEs <- net\$MEs

geneTree <- net\$dendrograms[[1]]

save(MEs, moduleLabels, moduleColors, geneTree,
file = "FemaleLiver-02-networkConstruction-auto.RData")

## 6构建网络与模块识别（分步法）

softPower <-  6

dissTOM <- 1-TOM

### 6.3 基因聚类并可视化

geneTree <-  hclust(as.dist(dissTOM), method = "average")

plot(geneTree,
xlab="", sub="",
main = "Gene clustering on TOM-based dissimilarity",
labels = F, hang = 0.04)

### 6.4 识别模块

minModuleSize <-  30

dynamicMods <- cutreeDynamic(dendro = geneTree, distM = dissTOM,
deepSplit = 2, pamRespectsDendro = F,
minClusterSize = minModuleSize)
table(dynamicMods)

### 6.5 数字转为颜色并可视化

dynamicColors <-  labels2colors(dynamicMods)

table(dynamicColors)

sizeGrWindow(8,6)

plotDendroAndColors(geneTree, dynamicColors,
"Dynamic Tree Cut",
dendroLabels = F, hang = 0.03,
addGuide = T, guideHang = 0.05,
main = "Gene dendrogram and module colors")

### 6.6 合并表达相似的模块

MEList <- moduleEigengenes (datExpr, colors = dynamicColors)
MEs <- MEList\$eigengenes

MEDiss = 1-cor(MEs);
METree <- hclust(as.dist(MEDiss), method = "average")

### 6.7 合并模块的可视化

sizeGrWindow(7,6)
plot(METree, main = "Clustering of module eigengenes",xlab = "", sub = "")

MEDissThres = 0.25
abline(h=MEDissThres, col = "red")
merge <- mergeCloseModules(datExpr, dynamicColors, cutHeight = MEDissThres, verbose = 3)
mergedColors <- merge\$colors
mergedMEs <- merge\$newMEs

sizeGrWindow(12,9)
plotDendroAndColors(geneTree, cbind(dynamicColors, mergedColors), c("Dynamic Tree Cut", "Merged dynamic"), dendroLabels = F, hang = 0.03, addGuide = T, guideHang = 0.05)

moduleColors <- mergedColors

colorOrder <- c("grey", standardColors(50))
moduleLabels <- match(moduleColors, colorOrder)-1
MEs <- mergedMEs

## 7save一下

save(MEs, moduleLabels, moduleColors, geneTree,
file = "FemaleLiver-02-networkConstruction-stepByStep.RData")

## 8如何引用

📍
Langfelder, P., Horvath, S. WGCNA: an R package for weighted correlation network analysis. BMC Bioinformatics 9, 559 (2008). https://doi.org/10.1186/1471-2105-9-559

📍 往期精彩

##### jamesbang
V1

wx🔍: Grassssss 卷起来了