/
enrichr.R
829 lines (792 loc) · 35.8 KB
/
enrichr.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
#' Retrieve a List of available libraries from Enrichr
#'
#' This function will retrieve a list of available libraries in Enrichr with
#' their statistics. And by default, will save those names as a global option
#' ("rba_enrichr_libs") to be available for other Enrichr functions that
#' internally require the names of Enrichr libraries.
#'
#' You should call this function once per R session with the argument
#' 'store_in_options = TRUE' before using \code{\link{rba_enrichr_enrich}}
#' or \code{\link{rba_enrichr}}.
#' \cr Nevertheless, rbioapi will do this for you in the background at the
#' first time you call any function which requires this.
#' \cr Note that using \code{\link{rba_enrichr}} is a more convenient way to
#' automatically perform this and other required function calls to enrich
#' your input gene-set.
#'
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/datasetStatistics"
#'
#' @param store_in_options logical: (default = TRUE) Should a list of available
#' Enrichr libraries be saved as a global option?
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return A data frame with the names of available library in Enrichr and their
#' statistics.
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \donttest{
#' rba_enrichr_libs()
#' }
#'
#' @family "Enrichr"
#' @seealso \code{\link{rba_enrichr}}
#' @export
rba_enrichr_libs <- function(store_in_options = FALSE,
organism = "human",
...){
## Load Global Options
.rba_ext_args(...)
## Check User-input Arguments
.rba_args(cons = list(list(arg = "store_in_options",
class = "logical"),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
.msg("Retrieving List of available libraries and statistics from Enrichr %s.",
organism)
## Build Function-Specific Call
parser_input <- list("json->list_simp",
function(x) {x[[1]]})
input_call <- .rba_httr(httr = "get",
url = .rba_stg("enrichr", "url"),
path = paste0(.rba_stg("enrichr", "pth", organism),
"datasetStatistics"),
accept = "application/json",
parser = parser_input,
save_to = .rba_file("enrichr_info.json"))
## Call API
final_output <- .rba_skeleton(input_call)
## Save Library Names as Global Options
if (isTRUE(store_in_options) && utils::hasName(final_output, "libraryName")) {
options(rba_enrichr_libs = final_output[["libraryName"]])
}
return(final_output)
}
#' Upload Your Gene-List to Enrichr
#'
#' Prior to perform enrichment, Enrichr requires you to upload your gene-list
#' and retrieve a 'user list ID'.
#'
#' Note that using \code{\link{rba_enrichr}} is a more convenient way to
#' automatically perform this and other required function calls to
#' perform enrichment analysis on your input gene-set.
#'
#' @section Corresponding API Resources:
#' "POST https://maayanlab.cloud/Enrichr/addList"
#'
#' @param gene_list A vector with Entrez gene symbols.
#' @param description (optional) A name or description to be associated with your
#' uploaded gene-set to Enrichr servers.
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return A list with two unique IDs for your uploaded gene sets.
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \donttest{
#' rba_enrichr_add_list(gene_list = c("TP53", "TNF", "EGFR"),
#' description = "tumoral genes")
#' }
#'
#' @family "Enrichr"
#' @seealso \code{\link{rba_enrichr}}
#' @export
rba_enrichr_add_list <- function(gene_list,
description = NULL,
organism = "human",
...){
## Load Global Options
.rba_ext_args(...)
## Check User-input Arguments
.rba_args(cons = list(list(arg = "gene_list",
class = "character"),
list(arg = "description",
class = "character"),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
.msg("Uploading %s gene symbols to Enrichr %s.",
length(gene_list), organism)
## Build POST API Request's URL
call_body <- .rba_query(init = list("format" = "text",
"list" = paste(unique(gene_list),
collapse = "\n")),
list("description",
!is.null(description),
description))
## Build Function-Specific Call
input_call <- .rba_httr(httr = "post",
url = .rba_stg("enrichr", "url"),
path = paste0(.rba_stg("enrichr", "pth", organism),
"addList"),
body = call_body,
accept = "application/json",
parser = "json->list_simp",
save_to = .rba_file("enrichr_add_list.json"))
## Call API
final_output <- .rba_skeleton(input_call)
return(final_output)
}
#' View an Uploaded Gene List
#'
#' Retrieve a list of uploaded genes under a 'user list ID'.
#'
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/view"
#
#' @param user_list_id a user_list_id returned to you after uploading a gene
#' list using \code{\link{rba_enrichr_add_list}}
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return A list containing the genes and description available under the
#' supplied user_list_id
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \dontrun{
#' rba_enrichr_view_list(user_list_id = 11111)
#' }
#'
#' @family "Enrichr"
#' @export
rba_enrichr_view_list <- function(user_list_id,
organism = "human",
...){
## Load Global Options
.rba_ext_args(...)
## Check User-input Arguments
.rba_args(cons = list(list(arg = "user_list_id",
class = c("numeric", "integer"),
len = 1),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
.msg("Retrieving the gene list under the ID %s from Enrichr %s.",
user_list_id, organism)
## Build GET API Request's query
call_query <- list("userListId" = user_list_id)
## Build Function-Specific Call
input_call <- .rba_httr(httr = "get",
url = .rba_stg("enrichr", "url"),
path = paste0(.rba_stg("enrichr", "pth", organism),
"view"),
query = call_query,
accept = "application/json",
parser = "json->list_simp",
save_to = .rba_file(sprintf("enrichr_view_list_%s.json",
user_list_id)))
## Call API
final_output <- .rba_skeleton(input_call)
return(final_output)
}
#' Internal function for rba_enrichr_enrich
#'
#' This is an internal helper function which will retrieve the enrichment
#' results of one_user_list id against one library name
#'
#' The function will be called within \code{\link{rba_enrichr_enrich}} and will
#' handle API requests to the server.
#'
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/enrich"
#'
#' @param user_list_id An ID returned to you after uploading a gene
#' list using \code{\link{rba_enrichr_add_list}}
#' @param gene_set_library a valid gene-set library name which exists
#' in the results retrieved via \code{\link{rba_enrichr_libs}}.
#' @param save_name default raw file name
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return A data frame with the enrichment results of the supplied user_list_id
#' against the gene_set_library
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @noRd
.rba_enrichr_enrich_internal <- function(user_list_id,
gene_set_library,
save_name,
organism = "human",
sleep_time = 0,
...){
## Load Global Options
.rba_ext_args(...)
## Build GET API Request's query
call_query <- list("userListId" = user_list_id,
"backgroundType" = gene_set_library)
## Build Function-Specific Call
parser_input <- function(x) {
httr::content(x,
as = "text",
type = "text/tab-separated-values",
encoding = "UTF-8")
}
input_call <- .rba_httr(httr = "get",
.rba_stg("enrichr", "url"),
path = paste0(.rba_stg("enrichr", "pth", organism),
"export"),
query = call_query,
httr::accept("text/tab-separated-values"),
parser = parser_input,
save_to = .rba_file(save_name))
## Call API
Sys.sleep(sleep_time)
final_output_raw <- .rba_skeleton(input_call)
final_output <- try(utils::read.delim(textConnection(final_output_raw),
sep = "\t", header = TRUE,
stringsAsFactors = FALSE),
silent = !get("diagnostics"))
if (is.data.frame(final_output)) {
return(final_output)
} else {
error_message <- paste0("Error: Couldn't parse the server response for the requested Enrichr analysis.",
"Please try again. If the problem persists, kindly report the issue to us.",
"The server's raw response is:",
as.character(final_output_raw),
collapse = "\n")
if (isTRUE(get("skip_error"))) {
return(error_message)
} else {
stop(error_message, call. = get("diagnostics"))
}
}
}
#' Get Enrichr Enrichment Results
#'
#' This function which will retrieve the enrichment results of your
#' supplied gene-list id against one or multiple Enrichr libraries.
#'
#' Note that using \code{\link{rba_enrichr}} is a more convenient way to
#' automatically perform this and other required function calls to
#' perform enrichment analysis on your input gene-set.
#'
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/enrich"
#'
#' @param user_list_id An ID returned to you after uploading a gene
#' list using \code{\link{rba_enrichr_add_list}}
#' @param gene_set_library One of the:
#' \enumerate{
#' \item "all" to select all of the available Enrichr gene-set libraries.
#' \item A gene-set library name existed in the results
#' retrieved via \code{\link{rba_enrichr_libs}}
#' \item If regex_library_name = TRUE, A partially-matching name a regex
#' pattern that correspond to one or more of Enrichr library names.
#' }
#' @param regex_library_name logical: if TRUE (default) the supplied
#' gene_set_library will be regarded as a regex or partially matching name. if
#' FALSE, gene_set_library will be considered exact match.
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param progress_bar logical: In case of selecting multiple Enrichr
#' libraries, should a progress bar be displayed?
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return A list containing data frames of the enrichment results of your
#' supplied gene-list against the selected Enrichr libraries.
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \dontrun{
#' rba_enrichr_enrich(user_list_id = "11111")
#' }
#' \dontrun{
#' rba_enrichr_enrich(user_list_id = "11111",
#' gene_set_library = "GO_Molecular_Function_2017",
#' regex_library_name = FALSE)
#' }
#' \dontrun{
#' rba_enrichr_enrich(user_list_id = "11111",
#' gene_set_library = "go",
#' regex_library_name = TRUE)
#' }
#'
#' @family "Enrichr"
#' @seealso \code{\link{rba_enrichr}}
#' @export
rba_enrichr_enrich <- function(user_list_id,
gene_set_library = "all",
regex_library_name = TRUE,
organism = "human",
progress_bar = TRUE,
...){
## Load Global Options
.rba_ext_args(...)
## get a list of available libraries
if (is.null(getOption("rba_enrichr_libs"))) {
.msg("Calling rba_enrichr_libs() to get the names of available Enrichr %s libraries.",
organism)
enrichr_libs <- rba_enrichr_libs(store_in_options = TRUE)
if (utils::hasName(enrichr_libs, "libraryName")) {
enrichr_libs <- enrichr_libs[["libraryName"]]
}
} else {
enrichr_libs <- getOption("rba_enrichr_libs")
}
if (length(enrichr_libs) <= 1) {
no_lib_msg <- "Error: Couldn't fetch available Enrichr libraries. Please manually run `rba_enrichr_libs(store_in_options = TRUE)`."
if (isTRUE(get("skip_error"))) {
return(no_lib_msg)
} else {
stop(no_lib_msg, call. = get("diagnostics"))
}
}
## handle different gene_set_library input situations
if (length(gene_set_library) > 1) {
run_mode <- "multiple"
} else if (gene_set_library == "all") {
run_mode <- "multiple"
gene_set_library <- enrichr_libs
} else {
if (isFALSE(regex_library_name)) {
run_mode <- "single"
} else {
gene_set_library <- grep(gene_set_library,
enrichr_libs,
ignore.case = TRUE, value = TRUE, perl = TRUE)
#check the results of regex
if (length(gene_set_library) == 0) {
if (isTRUE(get("skip_error"))) {
return("Your regex pattern did not match any Enrichr library name.")
} else {
stop("Your regex pattern did not match any Enrichr library name.",
call. = get("diagnostics"))
}
} else if (length(gene_set_library) == 1) {
run_mode <- "single"
} else if (length(gene_set_library) > 1) {
run_mode <- "multiple"
}
}
} #end of if length(gene_set_library) > 1
## Check User-input Arguments
.rba_args(cons = list(list(arg = "user_list_id",
class = c("numeric", "integer"),
len = 1),
list(arg = "gene_set_library",
class = "character",
val = enrichr_libs),
list(arg = "progress_bar",
class = "logical"),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
## call Enrichr API
if (run_mode == "single") {
.msg("Performing enrichment analysis on gene-list %s against Enrichr %s library: %s.",
user_list_id, organism, gene_set_library)
final_output <- .rba_enrichr_enrich_internal(user_list_id = user_list_id,
gene_set_library = gene_set_library,
save_name = sprintf("enrichr_%s_%s.json",
user_list_id,
gene_set_library),
...)
return(final_output)
} else {
.msg("Performing enrichment analysis on gene-list %s using multiple Enrichr %s libraries.",
user_list_id, organism)
.msg(paste0("Note: You have selected '%s' Enrichr %s libraries. Note that for ",
"each library, a separate call should be sent to Enrichr server. ",
"Thus, this could take a while depending on the number of selected ",
"libraries and your network connection."),
length(gene_set_library), organism)
## initiate progress bar
if (isTRUE(progress_bar)) {
pb <- utils::txtProgressBar(min = 0,
max = length(gene_set_library),
style = 3)
}
final_output <- lapply(gene_set_library,
function(x){
lib_enrich_res <- .rba_enrichr_enrich_internal(user_list_id = user_list_id,
gene_set_library = x,
save_name = sprintf("enrichr_%s_%s.json",
user_list_id,
x),
sleep_time = 0.5,
...)
#advance the progress bar
if (isTRUE(progress_bar)) {
utils::setTxtProgressBar(pb, which(gene_set_library == x))
}
return(lib_enrich_res)
})
if (isTRUE(progress_bar)) {close(pb)}
names(final_output) <- gene_set_library
return(final_output)
}
}
#' Find Enrichr Terms That Contain a Given Gene
#'
#' This function will search the gene and retrieve a list of Enrichr
#' Terms that contains that gene.
#'
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/genemap"
#'
#' @param gene character: An Entrez gene symbol.
#' @param catagorize logical: Should the category informations be included?
#' @param organism (default = "human") Which model organism version of Enrichr
#' to use? Available options are: "human", (H. sapiens & M. musculus),
#' "fly" (D. melanogaster), "yeast" (S. cerevisiae), "worm" (C. elegans)
#' and "fish" (D. rerio).
#' @param ... rbioapi option(s). See \code{\link{rba_options}}'s
#' arguments manual for more information on available options.
#'
#' @return a list containing the search results of your supplied gene.
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \donttest{
#' rba_enrichr_gene_map(gene = "p53")
#' }
#' \donttest{
#' rba_enrichr_gene_map(gene = "p53", catagorize = TRUE)
#' }
#'
#' @family "Enrichr"
#' @export
rba_enrichr_gene_map <- function(gene,
catagorize = FALSE,
organism = "human",
...){
## Load Global Options
.rba_ext_args(...)
## Check User-input Arguments
.rba_args(cons = list(list(arg = "gene",
class = "character",
len = 1),
list(arg = "catagorize",
class = "logical"),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
.msg("Finding terms that contain %s gene: %s.", organism, gene)
## Build GET API Request's query
call_query <- .rba_query(init = list("gene" = gene,
"json" = "true"),
list("setup",
isTRUE(catagorize),
"true"))
## Build Function-Specific Call
input_call <- .rba_httr(httr = "get",
url = .rba_stg("enrichr", "url"),
path = paste0(.rba_stg("enrichr", "pth", organism),
"genemap"),
query = call_query,
accept = "application/json",
parser = "json->list_simp",
save_to = .rba_file("enrichr_gene_map.json"))
## Call API
final_output <- .rba_skeleton(input_call)
return(final_output)
}
#' A One-step Wrapper for Gene-list Enrichment Using Enrichr
#'
#' This function is an easy-to-use wrapper for the multiple function calls
#' necessary to perform enrichment analysis on a given gene-list using Enrichr.
#' see details section for more information.
#'
#' This function will call other rba_enrichr_*** functions with the following
#' order:
#' \enumerate{
#' \item (If neccessary) Call \code{\link{rba_enrichr_libs}} to obtain a list
#' of available libraries in Enrichr.
#' \item Call \code{\link{rba_enrichr_add_list}} to upload your gene-list
#' and obtain a 'user list ID'.
#' \item Call \code{\link{rba_enrichr_enrich}} to perform enrichment analysis
#' on the gene-list against one or multiple Enrichr libraries
#' }
#' @section Corresponding API Resources:
#' "GET https://maayanlab.cloud/Enrichr/datasetStatistics"
#' \cr "POST https://maayanlab.cloud/Enrichr/addList"
#' \cr "GET https://maayanlab.cloud/Enrichr/enrich"
#'
#' @inheritParams rba_enrichr_add_list
#' @inheritParams rba_enrichr_enrich
#'
#' @return A list containing data frames of the enrichment results of your
#' supplied gene-list against the selected Enrichr libraries.
#'
#' @references \itemize{
#' \item Chen, E.Y., Tan, C.M., Kou, Y. et al. Enrichr: interactive and
#' collaborative HTML5 gene list enrichment analysis tool. Bioinformatics
#' 14, 128 (2013). https://doi.org/10.1186/1471-2105-14-128
#' \item Maxim V. Kuleshov, Matthew R. Jones, Andrew D. Rouillard, Nicolas
#' F. Fernandez, Qiaonan Duan, Zichen Wang, Simon Koplev, Sherry L. Jenkins,
#' Kathleen M. Jagodnik, Alexander Lachmann, Michael G. McDermott,
#' Caroline D. Monteiro, Gregory W. Gundersen, Avi Ma’ayan, Enrichr: a
#' comprehensive gene set enrichment analysis web server 2016 update,
#' Nucleic Acids Research, Volume 44, Issue W1, 8 July 2016, Pages W90–W97,
#' https://doi.org/10.1093/nar/gkw377
#' \item Xie, Z., Bailey, A., Kuleshov, M. V., Clarke, D. J. B.,
#' Evangelista, J. E., Jenkins, S. L., Lachmann, A., Wojciechowicz, M. L.,
#' Kropiwnicki, E., Jagodnik, K. M., Jeon, M., & Ma’ayan, A. (2021). Gene
#' set knowledge discovery with Enrichr. Current Protocols, 1, e90.
#' doi: 10.1002/cpz1.90
#' \item \href{https://maayanlab.cloud/Enrichr/help#api}{Enrichr API
#' Documentation}
#' \item \href{https://maayanlab.cloud/Enrichr/help#terms}{Citations note
#' on Enrichr website}
#' }
#'
#' @examples
#' \dontrun{
#' rba_enrichr(gene_list = c("TP53", "TNF", "EGFR"))
#' }
#' \donttest{
#' rba_enrichr(gene_list = c("TP53", "TNF", "EGFR"),
#' gene_set_library = "GO_Molecular_Function_2017",
#' regex_library_name = FALSE)
#' }
#' \donttest{
#' rba_enrichr(gene_list = c("TP53", "TNF", "EGFR"),
#' gene_set_library = "go",
#' regex_library_name = TRUE)
#' }
#'
#' @family "Enrichr"
#' @export
rba_enrichr <- function(gene_list,
description = NULL,
gene_set_library = "all",
regex_library_name = TRUE,
organism = "human",
progress_bar = FALSE,
...) {
## Load Global Options
.rba_ext_args(...)
## Check User-input Arguments
.rba_args(cons = list(list(arg = "gene_list",
class = "character"),
list(arg = "description",
class = "character"),
list(arg = "regex_library_name",
class = "logical"),
list(arg = "progress_bar",
class = "logical"),
list(arg = "organism",
class = "character",
no_null = TRUE,
val = c("human", "fly", "yeast", "worm", "fish"))
))
.msg("--Step 1/3:")
enrichr_libs <- rba_enrichr_libs(store_in_options = TRUE)
if (utils::hasName(enrichr_libs, "libraryName")) {
enrichr_libs <- enrichr_libs[["libraryName"]]
}
if (exists("enrichr_libs") && length(enrichr_libs) <= 1) { # Halt at step 1
no_lib_msg <- paste0("Error: Couldn't fetch available Enrichr libraries. Please manually run `rba_enrichr_libs(store_in_options = TRUE)`.",
"If the problem persists, kindly report this issue to us. The error message was: ",
try(enrichr_libs),
collapse = "\n")
if (isTRUE(get("skip_error"))) {
.msg(no_lib_msg)
return(no_lib_msg)
} else {
stop(no_lib_msg, call. = get("diagnostics"))
}
} else { # Proceed to step 2
.msg("--Step 2/3:")
Sys.sleep(2)
list_id <- rba_enrichr_add_list(gene_list = gene_list,
description = description,
...)
if (exists("list_id") && utils::hasName(list_id, "userListId")) { # proceed to step 3
.msg("--Step 3/3:")
Sys.sleep(2)
enriched <- rba_enrichr_enrich(user_list_id = list_id$userListId,
gene_set_library = gene_set_library,
regex_library_name = regex_library_name,
progress_bar = progress_bar,
...)
if (exists("enriched") && (is.list(enriched) || is.data.frame(enriched))) { # Finish step 3
return(enriched)
} else { # Halt at step 3
no_enriched_msg <- paste0("Error: Couldn't retrieve the submitted Enrichr analysis request.",
"Please retry or manually run the required steps as demonstrated in the `Enrichr & rbioapi` vignette article, section `Approach 2: Going step-by-step`",
"If the problem persists, kindly report this issue to us. The error message was: ",
try(enriched),
collapse = "\n")
if (isTRUE(get("skip_error"))) {
.msg(no_enriched_msg)
return(no_enriched_msg)
} else {
stop(no_enriched_msg, call. = get("diagnostics"))
}
}
} else { # Halt at step 2
no_list_msg <- paste0("Error: Couldn't upload your genes list to Enrichr.",
"Please retry or manually run the required steps as demonstrated in the `Enrichr & rbioapi` vignette article, section `Approach 2: Going step-by-step`",
"If the problem persists, kindly report this issue to us. The error message was: ",
try(list_id),
collapse = "\n")
if (isTRUE(get("skip_error"))) {
.msg(no_list_msg)
return(no_list_msg)
} else {
stop(no_list_msg, call. = get("diagnostics"))
}
}
}
}