---
title: "2.F: STRING & rbioapi"
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{2.F: STRING & rbioapi}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r message=FALSE, include=FALSE, results="hide", setup, echo=FALSE}
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, skip_error = TRUE)
```

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

# Introduction {#introduction .heading2}

STRING is a comprehensive database of protein-protein interactions (PPI) that in version 11.0, covers 24,584,628 proteins from 5,090 organisms. Directly quoting from their paper:

> The STRING database aims to collect, score and integrate all publicly available sources of protein--protein interaction information, and to complement these with computational predictions. Its goal is to achieve a comprehensive and objective global network, including direct (physical) as well as indirect (functional) interactions.
>
> (source: Szklarczyk, Damian, et al. "STRING v11: protein--protein association networks with increased coverage, supporting functional discovery in genome-wide experimental datasets." *Nucleic acids research* 47.D1 (2019): D607-D613. )

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

# Note about species argument {#note-about-species-argument .heading2}

You can find an argument named "species" in every rbioapi STRING function. Providing the species argument is not mandatory, but it has been recommended in STRING API's documentation to always specify the species. An exception is when your input proteins' vector length is more than 100; In such cases, the species argument is required. Otherwise, calling the function without providing the species will produce an ERROR.

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

# Map your IDs to STRING IDs {#map-your-ids-to-string-ids .heading2}

Although STRING API resources will handle and recognize a variety of identifiers, it is recommended that you first map your IDs to STRING IDs before using them in other rbioapi STRING functions.

```{r rba_string_map_ids}
## 1 We create a variable with our genes' NCBI IDs
proteins <- c("p53", "BRCA1", "cdk2", "Q99835", "CDC42","CDK1","KIF23",
              "PLK1","RAC2","RACGAP1","RHOA","RHOB", "PHF14", "RBM3")
## 2 Now we map our protein IDs
proteins_mapped <- rba_string_map_ids(ids = proteins,
                                      species = 9606)
