[#R] WGCNA 保守性分析

结果示例:

参考:

https://www.jianshu.com/p/9a03a35cabf3

https://www.jianshu.com/p/e9e2ef9ea693

输入文件示例:

#人和小鼠 ID 转化

Combining species datasets

#hsa.exp.2.xls#

#mmu17.exp.2.xls#

#mmu80.exp.2.xls##

代码:

library(WGCNA)
setwd("E:/GEO")

hsa = read.delim('hsa.exp.2.xls', row.names = 1, sep = '\t', check.names = FALSE)
mmu17=read.delim('mmu17.exp.2.xls', row.names = 1, sep = '\t', check.names = FALSE)
mmu80=read.delim('mmu80.exp.2.xls', row.names = 1, sep = '\t', check.names = FALSE)

hsa2 =hsa[ , -which(colnames(hsa) %in% c("Cntrl1","Cntrl2"))]
#sampleTree=hclust(dist(t(hsa)),method = "average")
#par(cex=0.5)
#plot(sampleTree)

#hsa2=hsa[ , -which(colnames(hsa) %in% c("C5","",""))]  #C5离群,去除

#sampleTree=hclust(dist(t(mmu17)),method = "average")
#par(cex=0.5)
#plot(sampleTree)

mmu17_2=mmu17 #没有


mmu80_2=mmu80[ , -which(colnames(mmu80) %in% c("GSM1904766","GSM1904768","GSM1904769","GSM1904771","GSM1904772","GSM1904773",
"GSM1904775","GSM1904777","GSM1904778","GSM1904780","GSM1904781","GSM1904782","GSM1904784","GSM1904785","GSM1904786","GSM1904788","GSM1904790",
"GSM1904791","GSM1904792","GSM1904793","GSM1904800","GSM1904801","GSM1904803","GSM1904804","GSM1904806","GSM1904814",
"GSM1904815","GSM1904816","GSM1904817","GSM1904818","GSM1904822","GSM1904823","GSM1904825","GSM1904826","GSM1904827","GSM1904829",
"GSM1904833","GSM1904834","GSM1904838","GSM1904839"))]

powers = c(c(1:10), seq(from = 12, to=50, by=2))
sft = pickSoftThreshold(t(mmu80_2), powerVector = powers, verbose = 5)
cex1 = 0.5
##画出
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")
hsa2_power=18
mmu17_2_power=26
mmu80_2_power=10

hsa_net = blockwiseModules(
  ##这个矩阵行是样本,列是基因
  t(hsa2), 
  power = 18,
  TOMType = "unsigned", minModuleSize = 40,
  reassignThreshold = 0, mergeCutHeight = 0.25,
  numericLabels = TRUE, pamRespectsDendro = FALSE,
  saveTOMs = TRUE,
  #saveTOMFileBase = "MyTOM",
  verbose = 3)
hsa_mergedColors = labels2colors(hsa_net$colors)
pdf("hsa_module.pdf",width = 10, height = 5)
plotDendroAndColors(hsa_net$dendrograms[[1]], hsa_mergedColors[hsa_net$blockGenes[[1]]], "Module colors",
                    dendroLabels = FALSE, hang = 0.03,
                    addGuide = TRUE, guideHang = 0.05)
dev.off()


mmu17_net = blockwiseModules(
  ##这个矩阵行是样本,列是基因
  t(mmu17_2), 
  power = 26,
  TOMType = "unsigned", minModuleSize = 40,
  reassignThreshold = 0, mergeCutHeight = 0.25,
  numericLabels = TRUE, pamRespectsDendro = FALSE,
  saveTOMs = TRUE,
  #saveTOMFileBase = "MyTOM",
  verbose = 3)
mmu17_mergedColors =labels2colors(mmu17_net$colors)
pdf("mmu17_module.pdf",width = 10, height = 5)
plotDendroAndColors(mmu17_net$dendrograms[[1]], mmu17_mergedColors[mmu17_net$blockGenes[[1]]], "Module colors",
                    dendroLabels = FALSE, hang = 0.03,
                    addGuide = TRUE, guideHang = 0.05)
dev.off()

mmu80_net = blockwiseModules(
  ##这个矩阵行是样本,列是基因
  t(mmu80_2), 
  power = 10,
  TOMType = "unsigned", minModuleSize = 40,
  reassignThreshold = 0, mergeCutHeight = 0.25,
  numericLabels = TRUE, pamRespectsDendro = FALSE,
  saveTOMs = TRUE,
  #saveTOMFileBase = "MyTOM",
  verbose = 3)
mmu80_mergedColors = labels2colors(mmu80_net$colors)
pdf("mmu80_module.pdf",width = 10, height = 5)
plotDendroAndColors(mmu80_net$dendrograms[[1]], mmu80_mergedColors[mmu80_net$blockGenes[[1]]], "Module colors",
                    dendroLabels = FALSE, hang = 0.03,
                    addGuide = TRUE, guideHang = 0.05)
dev.off()

#table(hsa_net$colors)
#labels2colors(net$colors)
hsa_mergedColors = labels2colors(hsa_net$colors)
mmu17_mergedColors =labels2colors(mmu17_net$colors)
mmu80_mergedColors = labels2colors(mmu80_net$colors)



#hsa+mmu17
nSets = 2
multiExpr = list()
hsa2 = subset(hsa2,rowSums(hsa2) >1) 
#write.table(hsa2,file="hsa.tmp,xls",sep = "\t")
mmu17_2=subset(mmu17_2,rowSums(mmu17_2) >1)
multiExpr[[1]] = list(data = t(hsa2))
multiExpr[[2]] = list(data = t(mmu17_2))
setLabels = c("Human", "Mouse17")
names(multiExpr) = setLabels
lapply(multiExpr, lapply, dim)

multiColor = list(Human = hsa_mergedColors,Mouse17=mmu17_mergedColors)
#colorList = list(hsa_mergedColors, mmu17_mergedColors)
#names(colorList) = setLabels

mp = modulePreservation(multiExpr, multiColor,
                        referenceNetworks = c(1:2),
                        loadPermutedStatistics = FALSE,
                        nPermutations = 200,
                        verbose = 3)
ref = 1
test = 2
statsObs = cbind(mp$quality$observed[[ref]][[test]][, -1], mp$preservation$observed[[ref]][[test]][, -1])
statsZ = cbind(mp$quality$Z[[ref]][[test]][, -1], mp$preservation$Z[[ref]][[test]][, -1])
print(cbind(statsObs[, c("medianRank.pres", "medianRank.qual")],
            signif(statsZ[, c("Zsummary.pres", "Zsummary.qual")], 2)) )
modColors = rownames(mp$preservation$observed[[ref]][[test]])
moduleSizes = mp$preservation$Z[[ref]][[test]][, 1]
row.names(statsZ[statsZ$Zsummary.pres<10,])

#去掉Z<10的module
#%in%不在这里的基因
plotMods = !(modColors %in% row.names(statsZ[statsZ$Zsummary.pres<-10,]))
#去掉了Z<10的基因
text = modColors[plotMods]
plotData = cbind(mp$preservation$observed[[ref]][[test]][, 2], mp$preservation$Z[[ref]][[test]][, 2])
##preservation可视化
mains = c("Preservation Median rank", "Preservation Zsummary")
##新开一个画图窗口
sizeGrWindow(10, 5)
pdf("hsa_mmu17_preservation.pdf",width = 20, height = 10)
##一行两列
par(mfrow = c(1,2))
##到四边的距离
par(mar = c(4.5,4.5,2.5,1))
for (p in 1:2){
  min = min(plotData[, p], na.rm = TRUE);
  max = max(plotData[, p], na.rm = TRUE);
  # Adjust ploting ranges appropriately
  if (p==2){
    if (min > -max/10) min = -max/10
    ylim = c(min - 0.1 * (max-min), max + 0.1 * (max-min))
  } else
    ylim = c(min - 0.1 * (max-min), max + 0.1 * (max-min))
  #bg 颜色 pch 圆圈的种类
  plot(moduleSizes[plotMods], plotData[plotMods, p], col = 1, bg = modColors[plotMods], pch = 21,
       main = mains[p],
       ##圆圈的大小
       cex = 2.4,
       ylab = mains[p], xlab = "Module size", log = "x",
       ylim = ylim,
       xlim = c(10, 2000), cex.lab = 1.2, cex.axis = 1.2, cex.main =1.4)
  ##贴上标签
  labelPoints(moduleSizes[plotMods], plotData[plotMods, p], text, cex = 1, offs = 0.08);
  # For Zsummary, add threshold lines
  if (p==2){
    abline(h=0)
    abline(h=2, col = "blue", lty = 2)
    abline(h=10, col = "darkgreen", lty = 2)
  }
}
dev.off()
print(signif(statsZ[, "Zsummary.pres", drop = FALSE],2))
overlap = cbind(mp$accuracy$observedCounts[[1]][[2]], mp$accuracy$observedFisherPvalues[[1]][[2]])
print(overlap)

#has+mmu80
nSets = 2
multiExpr = list()
hsa2 <- subset(hsa2,rowSums(hsa2) >1)

mmu80_2=subset(mmu80_2,rowSums(mmu80_2) >1)
multiExpr[[1]] = list(data = t(hsa2))
multiExpr[[2]] = list(data = t(mmu80_2))
setLabels = c("Human", "Mouse80")
names(multiExpr) = setLabels
lapply(multiExpr, lapply, dim)

multiColor = list(Human = hsa_mergedColors,Mouse80=mmu80_mergedColors)
#colorList = list(hsa_mergedColors, mmu17_mergedColors)
#names(colorList) = setLabels

mp_hsa_mm80 = modulePreservation(multiExpr, multiColor,
                        referenceNetworks = c(1:2),
                        loadPermutedStatistics = FALSE,
                        nPermutations = 200,
                        verbose = 3)

ref = 1
test = 2
statsObs = cbind(mp_hsa_mm80$quality$observed[[ref]][[test]][, -1], mp_hsa_mm80$preservation$observed[[ref]][[test]][, -1])
statsZ = cbind(mp_hsa_mm80$quality$Z[[ref]][[test]][, -1], mp_hsa_mm80$preservation$Z[[ref]][[test]][, -1])
print(cbind(statsObs[, c("medianRank.pres", "medianRank.qual")],
            signif(statsZ[, c("Zsummary.pres", "Zsummary.qual")], 2)) )
modColors = rownames(mp_hsa_mm80$preservation$observed[[ref]][[test]])
moduleSizes = mp_hsa_mm80$preservation$Z[[ref]][[test]][, 1]
#row.names(statsZ[statsZ$Zsummary.pres<10,])

#去掉Z<10的module
#%in%不在这里的基因
plotMods = !(modColors %in% row.names(statsZ[statsZ$Zsummary.pres<-10,]))
#去掉了Z<10的基因
text2 = modColors[plotMods]
plotData = cbind(mp_hsa_mm80$preservation$observed[[ref]][[test]][, 2], mp_hsa_mm80$preservation$Z[[ref]][[test]][, 2])
##preservation可视化
mains = c("Preservation Median rank", "Preservation Zsummary")
##新开一个画图窗口
sizeGrWindow(10, 5)
pdf("hsa_mmu80_preservation.pdf",width = 20, height = 10)
##一行两列
par(mfrow = c(1,2))
##到四边的距离
par(mar = c(4.5,4.5,2.5,1))
for (p in 1:2){
  min = min(plotData[, p], na.rm = TRUE);
  max = max(plotData[, p], na.rm = TRUE);
  # Adjust ploting ranges appropriately
  if (p==2){
    if (min > -max/10) min = -max/10
    ylim = c(min - 0.1 * (max-min), max + 0.1 * (max-min))
  } else
    ylim = c(min - 0.1 * (max-min), max + 0.1 * (max-min))
  #bg 颜色 pch 圆圈的种类
  plot(moduleSizes[plotMods], plotData[plotMods, p], col = 1, bg = modColors[plotMods], pch = 21,
       main = mains[p],
       ##圆圈的大小
       cex = 2.4,
       ylab = mains[p], xlab = "Module size", log = "x",
       ylim = ylim,
       xlim = c(10, 2000), cex.lab = 1.2, cex.axis = 1.2, cex.main =1.4)
  ##贴上标签
  labelPoints(moduleSizes[plotMods], plotData[plotMods, p], text2, cex = 1, offs = 0.08);
  # For Zsummary, add threshold lines
  if (p==2){
    abline(h=0)
    abline(h=2, col = "blue", lty = 2)
    abline(h=10, col = "darkgreen", lty = 2)
  }
}
dev.off()
print(signif(statsZ[, "Zsummary.pres", drop = FALSE],2))
overlap = cbind(mp_hsa_mm80$accuracy$observedCounts[[1]][[2]], mp_hsa_mm80$accuracy$observedFisherPvalues[[1]][[2]])
print(overlap)

table(hsa_net$colors)
table(mmu17_net$colors)
table(mmu80_net$colors)

for(i in 1:length(text)){
  y=hsa2[which(hsa_mergedColors==text[i]),]
  write.csv(y,paste(paste("hsa_mm17/hsa.",text[i],sep = ""),"csv",sep = "."),quote=F)
}

for(i in 1:length(text)){
  y=mmu17_2[which(mmu17_mergedColors==text[i]),]
  write.csv(y,paste(paste("hsa_mm17/mmu17.",text[i],sep = ""),"csv",sep = "."),quote=F)
}


for(i in 1:length(text2)){
  y=hsa2[which(hsa_mergedColors==text2[i]),]
  write.csv(y,paste(paste("hsa_mm80/hsa.",text2[i],sep = ""),"csv",sep = "."),quote=F)
}

for(i in 1:length(text2)){
  y=mmu80_2[which(mmu80_mergedColors==text2[i]),]
  write.csv(y,paste(paste("hsa_mm80/mmu80.",text2[i],sep = ""),"csv",sep = "."),quote=F)
}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值