一篇WGCNA文章復(fù)現(xiàn)-GSE85589-下集

前面由于對miRNA的探針數(shù)目沒有正確的理解,以為數(shù)據(jù)一直沒有下載完全,折騰了一番。后來經(jīng)老大jimmy提醒了,miRNA有2000左右的探針就是正常的呀。所以就可以愉快地繼續(xù)進(jìn)行分析啦!

下面是對數(shù)據(jù)集GSE85589中的原圖進(jìn)行復(fù)現(xiàn),一篇WGCNA文章的原圖就到手了!

1.下載數(shù)據(jù)+準(zhǔn)備數(shù)據(jù)

rm(list = ls())
options(stringsAsFactors = F)
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
options(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
library(GEOquery)
library(WGCNA)
f='GSE85589_eSet.Rdata'
# https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE106292

# 這個包需要注意兩個配置,一般來說自動化的配置是足夠的。
#Setting options('download.file.method.GEOquery'='auto')
#Setting options('GEOquery.inmemory.gpl'=FALSE)
if(!file.exists(f)){
  gset <- getGEO('GSE85589', destdir=".",
                 AnnotGPL = F,     ## 注釋文件
                 getGPL = F)       ## 平臺文件
  save(gset,file=f)   ## 保存到本地
}
load('GSE85589_eSet.Rdata')  ## 載入數(shù)據(jù)
class(gset)  #查看數(shù)據(jù)類型
length(gset)  #
class(gset[[1]])
# 因?yàn)檫@個GEO數(shù)據(jù)集只有一個GPL平臺,所以下載到的是一個含有一個元素的list
a=gset[[1]] 
dat=exprs(a) 
dim(dat)
pd=pData(a) 
save(dat,pd,file = 'step1-rawdata')
#######探索match的過程
# load('step1-rawdata')
# probe2symbol <- data.table::fread(file = "GPL19117-74051.txt")#對于這樣的一個文件,就這么容易讀進(jìn)去了
# 
# probe2symbol <- probe2symbol[grep("Homo sapiens", #挑選出智人的注釋信息
#                                   ignore.case = F, 
#                                   probe2symbol$`Species Scientific Name`), ]
# 
# dim(probe2symbol)
# table(probe2symbol$`Species Scientific Name`)#確保都是hs
# probe2symbol <- probe2symbol[ , c(1,4)]
# names(probe2symbol) <- c("PROBE_ID", "SYMBOL_ID")
# 
# dat <- as.data.frame(dat)
# dat[1:4,1:4]
# 
# rownames(dat) %in% probe2symbol$PROBE_ID
# table(rownames(dat) %in% probe2symbol$PROBE_ID)
# dat1 <- dat[match(rownames(dat),probe2symbol$PROBE_ID),]
# dat[1:4,1:4]
# dat2 <- dat[match(probe2symbol$PROBE_ID,rownames(dat)),]
# dat1[1:4,1:4]
# dat3 <- na.omit(dat2)
# dat2[1:4,1:4]
# 
# ids <- probe2symbol[match(rownames(dat),probe2symbol$PROBE_ID),]
# rownames(dat) <- ids$SYMBOL_ID
# table(duplicated(rownames(dat)))#table后發(fā)現(xiàn)false為2578行,正好是dat的行數(shù),說明沒有重復(fù)項(xiàng)
# #而其實(shí)table(duplicated(probe2symbol$SYMBOL_ID))后發(fā)現(xiàn),還是有很多重復(fù)的基因名的,只是我們這里作為2578個的基因名是沒有重復(fù)的/
# table(duplicated(probe2symbol$SYMBOL_ID))
# dat[1:4,1:4]

##############################################################################
load('step1-rawdata')
dat <- as.data.frame(dat)
dat[1:4,1:4]

probe2symbol <- data.table::fread(file = "GPL19117-74051.txt")#對于這樣的一個文件,就這么容易讀進(jìn)去了

probe2symbol <- probe2symbol[grep("Homo sapiens", #挑選出智人的注釋信息
                                  ignore.case = F, 
                                  probe2symbol$`Species Scientific Name`), ]

dim(probe2symbol)
table(probe2symbol$`Species Scientific Name`)#確保都是hs
probe2symbol <- probe2symbol[ , c(1,4)]
names(probe2symbol) <- c("PROBE_ID", "SYMBOL_ID")
ids <- probe2symbol
head(ids)
colnames(ids)=c('probe_id','symbol')  
ids=ids[ids$symbol != '',]
ids=ids[ids$probe_id %in%  rownames(dat),]
dat[1:4,1:4]   
dat=dat[ids$probe_id,] 
dat[1:4,1:4]
ids$median=apply(dat,1,median) #ids新建median這一列,列名為median,同時對dat這個矩陣按行操作,取每一行的中位數(shù),將結(jié)果給到median這一列的每一行
ids=ids[order(ids$symbol,ids$median,decreasing = T),]#對ids$symbol按照ids$median中位數(shù)從大到小排列的順序排序,將對應(yīng)的行賦值為一個新的ids
ids=ids[!duplicated(ids$symbol),]#將symbol這一列取取出重復(fù)項(xiàng),'!'為否,即取出不重復(fù)的項(xiàng),去除重復(fù)的gene ,保留每個基因最大表達(dá)量結(jié)果s
dat=dat[ids$probe_id,] #新的ids取出probe_id這一列,將dat按照取出的這一列中的每一行組成一個新的dat
rownames(dat)=ids$symbol#把ids的symbol這一列中的每一行給dat作為dat的行名
dat[1:4,1:4]  #保留每個基因ID第一次出現(xiàn)的信息
colnames(dat)

pdac <- grep('PC',pd$title)
normal <- grep('N[0-9]+\\b',pd$title)
x <- c(pdac,normal)
expr <- dat[,x]

#對pd_fil進(jìn)行去除冗余項(xiàng)
length(unique(pd[,1]))
length(unique(pd[,2]))
length(unique(pd[,3]))
apply(pd,2,function(x){length(unique(x))})

apply(pd,2,function(x){
  length(unique(x))> 1
})
pd_fil <- pd[,apply(pd,2,function(x){
  length(unique(x))> 1
})]

dim(pd_fil)
cli <- pd_fil[x,]
save(expr,cli,file = 'step1-input.Rdata')

2.做WGCNA分析

library(WGCNA)
rm(list = ls())
load('step1-input.Rdata')

#####step 1
datExpr0=as.data.frame(t(expr))
gsg = goodSamplesGenes(datExpr0, verbose = 3)
gsg$allOK
if(T){
sampleTree = hclust(dist(datExpr0), method = "average")
#sizeGrWindow(15,12)
par(cex = 0.6)
par(mar = c(0,4,2,0))
#png("dynamicColors_mergedColors.png",width = 800,height = 600)
pdf('Sample clustering.pdf',width = 25,height =20 )
plot(sampleTree, main = "Sample clustering to detect outliers", sub="", xlab="", cex.lab = 1.5, cex.axis = 1.5, cex.main = 2)
abline(h = 26.5, col = "red")
dev.off()
}
image-20191112220756510
#這個僅留做記錄,原文并沒有去掉離群樣本
clust = cutreeStatic(sampleTree, cutHeight = 26.5, minSize = 10)
table(clust) # 0代表切除的,1代表保留的
keepSamples = (clust==1)
datExpr = datExpr0[keepSamples, ]

下面是構(gòu)建無尺度網(wǎng)絡(luò)

###step 2
if(T){
  powers = c(c(1:10), seq(from = 12, to=30, by=2))
  # Call the network topology analysis function
  sft = pickSoftThreshold(datExpr, powerVector = powers, verbose = 5)
  #設(shè)置網(wǎng)絡(luò)構(gòu)建參數(shù)選擇范圍,計(jì)算無尺度分布拓?fù)渚仃?  png("step2-beta-value.png",width = 800,height = 600)
  # Plot the results:
  ##sizeGrWindow(9, 5)
  par(mfrow = c(1,2));
  cex1 = 0.9;
  # Scale-free topology fit index as a function of the soft-thresholding power
  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");
  # this line corresponds to using an R^2 cut-off of h
  abline(h=0.90,col="red")
  # Mean connectivity as a function of the soft-thresholding power
  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")
  dev.off()
}
sft 
sft$powerEstimate
save(sft,file = "step2_beta_value.Rdata")
image-20191121214914371
image-20191121214920739

如下原文所示,用的貝塔值是1,我的結(jié)果也是,然后R^2值是0.89,接下來選擇==一步法==

image-20191112225334998
###step 3
if(T){
  cor <- WGCNA::cor
  if(T){
    net = blockwiseModules(
      datExpr,
      power = 1,
      TOMType = "unsigned", minModuleSize = 30,
      reassignThreshold = 0, mergeCutHeight = 0.25,
      numericLabels = TRUE, pamRespectsDendro = FALSE,
      saveTOMs = F, 
      verbose = 3
    )
    table(net$colors) 
  }
  
  sizeGrWindow(12, 9)
  mergedColors = labels2colors(net$colors)
  pdf('step3-dynamicColors_mergedColors.pdf',width = 25,height =20 )
  # Plot the dendrogram and the module colors underneath
  plotDendroAndColors(net$dendrograms[[1]], mergedColors[net$blockGenes[[1]]],
                      "Module colors",
                      dendroLabels = FALSE, hang = 0.03,
                      addGuide = TRUE, guideHang = 0.05)
  dev.off()
  moduleLabels = net$colors
  moduleColors = labels2colors(net$colors)
  table(moduleColors)
  MEs = net$MEs;
  geneTree = net$dendrograms[[1]];
  save(MEs, moduleLabels, moduleColors, geneTree,
       file = "AS-green-FPKM-02-networkConstruction-auto.RData")
  
}
image-20191121214932208
###step 4
if(T){
  nGenes = ncol(datExpr)
  nSamples = nrow(datExpr)
  #首先針對樣本做個系統(tǒng)聚類
  datExpr_tree<-hclust(dist(datExpr), method = "average")
  #針對前面構(gòu)造的樣品矩陣添加對應(yīng)顏色
  sample_colors1 <- numbers2colors(as.numeric(factor(datTraits$group_list)), 
                                   colors = c("green","blue","red","yellow","black"),signed = FALSE)
  
  ssss=as.matrix(data.frame(group_list=sample_colors1))                         
  par(mar = c(1,4,3,1),cex=0.8)
  pdf('step4-sample-subtype-cluster.pdf',width = 25,height =20 )
  plotDendroAndColors(datExpr_tree, ssss,
                      groupLabels = colnames(sample),
                      cex.dendroLabels = 0.8,
                      marAll = c(1, 4, 3, 1),
                      cex.rowText = 0.01,
                      main = "Sample dendrogram and trait heatmap")
  dev.off()
}
image-20191113005038902
##step 5
table(datTraits)  
if(T){
  nGenes = ncol(datExpr)
  nSamples = nrow(datExpr)
  design1=model.matrix(~0+as.factor(datTraits$group_list))
  design=design1
  colnames(design) 
  colnames(design)=c(levels(as.factor(datTraits$group_list)) ) 
  moduleColors <- labels2colors(net$colors)
  # Recalculate MEs with color labels
  MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes
  MEs = orderMEs(MEs0); ##不同顏色的模塊的ME值矩 (樣本vs模塊)
  moduleTraitCor = cor(MEs, design , use = "p");
  moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)
  
  sizeGrWindow(10,6)
  # Will display correlations and their p-values
  textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
                     signif(moduleTraitPvalue, 1), ")", sep = "");
  dim(textMatrix) = dim(moduleTraitCor)
  png("step5-Module-trait-relationships.png",width = 800,height = 1200,res = 120)
  par(mar = c(6, 8.5, 3, 3));
  # Display the correlation values within a heatmap plot
  labeledHeatmap(Matrix = moduleTraitCor,
                 xLabels = colnames(design),
                 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"))
  dev.off()
  table( labels2colors(net$colors))
}

