WGCNA分析使用教程

WGCNA基本概念

加权基因共表达网络分析 (WGCNA, Weighted correlation network analysis)是用来描述不同样品之间基因关联模式的系统生物学方法,可以用来鉴定高度协同变化的基因集, 并根据基因集的内连性和基因集与表型之间的关联鉴定候补生物标记基因或治疗靶点。

相比于只关注差异表达的基因,WGCNA利用数千或近万个变化最大的基因或全部基因的信息识别感兴趣的基因集,并与表型进行显著性关联分析。一是充分利用了信息,二是把数千个基因与表型的关联转换为数个基因集与表型的关联,免去了多重假设检验校正的问题。

理解WGCNA,需要先理解下面几个术语和它们在WGCNA中的定义。

  • 共表达网络:定义为加权基因网络。点代表基因,边代表基因表达相关性。加权是指对相关性值进行冥次运算 (冥次的值也就是软阈值 (power, pickSoftThreshold这个函数所做的就是确定合适的power))。无向网络的边属性计算方式为 abs(cor(genex, geney)) ^ power;有向网络的边属性计算方式为 (1+cor(genex, geney)/2) ^ power; sign hybrid的边属性计算方式为cor(genex, geney)^power if cor>0 else 0。这种处理方式强化了强相关,弱化了弱相关或负相关,使得相关性数值更符合无标度网络特征,更具有生物意义。如果没有合适的power,一般是由于部分样品与其它样品因为某种原因差别太大导致的,可根据具体问题移除部分样品或查看后面的经验值
  • Module(模块):高度內连的基因集。在无向网络中,模块内是高度相关的基因。在有向网络中,模块内是高度正相关的基因。把基因聚类成模块后,可以对每个模块进行三个层次的分析:1. 功能富集分析查看其功能特征是否与研究目的相符;2. 模块与性状进行关联分析,找出与关注性状相关度最高的模块;3. 模块与样本进行关联分析,找到样品特异高表达的模块。
  • Connectivity (连接度):类似于网络中 “度” (degree)的概念。每个基因的连接度是与其相连的基因的边属性之和
  • Module eigengene E: 给定模型的第一主成分,代表整个模型的基因表达谱。这个是个很巧妙的梳理,我们之前讲过PCA分析的降维作用,之前主要是拿来做可视化,现在用到这个地方,很好的用一个向量代替了一个矩阵,方便后期计算。(降维除了PCA,还可以看看tSNE)
  • Intramodular connectivity: 给定基因与给定模型内其他基因的关联度,判断基因所属关系。
  • Module membership: 给定基因表达谱与给定模型的eigengene的相关性。
  • Hub gene: 关键基因 (连接度最多或连接多个模块的基因)。
  • Adjacency matrix (邻接矩阵):基因和基因之间的加权相关性值构成的矩阵。
  • TOM (Topological overlap matrix):把邻接矩阵转换为拓扑重叠矩阵,以降低噪音和假相关,获得的新距离矩阵,这个信息可拿来构建网络或绘制TOM图。

基本分析流程

WGCNA分析使用教程-图片1

  1. 构建基因共表达网络:使用加权的表达相关性。
  2. 识别基因集:基于加权相关性,进行层级聚类分析,并根据设定标准切分聚类结果,获得不同的基因模块,用聚类树的分枝和不同颜色表示。
  3. 如果有表型信息,计算基因模块与表型的相关性,鉴定性状相关的模块。
  4. 研究模型之间的关系,从系统层面查看不同模型的互作网络。
  5. 从关键模型中选择感兴趣的驱动基因,或根据模型中已知基因的功能推测未知基因的功能。
  6. 导出TOM矩阵,绘制相关性图。

WGCNA包实战

R包WGCNA是用于计算各种加权关联分析的功能集合,可用于网络构建,基因筛选,基因簇鉴定,拓扑特征计算,数据模拟和可视化等。

