1.数据加载 workingDir = "C:/Users/qixiaohan/Desktop/qixiaohan/WCGNA/DATA/5.17 lncRNA-mRNA"; setwd(workingDir); library(WGCNA); options(stringsAsFactors = FALSE); RNAData = read.csv("Microarray.csv"); dim(RNAData); names(RNAData); datExpr0 = as.data.frame(t(RNAData[, -c(1)])); names(datExpr0) = RNAData$锘緼ccession; rownames(datExpr0) = names(RNAData)[-c(1)]; gsg = goodSamplesGenes(datExpr0, verbose = 3); gsg$allOK sampleTree = hclust(dist(datExpr0), method = "average"); sizeGrWindow(12,9) par(cex = 0.6); par(mar = c(0,4,2,0)) plot(sampleTree, main = "Sample clustering to detect outliers", sub="", xlab="", cex.lab = 1.5, cex.axis = 1.5, cex.main = 2) abline(h = 50, col = "red"); clust = cutreeStatic(sampleTree, cutHeight = 100, minSize = 5) table(clust) keepSamples = (clust==1) datExpr = datExpr0[keepSamples, ] nGenes = ncol(datExpr) nSamples = nrow(datExpr) traitData = read.csv("ClinicalTraits.csv"); dim(traitData) names(traitData) allTraits = traitData[, -c(1)]; allTraits = allTraits[,c(1:10)]; dim(allTraits) names(allTraits) RNASamples = rownames(datExpr); traitRows = match(RNASamples, allTraits$samples); datTraits = allTraits[traitRows, -1]; rownames(datTraits) = allTraits[traitRows, 1]; collectGarbage(); sampleTree2 = hclust(dist(datExpr), method = "average") traitColors = numbers2colors(datTraits, signed = FALSE); plotDendroAndColors(sampleTree2, traitColors, groupLabels = names(datTraits), main = "Sample dendrogram and trait heatmap") save(datExpr, datTraits, file = "lncRNA-mRNA-01-dataInput.RData") 2A.构建模型、 powers = c(c(1:10), seq(from = 12, to=20, by=2)) sft = pickSoftThreshold(datExpr, powerVector = powers, verbose = 5) 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.40,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") net = blockwiseModules(datExpr, power = 6, TOMType = "unsigned", minModuleSize = 30, reassignThreshold = 0, mergeCutHeight = 0.25, numericLabels = TRUE, pamRespectsDendro = FALSE, saveTOMs = TRUE, saveTOMFileBase = "lncRNA-mRNATOM", verbose = 3) table(net$colors) sizeGrWindow(12, 9) mergedColors = labels2colors(net$colors) plotDendroAndColors(net$dendrograms[[1]], mergedColors[net$blockGenes[[1]]], "Module colors", dendroLabels = FALSE, hang = 0.03, addGuide = TRUE, guideHang = 0.05) moduleLabels = net$colors moduleColors = labels2colors(net$colors) MEs = net$MEs; geneTree = net$dendrograms[[1]]; save(MEs, moduleLabels, moduleColors, geneTree, file = "lncRNA-mRNA-02-networkConstruction-auto.RData") 2C.>5000个基因 enableWGCNAThreads() lnames = load(file = "lncRNA-01-dataInput.RData"); lnames powers = c(c(1:10), seq(from = 12, to=20, by=2)) sft = pickSoftThreshold(datExpr, powerVector = powers, verbose = 5) 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") bwnet = blockwiseModules(datExpr, maxBlockSize = 5500, power = 12, TOMType = "unsigned", minModuleSize = 30, reassignThreshold = 0, mergeCutHeight = 0.25, numericLabels = TRUE, saveTOMs = TRUE, saveTOMFileBase = "lncRNATOM-blockwise", verbose = 3) load(file = "lncRNA-02-networkConstruction-auto.RData"); bwLabels = matchLabels(bwnet$colors, moduleLabels); bwModuleColors = labels2colors(bwLabels) sizeGrWindow(6,6) plotDendroAndColors(bwnet$dendrograms[[1]], bwModuleColors[bwnet$blockGenes[[1]]], "Module colors", main = "Gene dendrogram and module colors in block 1", dendroLabels = FALSE, hang = 0.03, addGuide = TRUE, guideHang = 0.05) plotDendroAndColors(bwnet$dendrograms[[2]], bwModuleColors[bwnet$blockGenes[[2]]], "Module colors", main = "Gene dendrogram and module colors in block 2", dendroLabels = FALSE, hang = 0.03, addGuide = TRUE, guideHang = 0.05) sizeGrWindow(12,9) plotDendroAndColors(geneTree, cbind(moduleColors, bwModuleColors), c("Single block", "2 blocks"), main = "Single block gene dendrogram and module colors", dendroLabels = FALSE, hang = 0.03, addGuide = TRUE, guideHang = 0.05) singleBlockMEs = moduleEigengenes(datExpr, moduleColors)$eigengenes; blockwiseMEs = moduleEigengenes(datExpr, bwModuleColors)$eigengenes; single2blockwise = match(names(singleBlockMEs), names(blockwiseMEs)) signif(diag(cor(blockwiseMEs[, single2blockwise], singleBlockMEs)), 3) 3.Relating modules to external information and identifying important genes lnames = load(file = "lncRNA-mRNA-01-dataInput.RData"); lnames lnames = load(file = "lncRNA-mRNA-02-networkConstruction-auto.RData"); lnames nGenes = ncol(datExpr); nSamples = nrow(datExpr); MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes MEs = orderMEs(MEs0) moduleTraitCor = cor(MEs, datTraits, use = "p"); moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples); sizeGrWindow(10,6) textMatrix = paste(signif(moduleTraitCor, 2), "\n(", signif(moduleTraitPvalue, 1), ")", sep = ""); dim(textMatrix) = dim(moduleTraitCor) par(mar = c(6, 8.5, 3, 3)); labeledHeatmap(Matrix = moduleTraitCor, xLabels = names(datTraits), yLabels = names(MEs), ySymbols = names(MEs), colorLabels = FALSE, colors = greenWhiteRed(50), textMatrix = textMatrix, setStdMargins = FALSE, cex.text = 0.5, zlim = c(-1,1), main = paste("Module-trait relationships")) ss = as.data.frame(datTraits$ss); names(ss) = "salt-sensitive" modNames = substring(names(MEs), 3) geneModuleMembership = as.data.frame(cor(datExpr, MEs, use = "p")); MMPvalue = as.data.frame(corPvalueStudent(as.matrix(geneModuleMembership), nSamples)); names(geneModuleMembership) = paste("MM", modNames, sep=""); names(MMPvalue) = paste("p.MM", modNames, sep=""); geneTraitSignificance = as.data.frame(cor(datExpr, ss, use = "p")); GSPvalue = as.data.frame(corPvalueStudent(as.matrix(geneTraitSignificance), nSamples)); names(geneTraitSignificance) = paste("GS.", names(ss), sep=""); names(GSPvalue) = paste("p.GS.", names(ss), sep=""); module = "blue" column = match(module, modNames); moduleGenes = moduleColors==module; sizeGrWindow(7, 7); par(mfrow = c(1,1)); verboseScatterplot(abs(geneModuleMembership[moduleGenes, column]), abs(geneTraitSignificance[moduleGenes, 1]), xlab = paste("Module Membership in", module, "module"), ylab = "Gene significance for salt-sensitivity", main = paste("Module membership vs. gene significance\n"), cex.main = 1.2, cex.lab = 1.2, cex.axis = 1.2, col = module) dimnames(datExpr) dimnames(datExpr)[moduleColors=="blue"] annot = read.csv(file = "GeneAnnotation.csv"); dim(annot) names(annot) probes = names(datExpr) probes2annot = match(probes, annot$锘緼ccession) sum(is.na(probes2annot)) geneInfo0 = data.frame(锘緼ccession = probes, geneSymbol = annot$锘緼ccession[probes2annot], LocusLinkID = annot$EntrezGeneID[probes2annot], moduleColor = moduleColors, geneTraitSignificance, GSPvalue) modOrder = order(-abs(cor(MEs, ss, use = "p"))); for (mod in 1:ncol(geneModuleMembership)) { oldNames = names(geneInfo0) geneInfo0 = data.frame(geneInfo0, geneModuleMembership[, modOrder[mod]], MMPvalue[, modOrder[mod]]); names(geneInfo0) = c(oldNames, paste("MM.", modNames[modOrder[mod]], sep=""), paste("p.MM.", modNames[modOrder[mod]], sep="")) } geneOrder = order(geneInfo0$moduleColor); geneInfo = geneInfo0[geneOrder, ] write.csv(geneInfo, file = "geneInfo.csv") 5.Network visualization using WGCNA functions lnames = load(file = "lncRNA-mRNA-01-dataInput.RData"); lnames lnames = load(file = "lncRNA-mRNA-02-networkConstruction-auto.RData"); lnames nGenes = ncol(datExpr) nSamples = nrow(datExpr) dissTOM = 1-TOMsimilarityFromExpr(datExpr, power = 6); plotTOM = dissTOM^8; diag(plotTOM) = NA; sizeGrWindow(9,9) TOMplot(plotTOM, geneTree, moduleColors, main = "Network heatmap plot, all genes") MEs = moduleEigengenes(datExpr, moduleColors)$eigengenes ss = as.data.frame(datTraits$ss); names(ss) = "ss" MET = orderMEs(cbind(MEs, ss)) sizeGrWindow(5,7.5); par(cex = 0.9) plotEigengeneNetworks(MET, "", marDendro = c(0,4,1,2), marHeatmap = c(3,4,1,2), cex.lab = 0.8, xLabelsAngle = 90) sizeGrWindow(6,6); par(cex = 1.0) plotEigengeneNetworks(MET, "Eigengene dendrogram", marDendro = c(0,4,2,0), plotHeatmaps = FALSE) par(cex = 1.0) plotEigengeneNetworks(MET, "Eigengene adjacency heatmap", marHeatmap = c(3,4,2,2), plotDendrograms = FALSE, xLabelsAngle = 90) 6. Exporting a gene network to external visualization software lnames = load(file = "lncRNA-mRNA-01-dataInput.RData"); lnames lnames = load(file = "lncRNA-mRNA-02-networkConstruction-auto.RData"); lnames TOM = TOMsimilarityFromExpr(datExpr, power = 6); annot = read.csv(file = "GeneAnnotation.csv"); modules = c("blue"); probes = names(datExpr) inModule = is.finite(match(moduleColors, modules)); modProbes = probes[inModule]; modGenes = annot$gene_symbol[match(modProbes, annot$锘緼ccession)]; modTOM = TOM[inModule, inModule]; dimnames(modTOM) = list(modProbes, modProbes) cyt = exportNetworkToCytoscape(modTOM, edgeFile = paste("CytoscapeInput-edges-", paste(modules, collapse="-"), ".txt", sep=""), nodeFile = paste("CytoscapeInput-nodes-", paste(modules, collapse="-"), ".txt", sep=""), weighted = TRUE, threshold = 0.02, nodeNames = modProbes, altNodeNames = modGenes, nodeAttr = moduleColors[inModule]);