<img src="https://tva1.sinaimg.cn/large/006y8mN6gy1g9600thqfxj30no0xcq69.jpg" alt="image-20191121214946750" style="zoom:50%;" />

###step 6
###############首先計(jì)算模塊與基因的相關(guān)性矩陣
# 把各個module的名字提取出來(從第三個字符開始),用于一會重命名
modNames = substring(names(MEs), 3)
# 得到矩陣
geneModuleMembership = as.data.frame(cor(datExpr, MEs, use = "p"))
# 矩陣t檢驗(yàn)
MMPvalue = as.data.frame(corPvalueStudent(as.matrix(geneModuleMembership), nSamples))
# 修改列名
names(geneModuleMembership) = paste("MM", modNames, sep="")
names(MMPvalue) = paste("p.MM", modNames, sep="")


################再計(jì)算性狀與基因的相關(guān)性矩陣
PC <- as.data.frame(design[,2])
names(PC) = "PC"
# 得到矩陣
geneTraitSignificance = as.data.frame(cor(datExpr, PC, use = "p"))
# 矩陣t檢驗(yàn)
GSPvalue = as.data.frame(corPvalueStudent(as.matrix(geneTraitSignificance), nSamples))
# 修改列名
names(geneTraitSignificance) = paste("GS.", names(PC), sep="")
names(GSPvalue) = paste("p.GS.", names(PC), sep="")