输入数据和参数选择

  1. WGCNA本质是基于相关系数的网络分析方法,适用于多样品数据模式,一般要求样本数多于15个。样本数多于20时效果更好,样本越多,结果越稳定。
  2. 基因表达矩阵: 常规表达矩阵即可,即基因在行,样品在列,进入分析前做一个转置。RPKM、FPKM或其它标准化方法影响不大,推荐使用Deseq2的varianceStabilizingTransformationlog2(x+1)对标准化后的数据做个转换。如果数据来自不同的批次,需要先移除批次效应 (记得上次转录组培训课讲过如何操作)。如果数据存在系统偏移,需要做下quantile normalization
  3. 性状矩阵:用于关联分析的性状必须是数值型特征 (如下面示例中的HeightWeight,Diameter)。如果是区域或分类变量,需要转换为0-1矩阵的形式(1表示属于此组或有此属性,0表示不属于此组或无此属性,如样品分组信息WT, KO, OE)。
    1. ID WT KO OE Height Weight Diameter
    2. samp1 1 0 0 1 2 3
    3. samp2 1 0 0 2 4 6
    4. samp3 0 1 0 10 20 50
    5. samp4 0 1 0 15 30 80
    6. samp5 0 0 1 NA 9 8
    7. samp6 0 0 1 4 8 7
  4. 推荐使用Signed networkRobust correlation (bicor)。(这个根据自己的需要,看看上面写的每个网络怎么计算的,更知道怎么选择)
  5. 无向网络在power小于15或有向网络power小于30内,没有一个power值可以使无标度网络图谱结构R^2达到0.8或平均连接度降到100以下,可能是由于部分样品与其他样品差别太大造成的。这可能由批次效应样品异质性实验条件对表达影响太大等造成, 可以通过绘制样品聚类查看分组信息、关联批次信息、处理信息和有无异常样品 (可以使用之前讲过的热图简化,增加行或列属性)。如果这确实是由有意义的生物变化引起的,也可以使用后面程序中的经验power值。

安装WGCNA

WGCNA依赖的包比较多,bioconductor上的包需要自己安装,cran上依赖的包可以自动安装。通常在R中运行下面4条语句就可以完成WGCNA的安装。

建议在编译安装R时增加--with-blas --with-lapack提高矩阵运算的速度,具体见R和Rstudio安装

  1. #source("https://bioconductor.org/biocLite.R")
  2. #biocLite(c("AnnotationDbi", "impute","GO.db", "preprocessCore"))
  3. #site="https://mirrors.tuna.tsinghua.edu.cn/CRAN"
  4. #install.packages(c("WGCNA", "stringr", "reshape2"), repos=site)

 

WGCNA实战

实战采用的是官方提供的清理后的矩阵,原矩阵信息太多,容易给人误导,后台回复 WGCNA 获取数据。

