This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WGCNA_4.R
238 lines (183 loc) · 5.72 KB
/
WGCNA_4.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#=====================================================================================
#
# Code chunk 1 加载包、路径和数据
#
#=====================================================================================
rm(list=ls())
library(pheatmap)
library(clusterProfiler)
library(ReactomePA)
#library(DOSE)
library(Cairo)
setwd("F:/10_R/Data_Output/Clean_LI/WGCNA")
load("WGCNA_Episode_2.RData")
#=====================================================================================
#
# Code chunk 2 指定感兴趣模块
#
#=====================================================================================
module.selected <- "green3"
gene.selected <- colnames(datExpr)[(moduleColors==module.selected)]
#=====================================================================================
#
# Code chunk 3 绘制模块基因表达量热图
#
#=====================================================================================
## 将表达矩阵再次转置并取出指定的基因表达信息
dat.selected <- t(datExpr)[gene.selected,]
## 导入colSAMPLE并指定分组颜色
colSAMPLE <- read.csv(
"F:/10_R/Data_Input/colSAMPLE_Clean_LI.csv",
header=T,row.names=1)
annotation_col <- colSAMPLE[,c("cell","subject")]
ann_colors = list(colSAMPLE$cell, colSAMPLE$subject)
## 用pheatmap绘制热图
CairoPDF(paste("Heatmap_module_selected" , module.selected , ".pdf", sep = "") ,
8 , 8)
pheatmap(
dat.selected,
scale = "row" ,
cluster_cols = F,
cluster_rows = T,
clustering_distance_rows = "correlation",
show_rownames=T,
show_colnames=T,
border = F,
color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
treeheight_row = 15,
treeheight_col = 10,
legend = T,
annotation_col = annotation_col,
annotation_legend = TRUE,
annotation_colors = ann_colors,
fontsize = 7,
cellwidth = 15,
cellheight = 6
)
dev.off()
#=====================================================================================
#
# Code chunk 4 GO富集分析
#
#=====================================================================================
## 将ENTREZID加入表达矩阵
ENTREZID <- bitr(
rownames(dat.selected),
fromType="SYMBOL",
toType = "ENTREZID",
OrgDb="org.Hs.eg.db"
)
## 利用基因ID进行GO富集分析,需联网,运行较慢
enrichBP <- enrichGO(
ENTREZID$ENTREZID,
OrgDb = org.Hs.eg.db,
ont='BP',
pAdjustMethod = 'BH',
pvalueCutoff = 0.05,
qvalueCutoff = 0.2,
keyType = 'ENTREZID',
readable = T
)
## 使用simplify必须要指定ontology
a <- simplify(enrichBP, cutoff=0.7, by="p.adjust", select_fun=min)
enrichCC <- enrichGO(
ENTREZID$ENTREZID,
OrgDb = org.Hs.eg.db,
ont='CC',
pAdjustMethod = 'BH',
pvalueCutoff = 0.05,
qvalueCutoff = 0.2,
keyType = 'ENTREZID',
readable = T
)
## 使用simplify必须要指定ontology
b <- simplify(enrichCC, cutoff=0.7, by="p.adjust", select_fun=min)
library(ggplot2)
library(forcats)
library(DOSE)
ego3 <- mutate(enrichGO@result, richFactor = Count / as.numeric(sub("/\\d+", "", BgRatio)))
ego3 <- ego3[c(1,2,4,5,6,9,10,14,16,40,33,32),]
CairoPDF(file = "WGCNA_GO.pdf",8.5,4)
ggplot(ego3, showCategory = 12,
aes(richFactor, fct_reorder(Description, richFactor))) +
geom_segment(aes(xend=0, yend = Description)) +
geom_point(aes(color=p.adjust, size = Count)) +
scale_color_gradientn(colours=c("#f7ca64", "#46bac2", "#7e62a3"),
trans = "log10",
guide=guide_colorbar(reverse=TRUE, order=1)) +
scale_size_continuous(range=c(4, 9)) +
theme_dose(12) +
xlab("Rich Factor") +
ylab(NULL) +
ggtitle("GO Enrichment Analysis")
dev.off()
#=====================================================================================
#
# Code chunk 5 KEGG富集分析(太少了不能用)
#
#=====================================================================================
## 与GO富集分析相似
enrichKEGG <- enrichKEGG(
ENTREZID$ENTREZID,
organism = 'hsa',
keyType = 'kegg',
pvalueCutoff = 0.05,
pAdjustMethod = 'BH',
minGSSize = 5,
maxGSSize = 500,
qvalueCutoff = 0.2,
use_internal_data = FALSE,
# readable = T
)
dotplot(enrichKEGG, showCategory=30)
cnetplot(
enrichKEGG,
showCategory = 12,
categorySize="pvalue",
colorEdge = T,
circular = F
)
heatplot(enrichKEGG)
#=====================================================================================
#
# Code chunk 6 Reactome富集分析
#
#=====================================================================================
enrichPathway <- enrichPathway(
ENTREZID$ENTREZID,
pvalueCutoff = 0.05,
readable = T
)
## 此处可直接查看结果(其实是我还没弄好可视化方法)
enrichPathway@result
#=====================================================================================
#
# Code chunk 7 DO富集分析
#
#=====================================================================================
enrichDO <- enrichDO(
ENTREZID$ENTREZID,
ont = "DO",
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
# universe = names(geneList),
minGSSize = 5,
maxGSSize = 500,
qvalueCutoff = 0.05,
readable = T
)
## 可视化方法也没弄好,凑合看吧
enrichDO@result
#=====================================================================================
#
# Code chunk 8 保存结果
#
#=====================================================================================
save.image(
paste("WGCNA_Result_", module.selected, ".RData")
)
#=====================================================================================
#
# End of WGCNA part IV: The Final Episode
#
#=====================================================================================