WCGNA 教程(MLsx)

原文地址:https://mp.weixin.qq.com/s/3K7vQVOHZkUvYGI8XYUwiA

? ? ? ? ? ? ? ? ? ? 今天推薦給大家一個R包WGCNA,針對我們的表達譜數據進行分析。

簡單介紹:WGCNA首先假定基因網絡服從無尺度分布,并定義基因共表達相關矩陣、基因網絡形成的鄰接函數,然后計算不同節點的相異系數,并據此構建系統聚類樹。該聚類樹的不同分支代表不同的基因模塊(module),模塊內基因共表達程度高,而分屬不同模塊的基因共表達程度低。

主要應用:如果某些基因在一個生理過程或不同組織中總是具有相類似的表達變化,那么我們有理由認為這些基因在功能上是相關的,可以把它們定義為一個模塊(module)。當基因module被定義出來后,我們可以利用這些結果做很多進一步的工作,如篩選module的核心基因,關聯性狀,代謝通路建模,建立基因互作網絡等。

好了,言歸正傳,我們開始一步步進行演示!

載入WGCNA包,設置隨機種子,默認數據不進行因子轉換

先把原始數據轉列,轉成橫排是探針(基因),縱排是個體的順序,先變成數列,用as.data.fame,然后改列名rownames(design) <- design[,1]

design <- design[,-1]

##datExpr<-as.data.frame(datExpr)? (有可能需要先把數值轉為數據集)

##> datExpr1<-read.table("test.txt",header=T,stringsAsFactors = F)

##> datExpr1<-t(datExpr1)

##> colnames(datExpr1)<-datExpr1[1,]

##> datExpr1<-datExpr1[-1,]

##> datExpr1<-as.data.frame(datExpr1)

library(WGCNA)

set.seed(1)

options(stringsAsFactors = F)

構造性狀數據(亦或是分組數據)

obs<-paste("sam",1:10,sep="")

sample<-as.data.frame(diag(x=1,nrow = length(obs)))

rownames(sample)<-obs

colnames(sample)<-1:10

構造表達量數據

datExpr<-as.data.frame(t(matrix(runif(30000)+5,3000,10)))

rownames(datExpr)<-obs

names(datExpr)<-paste("transcript",1:3000,sep="")

明確樣本數和基因數

nGenes = ncol(datExpr)

nSamples = nrow(datExpr)

首先針對樣本做個系統聚類樹

datExpr_tree<-hclust(dist(datExpr), method = "average")

par(mar = c(0,5,2,0))

plot(datExpr_tree, main = "Sample clustering", sub="", xlab="", cex.lab = 2,

cex.axis = 1, cex.main = 1,cex.lab=1)

針對前面構造的樣品矩陣添加對應顏色

sample_colors <- numbers2colors(sample, colors = c("white","blue"),signed = FALSE)

構造10個樣品的系統聚類樹及性狀熱圖

par(mar = c(1,4,3,1),cex=0.8)

plotDendroAndColors(datExpr_tree, sample_colors,

groupLabels = colnames(sample),

cex.dendroLabels = 0.8,

marAll = c(1, 4, 3, 1),

cex.rowText = 0.01,

main = "Sample dendrogram and trait heatmap")

這個有什么意義呢?

你可以將樣本分為正常組和對照組,或者野生型和突變型等,從而可以查看樣本聚類情況!

針對10個樣品繪制主成分圖(在這里不考慮分組情況)

pca = prcomp(datExpr)

sampletype<-rownames(sample)

par(mar = c(4,4,4,6))

plot(pca$x[,c(1,2)],pch=16,col=rep(rainbow(nSamples),each=1),cex=1.5,main = "PCA map")

text(pca$x[,c(1,2)],row.names(pca$x),col="black",pos=3,cex=1)

legend("right",legend=sampletype,ncol = 1,xpd=T,inset = -0.15,

pch=16,cex=1,col=rainbow(length(sampletype)),bty="n")

library(scatterplot3d)

par(mar = c(4,4,4,4))

scatterplot3d(pca$x[,1:3], highlight.3d=F, col.axis="black",color = rep(rainbow(nSamples),each=1),cex.symbols=1.5,cex.lab=1,cex.axis=1, col.grid="lightblue", main="PCA map", pch=16)

legend("topleft",legend = row.names(pca$x) ,pch=16,cex=1,col=rainbow(nSamples), ncol = 2,bty="n")

選擇合適“軟閥值(soft thresholding power)”beta

powers = c(1:30)

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

設置網絡構建參數選擇范圍,計算無尺度分布拓撲矩陣

