source("http://bioconductor.org/biocLite.R") biocLite(c("GO.db", "preprocessCore", "impute")) install.packages(c("matrixStats", "Hmisc", "splines", "foreach", "doParallel", "reshape", "fastcluster", "dynamicTreeCut", "survival")) install.packages("WGCNA") install.packages("scatterplot3d") library(WGCNA) setwd("C:\\Users\\scikuangren\\Desktop\\WGCNA-R") inputdata1="GEO.txt" data0=read.table(inputdata1,sep="\t",row.names=1,header=T,check.names=F,quote="!") datSummary=rownames(data0) datExpr = t(data0) no.samples = dim(datExpr)[[1]] dim(datExpr) powers1=c(seq(1,10,by=1),seq(12,20,by=2)) RpowerTable=pickSoftThreshold(datExpr, powerVector=powers1)[[2]] cex1=1 par(mfrow=c(1,2)) pdf("beta.pdf") plot(RpowerTable[,1], -sign(RpowerTable[,3])*RpowerTable[,2],xlab=" Soft Threshold (power)",ylab="Scale Free Topology Model Fit,signed R^2",type="n") text(RpowerTable[,1], -sign(RpowerTable[,3])*RpowerTable[,2], labels=powers1,cex=cex1,col="red") abline(h=0.85,col="red") plot(RpowerTable[,1], RpowerTable[,5],xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n") text(RpowerTable[,1], RpowerTable[,5], labels=powers1, cex=cex1,col="red") dev.off() beta1=6 Connectivity=softConnectivity(datExpr,power=beta1) pdf("scalefree.pdf",15,10) par(mfrow=c(1,1)) scaleFreePlot(Connectivity, main=paste("soft threshold, power=",beta1), truncated=T) dev.off() ConnectivityCut = 1000 ConnectivityRank = rank(-Connectivity) restConnectivity = ConnectivityRank <= ConnectivityCut ADJrest = adjacency(datExpr[,restConnectivity], power=beta1) dissTOM=TOMdist(ADJrest) hierTOM = hclust(as.dist(dissTOM),method="average") colorh1= cutreeStaticColor(hierTOM,cutHeight = 0.8, minSize = 3) pdf("module.pdf") par(mfrow=c(2,1),mar=c(2,4,1,1)) plot(hierTOM, main="Cluster Dendrogram", labels=F, xlab="", sub="") plotColorUnderTree(hierTOM,colors=data.frame(module=colorh1)) title("Module (branch) color") dev.off() pdf("TOM.pdf") TOMplot(dissTOM , hierTOM, colorh1, terrainColors=TRUE) dev.off() pdf("cmd.pdf") cmd1=cmdscale(as.dist(dissTOM),3) pairs(cmd1, col=as.character(colorh1), main="MDS plot") dev.off() library(scatterplot3d) pdf("3d.pdf") par(mfrow=c(1,1), mar=c(4,3,2,3)+0.1) scatterplot3d(cmd1,color=colorh1,angle=250, xlab="Scaling Axis 1", ylab="Scaling Axis 2", zlab="Scaling Axis 3") dev.off() datME=moduleEigengenes(datExpr[,restConnectivity],colorh1)[[1]] dissimME=1-(t(cor(datME, method="p")))/2 hclustdatME=hclust(dist(dissimME), method="average" ) pdf("modul_cluster.pdf") par(mfrow=c(1,1)) plot(hclustdatME, main="Clustering tree based on the module eigengenes of modules") dev.off() pdf("modul_cor.pdf") pairs(datME) dev.off() modul<-signif(cor(datME, use="p"), 2) write.table(modul,"modul_cor.txt",sep="\t",quote=F) datME=moduleEigengenes(datExpr,colorh1)[[1]] color1=rep("grey",dim(datExpr)[[2]]) color1=as.character(colorh1) datKME=signedKME(datExpr, datME) datout=data.frame(datSummary, colorNEW=color1,datKME ) write.table(datout, "gene_module.xls", sep="\t", row.names=F,quote=F) exportNetworkToCytoscape(ADJrest,edgeFile="edge.txt",nodeFile="node.txt",threshold = 0.5) inputclinial="clinical_data.txt" dataclinial = read.table(inputclinial,sep="\t",row.names=1,header=T,check.names=F,quote="!") nGenes = ncol(datExpr) nSamples = nrow(datExpr) MEs0 = moduleEigengenes(datExpr,colorh1)$eigengenes MEsFemale = orderMEs(MEs0) modul_clinical_cor = cor(MEsFemale, dataclinial, use = "p") write.table(modul_clinical_cor,"module-clinial-cor.xls",sep="\t",quote=F) modul_clinical_p = corPvalueStudent(modul_clinical_cor, nSamples) write.table(modul_clinical_p,"modul-clinical-p.xls",sep="\t",quote=F) textMatrix = paste(signif(modul_clinical_cor, 2), " (", signif(modul_clinical_p, 1), ")",sep = "") dim(textMatrix) = dim(modul_clinical_cor) pdf("modul-clinical.pdf") par(mar = c(6, 8.5, 3, 3)) labeledHeatmap(Matrix = modul_clinical_cor, xLabels = names(dataclinial), yLabels = names(MEsFemale),ySymbols = names(MEsFemale), colorLabels = FALSE, colors = greenWhiteRed(50), textMatrix = textMatrix, setStdMargins = FALSE, cex.text = 1, zlim = c(-1,1), main = paste("Module-trait relationships")) dev.off() geneExp=datExpr gene_clinial_cor = cor(geneExp, dataclinial, use = "p") write.table(gene_clinial_cor,"gene_clinial_cor.xls",sep="\t",quote=F) gene_clinical_p = corPvalueStudent(gene_clinial_cor, nSamples) write.table(gene_clinical_p,"gene_clinical_p.xls",sep="\t",quote=F)