GO-KEGG富集分析


你上传的图片似乎是两个不同来源的基因富集分析结果的截图,一个来自服务器,另一个使用R语言生成的。这些分析有助于识别与一组基因显著相关的生物学过程、细胞组分和分子功能。

通过比较这两组结果,我可以观察到以下区别:

  1. 基因本体术语:虽然在识别的生物学过程中有一些重叠(例如,细胞周期过程和染色体分离),但在列出的具体基因本体(GO)术语上可能有差异。服务器基础的分析列出了更详细的GO术语。

  2. 统计值

    • 数量和百分比:服务器基础的分析提供了与每个术语相关的基因数量以及这个数量占分析总基因数的百分比。
    • P值和调整后的P值:两种分析都提供了p值和调整后的p值,但调整方法可能有所不同,这可能会影响术语的显著性水平。服务器分析使用了Bonferroni和Benjamini方法,而R分析报告了p.adjust和qvalue,这可能对应不同的统计校正方法。
    • 富集:服务器基础的分析包括倍数富集,这有助于了解与特定GO术语相关的基因出现的频率比随机预期的频率高多少。
  3. 数据呈现

    • 服务器分析包含了“倍数富集”、“Bonferroni”、“Benjamini”和“FDR”列,这些是不同的统计量度,用来考虑多重比较的影响。
    • R分析似乎侧重于“p.adjust”和“qvalue”列来调整多重比较的p值。
  4. 基因列表

    • 两种分析的每个术语的基因列表可能有差异。这可能是由于不同的基因集数据库、不同版本的基因本体或不同的纳入阈值所致。
    • 服务器分析包括了一个“列表总数”列,而R分析中这个列不明显。
  5. 背景集

    • R分析提供了一个“BgRatio”,这可能代表在参考集中与每个术语相关的基因的背景比率,这对于理解富集的背景很重要。
  6. 格式

    • 服务器分析用颜色编码进行了标记,以便更容易解释最显著的结果。
    • R分析呈现了更标准的表格格式,没有颜色编码。

总之,虽然两种分析基于同一组基因,但它们可能使用不同版本的GO数据库、不同的统计测试或显著性阈值,这可能导致GO术语及其相关统计数据有轻微的差异。

R能用哪两种方法吗?

是的,R可以使用Bonferroni和Benjamini-Hochberg方法来调整多重比较的p值。这些方法是校正假阳性发现率的常见手段,它们在许多R统计和生物信息学包中都有实现。例如,在R中使用p.adjust函数就可以应用这些方法:

1
2
3
4
5
# 使用Bonferroni方法
p.adjust(p.values, method = "bonferroni")

# 使用Benjamini-Hochberg方法
p.adjust(p.values, method = "BH") # 或者 method = "fdr"

其中p.values代表你的原始p值向量。method参数指定了要使用的调整方法。Bonferroni方法非常保守,适用于控制家族错误率(Family-wise Error Rate, FWER),而Benjamini-Hochberg方法则控制假发现率(False Discovery Rate, FDR),通常被认为更加强大。在基因富集分析等领域,Benjamini-Hochberg方法更为常用,因为它在降低假阳性的同时保留了更多的真阳性发现。

服务器的Category Term Count % PValue Genes List Total Pop Hits Pop Total Fold Enrichment Bonferroni Benjamini FDR
R语言的ONTOLOGY ID Description GeneRatio BgRatio pvalue p.adjust qvalue geneID Count

服务器和R语言提供的富集分析结果的列标题具有一些相似性,但也有差异。以下是每个标题所代表的含义:

服务器的结果列:

  • Category: 这可能是指GO的分类,如生物学过程(BP)、细胞组成(CC)或分子功能(MF)。
  • Term: GO术语或KEGG路径的描述。
  • Count: 在该术语下发现的基因数量。
  • %: 基因数量占分析中总基因数的百分比。
  • PValue: 术语富集的原始p值。
  • Genes: 与该术语相关联的基因列表。
  • List Total: 分析中的基因总数。
  • Pop Hits: 在参考基因集中与该术语相关的基因数量。
  • Pop Total: 参考基因集的总基因数量。
  • Fold Enrichment: 富集倍数,指的是观察到的与期望的比例。
  • Bonferroni: 使用Bonferroni方法校正后的p值。
  • Benjamini: 使用Benjamini-Hochberg方法校正后的p值。
  • FDR: 假发现率,是经过校正的p值,用于控制类型I错误。

R语言的结果列:

  • ONTOLOGY: 同上,指GO的分类,BP、CC或MF。
  • ID: GO术语或KEGG路径的ID。
  • Description: 术语或路径的描述。
  • GeneRatio: 在该术语下发现的基因数量与分析基因总数的比例。
  • BgRatio: 背景比率,可能是指该术语在背景基因集中的基因数量比例。
  • pvalue: 术语富集的原始p值。
  • p.adjust: 调整后的p值,可能默认是使用了某种校正方法,如Benjamini-Hochberg方法。
  • qvalue: 类似于FDR的一个校正后的p值,它提供了发现被错误地声明为显著的比例的估计。
  • geneID: 与该术语相关的基因ID列表。
  • Count: 同上,基因的数量。

虽然两者提供的信息类似,但具体的表格格式和某些统计数据的计算可能有所不同。这可能是因为不同的软件和包在处理富集分析时采用了不同的方法和默认设置。在实际应用中,研究者应根据实验设计和研究目标选择合适的方法和参数。

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")