数据读入

  1. library(WGCNA)
  2.  
  3. ## Loading required package: dynamicTreeCut
  4.  
  5. ## Loading required package: fastcluster
  6.  
  7. ##
  8. ## Attaching package: 'fastcluster'
  9.  
  10. ## The following object is masked from 'package:stats':
  11. ##
  12. ## hclust
  13.  
  14. ## ==========================================================================
  15. ## *
  16. ## * Package WGCNA 1.63 loaded.
  17. ## *
  18. ## * Important note: It appears that your system supports multi-threading,
  19. ## * but it is not enabled within WGCNA in R.
  20. ## * To allow multi-threading within WGCNA with all available cores, use
  21. ## *
  22. ## * allowWGCNAThreads()
  23. ## *
  24. ## * within R. Use disableWGCNAThreads() to disable threading if necessary.
  25. ## * Alternatively, set the following environment variable on your system:
  26. ## *
  27. ## * ALLOW_WGCNA_THREADS=<number_of_processors>
  28. ## *
  29. ## * for example
  30. ## *
  31. ## * ALLOW_WGCNA_THREADS=48
  32. ## *
  33. ## * To set the environment variable in linux bash shell, type
  34. ## *
  35. ## * export ALLOW_WGCNA_THREADS=48
  36. ## *
  37. ## * before running R. Other operating systems or shells will
  38. ## * have a similar command to achieve the same aim.
  39. ## *
  40. ## ==========================================================================
  41.  
  42. ##
  43. ## Attaching package: 'WGCNA'
  44.  
  45. ## The following object is masked from 'package:stats':
  46. ##
  47. ## cor
  48.  
  49. library(reshape2)
  50. library(stringr)
  51.  
  52. #
  53. options(stringsAsFactors = FALSE)
  54. # 打开多线程
  55. enableWGCNAThreads()
  56.  
  57. ## Allowing parallel execution with up to 47 working processes.
  58.  
  59. # 常规表达矩阵,log2转换后或
  60. # Deseq2的varianceStabilizingTransformation转换的数据
  61. # 如果有批次效应,需要事先移除,可使用removeBatchEffect
  62. # 如果有系统偏移(可用boxplot查看基因表达分布是否一致),
  63. # 需要quantile normalization
  64.  
  65. exprMat <- "WGCNA/LiverFemaleClean.txt"
  66.  
  67.  
  68. # 官方推荐 "signed" 或 "signed hybrid"
  69. # 为与原文档一致,故未修改
  70. type = "unsigned"
  71.  
  72. # 相关性计算
  73. # 官方推荐 biweight mid-correlation & bicor
  74. # corType: pearson or bicor
  75. # 为与原文档一致,故未修改
  76. corType = "pearson"
  77.  
  78. corFnc = ifelse(corType=="pearson", cor, bicor)
  79. # 对二元变量,如样本性状信息计算相关性时,
  80. # 或基因表达严重依赖于疾病状态时,需设置下面参数
  81. maxPOutliers = ifelse(corType=="pearson",1,0.05)
  82.  
  83. # 关联样品性状的二元变量时,设置
  84. robustY = ifelse(corType=="pearson",T,F)
  85.  
  86. ##导入数据##
  87. dataExpr <- read.table(exprMat, sep='\t', row.names=1, header=T,
  88. quote="", comment="", check.names=F)
  89.  
  90. dim(dataExpr)
  91.  
  92. ## [1] 3600 134
  93.  
  94. head(dataExpr)[,1:8]
  95.  
  96. ## F2_2 F2_3 F2_14 F2_15 F2_19 F2_20
  97. ## MMT00000044 -0.01810 0.0642 6.44e-05 -0.05800 0.04830 -0.15197410
  98. ## MMT00000046 -0.07730 -0.0297 1.12e-01 -0.05890 0.04430 -0.09380000
  99. ## MMT00000051 -0.02260 0.0617 -1.29e-01 0.08710 -0.11500 -0.06502607
  100. ## MMT00000076 -0.00924 -0.1450 2.87e-02 -0.04390 0.00425 -0.23610000
  101. ## MMT00000080 -0.04870 0.0582 -4.83e-02 -0.03710 0.02510 0.08504274
  102. ## MMT00000102 0.17600 -0.1890 -6.50e-02 -0.00846 -0.00574 -0.01807182
  103. ## F2_23 F2_24
  104. ## MMT00000044 -0.00129 -0.23600
  105. ## MMT00000046 0.09340 0.02690
  106. ## MMT00000051 0.00249 -0.10200
  107. ## MMT00000076 -0.06900 0.01440
  108. ## MMT00000080 0.04450 0.00167
  109. ## MMT00000102 -0.12500 -0.06820

