寫在前面
最近實(shí)在是忙的不行,根本沒時(shí)間更新,一到家就只想睡覺。??
今天寫個(gè)最近用到的分析方法,Weighted correlation network analysis
(WGCNA
),是非常經(jīng)典的生信分析方法了,現(xiàn)在被引有9913次
了,馬上就要破萬啦。??
網(wǎng)上相關(guān)的教程也是不勝枚舉,但多多少少是有些不盡人意的地方,有的少步驟,有的代碼不全。??
這里在仔細(xì)閱讀了官方手冊后,在這里和大家一起認(rèn)真地step by step
研究一下,查缺補(bǔ)漏吧。??
用到的包
rm(list = ls())
library(tidyverse)
library(WGCNA)
示例數(shù)據(jù)
數(shù)據(jù)是雌性小鼠肝臟的基因表達(dá)譜
,來自這篇paper
:??
Ghazalpour A, Doss S, Zhang B, et al. Integrating genetic and network analysis to characterize genes related to mouse weight. PLoS Genet. 2006;2(8):e130. doi:10.1371/journal.pgen.0020130
dat <- read.csv("./FemaleLiver-Data/LiverFemale3600.csv")
DT::datatable(dat)
整理數(shù)據(jù)
我們先提取表達(dá)矩陣,這里是需要轉(zhuǎn)置的。??
datExpr0 <- as.data.frame(t(dat[, -c(1:8)]))
names(datExpr0) <- dat$substanceBXH
rownames(datExpr0) <- names(dat)[-c(1:8)]
DT::datatable(datExpr0)
基因或樣本過濾
有一些表達(dá)值過低的基因或樣本,我們是需要過濾掉的,包里也是提供了相應(yīng)的函數(shù),我們看一下吧。??
5.1 查看是否有不好的基因或樣本
我們的數(shù)據(jù)里沒有不好的基因或者樣本。??
gsg <- goodSamplesGenes(datExpr0, verbose = 3);
gsg$allOK
5.2 自動(dòng)化過濾
這里提供一個(gè)if語句
,顯示不好的基因或者樣本,進(jìn)行自動(dòng)化過濾。??
if (!gsg$allOK)
{
## 打印已刪除的基因和樣本名稱
if (sum(!gsg$goodGenes)>0)
printFlush(paste("Removing genes:", paste(names(datExpr0)[!gsg$goodGenes], collapse = ", ")));
if (sum(!gsg$goodSamples)>0)
printFlush(paste("Removing samples:", paste(rownames(datExpr0)[!gsg$goodSamples], collapse = ", ")));
datExpr0 = datExpr0[gsg$goodSamples, gsg$goodGenes]
}
樣本聚類
接著我們需要對(duì)樣本進(jìn)行聚類,有一些outlier
的樣本可能還需要去除掉。??
6.1 繪制聚類樹
聚類的方法很多,這里整理一下:??
ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid"
sampleTree <- hclust(dist(datExpr0), method = "average");
plot(sampleTree,
main = "Sample clustering to detect outliers",
sub="",
xlab="",
cex.lab = 1.5, cex.axis = 1.5, cex.main = 2)
6.2 畫個(gè)紅線
這里我們有一個(gè)聚類比較差的樣本,我們把它去掉吧。??
plo9888888=t(sampleTree,
main = "Sample clustering to detect outliers",
sub="",
xlab="",
cex.lab = 1.5, cex.axis = 1.5, cex.main = 2
)
abline(h = 15, col = "red")
6.3 去除聚類異常的樣本
clust <- cutreeStatic(sampleTree, cutHeight = 15, minSize = 10)
table(clust)
6.4 提取過濾后矩陣
keepSamples <- (clust == 1)
datExpr <- datExpr0[keepSamples, ]
nGenes <- ncol(datExpr)
nSamples <- nrow(datExpr)
DT::datatable(datExpr)
加載臨床/性狀數(shù)據(jù)
接著我們把臨床或性狀數(shù)據(jù)(traits
)導(dǎo)入進(jìn)來,和前面的聚類樹一起繪圖。??
7.1 讀入traits
traitData <- read.csv("./FemaleLiver-Data/ClinicalTraits.csv");
DT::datatable(traitData)
7.2 整理traits
我們把一些不需要的traits
去掉,只保留我們自己需要的,這里需要和樣本名一一對(duì)應(yīng)上。??
allTraits <- traitData[, -c(31, 16)]
allTraits <- allTraits[, c(2, 11:36) ]
femaleSamples <- rownames(datExpr)
traitRows <- match(femaleSamples, allTraits$Mice)
datTraits <- allTraits[traitRows, -1]
rownames(datTraits) <- allTraits[traitRows, 1]
collectGarbage()
DT::datatable(datTraits)
繪制最終聚類樹
sampleTree2 <- hclust(dist(datExpr), method = "average")
traitColors <- numbers2colors(datTraits,
signed = F,
colors = greenWhiteRed(100)
)
plotDendroAndColors(sampleTree2,
traitColors,
groupLabels = names(datTraits),
main = "Sample dendrogram and trait heatmap")
save一下
這里我們保存一下數(shù)據(jù),下期繼續(xù)。??
save(datExpr, datTraits, file = "FemaleLiver-01-dataInput.RData")
補(bǔ)充一下
現(xiàn)在很多paper
都是先做差異基因分析,然后將DEGs
提取出來做WGCNA
,其實(shí)這種方法原作者并不推薦,還是推薦大家將所有基因初步過濾后進(jìn)行WGCNA
的分析,原文如下:??
"We do not recommend filtering genes by differential expression. WGCNA is designed to be an unsupervised analysis method that clusters genes based on their expression profiles. Filtering genes by differential expression will lead to a set of correlated genes that will essentially form a single (or a few highly correlated) modules."
如何引用
??
Langfelder, P., Horvath, S. WGCNA: an R package for weighted correlation network analysis. BMC Bioinformatics 9, 559 (2008). https://doi.org/10.1186/1471-2105-9-559
<center>最后祝大家早日不卷!~</center>
點(diǎn)個(gè)在看吧各位~ ?.???? ??? ?
<center> <b>?? 往期精彩 <b> </center>
?? <font size=1>?? ComplexHeatmap | 顏狗寫的高顏值熱圖代碼!</font>
?? <font size=1>?? ComplexHeatmap | 你的熱圖注釋還擠在一起看不清嗎!?</font>
?? <font size=1>?? Google | 谷歌翻譯崩了我們怎么辦!?(附完美解決方案)</font>
?? <font size=1>?? scRNA-seq | 吐血整理的單細(xì)胞入門教程</font>
?? <font size=1>?? NetworkD3 | 讓我們一起畫個(gè)動(dòng)態(tài)的桑基圖吧~</font>
?? <font size=1>?? RColorBrewer | 再多的配色也能輕松搞定!~</font>
?? <font size=1>?? rms | 批量完成你的線性回歸</font>
?? <font size=1>?? CMplot | 完美復(fù)刻N(yùn)ature上的曼哈頓圖</font>
?? <font size=1>?? Network | 高顏值動(dòng)態(tài)網(wǎng)絡(luò)可視化工具</font>
?? <font size=1>?? boxjitter | 完美復(fù)刻N(yùn)ature上的高顏值統(tǒng)計(jì)圖</font>
?? <font size=1>?? linkET | 完美解決ggcor安裝失敗方案(附教程)</font>
?? <font size=1>......</font>
本文由mdnice多平臺(tái)發(fā)布