if (!require('org.Rn.eg.db')) BiocManager::install('org.Rn.eg.db', update = FALSE);
if (!require('org.Dm.eg.db')) BiocManager::install('org.Dm.eg.db', update = FALSE);
if (!require('org.At.tair.db')) BiocManager::install('org.At.tair.db', update = FALSE);
if (!require('org.Dr.eg.db')) BiocManager::install('org.Dr.eg.db', update = FALSE);
if (!require('org.Sc.sgd.db')) BiocManager::install('org.Sc.sgd.db', update = FALSE);
if (!require('org.Ce.eg.db')) BiocManager::install('org.Ce.eg.db', update = FALSE);
if (!require('org.Bt.eg.db')) BiocManager::install('org.Bt.eg.db', update = FALSE);
if (!require('org.Gg.eg.db')) BiocManager::install('org.Gg.eg.db', update = FALSE);
if (!require('org.Ss.eg.db')) BiocManager::install('org.Ss.eg.db', update = FALSE);
if (!require('org.Mmu.eg.db')) BiocManager::install('org.Mmu.eg.db', update = FALSE);
if (!require('org.Cf.eg.db')) BiocManager::install('org.Cf.eg.db', update = FALSE);
if (!require('org.EcK12.eg.db')) BiocManager::install('org.EcK12.eg.db', update = FALSE);
if (!require('org.Xl.eg.db')) BiocManager::install('org.Xl.eg.db', update = FALSE);
if (!require('org.Pt.eg.db')) BiocManager::install('org.Pt.eg.db', update = FALSE);
if (!require('org.Ag.eg.db')) BiocManager::install('org.Ag.eg.db', update = FALSE);
if (!require('org.EcSakai.eg.db')) BiocManager::install('org.EcSakai.eg.db', update = FALSE);
if (!require('org.Mxanthus.db')) BiocManager::install('org.Mxanthus.db', update = FALSE);
if (!require('AnnotationDbi')) BiocManager::install('AnnotationDbi', update = FALSE);

if (!require('org.Hs.eg.db')) BiocManager::install('org.Hs.eg.db', update = FALSE);
if (!require('openxlsx')) install.packages('openxlsx');


#install.packages("shiny")
#install.packages("openxlsx")
#install.packages("ggplot2")
#install.packages("stringr")
#install.packages("enrichplot")
#install.packages("clusterProfiler")
#BiocManager::install("topGO")
#install.packages("circlize")
#install.packages("ComplexHeatmap")

#BiocManager::install("enrichplot")
#BiocManager::install("clusterProfiler")
#BiocManager::install("circlize")
#BiocManager::install("ComplexHeatmap")

suppressMessages(library(shiny))
suppressMessages(library(openxlsx))
suppressMessages(library(ggplot2))
suppressMessages(library(stringr))
suppressMessages(library(enrichplot))
suppressMessages(library(clusterProfiler))
suppressMessages(library(topGO))
suppressMessages(library(circlize))
suppressMessages(library(ComplexHeatmap))
suppressMessages(library(dplyr))
suppressMessages(library(ComplexHeatmap))
suppressMessages(library(ComplexHeatmap))


suppressMessages(library(org.Rn.eg.db))
suppressMessages(library(org.Dm.eg.db))
suppressMessages(library(org.At.tair.db))
suppressMessages(library(org.Dr.eg.db))
suppressMessages(library(org.Sc.sgd.db))
suppressMessages(library(org.Ce.eg.db))
suppressMessages(library(org.Bt.eg.db))
suppressMessages(library(org.Gg.eg.db))
suppressMessages(library(org.Ss.eg.db))
suppressMessages(library(org.Mmu.eg.db))
suppressMessages(library(org.Cf.eg.db))
suppressMessages(library(org.EcK12.eg.db))
suppressMessages(library(org.Xl.eg.db))
suppressMessages(library(org.Pt.eg.db))
suppressMessages(library(org.Ag.eg.db))
suppressMessages(library(org.EcSakai.eg.db))
suppressMessages(library(org.Mxanthus.db))

#if (!requireNamespace("BiocManager", quietly = TRUE))
#install.packages("BiocManager")
#BiocManager::install(c("org.At.tair.db", "org.Hs.eg.db", "org.Mm.eg.db", "org.Rn.eg.db", "org.Dm.eg.db",
# "org.Dr.eg.db", "org.Sc.sgd.db", "org.Ce.eg.db", "org.Bt.eg.db", "org.Gg.eg.db",
# "org.Ss.eg.db", "org.Mmu.eg.db", "org.Cf.eg.db", "org.EcK12.eg.db", "org.Xl.eg.db",
# "org.Pt.eg.db", "org.Ag.eg.db", "org.EcSakai.eg.db", "org.Mxanthus.db"))

# 用 switch 结构将 input$go_species 转换成数据库包名



#if (!requireNamespace("BiocManager", quietly = TRUE))
#install.packages("BiocManager")
#BiocManager::install(c("org.At.tair.db"))


library(shiny)
library(openxlsx)
library(ggplot2)
library(stringr)
#library(enrichplot)
library(clusterProfiler)
library(topGO)
library(circlize)
library(ComplexHeatmap)
library(org.Gg.eg.db)
library(org.Hs.eg.db)
library(dplyr)
library("AnnotationDbi")
library(org.Hs.eg.db)
#install.packages("ggridges")
library("ggridges")