数据筛选

  1. ## 筛选中位绝对偏差前75%的基因,至少MAD大于0.01
  2. ## 筛选后会降低运算量,也会失去部分信息
  3. ## 也可不做筛选,使MAD大于0即可
  4. m.mad <- apply(dataExpr,1,mad)
  5. dataExprVar <- dataExpr[which(m.mad >
  6. max(quantile(m.mad, probs=seq(0, 1, 0.25))[2],0.01)),]
  7.  
  8. ## 转换为样品在行,基因在列的矩阵
  9. dataExpr <- as.data.frame(t(dataExprVar))
  10.  
  11. ## 检测缺失值
  12. gsg = goodSamplesGenes(dataExpr, verbose = 3)
  13.  
  14. ## Flagging genes and samples with too many missing values...
  15. ## ..step 1
  16.  
  17. if (!gsg$allOK){
  18. # Optionally, print the gene and sample names that were removed:
  19. if (sum(!gsg$goodGenes)>0)
  20. printFlush(paste("Removing genes:",
  21. paste(names(dataExpr)[!gsg$goodGenes], collapse = ",")));
  22. if (sum(!gsg$goodSamples)>0)
  23. printFlush(paste("Removing samples:",
  24. paste(rownames(dataExpr)[!gsg$goodSamples], collapse = ",")));
  25. # Remove the offending genes and samples from the data:
  26. dataExpr = dataExpr[gsg$goodSamples, gsg$goodGenes]
  27. }
  28.  
  29. nGenes = ncol(dataExpr)
  30. nSamples = nrow(dataExpr)
  31.  
  32. dim(dataExpr)
  33.  
  34. ## [1] 134 2697
  35.  
  36. head(dataExpr)[,1:8]
  37.  
  38. ## MMT00000051 MMT00000080 MMT00000102 MMT00000149 MMT00000159
  39. ## F2_2 -0.02260000 -0.04870000 0.17600000 0.07680000 -0.14800000
  40. ## F2_3 0.06170000 0.05820000 -0.18900000 0.18600000 0.17700000
  41. ## F2_14 -0.12900000 -0.04830000 -0.06500000 0.21400000 -0.13200000
  42. ## F2_15 0.08710000 -0.03710000 -0.00846000 0.12000000 0.10700000
  43. ## F2_19 -0.11500000 0.02510000 -0.00574000 0.02100000 -0.11900000
  44. ## F2_20 -0.06502607 0.08504274 -0.01807182 0.06222751 -0.05497686
  45. ## MMT00000207 MMT00000212 MMT00000241
  46. ## F2_2 0.06870000 0.06090000 -0.01770000
  47. ## F2_3 0.10100000 0.05570000 -0.03690000
  48. ## F2_14 0.10900000 0.19100000 -0.15700000
  49. ## F2_15 -0.00858000 -0.12100000 0.06290000
  50. ## F2_19 0.10500000 0.05410000 -0.17300000
  51. ## F2_20 -0.02441415 0.06343181 0.06627665
软阈值筛选
  1. ## 查看是否有离群样品
  2. sampleTree = hclust(dist(dataExpr), method = "average")
  3. plot(sampleTree, main = "Sample clustering to detect outliers", sub="", xlab="")

WGCNA分析使用教程-图片2

软阈值的筛选原则是使构建的网络更符合无标度网络特征。

  1. powers = c(c(1:10), seq(from = 12, to=30, by=2))
  2. sft = pickSoftThreshold(dataExpr, powerVector=powers,
  3. networkType=type, verbose=5)
  4.  
  5. ## pickSoftThreshold: will use block size 2697.
  6. ## pickSoftThreshold: calculating connectivity for given powers...
  7. ## ..working on genes 1 through 2697 of 2697
  8. ## Power SFT.R.sq slope truncated.R.sq mean.k. median.k. max.k.
  9. ## 1 1 0.1370 0.825 0.412 587.000 5.95e+02 922.0
  10. ## 2 2 0.0416 -0.332 0.630 206.000 2.02e+02 443.0
  11. ## 3 3 0.2280 -0.747 0.920 91.500 8.43e+01 247.0
  12. ## 4 4 0.3910 -1.120 0.908 47.400 4.02e+01 154.0
  13. ## 5 5 0.7320 -1.230 0.958 27.400 2.14e+01 102.0
  14. ## 6 6 0.8810 -1.490 0.916 17.200 1.22e+01 83.7
  15. ## 7 7 0.8940 -1.640 0.869 11.600 7.29e+00 75.4
  16. ## 8 8 0.8620 -1.660 0.827 8.250 4.56e+00 69.2
  17. ## 9 9 0.8200 -1.600 0.810 6.160 2.97e+00 64.2
  18. ## 10 10 0.8390 -1.560 0.855 4.780 2.01e+00 60.1
  19. ## 11 12 0.8020 -1.410 0.866 3.160 9.61e-01 53.2
  20. ## 12 14 0.8470 -1.340 0.909 2.280 4.84e-01 47.7
  21. ## 13 16 0.8850 -1.250 0.932 1.750 2.64e-01 43.1
  22. ## 14 18 0.8830 -1.210 0.922 1.400 1.46e-01 39.1
  23. ## 15 20 0.9110 -1.180 0.926 1.150 8.35e-02 35.6
  24. ## 16 22 0.9160 -1.140 0.927 0.968 5.02e-02 32.6
  25. ## 17 24 0.9520 -1.120 0.961 0.828 2.89e-02 29.9
  26. ## 18 26 0.9520 -1.120 0.944 0.716 1.77e-02 27.5
  27. ## 19 28 0.9380 -1.120 0.922 0.626 1.08e-02 25.4
  28. ## 20 30 0.9620 -1.110 0.951 0.551 6.49e-03 23.5
  29.  
  30. par(mfrow = c(1,2))
  31. cex1 = 0.9
  32. # 横轴是Soft threshold (power),纵轴是无标度网络的评估参数,数值越高,
  33. # 网络越符合无标度特征 (non-scale)
  34. plot(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
  35. xlab="Soft Threshold (power)",
  36. ylab="Scale Free Topology Model Fit,signed R^2",type="n",
  37. main = paste("Scale independence"))
  38. text(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
  39. labels=powers,cex=cex1,col="red")
  40. # 筛选标准。R-square=0.85
  41. abline(h=0.85,col="red")
  42.  
  43. # Soft threshold与平均连通性
  44. plot(sft$fitIndices[,1], sft$fitIndices[,5],
  45. xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n",
  46. main = paste("Mean connectivity"))
  47. text(sft$fitIndices[,1], sft$fitIndices[,5], labels=powers,
  48. cex=cex1, col="red")