par(mfrow = c(1,2))

plot(pow$fitIndices[,1], -sign(pow$fitIndices[,3])*pow$fitIndices[,2],xlab="Soft Threshold (power)",ylab="Scale Free Topology Model Fit,signed R^2",type="n", main = paste("Scale independence"))

text(pow$fitIndices[,1], -sign(pow$fitIndices[,3])*pow$fitIndices[,2],labels=powers,cex=0.5,col="red")

plot(pow$fitIndices[,1], pow$fitIndices[,5],xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n",main = paste("Mean connectivity"))

text(pow$fitIndices[,1], pow$fitIndices[,5], labels=powers, cex=0.6,col="red")

參數beta取值默認是1:30,上述圖形的橫軸均代表權重參數β,左圖縱軸代表對應的網絡中log(k)與log(p(k))相關系數的平方。相關系數的平方越高,說明該網絡越逼近無網路尺度的分布。右圖的縱軸代表對應的基因模塊中所有基因鄰接函數的均值。

在這里,我們選擇β=6構建基因網絡。

接下來是非常重要的一塊內容就是構架基因網絡

大體思路:計算基因間的鄰接性,根據鄰接性計算基因間的相似性,然后推出基因間的相異性系數,并據此得到基因間的系統聚類樹。然后按照混合動態剪切樹的標準,設置每個基因模塊最少的基因數目為30。根據動態剪切法確定基因模塊后,再次分析,依次計算每個模塊的特征向量值,然后對模塊進行聚類分析,將距離較近的模塊合并為新的模塊。

1、計算樹之間的鄰接性

adjacency = adjacency(datExpr, power = softPower)?

2、計算樹之間的相異性

TOM = TOMsimilarity(adjacency)

dissTOM = 1-TOM

3、聚類分析

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

4、設置基因模塊中最少基因包含30個

minModuleSize = 30

dynamicMods

= cutreeDynamic(dendro = geneTree, distM = dissTOM, deepSplit =

2,pamRespectsDendro = FALSE, minClusterSize = minModuleSize)

5、基因分組上色

dynamicColors = labels2colors(dynamicMods)

table(dynamicColors)

6、計算基因模塊的特征值

MEList = moduleEigengenes(datExpr, colors = dynamicColors)

MEs = MEList$eigengenes

MEDiss = 1-cor(MEs)

METree = hclust(as.dist(MEDiss), method = "average")

7、建立系統聚類樹

MEDissThres = 0.4

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

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

7、基因模塊合并

merge = mergeCloseModules(datExpr, dynamicColors, cutHeight = MEDissThres, verbose = 3)

mergedColors = merge$colors

mergedMEs = merge$newMEs

8、繪制基因網絡圖

plotDendroAndColors(geneTree,

cbind(dynamicColors, mergedColors), c("Dynamic Tree Cut", "Merged

dynamic"), dendroLabels = FALSE, hang = 0.03, addGuide = TRUE, guideHang

= 0.05)

拓展分析

不同模塊基因熱圖及關鍵基因的表達

person=cor(datExpr,use = 'p')

corr<-TOM

Colors<-mergedColors

colnames(corr)<-colnames(datExpr)

rownames(corr)<-colnames(datExpr)

names(Colors)<-colnames(datExpr)

colnames(person)<-colnames(datExpr)

rownames(person)<-colnames(datExpr)

umc = unique(mergedColors)

lumc = length(umc)

for (i in c(1:lumc)){

if(umc[i]== "grey"){

next

}

ME=MEs[, paste("ME",umc[i], sep="")]

par(mfrow=c(2,1), mar=c(0.3, 5.5, 3, 2))

plotMat(t(scale(datExpr[,Colors==umc[i]])),nrgcols=30,rlabels=F,rcols=umc[i], main=umc[i], cex.main=2)

par(mar=c(5, 4.2, 0, 0.7))

barplot(ME, col=umc[i], main="", cex.main=2,ylab="eigengene expression",xlab="array sample")

}

一共會生成36個基因模塊熱圖,由于篇幅有限,僅僅展示2個

基因共表達網絡熱圖

kME=signedKME(datExpr, mergedMEs, outputColumnName = "kME", corFnc = "cor", corOptions = "use = 'p'")

if (dim(datExpr)[2]>=1500) nSelect=1500 else nSelect=dim(datExpr)[2]

set.seed(1)

select = sample(nGenes, size = nSelect)

selectTOM = dissTOM[select, select]

selectTree = hclust(as.dist(selectTOM), method = "average")

selectColors = moduleColors[select]

plotDiss = selectTOM^7

TOMplot(plotDiss, selectTree, selectColors, main = "Network heatmap plot")

模塊相關性熱圖

MEs = moduleEigengenes(datExpr, Colors)$eigengenes

MET = orderMEs(MEs)

plotEigengeneNetworks(MET, "Eigengene adjacency heatmap", marHeatmap = c(3,4,2,2), plotDendrograms = FALSE, xLabelsAngle = 90)

模塊與性狀相關性熱圖

moduleTraitCor = cor(MET, sample, use = "p")

moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)

textMatrix = paste(signif(moduleTraitCor, 2), "\n(",signif(moduleTraitPvalue, 1), ")", sep = "")

dim(textMatrix) = dim(moduleTraitCor)

par(mar = c(2, 4, 2, 1.5))

labeledHeatmap(Matrix = moduleTraitCor,

xLabelsAngle = 0,

cex.lab = 0.5,

xLabels = colnames(sample),

yLabels = names(MET),

ySymbols = names(MET),

colorLabels = FALSE,

colors = blueWhiteRed(100),

textMatrix = textMatrix,

setStdMargins = FALSE,

cex.text = 0.5,

zlim = c(-1,1),

yColorWidth=0.02,

xColorWidth = 0.05,

main = paste("Module-trait relationships"))

基因的系統樹圖及性狀相關性熱圖

geneTraitSignificance = as.data.frame(cor(datExpr, sample, use = "p"))

geneTraitColor=as.data.frame(numbers2colors(geneTraitSignificance,signed=TRUE,colors = colorRampPalette(c("blue","white","red"))(100)))

names(geneTraitColor)= colnames(sample)

par(mar = c(3.5, 7, 2, 1))

plotDendroAndColors(geneTree, cbind(mergedColors, geneTraitColor),

c("Module",colnames(sample)),dendroLabels = FALSE, hang = 0.03,

addGuide = TRUE, guideHang = 0.05)

不同模塊的基因顯著性圖

geneTraitSignificance = as.data.frame(cor(datExpr, sample, use = "p"))

GSPvalue = as.data.frame(corPvalueStudent(as.matrix(geneTraitSignificance), nSamples))

names(geneTraitSignificance) = paste("GS.", colnames(sample), sep="")

names(GSPvalue) = paste("GS.", colnames(sample), sep="")

modNames = substring(names(MET), 3)

for (module in modNames){

if(module== "grey"){ next }

column = match(module, modNames); # col number of interesting modules

moduleGenes = Colors==module;

par(mfrow = c(1,1))

verboseScatterplot(abs(kME[moduleGenes, column]),

abs(geneTraitSignificance[moduleGenes, 1]),

xlab = paste("Module Membership in", module, "module"),

ylab = "Gene significance",

main = paste("Module membership vs. gene significance

"),cex.main = 1.2, cex.lab = 1.2, pch=19,cex.axis = 1.2, col = module)}

好了,今天就到這里……

相關函數? plotMEpairs() (這個也很重要)

一步法:

TOM = TOMsimilarityFromExpr(data_matrix_mv, power = 6);

# Read in the annotation file

# annot = read.csv(file = "GeneAnnotation.csv");

# Select modules需要修改,選擇需要導出的模塊顏色

modules = c("turquoise");

# Select module probes選擇模塊探測

probes = colnames(data_matrix_mv)

inModule = is.finite(match(mergedColors, modules));

modProbes = probes[inModule];

#modGenes = annot$gene_symbol[match(modProbes, annot$substanceBXH)];

# Select the corresponding Topological Overlap

modTOM = TOM[inModule, inModule];

dimnames(modTOM) = list(modProbes, modProbes)

# Export the network into edge and node list files Cytoscape can read

cyt = exportNetworkToCytoscape(modTOM,

???????????????????????????????edgeFile = paste("AS-green-FPKM-One-step-CytoscapeInput-edges-", paste(modules, collapse="-"), ".txt", sep=""),

???????????????????????????????nodeFile = paste("AS-green-FPKM-One-step-CytoscapeInput-nodes-", paste(modules, collapse="-"), ".txt", sep=""),

???????????????????????????????weighted = TRUE,

???????????????????????????????threshold = 0.02,

???????????????????????????????nodeNames = modProbes,

???????????????????????????????#altNodeNames = modGenes,

???????????????????????????????nodeAttr = mergedColors[inModule]);

作者:蘇慕晨楓

鏈接:http://www.lxweimin.com/p/72c5b4e9ac3e

來源:簡書

簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

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

推薦閱讀更多精彩內容