Skip to content

Commit

Permalink
Version 1.0.8. Improving error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekGierlinski committed Apr 24, 2024
1 parent df1fcb7 commit 104b0d4
Show file tree
Hide file tree
Showing 29 changed files with 227 additions and 214 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fenr
Title: Fast functional enrichment for interactive applications
Version: 1.1.7
Version: 1.1.8
Authors@R: person(
given = "Marek",
family = "Gierlinski",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(functional_enrichment)
export(get_feature_terms)
export(get_term_features)
export(prepare_for_enrichment)
export(remove_cache)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.count)
importFrom(assertthat,is.string)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@
## Version 1.0.7

- Improved error handling with unresponsive servers - timeouts are now handled gracefully


## Version 1.0.8

- Further improving error handling, making sure `assert_url_path()` handles timeouts properly
- Introduced on_error = "ignore" for test purposes

9 changes: 4 additions & 5 deletions R/bioplanet.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ get_bioplanet_pathway_file <- function() {
#' (NCBI gene ID, gene symbol and pathway ID) from BioPlanet.
#'
#' @param use_cache Logical, if TRUE, the remote file will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#' @return A list with \code{terms} and \code{mapping} tibbles.
#' @export
#' @examples
#' bioplanet_data <- fetch_bp(on_error = "warn")
fetch_bp <- function(use_cache = TRUE, on_error = c("stop", "warn")) {
fetch_bp <- function(use_cache = TRUE, on_error = c("stop", "warn", "ignore")) {
on_error <- match.arg(on_error)

# Binding variables from non-standard evaluation locally
Expand Down
9 changes: 7 additions & 2 deletions R/caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ cached_url_path <- function(rname, fpath, use_cache) {
#' Remove all cache
#'
#' This function will remove all cached data used by `fenr`. The user will be
#' prompted for confirmation. Use with caution!
#' prompted for confirmation. Use only when you suspect the cache was corrupted.
#' Use with caution!
#'
#' @param ask Logical, whether to ask user for confirmation.
#' @return TRUE if successfully removed.
#' @noRd
#' @export
#' @examples
#' \dontrun{
#' remove_cache()
#' }
remove_cache <- function(ask = TRUE) {
cache <- cache_location()
bfc <- BiocFileCache::BiocFileCache(cache, ask = FALSE)
Expand Down
68 changes: 32 additions & 36 deletions R/go.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ extract_obo_terms <- function(parsed) {
#' Download GO term descriptions
#'
#' @param use_cache Logical, if TRUE, the remote file will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `FALSE`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble with term_id and term_name.
#' @noRd
fetch_go_terms <- function(use_cache, on_error = "stop") {
fetch_go_terms <- function(use_cache, on_error) {
obo_file <- get_go_obo_file()
if(!assert_url_path(obo_file, on_error))
return(NULL)
Expand All @@ -142,16 +141,15 @@ fetch_go_terms <- function(use_cache, on_error = "stop") {
#' without extension (e.g. for a file \file{goa_chicken.gaf} the designation is
#' \file{goa_chicken}).
#'
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble with columns \code{species} and \code{designation}.
#' @export
#' @examples
#' go_species <- fetch_go_species(on_error = "warn")
fetch_go_species <- function(on_error = c("stop", "warn")) {
fetch_go_species <- function(on_error = c("stop", "warn", "ignore")) {
on_error <- match.arg(on_error)
# Binding variables from non-standard evaluation locally
species <- designation <- `Species/Database` <- File <- NULL
Expand Down Expand Up @@ -184,14 +182,13 @@ fetch_go_species <- function(on_error = c("stop", "warn")) {
#' \url{http://current.geneontology.org/annotations}. Examples are
#' \file{goa_human} for human, \file{mgi} for mouse or \file{sgd} for yeast.
#' @param use_cache Logical, if TRUE, the remote file will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble with columns \code{gene_symbol}, \code{uniprot_id} and \code{term_id}.
#' @noRd
fetch_go_genes_go <- function(species, use_cache, on_error = "stop") {
fetch_go_genes_go <- function(species, use_cache, on_error) {
# Binding variables from non-standard evaluation locally
gene_id <- db_object_synonym <- symbol <- NULL
db_id <- go_term <- NULL
Expand Down Expand Up @@ -228,23 +225,24 @@ fetch_go_genes_go <- function(species, use_cache, on_error = "stop") {
#' species can be obtained using \code{fetch_go_species} - column
#' \code{designation}.
#' @param use_cache Logical, if TRUE, the remote file will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A list with \code{terms} and \code{mapping} tibbles.
#' @importFrom assertthat assert_that
#' @noRd
fetch_go_from_go <- function(species, use_cache, on_error = "stop") {
fetch_go_from_go <- function(species, use_cache, on_error) {
assert_that(!missing(species), msg = "Argument 'species' is missing.")
assert_species(species, "fetch_go_species", on_error)

mapping <- fetch_go_genes_go(species = species, use_cache = use_cache, on_error = on_error)
if(is.null(mapping))
return(NULL)

terms <- fetch_go_terms(use_cache = use_cache)
terms <- fetch_go_terms(use_cache = use_cache, on_error = on_error)
if(is.null(terms))
return(NULL)

list(
terms = terms,
Expand All @@ -261,15 +259,14 @@ fetch_go_from_go <- function(species, use_cache, on_error = "stop") {
#' biomaRt::useEnsembl(biomart = "ensembl"), followed by
#' biomaRt::listDatasets(mart).
#' @param use_cache Logical, if TRUE, the remote data will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble with columns \code{gene_id}, \code{gene_symbol} and
#' \code{term_id}.
#' @noRd
fetch_go_genes_bm <- function(dataset, use_cache = TRUE, on_error = c("stop", "warn")) {
fetch_go_genes_bm <- function(dataset, use_cache, on_error) {
xml <- get_biomart_xml(dataset) |>
stringr::str_replace_all("\\s", "%20")
req <- paste0(get_biomart_url(), "/biomart/martservice?query=", xml)
Expand Down Expand Up @@ -298,15 +295,14 @@ fetch_go_genes_bm <- function(dataset, use_cache = TRUE, on_error = c("stop", "w
#' biomaRt::useEnsembl(biomart = "ensembl"), followed by
#' biomaRt::listDatasets(mart).
#' @param use_cache Logical, if TRUE, the remote file will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @importFrom assertthat assert_that is.string
#' @return A list with \code{terms} and \code{mapping} tibbles.
#' @noRd
fetch_go_from_bm <- function(dataset, use_cache = TRUE, on_error = c("stop", "warn")) {
fetch_go_from_bm <- function(dataset, use_cache, on_error) {
assert_that(!missing(dataset), msg = "Argument 'dataset' is missing.")
assert_that(is.string(dataset))

Expand Down Expand Up @@ -353,10 +349,9 @@ fetch_go_from_bm <- function(dataset, use_cache = TRUE, on_error = c("stop", "wa
#' biomaRt::useEnsembl(biomart = "ensembl"), followed by
#' biomaRt::listDatasets(mart).
#' @param use_cache Logical, if TRUE, the remote data will be cached locally.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A list with \code{terms} and \code{mapping} tibbles.
#' @export
Expand All @@ -366,7 +361,8 @@ fetch_go_from_bm <- function(dataset, use_cache = TRUE, on_error = c("stop", "wa
#' go_data_ensembl <- fetch_go(dataset = "scerevisiae_gene_ensembl", on_error = "warn")
#' # Fetch GO data from Gene Ontology
#' go_data_go <- fetch_go(species = "sgd", on_error = "warn")
fetch_go <- function(species = NULL, dataset = NULL, use_cache = TRUE, on_error = c("stop", "warn")) {
fetch_go <- function(species = NULL, dataset = NULL, use_cache = TRUE,
on_error = c("stop", "warn", "ignore")) {
on_error <- match.arg(on_error)

assert_that(!(is.null(species) & is.null(dataset)),
Expand Down
6 changes: 4 additions & 2 deletions R/interactive_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#' @return A list of objects containing functional terms for GO and Reactome.
#' @export
#' @examples
#' \dontrun{
#' data(yeast_de)
#' term_data <- fetch_terms_for_example(yeast_de)
#' }
fetch_terms_for_example <- function(de) {
# All gene background
all_genes <- de$gene_id
Expand All @@ -19,7 +21,7 @@ fetch_terms_for_example <- function(de) {

# load Reactome pathways
message("Fetching Reactome data\n")
re <- fetch_reactome("Saccharomyces cerevisiae", on_error = "warn")
re <- fetch_reactome("Saccharomyces cerevisiae")
re_data <- prepare_for_enrichment(re$terms, re$mapping, all_genes, feature_name = "gene_id")

# Put all functional term data in one structure; Shiny app will access
Expand Down Expand Up @@ -173,9 +175,9 @@ main_plot <- function(de, input) {
#' @importFrom assertthat assert_that
#' @export
#' @examples
#' \dontrun{
#' data(yeast_de)
#' term_data <- fetch_terms_for_example(yeast_de)
#' if(interactive()) {
#' enrichment_interactive(yeast_de, term_data)
#' }
enrichment_interactive <- function(de, term_data) {
Expand Down
34 changes: 15 additions & 19 deletions R/kegg.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ get_kegg_url <- function() {

#' Find all species available from KEGG
#'
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble, in which column \code{designation} contains species
#' designations used in function \code{fetch_kegg}.
#' @export
#' @examples
#' spe <- fetch_kegg_species(on_error = "warn")
fetch_kegg_species <- function(on_error = c("stop", "warn")) {
fetch_kegg_species <- function(on_error = c("stop", "warn", "ignore")) {
on_error <- match.arg(on_error)

resp <- http_request(get_kegg_url(), "list/organism")
Expand All @@ -34,10 +33,9 @@ fetch_kegg_species <- function(on_error = c("stop", "warn")) {
#' Download pathway data from KEGG
#'
#' @param species A valid species designation used by KEGG.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A tibble with columns \code{gene_id} and \code{term_id}.
#' @noRd
Expand Down Expand Up @@ -118,14 +116,13 @@ parse_kegg_genes <- function(s) {
#' @param pathways A character vector with KEGG pathways
#' @param batch_size Number of pathways sent to KEGG database in one query. The
#' maximum allowed is 10.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#' @importFrom assertthat assert_that
#' @return A tibble with columns \code{gene_id} and \code{term_id}
#' @noRd
fetch_kegg_mapping <- function(pathways, batch_size, on_error = "stop") {
fetch_kegg_mapping <- function(pathways, batch_size, on_error) {
assert_that(is.character(pathways))
batches <- split(pathways, ceiling(seq_along(pathways) / batch_size))

Expand Down Expand Up @@ -167,17 +164,16 @@ fetch_kegg_mapping <- function(pathways, batch_size, on_error = "stop") {
#' full list of available KEGG species can be found by using \code{fetch_kegg_species}.
#' @param batch_size Number of pathways sent to KEGG database in one query. The
#' maximum allowed is 10.
#' @param on_error A character vector specifying the error handling method. It
#' can take values `"stop"` or `"warn"`. The default is `"stop"`. `"stop"`
#' will halt the function execution and throw an error, while `"warn"` will
#' issue a warning and return `NULL`.
#' @param on_error A character string indicating the error handling strategy:
#' either "stop" to halt execution, "warn" to issue a warning and return
#' `NULL` or "ignore" to return `NULL` without warnings. Defaults to "stop".
#'
#' @return A list with \code{terms} and \code{mapping} tibbles.
#' @importFrom assertthat assert_that is.count
#' @export
#' @examples
#' kegg_data <- fetch_kegg("mge", on_error = "warn")
fetch_kegg <- function(species, batch_size = 10, on_error = c("stop", "warn")) {
fetch_kegg <- function(species, batch_size = 10, on_error = c("stop", "warn", "ignore")) {
on_error <- match.arg(on_error)

assert_that(!missing(species), msg = "Argument 'species' is missing.")
Expand Down
Loading

0 comments on commit 104b0d4

Please sign in to comment.