WGCNA分析使用教程-图片3

  1. power = sft$powerEstimate
  2. power
  3.  
  4. ## [1] 6
经验power (无满足条件的power时选用)
  1. # 无向网络在power小于15或有向网络power小于30内,没有一个power值可以使
  2. # 无标度网络图谱结构R^2达到0.8,平均连接度较高如在100以上,可能是由于
  3. # 部分样品与其他样品差别太大。这可能由批次效应、样品异质性或实验条件对
  4. # 表达影响太大等造成。可以通过绘制样品聚类查看分组信息和有无异常样品。
  5. # 如果这确实是由有意义的生物变化引起的,也可以使用下面的经验power值。
  6. if (is.na(power)){
  7. power = ifelse(nSamples<20, ifelse(type == "unsigned", 9, 18),
  8. ifelse(nSamples<30, ifelse(type == "unsigned", 8, 16),
  9. ifelse(nSamples<40, ifelse(type == "unsigned", 7, 14),
  10. ifelse(type == "unsigned", 6, 12))
  11. )
  12. )
  13. }
网络构建
  1. ##一步法网络构建:One-step network construction and module detection##
  2. # power: 上一步计算的软阈值
  3. # maxBlockSize: 计算机能处理的最大模块的基因数量 (默认5000);
  4. # 4G内存电脑可处理8000-10000个,16G内存电脑可以处理2万个,32G内存电脑可
  5. # 以处理3万个
  6. # 计算资源允许的情况下最好放在一个block里面。
  7. # corType: pearson or bicor
  8. # numericLabels: 返回数字而不是颜色作为模块的名字,后面可以再转换为颜色
  9. # saveTOMs:最耗费时间的计算,存储起来,供后续使用
  10. # mergeCutHeight: 合并模块的阈值,越大模块越少
  11. net = blockwiseModules(dataExpr, power = power, maxBlockSize = nGenes,
  12. TOMType = type, minModuleSize = 30,
  13. reassignThreshold = 0, mergeCutHeight = 0.25,
  14. numericLabels = TRUE, pamRespectsDendro = FALSE,
  15. saveTOMs=TRUE, corType = corType,
  16. maxPOutliers=maxPOutliers, loadTOMs=TRUE,
  17. saveTOMFileBase = paste0(exprMat, ".tom"),
  18. verbose = 3)
  19.  
  20. ## Calculating module eigengenes block-wise from all genes
  21. ## Flagging genes and samples with too many missing values...
  22. ## ..step 1
  23. ## ..Working on block 1 .
  24. ## TOM calculation: adjacency..
  25. ## ..will use 47 parallel threads.
  26. ## Fraction of slow calculations: 0.000000
  27. ## ..connectivity..
  28. ## ..matrix multiplication (system BLAS)..
  29. ## ..normalization..
  30. ## ..done.
  31. ## ..saving TOM for block 1 into file WGCNA/LiverFemaleClean.txt.tom-block.1.RData
  32. ## ....clustering..
  33. ## ....detecting modules..
  34. ## ....calculating module eigengenes..
  35. ## ....checking kME in modules..
  36. ## ..removing 3 genes from module 1 because their KME is too low.
  37. ## ..removing 5 genes from module 12 because their KME is too low.
  38. ## ..removing 1 genes from module 14 because their KME is too low.
  39. ## ..merging modules that are too close..
  40. ## mergeCloseModules: Merging modules whose distance is less than 0.25
  41. ## Calculating new MEs...
  42.  
  43. # 根据模块中基因数目的多少,降序排列,依次编号为 `1-最大模块数`。
  44. # **0 (grey)**表示**未**分入任何模块的基因。
  45. table(net$colors)
  46.  
  47. ##
  48. ## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  49. ## 135 472 356 333 307 303 177 158 102 94 69 66 63 62
