换工作电脑来处理数据,首先打开终端,切换目录到压缩文件所在目录,按照流程走
rm(list = ls())
options(stringsAsFactors = F)
# 切换工作目录如果有必要的话
# A novel microglial subset plays a key role in myelinogenesis in developing brain
## https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE78809
#wget -c ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE48nnn/GSE78809/suppl/GSE78809_RAW.tar
tar -xf GSE78809_RAW.tar
gzip -d *.gz
## 首先在GSE78809_RAW目录里面生成tmp.txt文件,使用shell脚本
awk '{print FILENAME"\t"$0}' GSM*.txt |grep -v EnsEMBL_Gene_ID >tmp.txt
这里闹了个笑话,我直接复制了曾老师的指令,GSE号忘了改,导致终端报错,还是有点慌
tar: Error opening archive: Failed to open 'GSE48213_RAW.tar'
,没有仔细检查报错的内容,就把整句复制到谷歌里面搜索怎么解决,然后找到一堆更改终端的资料链接,甚至还去更新了一把brew update
浪费了不少时间,终于看到一句话“其中 tar 是Mac 系统自带的命令。”
既然是自带的应该不用安装(至于为啥会产生想自己安装,主要是由于之前安装过wget以为很多指令都需要安装)就是经验太少了。
好了言归正传,得到了tmp.txt文件以后才是重点,读取数据保存数据
a=read.table('tmp.txt',sep = '\t',stringsAsFactors = F)
library(reshape2)
fpkm <- dcast(a,formula = V2~V1)
rownames(fpkm)=fpkm[,1]
fpkm=log10(fpkm[,-1]+1)
colnames(fpkm)=sapply(colnames(fpkm),function(x) strsplit(x,"_")[[1]][1])
library(GEOquery)
a=getGEO('GSE78809')
pd=pData(a[[1]])
head(pd)
metadata=pData(a[[1]])[,c(2,10,11)]
datTraits = data.frame(gsm=metadata[,1],
cellline=trimws(sapply(as.character(metadata$characteristics_ch1),function(x) strsplit(x,":")[[1]][2])),
subtype=trimws(sapply(as.character(metadata$characteristics_ch1.1),function(x) strsplit(x,":")[[1]][2]))
)
save(fpkm,datTraits,file = 'GSE78809-wgcna-input.RData')
load('GSE78809-wgcna-input.RData')
library(WGCNA)
## step 1 :
if(T){
fpkm[1:4,1:4]
head(datTraits)
table(datTraits$subtype)
RNAseq_voom <- fpkm
## 因为WGCNA针对的是基因进行聚类,而一般我们的聚类是针对样本用hclust即可,所以这个时候需要转置
WGCNA_matrix = t(RNAseq_voom[order(apply(RNAseq_voom,1,mad), decreasing = T)[1:1000],])
datExpr0 <- WGCNA_matrix ## top 1000 mad genes
datExpr <- datExpr0
## 下面主要是为了防止临床表型与样本名字对不上
sampleNames = rownames(datExpr);
traitRows = match(sampleNames, datTraits$gsm)
rownames(datTraits) = datTraits[traitRows, 1]
}
## step 2
datExpr[1:4,1:4]
if(T){
powers = c(c(1:10), seq(from = 12, to=20, by=2))
# Call the network topology analysis function
sft = pickSoftThreshold(datExpr, powerVector = powers, verbose = 5)
#设置网络构建参数选择范围,计算无尺度分布拓扑矩阵
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()
}
## step3 构建加权共表达网络(Weight co-expression network)
## 首先是一步法完成网络构建
if(T){
net = blockwiseModules(
datExpr,
power = sft$powerEstimate,
maxBlockSize = 1000,
TOMType = "unsigned", minModuleSize = 30,
reassignThreshold = 0, mergeCutHeight = 0.25,
numericLabels = TRUE, pamRespectsDendro = FALSE,
saveTOMs = F,
verbose = 3
)
table(net$colors)
}
## 然后是分布法完成网络构建,仅供有探索精神的同学挑战。
## 构建加权共表达网络分为两步:
## 1. 计算邻近值,也是就是两个基因在不样品中表达量的表达相关系数(pearson correlation rho),
## 参考 2.b.2 in https://labs.genetics.ucla.edu/horvath/htdocs/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/FemaleLiver-02-networkConstr-man.pdf
## 2. 计算topology overlap similarity (TOM)。 WGCNA认为,只通过计算两个基因的表达相关系数构建共表达网络是不足够的。
## 于是他们用TOM表示两个基因在网络结构上的相似性,即两个基因如果具有相似的邻近基因,这两个基因更倾向于有相互作用。
## 参考 2.b.3 in https://labs.genetics.ucla.edu/horvath/htdocs/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/FemaleLiver-02-networkConstr-man.pdf
if(F){
#(1)网络构建 Co-expression similarity and adjacency
adjacency = adjacency(datExpr, power = sft$powerEstimate)
#(2) 邻近矩阵到拓扑矩阵的转换,Turn adjacency into topological overlap
TOM = TOMsimilarity(adjacency);
dissTOM = 1-TOM
# (3) 聚类拓扑矩阵 Call the hierarchical clustering function
geneTree = hclust(as.dist(dissTOM), method = "average");
# Plot the resulting clustering tree (dendrogram)
sizeGrWindow(12,9)
## 这个时候的geneTree与一步法的 net$dendrograms[[1]] 性质类似,但是还需要进行进一步处理
plot(geneTree, xlab="", sub="", main = "Gene clustering on TOM-based dissimilarity",
labels = FALSE, hang = 0.04);
#(4) 聚类分支的修整 dynamicTreeCut
# We like large modules, so we set the minimum module size relatively high:
minModuleSize = 30;
# Module identification using dynamic tree cut:
dynamicMods = cutreeDynamic(dendro = geneTree, distM = dissTOM,
deepSplit = 2, pamRespectsDendro = FALSE,
minClusterSize = minModuleSize);
table(dynamicMods)
#4. 绘画结果展示
# Convert numeric lables into colors
dynamicColors = labels2colors(dynamicMods)
table(dynamicColors)
# Plot the dendrogram and colors underneath
#sizeGrWindow(8,6)
plotDendroAndColors(geneTree, dynamicColors, "Dynamic Tree Cut",
dendroLabels = FALSE, hang = 0.03,
addGuide = TRUE, guideHang = 0.05,
main = "Gene dendrogram and module colors")
#5. 聚类结果相似模块的融合,Merging of modules whose expression profiles are very similar
#在聚类树中每一leaf是一个短线,代表一个基因,
#不同分之间靠的越近表示有高的共表达基因,将共表达极其相似的modules进行融合
# Calculate eigengenes
MEList = moduleEigengenes(datExpr, colors = dynamicColors)
MEs = MEList$eigengenes
# Calculate dissimilarity of module eigengenes
MEDiss = 1-cor(MEs);
# Cluster module eigengenes
METree = hclust(as.dist(MEDiss), method = "average");
# Plot the result
#sizeGrWindow(7, 6)
plot(METree, main = "Clustering of module eigengenes",
xlab = "", sub = "")
#选择有75%相关性的进行融合
MEDissThres = 0.25
# Plot the cut line into the dendrogram
abline(h=MEDissThres, col = "red")
# Call an automatic merging function
merge = mergeCloseModules(datExpr, dynamicColors, cutHeight = MEDissThres, verbose = 3)
# The merged module colors
mergedColors = merge$colors;
# Eigengenes of the new merged modules:
mergedMEs = merge$newMEs
}
## step 4 : 模块可视化
if(T){
# Convert labels to colors for plotting
mergedColors = labels2colors(net$colors)
table(mergedColors)
moduleColors=mergedColors
# Plot the dendrogram and the module colors underneath
png("step4-genes-modules.png",width = 800,height = 600)
plotDendroAndColors(net$dendrograms[[1]], mergedColors[net$blockGenes[[1]]],
"Module colors",
dendroLabels = FALSE, hang = 0.03,
addGuide = TRUE, guideHang = 0.05)
dev.off()
## assign all of the gene to their corresponding module
## hclust for the genes.
}
if(F){
#明确样本数和基因
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(as.numeric(factor(datTraits$subtype)),
colors = c("white","blue","red","green"),signed = FALSE)
## 这个给样品添加对应颜色的代码需要自行修改以适应自己的数据分析项目
# sample_colors <- numbers2colors( datTraits ,signed = FALSE)
## 如果样品有多种分类情况,而且 datTraits 里面都是分类信息,那么可以直接用上面代码,当然,这样给的颜色不明显,意义不大
#10个样品的系统聚类树及性状热图
par(mar = c(1,4,3,1),cex=0.8)
png("sample-subtype-cluster.png",width = 800,height = 600)
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")
dev.off()
}
## step 5 (最重要的) 模块和性状的关系
## 这一步主要是针对于连续变量,如果是分类变量,需要转换成连续变量方可使用
table(datTraits$subtype)
if(T){
nGenes = ncol(datExpr)
nSamples = nrow(datExpr)
design=model.matrix(~0+ datTraits$subtype)
colnames(design)=levels(datTraits$subtype)
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()
之后就是按顺序往下走,其中第二步和第四步得到2个图,
走到第5步出错了,我怀疑是之前对fpkm不是太了解看到矩阵里面的数值小的0大的超过1000,而测试数据都是很小的,就擅自把它 运算了一下
Error in labeledHeatmap(Matrix = moduleTraitCor, xLabels = colnames(design), :
Length of 'xLabels' must equal the number of columns in 'Matrix.'
但是如果不改那个计算,也走不成
到第三步就会出错
Error in blockwiseModules(datExpr, power = sft$powerEstimate, maxBlockSize = 2000, :
REAL() can only be applied to a 'numeric', not a 'integer'
作为数学渣的我无法理解REAL() 只能应用于"数字",而不能应用于"整数"
而且,第二步出的图也不太对:可能是那些设置的数值需要改动,或者就是前面已经出错了。还不知道错在哪里-_-
还是有必要再看看说明书,必须回头把优秀学徒的纯代码7步走完WGCNA好好学习一下。