-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvestigating_GO_in_R_3.R
2538 lines (2410 loc) · 146 KB
/
investigating_GO_in_R_3.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
997
998
999
1000
## Map GO terms from GSEA gmt files to identifiers, using information from GO.db or directly from GO ####
go <- read.delim("GSEA data/go.obo", stringsAsFactors=F)
go_names <- go[grep("name:", go[, 1]), ]
go_ids <- go[grep("^id:", go[, 1]), ]
length(go_names) # 47368
length(go_ids) # 47368
go_names <- gsub("name: ", "", go_names[1:47358])
go_ids <- gsub("id: ", "", go_ids[1:47358])
go_table <- data.frame(go_ids, go_names)
dim(go_table) # 47358 2
head(go_table)
go_table <- go_table[-grep("^obsolete", go_table$go_names), ]
dim(go_table) # 44411 2
names(go_table) <- c("id", "term")
library(GO.db)
go.db.terms <- Term(GOTERM)
length(go.db.terms) # 45050
head(go.db.terms)
sum(go.db.terms %in% go_table$term) # 43337
sum(names(go.db.terms) %in% go_table$id) # 43883
head(go.db.terms[-which(go.db.terms %in% go_table$term)], 20)
go_table[which(go_table$id %in% names(head(go.db.terms[-which(go.db.terms %in% go_table$term)], 20))), ]
# All of first 20 either obsolete or changes in terminology - no differences in punctuation, etc.
# Try matching both sources to GSEA GO terms
# Make all upper case:
go.db.terms <- toupper(go.db.terms)
go_table$term <- toupper(go_table$term)
# Remove brackets:
go.db.terms <- gsub("\\(", "", gsub("\\)", "", gsub("\\[", "", gsub("\\]", "", go.db.terms))))
go_table$term <- gsub("\\(", "", gsub("\\)", "", gsub("\\[", "", gsub("\\]", "", go_table$term))))
# Remove commas and points:
go.db.terms <- gsub("\\.", "", gsub(",", "", go.db.terms))
go_table$term <- gsub("\\.", "", gsub(",", "", go_table$term))
# Remove spaces and primes:
go.db.terms <- gsub(" ", "", gsub("'", "", go.db.terms))
go_table$term <- gsub(" ", "", gsub("'", "", go_table$term))
# Remove hyphens and forward slashes:
go.db.terms <- gsub("-", "", gsub("/", "", go.db.terms))
go_table$term <- gsub("-", "", gsub("/", "", go_table$term))
# Change + to "PLUS":
go.db.terms <- gsub("\\+", "PLUS", go.db.terms)
go_table$term <- gsub("\\+", "PLUS", go_table$term)
# Remove colons:
go.db.terms <- gsub(":", "", go.db.terms)
go_table$term <- gsub(":", "", go_table$term)
# Remove ">":
go.db.terms <- gsub(">", "", go.db.terms)
go_table$term <- gsub(">", "", go_table$term)
# Remove "=":
go.db.terms <- gsub("=", "", go.db.terms)
go_table$term <- gsub("=", "", go_table$term)
library(GSEABase)
go.gsea <- names(getGmt("GSEA data/c5.all.v7.1.symbols.gmt", geneIdType=SymbolIdentifier(), collectionType=GOCollection()))
# Remove "GO" and underscores from GSEA list
go.gsea <- gsub("_", "", go.gsea)
go.gsea <- gsub("^GO", "", go.gsea)
# Match
length(go.gsea) # 10192
sum(go.gsea %in% go.db.terms) # 10087 (105 missing)
sum(go.gsea %in% go_table$term) # 10106 (86 missing)
sum(go.gsea %in% unique(c(go.db.terms, go_table$term))) # 10189 (3 missing)
# Looks like best way to match terms used by GSEA is to combine the two sources
# (i.e. GO.db and .obo file directly downloaded from GO). Still means I'm using
# a list that clearly isn't the most up to date, but it should be reasonable since
# GSEA was updated in Jan 2020, and definitely defensible since a lot of people
# use GSEA.
# But whether combining the lists is the most useful thing to do depends on how I
# narrow down my list of GO terms. e.g. if I use GO.db to identify parent/child
# terms then there's no point in using terms from the .obo file that aren't in
# GO.db.
go.gsea[-which(go.gsea %in% unique(c(go.db.terms, go_table$term)))]
## Does it make sense to narrow down GO terms by level using GO.db? ####
library(GO.db)
# Example from https://www.biostars.org/p/367404/ for moving down levels
getAllBPChildren <- function(goids)
{
ans <- unique(unlist(mget(goids, GOBPCHILDREN), use.names=FALSE))
ans <- ans[!is.na(ans)]
}
level3_terms <- getAllBPChildren("GO:0002376")
level4_terms <- getAllBPChildren(level3_terms)
length(intersect(level3_terms, level4_terms)) # how many terms are in both lists
# Example from ?GOBPCHILDREN
## Objects in this package can be accessed using the select() interface
## from the AnnotationDbi package. See ?select for details.
xx <- as.list(GOBPCHILDREN)
# Remove GO IDs that do not have any children
xx <- xx[!is.na(xx)]
if(length(xx) > 0){
# Get the parent GO IDs for the first elents of xx
goids <- xx[[1]]
# Find out the GO terms for the first parent goid
GOID(GOTERM[[goids[1]]])
Term(GOTERM[[goids[1]]])
Synonym(GOTERM[[goids[1]]])
Secondary(GOTERM[[goids[1]]])
Definition(GOTERM[[goids[1]]])
Ontology(GOTERM[[goids[1]]])
}
#
# First want to identify terms at top, so those that don't have any parents (or ancestors)
goids <- GOID(GOTERM)
ans <- unique(unlist(mget(goids, GOBPPARENTS), use.names=F))
length(ans)
ans
goids
#
AllBPParents <- unique(unlist(mget(GOID(GOTERM), GOBPPARENTS), use.names=F))
# Error because terms in MF, CC don't appear in BP objects. Need to select BP objects only.
# Need to find out how to do that. Think should be able to use select() but don't know how.
# toTable(GOTERM) converts GOTERM to a table, with one line for each unique combination of
# ID, term, ontology, definition, synonym, secondary. Could use this to get a list of ids
# for BP. Probably a better way, but this should work.
BPids <- unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "BP")])
length(BPids) # 29699
MFids <- unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "MF")])
length(MFids) # 11148
CCids <- unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "CC")])
length(CCids) # 4202
AllBPParents <- unlist(mget(GOID(BPids), GOBPPARENTS))
length(AllBPParents) # 71746
head(AllBPParents)
sum(is.na(AllBPParents))
# Manually find BP ID and see what its parents are
mget(GOID("GO:0008150"), GOBPPARENTS) # is_a "all" - so base ontologies still have parents
head(as.list(GOTERM))
# Can also use this format:
GOBPPARENTS$"GO:0008150"
GOBPCHILDREN$"GO:0008150"
# Easiest might be to follow format of first example above and just rely on finding base
# ontology IDs manually.
getAllChildren <- function(goids, ontology)
{
ans <- unique(unlist(mget(goids, get(paste0("GO", ontology, "CHILDREN"))), use.names=FALSE))
ans <- ans[!is.na(ans)]
}
# If I want to avoid having very general terms, I should include terms at each level that
# don't appear at a lower level, i.e. each term should be only in its lowest level.
## Investigate BP levels ####
level1BP <- "GO:0008150"
level2BP <- getAllChildren(level1BP, "BP")
level1_2BP <- unique(c(level1BP, level2BP))
level3BPall <- getAllChildren(level2BP, "BP")
level3BPmin <- level3BPall[-which(level3BPall %in% level1_2BP)]
level1_3BP <- unique(c(level1_2BP, level3BPall))
level4BPall <- getAllChildren(level3BPmin, "BP")
level4BPmin <- level4BPall[-which(level4BPall %in% level1_3BP)]
level1_4BP <- unique(c(level1_3BP, level4BPall))
level5BPall <- getAllChildren(level4BPmin, "BP")
level5BPmin <- level5BPall[-which(level5BPall %in% level1_4BP)]
level1_5BP <- unique(c(level1_4BP, level5BPall))
level6BPall <- getAllChildren(level5BPmin, "BP")
level6BPmin <- level6BPall[-which(level6BPall %in% level1_5BP)]
level1_6BP <- unique(c(level1_5BP, level6BPall))
level7BPall <- getAllChildren(level6BPmin, "BP")
level7BPmin <- level7BPall[-which(level7BPall %in% level1_6BP)]
level1_7BP <- unique(c(level1_6BP, level7BPall))
level8BPall <- getAllChildren(level7BPmin, "BP")
level8BPmin <- level8BPall[-which(level8BPall %in% level1_7BP)]
level1_8BP <- unique(c(level1_7BP, level8BPall))
level9BPall <- getAllChildren(level8BPmin, "BP")
level9BPmin <- level9BPall[-which(level9BPall %in% level1_8BP)]
level1_9BP <- unique(c(level1_8BP, level9BPall))
level10BPall <- getAllChildren(level9BPmin, "BP")
level10BPmin <- level10BPall[-which(level10BPall %in% level1_9BP)]
level1_10BP <- unique(c(level1_9BP, level10BPall))
level1_9BP <- unique(c(level1_8BP, level9BPall))
level11BPall <- getAllChildren(level10BPmin, "BP")
level11BPmin <- level11BPall[-which(level11BPall %in% level1_10BP)]
level1_11BP <- unique(c(level1_10BP, level11BPall))
level12BPall <- getAllChildren(level11BPmin, "BP")
level12BPmin <- level12BPall[-which(level12BPall %in% level1_11BP)]
level1_12BP <- unique(c(level1_11BP, level12BPall))
level13BPall <- getAllChildren(level12BPmin, "BP")
level13BPmin <- level13BPall[-which(level13BPall %in% level1_12BP)]
level1_13BP <- unique(c(level1_12BP, level13BPall))
length(level2BP) # 32
length(level1_2BP) # 33
length(level3BPall) # 558
length(level3BPmin) # 554
length(level1_3BP) # 587
length(level4BPall) # 3759
length(level4BPmin) # 3591
length(level1_4BP) # 4178
length(level5BPall) # 9993
length(level5BPmin) # 8171
length(level1_5BP) # 12349
length(level6BPall) # 14396
length(level6BPmin) # 8618
length(level1_6BP) # 20967
length(level7BPall) # 11979
length(level7BPmin) # 5020
length(level1_7BP) # 25987
length(level8BPall) # 6793
length(level8BPmin) # 2479
length(level1_8BP) # 28466
length(level9BPall) # 3071
length(level9BPmin) # 989
length(level1_9BP) # 29455
length(level10BPall) # 893
length(level10BPmin) # 208
length(level1_10BP) # 29663
length(level11BPall) # 175
length(level11BPmin) # 34
length(level1_11BP) # 29697
length(level12BPall) # 25
length(level12BPmin) # 2
length(level1_12BP) # 29699
length(level13BPall) # 0
length(level13BPmin) # 0
length(level1_13BP) # 29699
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "BP")])) # 29699
# Look at some examples of terms in each level
for (i in sample(length(level2BP), 10)) {
print(Term(GOTERM[[level2BP[i]]]))
} # 32
# rhythmic process; regulation of biological process; localization; carbon utilization;
# multi-organism process; locomotion; cellular process; immune system process; growth; signaling
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level2BP)]), value=T)
# none
for (i in sample(length(level3BPmin), 10)) {
print(Term(GOTERM[[level3BPmin[i]]]))
} # 554
# fertilization, exchange of chromosomal proteins; positive regulation of fibrinolysis;
# septum digestion after cytokinesis; leukocyte proliferation; immunological memory process;
# multicellular organism growth; positive regulation of protein localization to cell-cell adherens junction;
# leukocyte activation; embryo implantation; negative regulation of DNA-binding transcription factor activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level3BPmin)]), value=T)
# Some terms that are quite specific, e.g. "regulation of transcription from RNA polymerase II promoter
# involved in spermatogenesis", which doesn't have any children, so excluding this level would risk missing
# some terms completely.
for (i in sample(length(level4BPmin), 10)) {
print(Term(GOTERM[[level4BPmin[i]]]))
} # 3591
# head segmentation; primary spermatocyte growth; epithelial cell proliferation involved in salivary gland morphogenesis;
# positive regulation of cellular response to hepatocyte growth factor stimulus;
# negative regulation of c-di-cGMP signaling; rhombomere development; cell wall integrity MAPK cascade;
# microtubule bundle formation involved in horsetail-astral microtubule organization;
# regulation of entry into reproductive diapause; response to herbicide
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level4BPmin)]), value=T)
# Mix of general and specific terms, e.g. "tRNA folding", "negative regulation of mitochondrial unfolded protein response
# by negative regulation of transcription from RNA polymeras II promoter", so shouldn't exclude, unless can be
# confident that child terms of these terms will be informative, which surely will be the case.
for (i in sample(length(level5BPmin), 10)) {
print(Term(GOTERM[[level5BPmin[i]]]))
} # 8171
# negative regulation of extrachromosomal rDNA circle accumulation involved in replicative cell aging;
# ureter morphogenesis; response to muscle stretch; positive regulation of trypanothione biosynthetic process;
# negative regulation of histamine uptake; embryonic crystal cell differentiation;
# positive regulation of VCP-NPL4-UFD1 AAA ATPase complex assembly; positive regulation of glycolytic process;
# border follicle cell migration; positive regulatiokn of phosphatidylserine exposure on apoptotic cell surface
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level5BPmin)]), value=T)
# Again a mix of general and specific terms.
for (i in sample(length(level6BPmin), 10)) {
print(Term(GOTERM[[level6BPmin[i]]]))
} # 8618
# L-aspartate transmembrane transport; lateral line ganglion development; Golgi vesicle budding;
# regulation of sensory neuron axon guidance;
# smoothened signaling pathway involved in growh plate cartilage chondrocyte development;
# positive regulation of sterol import; photosynthetic phosphorylation; RNA (guanin-N7)-methylation;
# regulation of myeloid leukocyte mediated immunity; metabolism by symbiont of host xylan
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level6BPmin)]), value=T)
# Again a mix of general and specific terms.
for (i in sample(length(level7BPmin), 10)) {
print(Term(GOTERM[[level7BPmin[i]]]))
} # 5020
# 2-deoxyribose 1-phosphate catabolic process; helper T cell chemotaxis;
# positive regulation of sodium ion export across plasma membrane; filtration diaphragm assembly;
# mini excitatory postsynaptic potential; N-terminal peptidyl-arginine acetulation; regulation of ERK5 cascade;
# positive regulation of symbiont of host adenylate cyclase activity; modulation by symbiont of host immune reponse;
# activation of microtubule activation
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level7BPmin)]), value=T)
# Again a mix of general and specific terms, but a lot look like they would probably be adequately covered by parent terms.
for (i in sample(length(level8BPmin), 10)) {
print(Term(GOTERM[[level8BPmin[i]]]))
} # 2479
# regulation of dense core cranule exocytosis; positive regulation of striated muscle cell apoptotic process;
# dCDP phosphorylation; cellular response to diacul bacterial lipopeptide; D-leucine catabolic process;
# enzyme active site formation via O-sulfo-L-threonine; tRNA wobble base cytosine methylation;
# neuromast hair cell differentiation involved in neuromast regeneration; CMP phosphorylation; O-glycan processing, core 2
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level8BPmin)]), value=T) # none
# Most look like they would probably be adequately covered by parent terms, but still some fairly general, e.g. "mRNA
# cleavage", whose parent terms aren't very informative: "mRNA metabolic process", "RNA phosphodiester bond hydrolysis".
for (i in sample(length(level9BPmin), 10)) {
print(Term(GOTERM[[level9BPmin[i]]]))
} # 989
# C-terminal peptidyl-arginine amidation; group A colicin transport; propanediol catabolic process;
# regulation of cAMP-dependent protein kinase activity; GDP-fucose import into Golgi lumen; hypoxanthine oxidation;
# peptidyl-aspartagine methylation; DNM1L-mediated stimulation of mitophagy in response to mitochondrial depolarization;
# laminarabiose transport; hexuronate transmembrane transport
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level9BPmin)]), value=T)
# Generally look like would have informative parent terms.
for (i in sample(length(level10BPmin), 10)) {
print(Term(GOTERM[[level10BPmin[i]]]))
} # 208
# passive induction of host innate immune response by virus; ferrous iron transmembrane transport;
# negative regulation of cytosolic calcium ion concentration; store-operated calcium entry;
# activation of protein kinase A activity; epinephrine-mediated vasodilation;
# nuclear tRNA 3'-trailer cleavage, endonucleolytic; positive regulation of cytosolic calcium ion concentration;
# smooth endoplasmic reticulum calcium ion homeostasis; protein monoubiquitination
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level10BPmin)]), value=T)
# All quite specific.
for (i in sample(length(level11BPmin), 10)) {
print(Term(GOTERM[[level11BPmin[i]]]))
} # 34
# alpha-tubulin acetylation; T follicular helper cell differentiation; protein localization to microtubule plus-end;
# iron assimilation by capture and transport; protein K69-linked ufmylation; peptidyl-lysine N6-acetylation;
# capsanthin catabolic process; D-glucuronate transmembrane transport; calcium ion export across plasma membrane;
# negative regulatiokn of smooth endoplasmic reticulum calcium ion concentration
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level11BPmin)]), value=T)
# none
for (i in 1:length(level12BPmin)) {
print(Term(GOTERM[[level12BPmin[i]]]))
} # 2
# asymmetric protein localization to old mitotic spindle pole body;
# asymmetric protein localization to new mitotic spindle pole body
## Investigate MF levels ####
level1MF <- "GO:0003674"
level2MF <- getAllChildren(level1MF, "MF")
level1_2MF <- unique(c(level1MF, level2MF))
level3MF <- getAllChildren(level2MF, "MF") # No level 3 terms in level 2
level1_3MF <- unique(c(level1_2MF, level3MF))
level4MFall <- getAllChildren(level3MF, "MF")
level4MFmin <- level4MFall[-which(level4MFall %in% level1_3MF)]
level1_4MF <- unique(c(level1_3MF, level4MFall))
level5MFall <- getAllChildren(level4MFmin, "MF")
level5MFmin <- level5MFall[-which(level5MFall %in% level1_4MF)]
level1_5MF <- unique(c(level1_4MF, level5MFall))
level6MFall <- getAllChildren(level5MFmin, "MF")
level6MFmin <- level6MFall[-which(level6MFall %in% level1_5MF)]
level1_6MF <- unique(c(level1_5MF, level6MFall))
level7MFall <- getAllChildren(level6MFmin, "MF")
level7MFmin <- level7MFall[-which(level7MFall %in% level1_6MF)]
level1_7MF <- unique(c(level1_6MF, level7MFall))
level8MFall <- getAllChildren(level7MFmin, "MF")
level8MFmin <- level8MFall[-which(level8MFall %in% level1_7MF)]
level1_8MF <- unique(c(level1_7MF, level8MFall))
level9MFall <- getAllChildren(level8MFmin, "MF")
level9MFmin <- level9MFall[-which(level9MFall %in% level1_8MF)]
level1_9MF <- unique(c(level1_8MF, level9MFall))
level10MFall <- getAllChildren(level9MFmin, "MF")
level10MFmin <- level10MFall[-which(level10MFall %in% level1_9MF)]
level1_10MF <- unique(c(level1_9MF, level10MFall))
level1_9MF <- unique(c(level1_8MF, level9MFall))
level11MFall <- getAllChildren(level10MFmin, "MF")
level11MFmin <- level11MFall[-which(level11MFall %in% level1_10MF)]
level1_11MF <- unique(c(level1_10MF, level11MFall))
level12MFall <- getAllChildren(level11MFmin, "MF")
level12MFmin <- level12MFall[-which(level12MFall %in% level1_11MF)]
level1_12MF <- unique(c(level1_11MF, level12MFall))
level13MFall <- getAllChildren(level12MFmin, "MF")
level13MFmin <- level13MFall[-which(level13MFall %in% level1_12MF)]
level1_13MF <- unique(c(level1_12MF, level13MFall))
length(level2MF) # 15
length(level1_2MF) # 16
length(level3MF) # 146
length(level1_3MF) # 162
length(level4MFall) # 871
length(level4MFmin) # 869
length(level1_4MF) # 1031
length(level5MFall) # 2233
length(level5MFmin) # 2098
length(level1_5MF) # 3129
length(level6MFall) # 5388
length(level6MFmin) # 5054
length(level1_6MF) # 8183
length(level7MFall) # 2332
length(level7MFmin) # 1926
length(level1_7MF) # 10109
length(level8MFall) # 997
length(level8MFmin) # 727
length(level1_8MF) # 10836
length(level9MFall) # 271
length(level9MFmin) # 201
length(level1_9MF) # 11037
length(level10MFall) # 95
length(level10MFmin) # 79
length(level1_10MF) # 11116
length(level11MFall) # 21
length(level11MFmin) # 13
length(level1_11MF) # 11129
length(level12MFall) # 22
length(level12MFmin) # 19
length(level1_12MF) # 11148
length(level13MFall) # 0
length(level13MFmin) # 0
length(level1_13MF) # 11148
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "MF")])) # 11148
# Look at some examples of terms in each level
for (i in sample(length(level2MF), 10)) {
print(Term(GOTERM[[level2MF[i]]]))
} # 15
# translation regulator activity; molecular carrier activity; transporter activity; structural molecule activity;
# toxin activity; cargo receptor activity; hijacked molecular function; antioxidant activity;
# molecular function regulator; molecular transducer activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level2MF)]), value=T)
# none
for (i in sample(length(level3MF), 10)) {
print(Term(GOTERM[[level3MF[i]]]))
} # 146
# metal cluster binding; amide binding; microfibril binding; neurotransmitter transporter activity;
# transforming growth factor beta receptor, cytoplasmic mediator activity; general transcription initiation factor activity;
# general transcription initiation factor activity; dinitrosyl-iron complex binding; structural constituent of cytoskeleton;
# structural constituent of eye lens; apolipoprotein receptor activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level3MF)]), value=T)
# Two out of 3 terms don't have any children, so really can't exclude this level.
for (i in sample(length(level4MFmin), 10)) {
print(Term(GOTERM[[level4MFmin[i]]]))
} # 869
# sn-glycerol 3-phosphate binding; tyrosine binding; nitric-oxide synthase regulator activity; clathrin adaptor activity;
# carbon phosphorus lysase activity; TFIIH-class transcription factor complex binding; riboflavin binding;
# violaxanthin de-epoxidase activity; phosphopantetheine binding; 4-alpha-glucanotransferase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level4MFmin)]), value=T)
# Mix of general and specific terms.
for (i in sample(length(level5MFmin), 10)) {
print(Term(GOTERM[[level5MFmin[i]]]))
} # 2098
# methylation-dependent protein binding; talin binding; sepiapterin deaminase activity; trichloro-p-hydroquinone reductive
# dehalogenase activity; ion channel activity; chalcone isomerase activity; ATP-dependent RNA helicase activity;
# diacyl lipopeptide binding; oxidoreductase activity, acting on single donors with incorporation of molecular oxygen,
# incorporation of two atoms of oxygen;dehydroascorbic acid transmembrane transporter activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level5MFmin)]), value=T)
# Some very specific terms, but still some fairly general.
for (i in sample(length(level6MFmin), 10)) {
print(Term(GOTERM[[level6MFmin[i]]]))
} # 5054
# DNA-dependent protein kinase activity; peptidase activator activity involved in apoptotic process; single-stranded DNA
# binding; cyclomaltodextrin glucanotransferase activity; uracil:catio symporter activity; 6C-naringenin dibenzoylmethane
# tautomer glucoosyltransferase activity; ferredoxin-NAD(P) reductase activity;
# 4alpha-formyl-5alpha-cholesta-7,24-dien-3beta-ol-4alpha-methyl oxidase activity; glucose-6-phosphate 3-dehydrogenase
# activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level6MFmin)]), value=T)
# Again a mix of general and specific terms.
for (i in sample(length(level7MFmin), 10)) {
print(Term(GOTERM[[level7MFmin[i]]]))
} # 1926
# miRNA binding; glucuronoarabinoxylan endo-1,4-beta-xylanase activity; diisopropyl-fluorophosphatase activity;
# karmpferol 3-0-galactosyltransferase activity; eoxin E4 synthase activity; very-long-chain-(S)-2-hydroxy-acid oxidase
# activity; undecaprenyl-phosphate mannosyltransferase activity; plastid single-subunit type RNA polymerase biding;
# ecdysteroid-phosphate phosphatase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level7MFmin)]), value=T)
# Again a mix of general and specific terms.
for (i in sample(length(level8MFmin), 10)) {
print(Term(GOTERM[[level8MFmin[i]]]))
} # 727
# N-hydroxyarylamine O-acetyltransferase activity; store-operated calcium channel activity; autotransporter activity;
# palmitoyl-CoA hydrolase activity; N6-hydroxylysine O-acetyltransferase activity; long-chain acyl-CoA hydrolase activity;
# ADP reductase activity; acetyl CoA:(Z)-3-hexen-1-ol acetyltransferase activity; translation release factor activity;
# 5-aminolevulinate synthase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level8MFmin)]), value=T)
# Mostly very specific terms that surely would be adequately covered by parent terms.
for (i in sample(length(level9MFmin), 10)) {
print(Term(GOTERM[[level9MFmin[i]]]))
} # 201
# inositol 1,4,5-trisphosphate-sensitive calcium-release channel activity; RNA polymerase II proximal promoter
# sequence-specific DNA binding; insulin-responsive glucose:proton symporter activity; translation release factor
# activity, codon nonspecific; voltage-gated sodium channel activity involved in cardiac muscle cell action potential;
# G-protein activated inward rectifier potassium channel activity; dinucleotide insertion or deletion binding;
# inositol phosphorylceramide phospholipase activity; phosphatidylinositol-3,4-bisphosphate 4-phosphatase activity;
# inositol-2,4,5-triphosphate 5-phosphatase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level9MFmin)]), value=T)
# All look like very specific terms.
for (i in sample(length(level10MFmin), 10)) {
print(Term(GOTERM[[level10MFmin[i]]]))
} # 79
# carbohydrate response element binding"
# single-stranded DNA-dependent CTPase activity; tubulin N-acetyltransferase activity; mannosyl-inositol
# phosphorylceramide phospholipase activity; tubulin-dependent ATPase activity;
# ryanodine-sensitive calcium-release channel activity involved in regulation of postsynaptic cytosolic calcium levels;
# voltage-gated calcium channel activity involved SA node cell action potential; ATP:3'-cytidine-cytidine-tRNA
# adenylyltransferase activity; GTP-dependent helicase activity; peptide-glutamate-N-acetyltransferase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level10MFmin)]), value=T)
# Only 2 terms, probably have informative parents.
for (i in sample(length(level11MFmin), 10)) {
print(Term(GOTERM[[level11MFmin[i]]]))
} # 13
# cohesin ATPase activity;
# ATP-dependent microtubule motor activity, plus-end-directed; RNA translocase activity; H3 histone acetyltransferase
# activity; ATP-dependent microtubule motor activity, minus-end-directed; single-stranded DNA-dependent ATPase activity;
# protein-DNA loading ATPase activity; H2A histone acetyltransferase activity; H2B histone acetyltransferase activity;
# H4 histone acetyltransferase activity
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level11MFmin)]), value=T)
# Only 1 term, "RNA translocase activity", which has parent "RNA-dependent ATPase activity", which has parent "ATPase
# activity, coupled", which has parent "ATPase activity", which has parent "nucleoside-triphosphatase activity", which has
# parent "pyrophosphatase activity", which has parent "hydrolase activity, acting on acid anhydrides, in
# phosphorus-containing anhydrides", which has parent "hydrolase activity, acting on acid anhydrides", which has parent
# "hydrolase activity", which has parent "catalytic activity", which has parent "molecular function". It's really
# impossible to say which level would be the most useful. It's difficult to justify excluding even level 11 terms based on
# this.
for (i in 1:length(level12MFmin)) {
print(Term(GOTERM[[level12MFmin[i]]]))
} # 19
# histone acetyltransferase activity (H3-K56 specific); histone acetyltransferase activity (H3-K14 specific); etc.
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level12MFmin)]), value=T)
# "RNA translocase activity involved in viral RNA genome packaging" definitely very specific.
## Investigate CC levels ####
level1CC <- "GO:0005575"
level2CC <- getAllChildren(level1CC, "CC")
level1_2CC <- unique(c(level1CC, level2CC))
level3CCall <- getAllChildren(level2CC, "CC")
level3CCmin <- level3CCall[-which(level3CCall %in% level1_2CC)]
level1_3CC <- unique(c(level1_2CC, level3CCall))
level4CCall <- getAllChildren(level3CCmin, "CC")
level4CCmin <- level4CCall[-which(level4CCall %in% level1_3CC)]
level1_4CC <- unique(c(level1_3CC, level4CCall))
level5CCall <- getAllChildren(level4CCmin, "CC")
level5CCmin <- level5CCall[-which(level5CCall %in% level1_4CC)]
level1_5CC <- unique(c(level1_4CC, level5CCall))
level6CCall <- getAllChildren(level5CCmin, "CC")
level6CCmin <- level6CCall[-which(level6CCall %in% level1_5CC)]
level1_6CC <- unique(c(level1_5CC, level6CCall))
level7CCall <- getAllChildren(level6CCmin, "CC")
level7CCmin <- level7CCall[-which(level7CCall %in% level1_6CC)]
level1_7CC <- unique(c(level1_6CC, level7CCall))
level8CCall <- getAllChildren(level7CCmin, "CC")
level8CCmin <- level8CCall[-which(level8CCall %in% level1_7CC)]
level1_8CC <- unique(c(level1_7CC, level8CCall))
level9CCall <- getAllChildren(level8CCmin, "CC")
level9CCmin <- level9CCall[-which(level9CCall %in% level1_8CC)]
level1_9CC <- unique(c(level1_8CC, level9CCall))
level10CCall <- getAllChildren(level9CCmin, "CC")
level10CCmin <- level10CCall[-which(level10CCall %in% level1_9CC)]
level1_10CC <- unique(c(level1_9CC, level10CCall))
level1_9CC <- unique(c(level1_8CC, level9CCall))
level11CCall <- getAllChildren(level10CCmin, "CC")
level11CCmin <- level11CCall[-which(level11CCall %in% level1_10CC)]
level1_11CC <- unique(c(level1_10CC, level11CCall))
level12CCall <- getAllChildren(level11CCmin, "CC")
level12CCmin <- level12CCall[-which(level12CCall %in% level1_11CC)]
level1_12CC <- unique(c(level1_11CC, level12CCall))
level13CCall <- getAllChildren(level12CCmin, "CC")
level13CCmin <- level13CCall[-which(level13CCall %in% level1_12CC)]
level1_13CC <- unique(c(level1_12CC, level13CCall))
length(level2CC) # 21
length(level1_2CC) # 22
length(level3CCall) # 755
length(level3CCmin) # 748
length(level1_3CC) # 770
length(level4CCall) # 1381
length(level4CCmin) # 1118
length(level1_4CC) # 1888
length(level5CCall) # 2136
length(level5CCmin) # 1383
length(level1_5CC) # 3271
length(level6CCall) # 1403
length(level6CCmin) # 690
length(level1_6CC) # 3961
length(level7CCall) # 555
length(level7CCmin) # 195
length(level1_7CC) # 4156
length(level8CCall) # 126
length(level8CCmin) # 41
length(level1_8CC) # 4197
length(level9CCall) # 18
length(level9CCmin) # 4
length(level1_9CC) # 4201
length(level10CCall) # 6
length(level10CCmin) # 1
length(level1_10CC) # 4202
length(level11CCall) # 0
length(level11CCmin) # 0
length(level1_11CC) # 4202
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "CC")])) # 4202
# Look at some examples of terms in each level
for (i in sample(length(level2CC), 10)) {
print(Term(GOTERM[[level2CC[i]]]))
} # 21
# supramolecular complex; symplast; membrane part; cell junction; organelle part; protein-containing complex;
# mitochondrion-associated adherens complex; virion; membrane; synapse
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level2CC)]), value=T)
# none
for (i in sample(length(level3CCmin), 10)) {
print(Term(GOTERM[[level3CCmin[i]]]))
} # 748
# enterobactin synthetase complex; mediator complex; intrinsic component of synaptic membrane; sulfite reductase complex
# (NADPH); BID-BCL-cl complex; retromer, tubulation complex; eukaryotic translation initiation factor 4F complex;
# ventral disc lateral crest; postsynaptic specialization membrane; cell pole
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level3CCmin)]), value=T)
# Some very general but some very specific terms, all involving "complex"
for (i in sample(length(level4CCmin), 10)) {
print(Term(GOTERM[[level4CCmin[i]]]))
} # 1118
# pollen wall; coated vesicle membrane; cell training edge membrane; nucleotide-excision repair complex; mitochondrial
# part; bacterial-type flagellum hook-filament junction; gas vesicle shell; mannosyl-oligosaccharide 1,2-alpha-mannosidase
# complex; Mitochondria-associated ER Membrane; spine mat
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level4CCmin)]), value=T) # none
# Level of detail that wouldn't want to exclude.
for (i in sample(length(level5CCmin), 10)) {
print(Term(GOTERM[[level5CCmin[i]]]))
} # 1383
# cell cortex part; Cdc48p-Npl4p-Vms1p AAA ATPase complex; Fused-Smurf ubiquitin ligase complex; myosin III complex;
# transmembrane actin-associated (TAN) line; anchored component of postsynaptic density membrane; lamin filament;
# fascia adherens; alphaPDGFR-SHP-2-complex; intrinsic component of mycolate outer membrane
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level5CCmin)]), value=T) # none
# Generally quite specific terms.
for (i in sample(length(level6CCmin), 10)) {
print(Term(GOTERM[[level6CCmin[i]]]))
} # 690
# septin cytoskeleton; CatSper complex; Rpd3S complex; chloroplast thylakoid lumen; thiazole synthase complex;
# extrinsic component of stromal side of plastic inner membrane; nucleolar exosome (RNase complex); chromatoid body;
# protein kinase complex; RNA polymerase I upstream activating factor complex
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level6CCmin)]), value=T) # none
# Generally very specific terms. Would expect all to have informative parent terms.
for (i in sample(length(level7CCmin), 10)) {
print(Term(GOTERM[[level7CCmin[i]]]))
} # 195
# terminal cisterna lumen; SCF-Ufo1/Pof10 ubiquitin ligase complex; host cell rough endoplasmic reticulum; Z chromosome;
# early recombination nodule; hypolemmal cisterna; MLL1 complex; Golgi cis cisterna membrane; iridosome;
# U4/U6 x US tri-snRNP complex
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level7CCmin)]), value=T)
# none
for (i in sample(length(level8CCmin), 10)) {
print(Term(GOTERM[[level8CCmin[i]]]))
} # 41
# glial limiting end-foot; amylin receptor complex 3; condensed chromatin of inactivated sex chromosome;
# NuA3b histon acetyltransferase complex; chromatin of active sex chromosome; heterochromatin domain; intercalary
# heterochromatin; Mst2 hisone acetyltransferase complex; bacteroid-containing symbiosome; alpha-heterochromatin
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level8CCmin)]), value=T)
# none
for (i in sample(length(level9CCmin), 10)) {
print(Term(GOTERM[[level9CCmin[i]]]))
} # 4
# C-terminal peptidyl-arginine amidation; group A colicin transport; propanediol catabolic process;
# regulation of cAMP-dependent protein kinase activity; GDP-fucose import into Golgi lumen; hypoxanthine oxidation;
# peptidyl-aspartagine methylation; DNM1L-mediated stimulation of mitophagy in response to mitochondrial depolarization;
# laminarabiose transport; hexuronate transmembrane transport
grep("RNA", unname(Term(GOTERM)[which(GOID(GOTERM) %in% level9CCmin)]), value=T)
# none
for (i in 1:length(level10CCmin)) {
print(Term(GOTERM[[level10CCmin[i]]]))
} # SAS acetyltransferase complex
## Create subsets based on various level thresholds ####
# May be best to keep all terms, but test a couple of thresholds: discarding levels 1-2 and 9 upwards, which should really
# only exclude terms that are better represented by parents or children but won't reduce the number of terms by much, and
# more stringently discard levels 1-3 and 7 upwards, which risks excluding informative terms that don't have informative
# parents or children and/or don't have children.
# Note that what I'm calling level 1 (i.e. BP, MF, CC) seems to be called level 2 by at least some people (which makes
# sense really, making level 1 the overall root). It will probably be best to use that terminology if I talk about levels
# in paper or thesis, but not really any need to change it in all the code now.
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "BP")])) # 29699
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "MF")])) # 11148
length(unique(toTable(GOTERM)$go_id[which(toTable(GOTERM)$Ontology == "CC")])) # 4202
length(Term(GOTERM)) # 45050 = 29699 + 11148 + 4202 + 1 (1 entry with id and term "all" has Ontology "universal")
length(unique(toTable(GOTERM)$go_id)) # 45050
dim(unique(toTable(GOTERM)[, c("go_id", "Term")])) # 45050 2
identical(GOID(GOTERM), unique(toTable(GOTERM)$go_id)) # FALSE
head(GOID(GOTERM))
head(unique(toTable(GOTERM)$go_id))
identical(sort(unname(GOID(GOTERM))), sort(unique(toTable(GOTERM)$go_id))) # TRUE
identical(sort(unname(Term(GOTERM))), sort(unique(toTable(GOTERM)$Term))) # TRUE
# Different order for each but same information. Doesn't matter which I use as long as it's consistent and allows me
# to map between terms and IDs.
all_GO.db_terms <- unique(toTable(GOTERM)[c("go_id", "Term", "Ontology")])
dim(all_GO.db_terms) # 45050 3
# Add column with manipulated terms for matching to MSigDB terms
GO.db_stripped <- toupper(all_GO.db_terms$Term)
# Remove brackets:
GO.db_stripped <- gsub("\\(", "", gsub("\\)", "", gsub("\\[", "", gsub("\\]", "", GO.db_stripped))))
# Remove commas and points:
GO.db_stripped <- gsub("\\.", "", gsub(",", "", GO.db_stripped))
# Remove spaces and primes:
GO.db_stripped <- gsub(" ", "", gsub("'", "", GO.db_stripped))
# Remove hyphens and forward slashes:
GO.db_stripped <- gsub("-", "", gsub("/", "", GO.db_stripped))
# Change + to "PLUS":
GO.db_stripped <- gsub("\\+", "PLUS", GO.db_stripped)
# Remove colons:
GO.db_stripped <- gsub(":", "", GO.db_stripped)
# Remove ">":
GO.db_stripped <- gsub(">", "", GO.db_stripped)
# Remove "=":
GO.db_stripped <- gsub("=", "", GO.db_stripped)
all_GO.db_terms <- cbind(all_GO.db_terms, GO.db_stripped)
all_GO.db_BP <- all_GO.db_terms[which(all_GO.db_terms$Ontology == "BP"), ]
all_GO.db_MF <- all_GO.db_terms[which(all_GO.db_terms$Ontology == "MF"), ]
all_GO.db_CC <- all_GO.db_terms[which(all_GO.db_terms$Ontology == "CC"), ]
level3to8_BP <- all_GO.db_BP[which((all_GO.db_BP$go_id %in% level1_8BP) & !(all_GO.db_BP$go_id %in% level1_2BP)), ]
level4to6_BP <- all_GO.db_BP[which((all_GO.db_BP$go_id %in% level1_6BP) & !(all_GO.db_BP$go_id %in% level1_3BP)), ]
level3to8_MF <- all_GO.db_MF[which((all_GO.db_MF$go_id %in% level1_8MF) & !(all_GO.db_MF$go_id %in% level1_2MF)), ]
level4to6_MF <- all_GO.db_MF[which((all_GO.db_MF$go_id %in% level1_6MF) & !(all_GO.db_MF$go_id %in% level1_3MF)), ]
level3to8_CC <- all_GO.db_CC[which((all_GO.db_CC$go_id %in% level1_8CC) & !(all_GO.db_CC$go_id %in% level1_2CC)), ]
level4to6_CC <- all_GO.db_CC[which((all_GO.db_CC$go_id %in% level1_6CC) & !(all_GO.db_CC$go_id %in% level1_3CC)), ]
level3to8_combined <- rbind(level3to8_BP, level3to8_MF, level3to8_CC)
level4to6_combined <- rbind(level4to6_BP, level4to6_MF, level4to6_CC)
rbind(c(nrow(all_GO.db_terms), nrow(all_GO.db_BP), nrow(all_GO.db_MF), nrow(all_GO.db_CC)),
c(nrow(level3to8_combined), nrow(level3to8_BP), nrow(level3to8_MF), nrow(level3to8_CC)),
c(nrow(level4to6_combined), nrow(level4to6_BP), nrow(level4to6_MF), nrow(level4to6_CC)))
# [,1] [,2] [,3] [,4]
# [1,] 45050 29699 11148 4202
# [2,] 43428 28433 10820 4175
# [3,] 31592 20380 8021 3191
# Excluding levels 1-2 and 9+ removes only 3.6% of terms overall, and excluding levels 1-3 and 7+ removes 30%.
# Want to run a few examples to see which version gives the most informative categories. Run each with and without
# applying some sort of reduction of redundancy. Methods like GOSemSim identify pairwise semantic similarity (i.e.
# similarity of meaning rather than just words) between terms, but don't give a way of choosing which to keep.
# REVIGO takes a list of GO IDs and p-values and chooses terms to keep (I think based on keeping the term with the
# lowest p-value), and seems to do more than just pairwise comparisons, although it's probably just combining
# pairwise comparisons based on the given similarity threshold). REVIGO is browser based, but there is a Bioconductor
# package, rrvgo, based on REVIGO. There is also a function, simplify(), in clusterProfiler, which uses GOSemSim to
# identify redundancy and remove redundant terms. It looks like it chooses which term to keep based on p-value, so
# should be similar to REVIGO/rrvgo. For both, you need to choose a similarity threshold, so should also try a few
# different values to see which gives the most useful terms.
# Will end up with probably 9 different variants to test - 3 sets of levels and 3 thresholds - for each of the 3
# ontologies, but will use the same set of levels and same threshold for each ontology. Actually will probably look
# at each ontology separately and all together - or possibly just all together initially at least.
## Map full and reduced GO term lists to MSigDB list and export ####
GO.db_stripped_combined_full <- as.character(all_GO.db_terms$GO.db_stripped)
GO.db_stripped_combined_3to8 <- as.character(level3to8_combined$GO.db_stripped)
GO.db_stripped_combined_4to6 <- as.character(level4to6_combined$GO.db_stripped)
GO.db_stripped_BP_full <- as.character(all_GO.db_BP$GO.db_stripped)
GO.db_stripped_BP_3to8 <- as.character(level3to8_BP$GO.db_stripped)
GO.db_stripped_BP_4to6 <- as.character(level4to6_BP$GO.db_stripped)
GO.db_stripped_MF_full <- as.character(all_GO.db_MF$GO.db_stripped)
GO.db_stripped_MF_3to8 <- as.character(level3to8_MF$GO.db_stripped)
GO.db_stripped_MF_4to6 <- as.character(level4to6_MF$GO.db_stripped)
GO.db_stripped_CC_full <- as.character(all_GO.db_CC$GO.db_stripped)
GO.db_stripped_CC_3to8 <- as.character(level3to8_CC$GO.db_stripped)
GO.db_stripped_CC_4to6 <- as.character(level4to6_CC$GO.db_stripped)
library(GSEABase)
GSEA_data <- getGmt("GSEA data/c5.all.v7.1.symbols.gmt", geneIdType=SymbolIdentifier(), collectionType=GOCollection())
go.gsea <- names(GSEA_data)
# Remove "GO" and underscores from GSEA list
go.gsea_stripped <- gsub("_", "", go.gsea)
go.gsea_stripped <- gsub("^GO", "", go.gsea_stripped)
GO.GSEA_stripped_combined_full <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_combined_full)]
GO.GSEA_stripped_combined_3to8 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_combined_3to8)]
GO.GSEA_stripped_combined_4to6 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_combined_4to6)]
GO.GSEA_stripped_BP_full <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_BP_full)]
GO.GSEA_stripped_BP_3to8 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_BP_3to8)]
GO.GSEA_stripped_BP_4to6 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_BP_4to6)]
GO.GSEA_stripped_MF_full <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_MF_full)]
GO.GSEA_stripped_MF_3to8 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_MF_3to8)]
GO.GSEA_stripped_MF_4to6 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_MF_4to6)]
GO.GSEA_stripped_CC_full <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_CC_full)]
GO.GSEA_stripped_CC_3to8 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_CC_3to8)]
GO.GSEA_stripped_CC_4to6 <- go.gsea[which(go.gsea_stripped %in% GO.db_stripped_CC_4to6)]
rbind(c(length(GO.GSEA_stripped_combined_full), length(GO.GSEA_stripped_BP_full),
length(GO.GSEA_stripped_MF_full), length(GO.GSEA_stripped_CC_full)),
c(length(GO.GSEA_stripped_combined_3to8), length(GO.GSEA_stripped_BP_3to8),
length(GO.GSEA_stripped_MF_3to8), length(GO.GSEA_stripped_CC_3to8)),
c(length(GO.GSEA_stripped_combined_4to6), length(GO.GSEA_stripped_BP_4to6),
length(GO.GSEA_stripped_MF_4to6), length(GO.GSEA_stripped_CC_4to6)))
# [,1] [,2] [,3] [,4]
# [1,] 10087 7472 1622 993
# [2,] 9827 7273 1567 987
# [3,] 7402 5529 1158 715
GSEA_data_GO.db_combined_full <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_combined_full)]
GSEA_data_GO.db_combined_3to8 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_combined_3to8)]
GSEA_data_GO.db_combined_4to6 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_combined_4to6)]
GSEA_data_GO.db_BP_full <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_BP_full)]
GSEA_data_GO.db_BP_3to8 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_BP_3to8)]
GSEA_data_GO.db_BP_4to6 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_BP_4to6)]
GSEA_data_GO.db_MF_full <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_MF_full)]
GSEA_data_GO.db_MF_3to8 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_MF_3to8)]
GSEA_data_GO.db_MF_4to6 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_MF_4to6)]
GSEA_data_GO.db_CC_full <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_CC_full)]
GSEA_data_GO.db_CC_3to8 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_CC_3to8)]
GSEA_data_GO.db_CC_4to6 <- GSEA_data[which(names(GSEA_data) %in% GO.GSEA_stripped_CC_4to6)]
rbind(c(length(GSEA_data_GO.db_combined_full), length(GSEA_data_GO.db_BP_full),
length(GSEA_data_GO.db_MF_full), length(GSEA_data_GO.db_CC_full)),
c(length(GSEA_data_GO.db_combined_3to8), length(GSEA_data_GO.db_BP_3to8),
length(GSEA_data_GO.db_MF_3to8), length(GSEA_data_GO.db_CC_3to8)),
c(length(GSEA_data_GO.db_combined_4to6), length(GSEA_data_GO.db_BP_4to6),
length(GSEA_data_GO.db_MF_4to6), length(GSEA_data_GO.db_CC_4to6)))
# Match expected lengths.
toGmt(GSEA_data_GO.db_combined_full, "GSEA data/GSEA_data_GO.db_combined_full.gmt")
toGmt(GSEA_data_GO.db_combined_3to8, "GSEA data/GSEA_data_GO.db_combined_3to8.gmt")
toGmt(GSEA_data_GO.db_combined_4to6, "GSEA data/GSEA_data_GO.db_combined_4to6.gmt")
toGmt(GSEA_data_GO.db_BP_full, "GSEA data/GSEA_data_GO.db_BP_full.gmt")
toGmt(GSEA_data_GO.db_BP_3to8, "GSEA data/GSEA_data_GO.db_BP_3to8.gmt")
toGmt(GSEA_data_GO.db_BP_4to6, "GSEA data/GSEA_data_GO.db_BP_4to6.gmt")
toGmt(GSEA_data_GO.db_MF_full, "GSEA data/GSEA_data_GO.db_MF_full.gmt")
toGmt(GSEA_data_GO.db_MF_3to8, "GSEA data/GSEA_data_GO.db_MF_3to8.gmt")
toGmt(GSEA_data_GO.db_MF_4to6, "GSEA data/GSEA_data_GO.db_MF_4to6.gmt")
toGmt(GSEA_data_GO.db_CC_full, "GSEA data/GSEA_data_GO.db_CC_full.gmt")
toGmt(GSEA_data_GO.db_CC_3to8, "GSEA data/GSEA_data_GO.db_CC_3to8.gmt")
toGmt(GSEA_data_GO.db_CC_4to6, "GSEA data/GSEA_data_GO.db_CC_4to6.gmt")
## Ran each of these on voom and lnHMdisp brca datasets (so 24 runs altogether).
## Import results, look at stats and test out rrvgo ####
folders <- c(
"all_full_lnHMdisp.brca.GseaPreranked.1594381710427",
"all_full_voom.brca.GseaPreranked.1594382319270",
"all_levels_3to8_lnHMdisp.brca.GseaPreranked.1594378947490",
"all_levels_3to8_voom.brca.GseaPreranked.1594379546460",
"all_levels_4to6_lnHMdisp.brca.GseaPreranked.1594381108970",
"all_levels_4to6_voom.brca.GseaPreranked.1594380594371",
"BP_full_lnHMdisp.brca.GseaPreranked.1594367212385",
"BP_full_voom.brca.GseaPreranked.1594368037535",
"BP_levels_3to8_lnHMdisp.brca.GseaPreranked.1594364121920",
"BP_levels_3to8_voom.brca.GseaPreranked.1594364995851",
"BP_levels_4to6_lnHMdisp.brca.GseaPreranked.1594366506792",
"BP_levels_4to6_voom.brca.GseaPreranked.1594365869348",
"CC_full_lnHMdisp.brca.GseaPreranked.1594377336137",
"CC_full_voom.brca.GseaPreranked.1594377984307",
"CC_levels_3to8_lnHMdisp.brca.GseaPreranked.1594376754526",
"CC_levels_3to8_voom.brca.GseaPreranked.1594376104051",
"CC_levels_4to6_lnHMdisp.brca.GseaPreranked.1594376860767",
"CC_levels_4to6_voom.brca.GseaPreranked.1594376929826",
"MF_full_lnHMdisp.brca.GseaPreranked.1594378816723",
"MF_full_voom.brca.GseaPreranked.1594378730650",
"MF_levels_3to8_lnHMdisp.brca.GseaPreranked.1594378455073",
"MF_levels_3to8_voom.brca.GseaPreranked.1594378303515",
"MF_levels_4to6_lnHMdisp.brca.GseaPreranked.1594378560064",
"MF_levels_4to6_voom.brca.GseaPreranked.1594378628283"
)
files <- c(
"gsea_report_for_na_pos_1594381710427.xls",
"gsea_report_for_na_pos_1594382319270.xls",
"gsea_report_for_na_pos_1594378947490.xls",
"gsea_report_for_na_pos_1594379546460.xls",
"gsea_report_for_na_pos_1594381108970.xls",
"gsea_report_for_na_pos_1594380594371.xls",
"gsea_report_for_na_pos_1594367212385.xls",
"gsea_report_for_na_pos_1594368037535.xls",
"gsea_report_for_na_pos_1594364121920.xls",
"gsea_report_for_na_pos_1594364995851.xls",
"gsea_report_for_na_pos_1594366506792.xls",
"gsea_report_for_na_pos_1594365869348.xls",
"gsea_report_for_na_pos_1594377336137.xls",
"gsea_report_for_na_pos_1594377984307.xls",
"gsea_report_for_na_pos_1594376754526.xls",
"gsea_report_for_na_pos_1594376104051.xls",
"gsea_report_for_na_pos_1594376860767.xls",
"gsea_report_for_na_pos_1594376929826.xls",
"gsea_report_for_na_pos_1594378816723.xls",
"gsea_report_for_na_pos_1594378730650.xls",
"gsea_report_for_na_pos_1594378455073.xls",
"gsea_report_for_na_pos_1594378303515.xls",
"gsea_report_for_na_pos_1594378560064.xls",
"gsea_report_for_na_pos_1594378628283.xls"
)
analyses <- c(
"all_full_lnHMdisp",
"all_full_voom",
"all_3to8_lnHMdisp",
"all_3to8_voom",
"all_4to6_lnHMdisp",
"all_4to6_voom",
"BP_full_lnHMdisp",
"BP_full_voom",
"BP_3to8_lnHMdisp",
"BP_3to8_voom",
"BP_4to6_lnHMdisp",
"BP_4to6_voom",
"CC_full_lnHMdisp",
"CC_full_voom",
"CC_3to8_lnHMdisp",
"CC_3to8_voom",
"CC_4to6_lnHMdisp",
"CC_4to6_voom",
"MF_full_lnHMdisp",
"MF_full_voom",
"MF_3to8_lnHMdisp",
"MF_3to8_voom",
"MF_4to6_lnHMdisp",
"MF_4to6_voom"
)
for (i in 1:24) {
assign(
analyses[i],
read.delim(
paste0(
"Results/GSEA results June 2020/jul10/",
folders[i],
"/",
files[i]
)
)
)
assign(
analyses[i],
get(analyses[i])[c("NAME", "SIZE", "NES", "NOM.p.val", "FDR.q.val")]
)
}
rm(folders, files, i)
for (i in 1:24) {
assign(
paste0("number_q_0.05_", analyses[i]),
sum(get(analyses[i])$FDR.q.val < 0.05)
)
# assign(
# paste0("number_q_0.01_", analyses[i]),
# sum(get(analyses[i])$FDR.q.val < 0.01)
# )
}
c(number_q_0.05_all_full_lnHMdisp, number_q_0.05_all_full_voom) # 44 746
c(number_q_0.05_all_3to8_lnHMdisp, number_q_0.05_all_3to8_voom) # 38 742
c(number_q_0.05_all_4to6_lnHMdisp, number_q_0.05_all_4to6_voom) # 28 599
c(number_q_0.01_all_full_lnHMdisp, number_q_0.01_all_full_voom) # 5 401
c(number_q_0.01_all_3to8_lnHMdisp, number_q_0.01_all_3to8_voom) # 5 394
c(number_q_0.01_all_4to6_lnHMdisp, number_q_0.01_all_4to6_voom) # 3 313
library(rrvgo)
library(clusterProfiler)
library(GOSemSim)
# simplify() in clusterProfiler only takes output of clusterProfiler enrichment functions
# as input, so easiest to use rrvgo (and no real reason to choose one or the other anyway).
# First need to create similarity matrix using calculateSimMatrix(), which takes a list of
# GO terms, an OrgDb object for an organism, the ontology of interest, and which method to
# use to calculate similarity scores. Then use reduceSimMatrix() with similarity matrix,
# scores and threshold to group terms. Scores should be transformed so that higher is
# better (authors suggest -log, but I don't think it matters as long as the direction is
# right and the order is maintained).
# GOSemSim::godata() us used by default to obtain semdata argument of calculateSimMatrix(),
# an "object with prepared GO DATA for measuring semantic similarity" - i.e. a GOSemSimDATA
# object. Similarity measures are "Resnik", "Lin", "Rel", "Jiang" or "Wang", as implemented
# in GOSemSim.
BP_semdata <- GOSemSim::godata(OrgDb="org.Hs.eg.db", ont="BP")
MF_semdata <- GOSemSim::godata(OrgDb="org.Hs.eg.db", ont="MF")
CC_semdata <- GOSemSim::godata(OrgDb="org.Hs.eg.db", ont="CC")
# Need to map GO terms from GSEA back to ... whatever list of terms GOSemSim/rrvgo use.
# That is presumably whatever comes from the OrgDb object, in this case org.Hs.eg.db, which
# has "GOSOURCEURL: http://current.geneontology.org/ontology/go-basic.obo".
head(keys(org.Hs.eg.db, keytype="GO"))
head(keys(org.Hs.eg.db, keytype="GOALL"))
# Not sure what the difference is between these, but GOALL contains all entries in GO.
# Both are GO IDs rather than names, so looks like I'll need to map MSigDB GO terms back to
# IDs from GO itself.
# Get GO terms and IDs directly downloaded from GO:
go <- read.delim("GSEA data/go.obo", stringsAsFactors=F)
go_names <- go[grep("name:", go[, 1]), ]
go_names <- gsub("name: ", "", go_names[1:47358])
go_ids <- go[grep("^id:", go[, 1]), ]
go_ids <- gsub("id: ", "", go_ids[1:47358])
go_table <- data.frame(go_ids, go_names)
go_table <- go_table[-grep("^obsolete", go_table$go_names), ]
names(go_table) <- c("id", "term")
# This version is from 2020-06-01. org.Hs.eg.db has GOSOURCEDATE 2020-05-02. Doesn't seem
# to be an obvious source to download old versions from, so I'll just see how well the two
# versions match for how and hope that's ok.
# Manipulate terms to allow matching with MSigDB terms:
# Add column with manipulated terms for matching to MSigDB terms
go_term_stripped <- toupper(go_table$term)
# Remove brackets:
go_term_stripped <- gsub("\\(", "", gsub("\\)", "", gsub("\\[", "", gsub("\\]", "", go_term_stripped))))
# Remove commas and points:
go_term_stripped <- gsub("\\.", "", gsub(",", "", go_term_stripped))
# Remove spaces and primes:
go_term_stripped <- gsub(" ", "", gsub("'", "", go_term_stripped))
# Remove hyphens and forward slashes:
go_term_stripped <- gsub("-", "", gsub("/", "", go_term_stripped))
# Change + to "PLUS":