层级聚类树展示各个模块
  1. ## 灰色的为**未分类**到模块的基因。
  2. # Convert labels to colors for plotting
  3. moduleLabels = net$colors
  4. moduleColors = labels2colors(moduleLabels)
  5. # Plot the dendrogram and the module colors underneath
  6. # 如果对结果不满意,还可以recutBlockwiseTrees,节省计算时间
  7. plotDendroAndColors(net$dendrograms[[1]], moduleColors[net$blockGenes[[1]]],
  8. "Module colors",
  9. dendroLabels = FALSE, hang = 0.03,
  10. addGuide = TRUE, guideHang = 0.05)

WGCNA分析使用教程-图片4

绘制模块之间相关性图
  1. # module eigengene, 可以绘制线图,作为每个模块的基因表达趋势的展示
  2. MEs = net$MEs
  3.  
  4. ### 不需要重新计算,改下列名字就好
  5. ### 官方教程是重新计算的,起始可以不用这么麻烦
  6. MEs_col = MEs
  7. colnames(MEs_col) = paste0("ME", labels2colors(
  8. as.numeric(str_replace_all(colnames(MEs),"ME",""))))
  9. MEs_col = orderMEs(MEs_col)
  10.  
  11. # 根据基因间表达量进行聚类所得到的各模块间的相关性图
  12. # marDendro/marHeatmap 设置下、左、上、右的边距
  13. plotEigengeneNetworks(MEs_col, "Eigengene adjacency heatmap",
  14. marDendro = c(3,3,2,4),
  15. marHeatmap = c(3,4,2,2), plotDendrograms = T,
  16. xLabelsAngle = 90)

WGCNA分析使用教程-图片5

  1. ## 如果有表型数据,也可以跟ME数据放一起,一起出图
  2. #MEs_colpheno = orderMEs(cbind(MEs_col, traitData))
  3. #plotEigengeneNetworks(MEs_colpheno, "Eigengene adjacency heatmap",
  4. # marDendro = c(3,3,2,4),
  5. # marHeatmap = c(3,4,2,2), plotDendrograms = T,
  6. # xLabelsAngle = 90)

可视化基因网络 (TOM plot)

  1. # 如果采用分步计算,或设置的blocksize>=总基因数,直接load计算好的TOM结果
  2. # 否则需要再计算一遍,比较耗费时间
  3. # TOM = TOMsimilarityFromExpr(dataExpr, power=power, corType=corType, networkType=type)
  4. load(net$TOMFiles[1], verbose=T)
  5.  
  6. ## Loading objects:
  7. ## TOM
  8.  
  9. TOM <- as.matrix(TOM)
  10.  
  11. dissTOM = 1-TOM
  12. # Transform dissTOM with a power to make moderately strong
  13. # connections more visible in the heatmap
  14. plotTOM = dissTOM^7
  15. # Set diagonal to NA for a nicer plot
  16. diag(plotTOM) = NA
  17. # Call the plot function
  18.  
  19. # 这一部分特别耗时,行列同时做层级聚类
  20. TOMplot(plotTOM, net$dendrograms, moduleColors,
  21. main = "Network heatmap plot, all genes")

WGCNA分析使用教程-图片6