## 3 What we need and will use for the rest of this vignette is the `stringId` column
```

```{r rba_string_map_ids_results, echo=FALSE}
if (is.data.frame(proteins_mapped)) {
  DT::datatable(data = proteins_mapped,
              options = list(scrollX = TRUE, 
                             paging = TRUE,
                             fixedHeader = TRUE,
                             keys = TRUE,
                             pageLength = 5))
  proteins_mapped <- proteins_mapped$stringId
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

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

# Get interaction network of a protein set {#get-interaction-network-of-a-protein-set .heading2}

You can retrieve a list of interactions that the proteins in your set have with each other along with the STRING annotations of each interaction. You may filter the results by using required_score and network_type arguments.

See the 'values' section `rba_string_interactions_network` function's manual for information on the returned columns.

```{r rba_string_interactions_network}
int_net <- rba_string_interactions_network(ids = proteins_mapped,
                                          species = 9606,
                                          required_score = 500)
```

```{r rba_string_interactions_network_results, echo=FALSE}
if (is.data.frame(int_net)) {
  DT::datatable(data = int_net,
              options = list(scrollX = TRUE, 
                             paging = TRUE,
                             fixedHeader = TRUE,
                             keys = TRUE,
                             pageLength = 5))
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

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

# Get interaction partners of a protein set {#get-interaction-partners-of-a-protein-set .heading2}

In the last example, we only obtained the interaction which our proteins have among themselves, what if we wanted to get a list of every protein which interact with our protein(s)?

To do that, we can use `rba_string_interaction_partners`:

```{r rba_string_interaction_partners}
## Although we supply only one protein ID here (CD40 protein), you can provide a vector of proteins as the input
int_partners <- rba_string_interaction_partners(ids = "9606.ENSP00000361359",
                                               species = 9606,
                                               required_score = 900)
```

```{r rba_string_interaction_partners_restults, echo=FALSE}
if (is.data.frame(int_partners)) {
  DT::datatable(data = int_partners,
              options = list(scrollX = TRUE, 
                             paging = TRUE,
                             fixedHeader = TRUE,
                             keys = TRUE,
                             pageLength = 5))
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

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

# Get network image of a protein set {#get-network-image-of-a-protein-set .heading2}

Let's go back to the interaction network. As you must have seen in the STRING webpages, STRING plots the interaction network of your proteins with many customizations available. You can also do that with STRING API services. `rba_string_network_image` function is very flexible and you have a variety of options; see the function's manual.

```{r rba_string_network_image_ex1, fig.show='hide'}
## Example 1:
graph_1 <- rba_string_network_image(ids = proteins_mapped,
                                   image_format = "image",
                                   species = 9606,
                                   save_image = FALSE,
                                   required_score = 500,
                                   network_flavor = "confidence")
```

```{r rba_string_network_image_ex1_image, echo=FALSE, fig.cap="Network images - Example 1", fig.align='center'}
if (is.array(graph_1)) {
  grid::grid.raster(graph_1, just = "center")
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

```{r rba_string_network_image_ex2, fig.show='hide'}
## Example 2:
graph_2 <- rba_string_network_image(ids = proteins_mapped,
                                    image_format = "image",
                                    species = 9606,
                                    save_image = FALSE,
                                    required_score = 500,
                                    add_color_nodes = 5,
                                    add_white_nodes = 5,
                                   network_flavor = "actions")
```

```{r rba_string_network_image_ex2_image, echo=FALSE, fig.cap="Network images - Example 2", fig.align='center'}
if (is.array(graph_2)) {
  grid::grid.raster(graph_2, just = "center")
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

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

# Enrichment using STRING {#enrichment-using-string .heading2}

STRING let you perform two types of enrichments. See [STRING's paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3531103/ "STRING v9.1: protein-protein interaction networks, with increased coverage and integration") for more information.

## Functional enrichment {#functional-enrichment .heading3}

The first type is the conventional type, which statistically tests your supplied gene sets against some sets of annotation. Currently, STRING supports Gene Ontology, KEGG pathways, UniProt Keywords, PubMed publications, Pfam domains, InterPro domains, and SMART domains. ([source](https://version11.string-db.org/help/api/#getting-functional-enrichment "STRING API - Getting functional enrichment")).

```{r rba_string_enrichment}
enriched <- rba_string_enrichment(ids = proteins_mapped,
                                  species = 9606, )
```

As usual, we inspect the output using the `str()` function. As you can see below, the enrichment results of each category can be found as the returned list's elements.

```{r rba_string_enrichment_str}
str(enriched, max.level = 1)
```

Let us see the "DISEASES" results as an example. Below, we can see which terms of the [Human Disease Ontology](https://disease-ontology.org/ "Human Disease Ontology") were over-represented:

```{r rba_string_enrichment_restults, echo=FALSE}
if (is.data.frame(enriched$DISEASES)) {
  DT::datatable(data = enriched$DISEASES,
              options = list(scrollX = TRUE, 
                             paging = TRUE,
                             fixedHeader = TRUE,
                             keys = TRUE,
                             pageLength = 5))
} else {
  print("Vignette building failed. It is probably because the web service was down during the building.")
}
```

**Please Note:** Other services supported by rbioapi also provide Over-representation analysis tools. Please see the vignette article [Do with rbioapi: Over-Representation (Enrichment) Analysis](rbioapi_do_enrich.html) ([link to the documentation site](https://rbioapi.moosa-r.com/articles/rbioapi_do_enrich.html)) for an in-depth review.

## Protein-protein interaction enrichment {#protein-protein-interaction-enrichment .heading3}

Even without incorporating annotation data, STRING can calculate if your proteins are functionally related. Briefly, STRING accomplishes this by comparing the interactions' distribution in your protein-set to the interactions' distribution in the proteome. Read [STRING's paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3531103/#__sec4title "STRING v9.1: protein-protein interaction networks, with increased coverage and integration") for more information.

```{r rba_string_enrichment_ppi}
rba_string_enrichment_ppi(ids = proteins_mapped,
                          species = 9606)
```

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

# Get functional annotations {#get-functional-annotations .heading2}

As you have seen above, STRING maps the proteins to multiple annotation sources. You can obtain any annotation associated with your proteins without performing enrichment analysis and retrieving just the significant portion.

```{r rba_string_annotations, eval=FALSE}
annotations <- rba_string_annotations(ids = "9606.ENSP00000269305",
                                     species = 9606)

## This function returns large results, so the results was not shown in this vignette.
```

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

# See also in Functions' manuals {#see-also-in-functions-manuals .heading2}

Some rbioapi STRING functions were not covered in this vignette, please check their manuals:

-   `rba_string_homology_intra()`

-   `rba_string_homology_inter()`

-   `rba_string_version()`

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

# How to Cite? {#Citations}

To cite STRING (Please see <https://string-db.org/cgi/about?footer_active_subpage=references>):

-   Damian Szklarczyk, Annika L Gable, Katerina C Nastou, David Lyon, Rebecca Kirsch, Sampo Pyysalo, Nadezhda T Doncheva, Marc Legeay, Tao Fang, Peer Bork, Lars J Jensen, Christian von Mering, The STRING database in 2021: customizable protein--protein networks, and functional characterization of user-uploaded gene/measurement sets, *Nucleic Acids Research*, Volume 49, Issue D1, 8 January 2021, Pages D605--D612, <https://doi.org/10.1093/nar/gkaa1074>

To cite rbioapi:

-   Moosa Rezwani, Ali Akbar Pourfathollah, Farshid Noorbakhsh, rbioapi: User-Friendly R Interface to Biologic Web Services' API, *Bioinformatics*, 2022;, btac172, <https://doi.org/10.1093/bioinformatics/btac172>

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

# Links {.heading2}

-   [This article in rbioapi documentation site](https://rbioapi.moosa-r.com/articles/rbioapi_string.html "2.E: STRING & rbioapi")

-   [Functions references in rbioapi documentation site](https://rbioapi.moosa-r.com/reference/index.html#section-string-rba-string- "rbioapi reference")

-   [rbioapi vignette index](rbioapi.html "rbioapi: User-Friendly R Interface to Biologic Web Services' API")

# Session info {#session-info .heading2}

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