ui <- fluidPage(
titlePanel("Based on R code, GO and KEGG GSEA enrichment analysis."),
sidebarLayout(
sidebarPanel(
#fileInput("file", "上传差异表达数据", accept = c('.xlsx','.csv','.txt')),
fileInput("file", "Upload species gene symbols for annotation.(xlsx,csv,txt)", accept = c('.xlsx','.csv','.txt')),
#selectInput("go_species", "选择物种GO(Gg:鸡.HS:人.Ss:猪):", choices = c('Gg','Hs','Ss')),
selectInput("go_species", "Choose a species-GO(Gg:chicken.HS:people.Ss:pig.et al):", choices = c('Gg','Hs','Ss','Mm','Rn','Dm','Dr','Ce','Bt','Mmu','Cf','EcK12','Xl','Pt','Ag','EcSakai','At.tair','Mxanthus')),
actionButton("goToBioconductor", "GO species abbreviation index table"),
downloadButton("download_GoTable", "Download GO result table."),
sliderInput("go_pvalueCutoff", "GO pvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),
sliderInput("go_qvalueCutoff", "GO qvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),
#actionButton("go", "Automated analysis"),

#
sliderInput("plotHeight1", label = "GO chart height:", min = 200, max = 1200, value = 500),
sliderInput("plotWidth1", label = "GO chart width:", min = 200, max = 1200, value = 600),

sliderInput("font1" , label = "GO font size:", min = 0, max = 40, value = 10),







#
#kegg
selectInput("kegg_species", "Choose a species-KEGG(gga:chicken.hsa:people.ssc:pig,et al):", choices = c('gga','hsa','ssc','mmu','rno','dme','ath','dre','sce','cel','bta','cfa','eco','xla','ptr','aga','ecs','mxn')),
actionButton("goToKEGG", "KEGG species abbreviation index table"),

downloadButton("download_KEGGTable", "Download KEGG result table."),
sliderInput("kegg_pvalueCutoff", "KEGG pvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),
sliderInput("kegg_qvalueCutoff", "KEGG qvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),

sliderInput("plotHeight2", label = "KEGG chart height:", min = 200, max = 1200, value = 500),
sliderInput("plotWidth2", label = "KEGG chart width:", min = 200, max = 1200, value = 600),

sliderInput("font2" , label = "KEGG font size:", min = 0, max = 40, value = 15),

actionButton("bilibili", "Click to view the demonstration video of this plugin"),
#
#GSEA
#sliderInput("GSEA_pvalueCutoff", "GSEA pvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),
#


#downloadButton("download_GseaTable", "Download gsea result table.")
),
mainPanel(
tabsetPanel(
tabPanel("GO enrichment analysis",
plotOutput("GO_barplot"),
plotOutput("GO_barplot_beauty"),
plotOutput("GO_dotplot"),
plotOutput("GO_cnetplot"),
plotOutput("GO_heatplot"),
plotOutput("GO_emapplot")

),

#
tabPanel("KEGG enrichment analysis",
plotOutput("KEGG_barplot"),
plotOutput("KEGG_dotplot"),
plotOutput("KEGG_cnetplot"),
plotOutput("KEGG_plot")),
#
tabPanel("GSEA enrichment analysis",
fluidRow(
#column(6, fileInput("gsea_file", label = h4("上传差异表达文件"),
#accept = ".txt")),
column(6, actionButton("gsea_button", label = "开始GSEA分析",
class = "btn-primary")),
#加的
sliderInput("GSEA_pvalueCutoff", "GSEA pvalueCutoff", min = 0, max = 1, value = 0.05, step = 0.01),
sliderInput("plotHeight3", label = "GSEA chart height:", min = 200, max = 1200, value = 500),
sliderInput("plotWidth3", label = "GSEA chart width:", min = 200, max = 1200, value = 600),


sliderInput("font3" , label = "GSEA font size:", min = 0, max = 40, value = 15),
downloadButton("download_GseaTable", "Download gsea result table."),

#
),



tabPanel(
#column(12, plotOutput("dot_plot_gsea", height = "400px")),
column(12, plotOutput("dot_plot_gsea", height = "400px")),
column(12, plotOutput("ridge_plot_gsea", height = "400px")),
column(12, plotOutput("gsea_plot", height = "400px")),
column(12, plotOutput("gsea_plot1", height = "400px")),
column(12, plotOutput("dot_plot_gsea1", height = "400px")),

)


)
)
)
)
)
server <- function(input, output) {


# GSEA analysis
geneList_gsea <- reactive({
if (!is.null(data())) {
gene <- data()$SYMBOL
gene <- bitr(gene, fromType = "SYMBOL", toType = "ENTREZID", OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'), drop = TRUE)
gene <- dplyr::distinct(gene, SYMBOL, .keep_all = TRUE)
data_all <- data() %>% inner_join(gene, by = "SYMBOL")
data_all_sort <- data_all %>% arrange(desc(logFC))
geneList <- data_all_sort$logFC
names(geneList) <- data_all_sort$ENTREZID
geneList
}
})

gsea_analysis <- function() {
observeEvent(input$gsea_button, {
if (!is.null(geneList_gsea())) {
#gsea <- gseKEGG(geneList_gsea(), organism = "hsa", pvalueCutoff = 1)
gsea <- gseKEGG(geneList_gsea(), organism = input$kegg_species, pvalueCutoff = input$GSEA_pvalueCutoff)

gsea <- setReadable(gsea, OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'), keyType = "ENTREZID")
output$dot_plot_gsea <- renderPlot({
dotplot(gsea)
},width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))
#output$ridge_plot_gsea <- renderPlot({
# ridgeplot(gsea,label_format = 100)
#})
output$gsea_plot1 <- renderPlot({
gseaplot2(gsea, 1, pvalue_table = TRUE)
},width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))


output$ridge_plot_gsea <- renderPlot({
if (!is.null(gsea)) {
ridgeplot(gsea, label_format = 100) +
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font3 - 2),
axis.title = element_text(size = input$font3 + 2),
legend.text = element_text(size = input$font3)
)
}
},width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))

#else {
# plot(NULL, xlim = c(1, 10), ylim = c(1, 10), type="n", xlab="", ylab="")
#}
#})

#output$gsea_plot <- renderPlot({
#if (!is.null(gsea)) {
#gseaplot(gsea) %+%
#ggtitle("Gene set enrichment analysis plot") +
#theme(
# text = element_text(size = input$font1, family = "Arial", face = "bold"),
# axis.text.x = element_text(size = input$font3 - 2),
# axis.text.y = element_text(size = input$font3 - 2),
#axis.title = element_text(size = input$font3 + 2),
#legend.text = element_text(size = input$font3)
# )
#}
#},width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))





#else {
# plot(NULL, xlim = c(1, 10), ylim = c(1, 10), type="n", xlab="", ylab="")
#}
#})







#
#output$dot_plot_gsea1 <- renderPlot({
#dotplot(gsea)
#})

output$dot_plot_gsea1 <- renderPlot({
if (!is.null(gsea)) {
dotplot(gsea, label_format = 100) +
theme(
text = element_text(size = input$font3, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font3 - 2),
axis.title = element_text(size = input$font3 + 2),
legend.text = element_text(size = input$font3)
)
}
},width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))


# else {
# plot(NULL, xlim = c(1, 10), ylim = c(1, 10), type="n", xlab="", ylab="")
#}
#})


#

download_GseaTable()
}
})



}

gsea_analysis()


gsea_result <- reactive({
if (!is.null(geneList_gsea())) {
#gse <- gseKEGG(geneList_gsea(), organism = "hsa", pvalueCutoff = 1)
gse <- gseKEGG(geneList_gsea(), organism = input$kegg_species, pvalueCutoff = 1)
gse <- setReadable(gse, OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'), keyType = "ENTREZID")
gse
}
})




#kegg
KEGG_result <- reactive({
if (is.null(gene())) {
return(NULL)
}
enrichKEGG(gene()$ENTREZID,
organism = input$kegg_species,
pvalueCutoff = input$kegg_pvalueCutoff,
qvalueCutoff = input$kegg_qvalueCutoff)
})









output$download_KEGGTable <- downloadHandler(
filename = function() {
paste("KEGG_result_", input$kegg_species, ".xlsx", sep = "")
},
content = function(file) {
write.xlsx(KEGG_result(), file, sheetName = "KEGG_result")
}
)


output$KEGG_barplot <- renderPlot({
if (!is.null(KEGG_result())) {
barplot(KEGG_result(), showCategory = 40, title = 'KEGG Pathway') +
ggtitle("KEGG enrichment analysis bar chart") +
theme(
text = element_text(size = input$font2, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font2 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth2), height = reactive(input$plotHeight2))

output$KEGG_dotplot <- renderPlot({
if (!is.null(KEGG_result())) {
dotplot(KEGG_result()) +
ggtitle("KEGG enrichment analysis dot plot") +
theme(
text = element_text(size = input$font2, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font2 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth2), height = reactive(input$plotHeight2))

output$KEGG_cnetplot <- renderPlot({
if (!is.null(KEGG_result())) {
enrichplot::cnetplot(KEGG_result(), circular=FALSE, colorEdge = TRUE)+
ggtitle("KEGG enrichment analysis Cnetplot

") +
theme(
text = element_text(size = input$font2, family = "Arial", face = "bold"),
axis.text = element_text(size = input$font2 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth2), height = reactive(input$plotHeight2))

output$KEGG_plot <- renderPlot({
if (!is.null(KEGG_result())) {
enrichplot::heatplot(KEGG_result(), showCategory = 20)+
ggtitle("KEGG enrichment analysis heatmap") +
theme(
text = element_text(size = input$font2, family = "Arial", face = "bold"),
axis.text = element_text(size = input$font2 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth2), height = reactive(input$plotHeight2))



#



# GO analysis
#gene <- reactive({
#if (is.null(data())) {
# return(NULL)
#}
#bitr(data()$SYMBOL, fromType = 'SYMBOL', toType = 'ENTREZID',
# OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'))
#})

#GO_result <- reactive({
#if (is.null(gene())) {
# return(NULL)
# }
#enrichGO(gene()$ENTREZID,
# OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'),
#keyType = "ENTREZID",
# ont = "ALL",
# pvalueCutoff = input$go_pvalueCutoff,
# qvalueCutoff = input$go_qvalueCutoff,
#readable = T)
#})

gene <- reactive({
if (is.null(data())) {
return(NULL)
}


if(input$go_species == "At.tair" ) {

bitr(data()$SYMBOL, fromType = 'SYMBOL', toType = 'ENTREZID',
OrgDb = org.At.tair.db,
)
} else if (input$go_species == "Mxanthus") {
bitr(data()$SYMBOL, fromType = 'SYMBOL', toType = 'ENTREZID',
OrgDb = org.Mxanthus.db,
)

} else {
bitr(data()$SYMBOL, fromType = 'SYMBOL', toType = 'ENTREZID',
OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'))

}

})

GO_result <- reactive({
if (is.null(gene())) {
return(NULL)
}




if(input$go_species == "At.tair") {
enrichGO(gene()$ENTREZID,
OrgDb = org.At.tair.db,
keyType = "ENTREZID",
ont = "ALL",
pvalueCutoff = input$go_pvalueCutoff,
qvalueCutoff = input$go_qvalueCutoff,
readable = T)
} else if(input$go_species == "Mxanthus") {
enrichGO(gene()$ENTREZID,
OrgDb = org.Mxanthus.db,
keyType = "ENTREZID",
ont = "ALL",
pvalueCutoff = input$go_pvalueCutoff,
qvalueCutoff = input$go_qvalueCutoff,
readable = T)
}

else {
enrichGO(gene()$ENTREZID,
OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'),
keyType = "ENTREZID",
ont = "ALL",
pvalueCutoff = input$go_pvalueCutoff,
qvalueCutoff = input$go_qvalueCutoff,
readable = T)
}


#enrichGO(gene()$ENTREZID,
#OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'),
# keyType = "ENTREZID",
# ont = "ALL",
#pvalueCutoff = input$go_pvalueCutoff,
#qvalueCutoff = input$go_qvalueCutoff,
# readable = T)





})





#output$GO_barplot <- renderPlot({
#if (!is.null(GO_result())) {
#barplot(GO_result(), split="ONTOLOGY") + facet_grid(ONTOLOGY~., scale = "free") +
# ggtitle("GO富集分析柱状图")
#}
#})

output$GO_barplot <- renderPlot({
if (!is.null(GO_result())) {
barplot(GO_result(), split="ONTOLOGY") + facet_grid(ONTOLOGY~., scale = "free") +
ggtitle("GO enrichment analysis bar chart") +
#theme(text = element_text(size = input$font1 , family = "Arial", face = "bold"))
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))








#
output$GO_barplot_beauty <- renderPlot({
if (!is.null(GO_result())) {
GO_result_df <- as.data.frame(GO_result())

# 提取前10个显著条目并添加必要的标识符和属性
BP_top10 <- GO_result_df %>%
filter(ONTOLOGY == "BP") %>%
arrange(p.adjust) %>%
head(10)
CC_top10 <- GO_result_df %>%
filter(ONTOLOGY == "CC") %>%
arrange(p.adjust) %>%
head(10)
MF_top10 <- GO_result_df %>%
filter(ONTOLOGY == "MF") %>%
arrange(p.adjust) %>%
head(10)
merge_data <- rbind(BP_top10, CC_top10, MF_top10)
merge_data$ONTOLOGY <- factor(merge_data$ONTOLOGY, levels = c("MF", "CC", "BP"))
merge_data$logPvalue <- (-log10(merge_data$p.adjust))

ggplot(merge_data, aes(x = Description, y = logPvalue, fill = ONTOLOGY)) +
geom_bar(stat = "identity", width = 0.8, color = "black") +
scale_x_discrete(limits = unique(merge_data$Description[order(merge_data$ONTOLOGY)])) +
coord_flip() +
scale_y_continuous(expand = c(0, 0), limits = c(0, max(merge_data$logPvalue) + 1)) +
theme(panel.background = element_rect(fill = "white"),
axis.text = element_text(size = input$font1 + 2, family = "Arial"),
axis.title = element_text(size = input$font1 + 4, family = "Arial", colour = "black"),
legend.text = element_text(size = input$font1 + 2, family = "Arial"),
legend.title = element_text(size = input$font1 + 4, family = "Arial"),
legend.position = "top") +
labs(x = "GO term", y = "-log10 P-value") +
scale_fill_manual(values = c("#1F77B4", "#FF7F0E", "#2CA02C"), name = "ONTOLOGY")
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))



#
output$GO_dotplot <- renderPlot({
if (!is.null(GO_result())) {
dotplot(GO_result(), split="ONTOLOGY") + facet_grid(ONTOLOGY~., scale = "free") +
ggtitle("GO enrichment analysis dot plot") +
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font1 + 2),
legend.text = element_text(size = input$font1)
)
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))

output$GO_cnetplot <- renderPlot({
if (!is.null(GO_result())) {
enrichplot::cnetplot(GO_result(), circular=FALSE, colorEdge = TRUE) +
ggtitle("Gene-pathway association network diagram") +
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font1 + 2),
legend.text = element_text(size = input$font1)
)
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))

output$GO_heatplot <- renderPlot({
if (!is.null(GO_result())) {
enrichplot::heatplot(GO_result(), showCategory = 50) +
ggtitle("Gene-pathway association heatmap") +
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font1 + 2),
legend.text = element_text(size = input$font1)
)
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))

output$GO_emapplot <- renderPlot({
if (!is.null(GO_result())) {
GO2 <- pairwise_termsim(GO_result())
enrichplot::emapplot(GO2, showCategory = 50, color = "p.adjust", layout = "kk") +
ggtitle("GO enrichment analysis emapplot") +
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font1 + 2),
legend.text = element_text(size = input$font1)
)
}
}, width = reactive(input$plotWidth1 + 700), height = reactive(input$plotHeight1 + 600))


#


#download_GO_table <- function() {
#output$download_GoTable <- downloadHandler(
#filename = function() {
#paste("GO_result_", input$go_species, ".xlsx", sep = "")
#},
#content = function(file) {
#write.xlsx(GO_result(), file, sheetName = "GO_result")
#}
#)
#}
#
output$download_GoTable <- downloadHandler(
filename = function() {
paste("GO_result_", input$go_species, ".xlsx", sep = "")
},
content = function(file) {
write.xlsx(GO_result(), file, sheetName = "GO_result")
}
)
#



go_analysis <- function() {
observeEvent(input$go, {
if (!is.null(GO_result())) {
download_GO_table()
}
})
}

go_analysis()

# read data
data <- reactive({
if (is.null(input$file)) {
return(NULL)
}
file_type <- tools::file_ext(input$file$datapath)
if (file_type == "xlsx") {
info <- openxlsx::read.xlsx(input$file$datapath,rowNames = F,colNames = T)
}else if (file_type == "csv") {
info <- read.csv(input$file$datapath, header = T, row.names = NULL)
}else if (file_type == "txt") {
info <- read.table(input$file$datapath, header = T, row.names = NULL)
}
names(info) <- c('SYMBOL', 'logFC', 'pvalue', 'padj')[1:ncol(info)]
info
})

# Download GSEA result table

# Download GSEA result table
download_GseaTable <- function() {
output$download_GseaTable <- downloadHandler(
filename = function() {
paste("GSEA_result_", input$go_species, ".xlsx", sep = "")
},
content = function(file) {
write.xlsx(gsea_result(), file, sheetName = "GSEA_result")
}
)
}

#output$gsea_plot <- renderPlot({
#if (!is.null(gsea_result())) {
#gseaplot2(gsea_result(), 1, pvalue_table = TRUE)
#}
#}, width = reactive(input$plotWidth3), height = reactive(input$plotHeight3))

observe({
if (is.null(GO_result()) || is.null(KEGG_result())) {
showModal(
modalDialog(
title = "Notice!",
"1,Please set the corresponding species before entering the file!

2,The default pvalueCutoff and qvalueCutoff are 0.05. If an error occurs, please increase them appropriately to ensure visualization of the analysis results",
easyClose = TRUE,
footer = NULL
)
)
}
})

observeEvent(input$goToBioconductor, {
browseURL("http://bioconductor.org/packages/release/BiocViews.html#___OrgDb")
})
observeEvent(input$goToKEGG, {
browseURL("http://www.genome.jp/kegg/catalog/org_list.html")
})
observeEvent(input$bilibili, {
browseURL("https://www.bilibili.com/video/BV1Xk4y1p78B/?vd_source=c50dbc766a1b1e8259ff0a8e3c23dd52")
})
}

shinyApp(ui = ui, server = server)
```




## 原始
~~~ bash
library(openxlsx)#读取.xlsx文件
library(ggplot2)#柱状图和点状图
library(stringr)#基因ID转换
library(enrichplot)#GO,KEGG,GSEA
library(clusterProfiler)#GO,KEGG,GSEA
#install.packages("GOplot")

# 安装Bioconductor
#if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#BiocManager::install()

# 安装'topGO'包
#BiocManager::install("topGO")

# 加载'topGO'包
library(topGO)
library(org.Hs.eg.db)

library(org.Hs.eg.db)

library(org.At.tair.db)

# 安装Rgraphviz
#install.packages("Rgraphviz")

# 加载 Rgraphviz 包

#BiocManager::install("Rgraphviz")
library(Rgraphviz)




library(GOplot)
library(GOplot)#弦图,弦表图,系统聚类图
library(DOSE)
library(ggnewscale)
#install.packages("topGO")
library(topGO)#绘制通路网络图
library(circlize)#绘制富集分析圈图
library(ComplexHeatmap)#绘制图例



#载入差异表达数据,只需基因ID(GO,KEGG,GSEA需要)和Log2FoldChange(GSEA需要)即可
info <- read.xlsx( "At.tair.txt.xlsx", rowNames = F,colNames = T)

#指定富集分析的物种库
GO_database <- 'org.At.tair.db'
KEGG_database <- 'ath'
#GO_database <- 'org.Hs.eg.db' #GO分析指定物种,物种缩写索引表详见http://bioconductor.org/packages/release/BiocViews.html#___OrgDb
#KEGG_database <- 'hsa' #KEGG分析指定物种,物种缩写索引表详见http://www.genome.jp/kegg/catalog/org_list.html

#gene ID转换
gene <- bitr(info$gene_symbol,fromType = 'SYMBOL',toType = 'ENTREZID',OrgDb = org.At.tair.db)






GO<-enrichGO( gene$ENTREZID,#GO富集分析
OrgDb = GO_database,
keyType = "ENTREZID",#设定读取的gene ID类型
ont = "ALL",#(ont为ALL因此包括 Biological Process,Cellular Component,Mollecular Function三部分)
pvalueCutoff = 0.05,#设定p值阈值
qvalueCutoff = 0.05,#设定q值阈值
readable = T
)


KEGG<-enrichKEGG(gene$ENTREZID,#KEGG富集分析
organism = KEGG_database,
pvalueCutoff = 0.05,
qvalueCutoff = 0.05)



#names(info) <- c('SYMBOL','Log2FoldChange','pvalue','padj')
names(info) <- c('SYMBOL')
info_merge <- merge(info,gene,by='SYMBOL')#合并转换后的基因ID和Log2FoldChange
GSEA_input <- info_merge$Log2FoldChange
names(GSEA_input) = info_merge$ENTREZID
GSEA_input = sort(GSEA_input, decreasing = TRUE)
GSEA_KEGG <- gseKEGG(GSEA_input, organism = KEGG_database, pvalueCutoff = 0.05)#GSEA富集分析



barplot(GO, split="ONTOLOGY")+facet_grid(ONTOLOGY~., scale="free")#柱状图
barplot(KEGG,showCategory = 40,title = 'KEGG Pathway')
dotplot(GO, split="ONTOLOGY")+facet_grid(ONTOLOGY~., scale="free")#点状图
dotplot(KEGG)




enrichplot::cnetplot(GO,circular=FALSE,colorEdge = TRUE)#基因-通路关联网络图
enrichplot::cnetplot(KEGG,circular=FALSE,colorEdge = TRUE)#circluar为指定是否环化,基因过多时建议设置为FALSE


enrichplot::heatplot(GO,showCategory = 50)#基因-通路关联热图
enrichplot::heatplot(KEGG,showCategory = 50)

GO2 <- pairwise_termsim(GO)
KEGG2 <- pairwise_termsim(KEGG)
enrichplot::emapplot(GO2,showCategory = 50, color = "p.adjust", layout = "kk")#通路间关联网络图
enrichplot::emapplot(KEGG2,showCategory =50, color = "p.adjust", layout = "kk")

write.table(KEGG$ID, file = "KEGG_IDs.txt", #将所有KEGG富集到的通路写入本地文件查看
append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
col.names = TRUE, qmethod = c("escape", "double"),
fileEncoding = "")
browseKEGG(KEGG,"hsa05166")#选择其中的hsa05166通路进行展示






GO_BP<-enrichGO( gene$ENTREZID,#GO富集分析BP模块
OrgDb = GO_database,
keyType = "ENTREZID",
ont = "BP",
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
qvalueCutoff = 0.05,
minGSSize = 10,
maxGSSize = 500,
readable = T)
plotGOgraph(GO_BP)#GO-BP功能网络图
GO_CC<-enrichGO( gene$ENTREZID,#GO富集分析CC模块
OrgDb = GO_database,
keyType = "ENTREZID",
ont = "CC",
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
qvalueCutoff = 0.05,
minGSSize = 10,
maxGSSize = 500,
readable = T)
plotGOgraph(GO_CC)#GO-CC功能网络图
GO_MF<-enrichGO( gene$ENTREZID,#GO富集分析MF模块
OrgDb = GO_database,
keyType = "ENTREZID",
ont = "MF",
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
qvalueCutoff = 0.05,
minGSSize = 10,
maxGSSize = 500,
readable = T)



genedata<-data.frame(ID=info$SYMBOL,logFC=info$Log2FoldChange)
#genedata<-data.frame(ID=info$gene_symbol,logFC=info$log2FoldChange)
write.table(GO$ONTOLOGY, file = "GO_ONTOLOGYs.txt", #将所有GO富集到的基因集所对应的类型写入本地文件从而得到BP/CC/MF各自的起始位置如我的数据里是1,2103,2410
append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
col.names = TRUE, qmethod = c("escape", "double"),
fileEncoding = "")

#genedata <- genedata[, 2:ncol(genedata)]
#genedata <- as.data.frame(genedata)

#genedata <- genedata[-1, ] # 去掉第一行
#colnames(gene)[1] <- "ID" # 修改第一列的名字为SYMBOL




GOplotIn_BP<-GO[1:10,c(2,3,7,9)] #提取GO富集BP的前10行,提取ID,Description,p.adjust,GeneID四列
GOplotIn_CC<-GO[11:20,c(2,3,7,9)]
GOplotIn_MF<-GO[21:30,c(2,3,7,9)]
#GOplotIn_CC<-GO[2103:2112,c(2,3,7,9)]#提取GO富集CC的前10行,提取ID,Description,p.adjust,GeneID四列
#GOplotIn_MF<-GO[2410:2419,c(2,3,7,9)]#提取GO富集MF的前10行,提取ID,Description,p.adjust,GeneID四列
GOplotIn_BP$geneID <-str_replace_all(GOplotIn_BP$geneID,'/',',') #把GeneID列中的’/’替换成‘,’
GOplotIn_CC$geneID <-str_replace_all(GOplotIn_CC$geneID,'/',',')
GOplotIn_MF$geneID <-str_replace_all(GOplotIn_MF$geneID,'/',',')
names(GOplotIn_BP)<-c('ID','Term','adj_pval','Genes')#修改列名,后面弦图绘制的时候需要这样的格式
names(GOplotIn_CC)<-c('ID','Term','adj_pval','Genes')
names(GOplotIn_MF)<-c('ID','Term','adj_pval','Genes')
GOplotIn_BP$Category = "BP"#分类信息
GOplotIn_CC$Category = "CC"
GOplotIn_MF$Category = "MF"
circ_BP<-GOplot::circle_dat(GOplotIn_BP,genedata) #GOplot导入数据格式整理
circ_CC<-GOplot::circle_dat(GOplotIn_CC,genedata)
circ_MF<-GOplot::circle_dat(GOplotIn_MF,genedata)
chord_BP<-chord_dat(data = circ_BP,genes = genedata) #生成含有选定基因的数据框
chord_CC<-chord_dat(data = circ_CC,genes = genedata)
chord_MF<-chord_dat(data = circ_MF,genes = genedata)
GOChord(data = chord_BP,#弦图
title = 'GO-Biological Process',space = 0.01,#GO Term间距
limit = c(1,1),gene.order = 'logFC',gene.space = 0.25,gene.size = 5,
lfc.col = c('red','white','blue'), #上下调基因颜色
process.label = 10) #GO Term字体大小
GOChord(data = chord_CC,title = 'GO-Cellular Component',space = 0.01,
limit = c(1,1),gene.order = 'logFC',gene.space = 0.25,gene.size = 5,
lfc.col = c('red','white','blue'),
process.label = 10)
GOChord(data = chord_MF,title = 'GO-Mollecular Function',space = 0.01,
limit = c(1,1),gene.order = 'logFC',gene.space = 0.25,gene.size = 5,
lfc.col = c('red','white','blue'),
process.label = 10)


GOCircle(circ_BP) #弦表图
GOCircle(circ_CC)
GOCircle(circ_MF)


chord<-chord_dat(data = circ_BP,genes = genedata) #生成含有选定基因的数据框
GOCluster(circ_BP,GOplotIn_BP$Term) #系统聚类图
chord<-chord_dat(data = circ_CC,genes = genedata)
GOCluster(circ_CC,GOplotIn_CC$Term)
chord<-chord_dat(data = circ_MF,genes = genedata)
GOCluster(circ_MF,GOplotIn_MF$Term)



ridgeplot(GSEA_KEGG)
gseaplot2(GSEA_KEGG,1)
gseaplot2(GSEA_KEGG,1:30)#30是根据ridgeplot中有30个富集通路得到的



write.csv(KEGG, row.names=FALSE) # 导出 CSV 文件
# 或
library(openxlsx) # 导入 openxlsx 包
write.xlsx(KEGG, row.names=FALSE) # 导出 Excel 文件


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

我的shiny代码部分是这样子的


ui
plotOutput("GO_barplot"),


enrichGO(gene()$ENTREZID,
OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'),
keyType = "ENTREZID",
ont = "ALL",
pvalueCutoff = input$go_pvalueCutoff,
qvalueCutoff = input$go_qvalueCutoff,
readable = T)


server:
output$GO_barplot <- renderPlot({
if (!is.null(GO_result())) {
barplot(GO_result(), split="ONTOLOGY") + facet_grid(ONTOLOGY~., scale = "free") +
ggtitle("GO enrichment analysis bar chart") +
#theme(text = element_text(size = input$font1 , family = "Arial", face = "bold"))
theme(
text = element_text(size = input$font1, family = "Arial", face = "bold"),
axis.text.y = element_text(size = input$font1 - 2),
axis.title = element_text(size = input$font2 + 2),
legend.text = element_text(size = input$font2)
)
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))



等等,我想把下面的代码,转换成plotOutput("GO_barplot1"),也用shiny的形式添加进去,能帮我改改吗?

###GO富集分析###
GO_enrich = enrichGO(gene = entrezid_all[,1], #表示前景基因,即待富集的基因列表;[,1]表示对entrezid_all数据集的第1列进行处理
OrgDb = org.Hs.eg.db,
keyType = "ENTREZID", #输入数据的类型
ont = "ALL", #可以输入CC/MF/BP/ALL
#universe = 背景数据集 # 表示背景基因,无参的物种选择组装出来的全部unigenes作为背景基因;有参背景基因则不需要。
pvalueCutoff = 1,qvalueCutoff = 1, #表示筛选的阈值,阈值设置太严格可导致筛选不到基因。可指定 1 以输出全部
readable = T) #是否将基因ID映射到基因名称。
GO_enrich = data.frame(GO_enrich) #将GO_enrich导成数据框格式


###GO/KEGG富集结果可视化###
#数据载入与处理#

#纵向柱状图#
ggplot(go_enrich,
aes(x=term,y=Count, fill=ONTOLOGY)) + #x、y轴定义;根据ONTOLOGY填充颜色
geom_bar(stat="identity", width=0.8) + #柱状图宽度
scale_fill_manual(values = c("#6666FF", "#33CC33", "#FF6666") ) + #柱状图填充颜色
facet_grid(ONTOLOGY~., scale = 'free_y', space = 'free_y')+
coord_flip() + #让柱状图变为纵向
xlab("GO term") + #x轴标签
ylab("Gene_Number") + #y轴标签
labs(title = "GO Terms Enrich")+ #设置标题
theme_bw()





## 这个修改会出现一堆黑色的图
ui:
plotOutput("GO_barplot1"),

output$GO_barplot1 <- renderPlot({
if (!is.null(input$go_species) ) {
GO_enrich <- enrichGO(
gene = gene()$ENTREZID,
OrgDb = paste0('org.', str_extract(input$go_species, '\\w+'), '.eg.db'),
keyType = "ENTREZID",
ont = "ALL",
pvalueCutoff = input$go_pvalueCutoff,
qvalueCutoff = input$go_qvalueCutoff,
readable = TRUE
)
GO_enrich_df <- data.frame(GO_enrich) # 将GO_enrich转换为数据框

# 绘制GO富集分析的柱状图
ggplot(GO_enrich_df, aes(x=Description, y=Count, fill=ONTOLOGY)) +
geom_bar(stat="identity", width=0.8) +
scale_fill_manual(values = c("#6666FF", "#33CC33", "#FF6666")) +
facet_grid(ONTOLOGY ~ ., scale = 'free_y', space = 'free_y') +
coord_flip() +
xlab("GO term") +
ylab("Gene Number") +
labs(title = "GO Terms Enrichment") +
theme_bw()
}
}, width = reactive(input$plotWidth1), height = reactive(input$plotHeight1))


要在图表的右侧添加p-value的信息,它显示的adjust的信息

GO<-enrichGO( gene$ENTREZID,#GO富集分析
OrgDb = GO_database,
keyType = “ENTREZID”,#设定读取的gene ID类型
ont = “ALL”,#(ont为ALL因此包括 Biological Process,Cellular Component,Mollecular Function三部分)
pvalueCutoff = 0.05,#设定p值阈值
qvalueCutoff = 0.05,#设定q值阈值
readable = T
)

除了这些参数还有什么参数吗?

是的,enrichGO函数提供了多个参数来细化GO富集分析,除了你提到的这些参数外,还包括但不限于以下一些:

  • universe:用于指定背景基因集,即可能出现的所有基因。如果不指定,分析会默认使用OrgDb中的所有基因作为背景集。

  • minGSSizemaxGSSize:分别用于设置富集分析中考虑的基因集的最小和最大尺寸。这可以帮助过滤掉太小或太大的基因集,这些基因集可能因为尺寸原因而不具代表性或统计意义。

  • qvalueCutoff:用于设置q值(校正后的p值)的截断值,用于控制假发现率(FDR)。

  • pAdjustMethod:用于调整p值的方法。包括”holm”、”hochberg”、”hommel”、”bonferroni”、”BH”(也称为”fdr”)、”BY”等,这些方法可以控制多重检验的错误率。

  • evidence:允许用户根据证据代码过滤GO条目,例如通过特定实验验证的条目。

  • ont:指定进行富集分析的本体类别,包括”BP”(生物过程)、”CC”(细胞组分)和”MF”(分子功能)以及”ALL”。

  • readable:当设置为TRUE时,会尝试将基因ID转换为更易读的基因名称。

  • pool:如果设置为TRUE,则会汇总不同本体的相同条目,这可能有助于简化输出结果。

这些参数允许用户根据具体的研究需求调整分析的细节。选择合适的参数设置可以帮助用户获得更准确和具有解释性的结果。为了深入了解这些参数及其用法,建议参阅DOSE包的官方文档或使用?enrichGO命令查看更详细的帮助信息。

一般情况下,用什么,给我代码

library(clusterProfiler)
library(org.Hs.eg.db) # 以人类基因为例,如果是其他物种请替换相应的OrgDb包

假设你已经有了一个基因列表(Entrez ID格式)

gene_list <- c(“geneID1”, “geneID2”, “geneID3”, …) # 请用实际的基因ID替换这里的示例ID

进行GO富集分析

result <- enrichGO(gene = gene_list,
OrgDb = org.Hs.eg.db,
keyType = “ENTREZID”,
ont = “ALL”, # 包括生物过程(BP)、细胞组分(CC)和分子功能(MF)
pAdjustMethod = “BH”, # 控制假发现率,”BH”即Benjamini-Hochberg方法
pvalueCutoff = 0.05, # p值截断
qvalueCutoff = 0.05, # q值截断,控制假发现率
readable = TRUE) # 转换基因ID为可读的基因名

查看富集分析结果

head(result)

可视化富集分析结果

library(DOSE)
barplot(result, showCategory=20) # 显示前20个显著的GO项


文章作者: 花粱
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 花粱 !
  目录