导出网络用于Cytoscape

  1. probes = colnames(dataExpr)
  2. dimnames(TOM) <- list(probes, probes)
  3.  
  4. # Export the network into edge and node list files Cytoscape can read
  5. # threshold 默认为0.5, 可以根据自己的需要调整,也可以都导出后在
  6. # cytoscape中再调整
  7. cyt = exportNetworkToCytoscape(TOM,
  8. edgeFile = paste(exprMat, ".edges.txt", sep=""),
  9. nodeFile = paste(exprMat, ".nodes.txt", sep=""),
  10. weighted = TRUE, threshold = 0,
  11. nodeNames = probes, nodeAttr = moduleColors)

WGCNA分析使用教程-图片7

关联表型数据

  1. trait <- "WGCNA/TraitsClean.txt"
  2. # 读入表型数据,不是必须的
  3. if(trait != "") {
  4. traitData <- read.table(file=trait, sep='\t', header=T, row.names=1,
  5. check.names=FALSE, comment='',quote="")
  6. sampleName = rownames(dataExpr)
  7. traitData = traitData[match(sampleName, rownames(traitData)), ]
  8. }
  9.  
  10. ### 模块与表型数据关联
  11. if (corType=="pearsoon") {
  12. modTraitCor = cor(MEs_col, traitData, use = "p")
  13. modTraitP = corPvalueStudent(modTraitCor, nSamples)
  14. } else {
  15. modTraitCorP = bicorAndPvalue(MEs_col, traitData, robustY=robustY)
  16. modTraitCor = modTraitCorP$bicor
  17. modTraitP = modTraitCorP$p
  18. }
  19.  
  20. ## Warning in bicor(x, y, use = use, ...): bicor: zero MAD in variable 'y'.
  21. ## Pearson correlation was used for individual columns with zero (or missing)
  22. ## MAD.
  23.  
  24. # signif表示保留几位小数
  25. textMatrix = paste(signif(modTraitCor, 2), "\n(", signif(modTraitP, 1), ")", sep = "")
  26. dim(textMatrix) = dim(modTraitCor)
  27. labeledHeatmap(Matrix = modTraitCor, xLabels = colnames(traitData),
  28. yLabels = colnames(MEs_col),
  29. cex.lab = 0.5,
  30. ySymbols = colnames(MEs_col), colorLabels = FALSE,
  31. colors = blueWhiteRed(50),
  32. textMatrix = textMatrix, setStdMargins = FALSE,
  33. cex.text = 0.5, zlim = c(-1,1),
  34. main = paste("Module-trait relationships"))

WGCNA分析使用教程-图片8

模块内基因与表型数据关联, 从上图可以看到MEmagentaInsulin_ug_l相关,选取这两部分进行分析。

  1. ## 从上图可以看到MEmagenta与Insulin_ug_l相关
  2.  
  3. ## 模块内基因与表型数据关联
  4.  
  5. # 性状跟模块虽然求出了相关性,可以挑选最相关的那些模块来分析,
  6. # 但是模块本身仍然包含非常多的基因,还需进一步的寻找最重要的基因。
  7. # 所有的模块都可以跟基因算出相关系数,所有的连续型性状也可以跟基因的表达
  8. # 值算出相关系数。
  9. # 如果跟性状显著相关基因也跟某个模块显著相关,那么这些基因可能就非常重要
  10. # 。
  11.  
  12. ### 计算模块与基因的相关性矩阵
  13.  
  14. if (corType=="pearsoon") {
  15. geneModuleMembership = as.data.frame(cor(dataExpr, MEs_col, use = "p"))
  16. MMPvalue = as.data.frame(corPvalueStudent(
  17. as.matrix(geneModuleMembership), nSamples))
  18. } else {
  19. geneModuleMembershipA = bicorAndPvalue(dataExpr, MEs_col, robustY=robustY)
  20. geneModuleMembership = geneModuleMembershipA$bicor
  21. MMPvalue = geneModuleMembershipA$p
  22. }
  23.  
  24.  
  25. # 计算性状与基因的相关性矩阵
  26.  
  27. ## 只有连续型性状才能进行计算,如果是离散变量,在构建样品表时就转为0-1矩阵。
  28.  
  29. if (corType=="pearsoon") {
  30. geneTraitCor = as.data.frame(cor(dataExpr, traitData, use = "p"))
  31. geneTraitP = as.data.frame(corPvalueStudent(
  32. as.matrix(geneTraitCor), nSamples))
  33. } else {
  34. geneTraitCorA = bicorAndPvalue(dataExpr, traitData, robustY=robustY)
  35. geneTraitCor = as.data.frame(geneTraitCorA$bicor)
  36. geneTraitP = as.data.frame(geneTraitCorA$p)
  37. }
  38.  
  39. ## Warning in bicor(x, y, use = use, ...): bicor: zero MAD in variable 'y'.
  40. ## Pearson correlation was used for individual columns with zero (or missing)
  41. ## MAD.
  42.  
  43. # 最后把两个相关性矩阵联合起来,指定感兴趣模块进行分析
  44. module = "magenta"
  45. pheno = "Insulin_ug_l"
  46. modNames = substring(colnames(MEs_col), 3)
  47. # 获取关注的列
  48. module_column = match(module, modNames)
  49. pheno_column = match(pheno,colnames(traitData))
  50. # 获取模块内的基因
  51. moduleGenes = moduleColors == module
  52.  
  53. sizeGrWindow(7, 7)
  54. par(mfrow = c(1,1))
  55. # 与性状高度相关的基因,也是与性状相关的模型的关键基因
  56. verboseScatterplot(abs(geneModuleMembership[moduleGenes, module_column]),
  57. abs(geneTraitCor[moduleGenes, pheno_column]),
  58. xlab = paste("Module Membership in", module, "module"),
  59. ylab = paste("Gene significance for", pheno),
  60. main = paste("Module membership vs. gene significance\n"),
  61. cex.main = 1.2, cex.lab = 1.2, cex.axis = 1.2, col = module)

