---
title: "1: rbioapi: User-Friendly R Interface to Biologic Web Services' API"
author: "Moosa Rezwani"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: true
    anchor_sections: true
    number_sections: true
    self_contained: true
    dev: png
    encoding: 'UTF-8'

vignette: >
  %\VignetteIndexEntry{1: rbioapi: User-Friendly R Interface to Biologic Web Services' API}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE, setup, echo=FALSE, results="hide"}
knitr::opts_chunk$set(echo = TRUE,
                      eval = TRUE,
                      message = FALSE,
                      warning = FALSE,
                      collapse = TRUE,
                      tidy = FALSE,
                      cache = FALSE,
                      dev = "png",
                      comment = "#>")
library(rbioapi)
rba_options(timeout = 600)
```

------------------------------------------------------------------------

# What does rbioapi do? {#what-does-rbioapi-do .heading2}

Currently fully supports miEAA, PANTHER, Reactome, STRING, and UniProt!

rbioapi is an interface to Biological databases and web services. The goal of rbioapi is to provide a user-friendly and consistent interface to biological databases and services; It is designed in a way that insulates the user from technicalities when it comes to using API services and creates a unified and easy-to-implement tool to connect to biological databases and services.

With rbioapi, You are not required to have any prior technical knowledge. **Just fill in a function's arguments and the rest is internally handled for you**.

------------------------------------------------------------------------

# What is Supported by rbioapi? {#what-is-supported-by-rbioapi .heading2}

rbioapi is dedicated to **Biological or Medical** databases and web services. Currently, rbioapi supports and covers every API resources in the following services: (in alphabetical order!):

1.  [miEAA](https://ccb-compute2.cs.uni-saarland.de/mieaa2 "miRNA Enrichment Analysis and Annotation Tool (miEAA)") ([rbioapi vignette article](rbioapi_mieaa.html "rbioapi & miEAA vignette article"))
2.  [PANTHER](http://www.pantherdb.org "Protein Analysis THrough Evolutionary Relationships (PANTHER)") ([rbioapi vignette article](rbioapi_panther.html "rbioapi & PANTHER vignette article"))
3.  [Reactome](https://reactome.org/) ([rbioapi vignette article](rbioapi_reactome.html "rbioapi & Reactome vignette article"))
4.  [STRING](https://string-db.org/ "STRING: Protein-Protein Interaction Networks Functional Enrichment Analysis") ([rbioapi vignette article](rbioapi_string.html "rbioapi & STRING vignette article"))
5.  [UniProt](https://www.uniprot.org "Universal Protein Resource (UniProt)") ([rbioapi vignette article](rbioapi_uniprot.html "rbioapi & UniProt vignette article"))

Each of the services has its dedicated vignette article. However, In this article, I will write about the general framework of rbioapi. Make sure to check the vignette article of each service to learn more about how to use them.

**Note That:** rbioapi is an ongoing project. New databases and services will be implemented periodically in order to gradually make the package as comprehensive as possible. Do you see yourself often using a certain database/service? Feel free to suggest any database/service by creating an issue on our GitHub [repository](https://github.com/moosa-r/ "rbioapi GitHub repositry"). I will appreciate any suggestions.

------------------------------------------------------------------------

# How to install?

You can install the stable released version of rbioapi from [CRAN](https://CRAN.R-project.org) with:

``` {#install_cran .r}
install.packages("rbioapi")
```

However, the CRAN version is released at most once every 1-2 months, You can install the development version from [GitHub](https://github.com/) with:

``` {#install_github .r}
install.packages("devtools")
devtools::install_github("moosa-r/rbioapi")
```

------------------------------------------------------------------------

# Design philosophy of rbioapi {#design-philosophy-of-rbioapi .heading2}

-   The functions' structure should be **consistent across all the databases and services**. There should not be any learning effort when using functions from different services.

-   The interface should be **simple plugging values in a function's arguments** and running the code. No function should explicitly demand the user to run another function beforehand.

-   The functions' names and arguments should be as **faithful** as possible to the original API resources. This is to ensure that the users could conveniently connect the dots between rbioapi and the web service.

-   The user should be completely **insulated from any technicalities**.

-   **Beginner** end of users spectrum should conveniently use rbioapi. For example, to prevent errors, user input arguments will be vigorously checked; Or for example, if any service's resource requires particular input conditions, those conditions will be checked during user's input validation.

-   Produce **Informative messages**, warnings, and error. For example, if a server returns an error in a particular format, convert the server's error response to R error.

------------------------------------------------------------------------

# Naming conventions {#naming-conventions .heading2}

To make the namespace more organized, functions has been named with the following pattern:

> rba\_[service_name]\_[resource_name]

For example, `rba_string_version()` will call [STRING](https://string-db.org/ "STRING: Protein-Protein Interaction Networks Functional Enrichment Analysis")'s version resource.

```{r naming_example, echo=TRUE, message=TRUE}
rba_string_version()
```

Thus, to this version, rbioapi function will have one of the following naming schema:

1.  rba_mieaa\_\*
2.  rba_panther\_\*
3.  rba_reactome\_\*
4.  rba_string\_\*
5.  rba_uniprot\_\*

There are three exceptions: `rba_options()`, `rba_connection_test()`, and `rba_pages()`; These are helper functions. More on that later.

------------------------------------------------------------------------

# Changing the options {#changing-the-options .heading2}

To provide more control, multiple options have been implemented. Refer to the manual of `rba_options()` function for a full description of available options. In short, some of the options will govern rbioapi's connection with servers (e.g. timeout, retry) and some of the options will modify your experience with rbioapi (e.g. verbose, diagnostics, save_file). There are two ways that you may use to change any option. Also, you can get table of available rbioapi options and their current values by calling `rba_options()`without any argument:

```{r rba_options, echo=TRUE, message=TRUE}
rba_options()
```

Now, let us consider the ways in which we can alter the settings:

## Change the option globally {#changing-the-options-change-the-option-globally .heading3}

Changing an option globally means that for the rest of your R session, any rbioapi function will respect the changed option. To do this, use `rba_options().` Each argument in this function corresponds to a certain option; Thus by running this function with your desired new values, you could globally alter that rbioapi option. for example:

```{r rba_options_global, eval=FALSE}
rba_options(save_file = TRUE)
## From now on, the raw file of server's response will be saved to your working directory.
rba_options(verbose = FALSE)
## From now on, the package will be quiet.
```

## Change the option only within a function call {#changing-the-options-change-the-option-only-within-a-function-call .heading3}

You can pass additional arguments to any rbioapi function using "ellipsis" (the familiar `…` or dot dot dot!). Meaning that you can call any function with additional arguments where each is 'option = value' pair. This way, any changes in options will be confined within that particular function call. For example:

```{r rba_options_ellipsis1, eval=FALSE}
## Save the server's raw response file:
x <- rba_reactome_species(only_main = TRUE, save_file = "reactome_species.json")
## Also, in the case of connection failure, retry up to 10 times:
x <- rba_reactome_species(only_main = TRUE,
                         save_file = "reactome_species.json", retry_max = 10)
```

```{r rba_options_ellipsis2, echo=TRUE, message=TRUE}
## Run these codes in your own R session to see the difference.
## show internal diagnostics boring details
x <- rba_uniprot_proteins_crossref(db_id = "CD40", db_name = "HGNC", diangnostics = TRUE)
## The next function you call, will still use the default rbioapi options
x <- rba_uniprot_proteins_crossref(db_id = "CD40", db_name = "HGNC")
```

------------------------------------------------------------------------

# Connection test {#connection-test .heading2}

The second exception in functions' naming schema is `rba_connection_test()`. Run this simple function to check your connection with the supported services/databases. If you encounter errors when using rbioapi, kindly run this function to make sure that your internet connection or the servers are fine.

```{r rba_connection_test, message=TRUE}
rba_connection_test()
```

------------------------------------------------------------------------

# Iterating over paginated results

Some API resources will return paginated responses. This is particularly common in API resources which return potentially very large responses. In rbioapi, for these cases, there are arguments such as "page_number" (with default value of 1) and -if the API resource allows- "page_size". To save your time, you may use `rba_pages()`. This function will iterate over the pages you have specified.

Take rba_uniprot_taxonomy_name as an example. This function allows you to search taxonomic nodes in [UniProt](https://www.uniprot.org "Universal Protein Resource (UniProt)"). The response can potentially have a huge size, so [UniProt](https://www.uniprot.org "Universal Protein Resource (UniProt)") returns a paginated response. For example, if we search for nodes that contain "adenovirus", there is a large number of hits:

```{r rba_pages1, echo=TRUE}
adeno <- rba_uniprot_taxonomy_name(name = "adenovirus",
                                   search_type = "contain",
                                   page_number = 1)
adeno$pageInfo
```

As you can see, the server has returned the first page of the response, to retrieve the other pages, you should make separate calls and change the "page_number" argument within each call, or simply use `rba_pages()` as demonstrated below:

```{r rba_pages2, echo=TRUE}
adeno_pages = rba_pages(quote(rba_uniprot_taxonomy_name(name = "adenovirus",
                                   search_type = "contain",
                                   page_number = "pages:1:3")))
## You can inspect the structure of the response:
str(adeno_pages, max.level = 2)
```

As you can see, what we have done was:

1.  Wrap the function call in `qoute()` and enter that as the input for `rba_pages()`.

2.  Replace the argument we want to iterate over it, with a string in this format: "pages:start:end". For example, we provided page_number = "pages:1:3" to get the responses of pages 1 to 3.

------------------------------------------------------------------------

# How and what to cite? {#how-to-cite .heading2}

rbioapi is an interface between you and other databases and services. Thus, if you have used rbioapi in published research, kindly **in addition to citing rbioapi, [*make sure to fully and properly cite the databases/services you have used*]{.ul}**. Suggested citations have been added in the functions' manuals, under the "references" section; Nevertheless, it is the user's responsibility to check for proper citations and to properly cite the database/services that they have used.

------------------------------------------------------------------------

# Code of conduct {#code-of-conduct .heading2}

When using rbioapi, remember that you are querying data from web services; So please be considerate. Never flood a server with requests, if you need to download *unreasonably* large volumes of data, directly downloading the databases provided in those services may be a better alternative. If you see yourself being rate-limited from any server (HTTP **429 Too Many Requests** response status code), know that you are sending more requests than what the server interprets as normal behavior, so please seek other methods or use `Sys.sleep()` between your requests.

------------------------------------------------------------------------

# What next? {#what-next .heading2}

Each supported service has a dedicated vignette article. Make sure to check those too.

1.  [miEAA](rbioapi_mieaa.html "rbioapi & miEAA vignette article")
2.  [PANTHER](rbioapi_panther.html "rbioapi & PANTHER vignette article")
3.  [Reactome](rbioapi_reactome.html "rbioapi & Reactome vignette article")
4.  [STRING](rbioapi_string.html "rbioapi & STRING vignette article")
5.  [UniProt](rbioapi_uniprot.html "rbioapi & UniProt vignette article")

------------------------------------------------------------------------

# Session info {#session-info .heading2}

```{r sessionInfo, echo=FALSE}
sessionInfo()
```