###############最后把兩個相關(guān)性矩陣聯(lián)合起來,指定感興趣模塊進(jìn)行分析
###turquoise模塊
module = "turquoise"
column = match(module, modNames)#找到目標(biāo)模塊所在列
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 PC",
                   main = paste("Module membership vs. gene significance\n"),
                   cex.main = 1.2, cex.lab = 1.2, cex.axis = 1.2, col = module)


###brown模塊
module = "brown"
column = match(module, modNames)#找到目標(biāo)模塊所在列
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 PC",
                   main = paste("Module membership vs. gene significance\n"),
                   cex.main = 1.2, cex.lab = 1.2, cex.axis = 1.2, col = module)

image-20191121214958583
image-20191121215006188
###step 7
MEs = moduleEigengenes(datExpr, moduleColors)$eigengenes
## 只有連續(xù)型性狀才能只有計(jì)算
## 這里把是否屬于 Luminal 表型這個變量用0,1進(jìn)行數(shù)值化。
PC = as.data.frame(design[,2]);
names(PC) = "PC"
# Add the weight to existing module eigengenes
MET = orderMEs(cbind(MEs, PC))
# Plot the relationships among the eigengenes and the trait
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)
# Plot the dendrogram
sizeGrWindow(6,6);
par(cex = 1.0)
## 模塊的聚類圖
plotEigengeneNetworks(MET, "Eigengene dendrogram", marDendro = c(0,4,2,0),
                      plotHeatmaps = FALSE)
# Plot the heatmap matrix (note: this plot will overwrite the dendrogram plot)
par(cex = 1.0)
## 性狀與模塊熱圖
plotEigengeneNetworks(MET, "Eigengene adjacency heatmap", marHeatmap = c(3,4,2,2),
                      plotDendrograms = FALSE, xLabelsAngle = 90)

image-20191121215014443

文章中的原圖

image-20191121215025849
image-20191113010507609

最后友情宣傳生信技能樹

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,578評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內(nèi)容