WGCNA分析使用教程-图片9

分步法展示每一步都做了什么

  1. ### 计算邻接矩阵
  2. adjacency = adjacency(dataExpr, power = power)
  3.  
  4. ### 把邻接矩阵转换为拓扑重叠矩阵,以降低噪音和假相关,获得距离矩阵。
  5. TOM = TOMsimilarity(adjacency)
  6. dissTOM = 1-TOM
  7.  
  8. ### 层级聚类计算基因之间的距离树
  9. geneTree = hclust(as.dist(dissTOM), method = "average")
  10.  
  11. ### 模块合并
  12. # We like large modules, so we set the minimum module size relatively high:
  13. minModuleSize = 30
  14. # Module identification using dynamic tree cut:
  15. dynamicMods = cutreeDynamic(dendro = geneTree, distM = dissTOM,
  16. deepSplit = 2, pamRespectsDendro = FALSE,
  17. minClusterSize = minModuleSize)
  18. # Convert numeric lables into colors
  19. dynamicColors = labels2colors(dynamicMods)
  20.  
  21. ### 通过计算模块的代表性模式和模块之间的定量相似性评估,合并表达图谱相似
  22. 的模块
  23. MEList = moduleEigengenes(datExpr, colors = dynamicColors)
  24. MEs = MEList$eigengenes
  25. # Calculate dissimilarity of module eigengenes
  26. MEDiss = 1-cor(MEs)
  27. # Cluster module eigengenes
  28. METree = hclust(as.dist(MEDiss), method = "average")
  29. MEDissThres = 0.25
  30.  
  31. # Call an automatic merging function
  32. merge = mergeCloseModules(datExpr, dynamicColors, cutHeight = MEDissThres, verbose = 3)
  33. # The merged module colors
  34. mergedColors = merge$colors;
  35. # Eigengenes of the new merged
  36.  
  37. ## 分步法完结

Reference:

  1. 官网https://labs.genetics.ucla.edu/horvath/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/
  2. 术语解释https://labs.genetics.ucla.edu/horvath/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/Simulated-00-Background.pdf
  3. FAQhttps://labs.genetics.ucla.edu/horvath/CoexpressionNetwork/Rpackages/WGCNA/faq.html

评论  4  访客  4
    • 2144069847 0

      获取WGCNA

      • syg_bioinfo 0

        获取WGCNA

        • 科研小白 0

          获取WGCNA

          • haha 0

            获取WGCNA

          发表评论

          匿名网友

          拖动滑块以完成验证
          加载失败