---
title: "scNetViz: EMBL-EBI Single Cell Expression Atlas"
author: "Krishna Choudhary, Alex Pico"
output: html_notebook
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
  eval=FALSE
)
```
*The R markdown is available from the pulldown menu for* Code *at the upper-right, choose "Download Rmd", or [download the Rmd from GitHub](https://raw.githubusercontent.com/cytoscape/cytoscape-automation/master/for-scripters/R/notebooks/use-case-1.Rmd).*

<hr />

In this example, we will browse a public single cell expression atlas, explore a particular dataset, perform differential expression analysis based on provided categories, generate networks from the top genes from each category, and functionally characterize and visualize the networks.

# Installation
```{r}
if(!"RCy3" %in% installed.packages()){
    install.packages("BiocManager")
    BiocManager::install("RCy3")
}
library(RCy3)
```

# Required software
RCy3 works by connecting with Cytoscape. You will need to install and launch Cytoscape:

* Download the latest Cytoscape from http://www.cytoscape.org/download.php
* Complete installation wizard
* Launch Cytoscape


```{r}
cytoscapePing()
```

For this vignette, you’ll need the following apps: 

* the [STRING](https://string-db.org/) app, 
* the [enhancedGraphics](https://www.cgl.ucsf.edu/cytoscape/utilities3/enhancedcg.shtml) app, 
* the [cyBrowser](https://www.cgl.ucsf.edu/cytoscape/utilities3/cybrowser.shtml) app, and
* the [cyPlot](http://apps.cytoscape.org/apps/cyplot) app.

```{r}
#available in Cytoscape 3.7.0 and above
installApp('STRINGapp')
installApp('enhancedGraphics')
installApp('cyBrowser')
installApp('cyPlot')
installApp('scNetViz')
```

# Pull data from the EMBL-EBI Single-Cell Expression Atlas
Use the accession number of single-cell experiment to pull data from the  [Single-Cell Expression Atlas](https://www.ebi.ac.uk/gxa/sc/experiments) of EMBL-EBI.


```{r}
#Load data with Accession number E-GEOD-81383
RCy3::commandsRun('scnetviz load gxa experiment accession=E-GEOD-81383')
```

This loads the data and opens an experiment table with three tabs, named _TPM_, _Categories_, and _DiffExp_. 

# Differential expression analysis

Run differential expression analysis for the row with `true` value of `sel.K` (default). 
```{r}
RCy3::commandsRun('scnetviz calculate diffexp accession=E-GEOD-81383')
```

##Query STRING database for interaction networks

Fetch interaction networks from the [STRING](https://string-db.org/) database.

```{r}
RCy3::commandsRun('scnetviz create network accession=E-GEOD-81383')
```

The following command runs both the differential expression analysis and fetches interaction networks simultaneously.
```{r}
RCy3::commandsRun('scnetviz create all experiment=E-GEOD-81383')
```

# Functional enrichment analysis

Check the networks available in the current Cytoscape session.
```{r results=FALSE}
RCy3::commandsRun('network list')
```

Perform functional enrichment analysis for the network selected in the current session, which is the last network that was fetched unless another network is manually selected from Cytoscape. This uses the [_stringApp_](https://www.cgl.ucsf.edu/cytoscape/stringApp/index.shtml). To view the results in the Cytoscape application, you may have to activate the _Show enrichment panel_ option under __STRING Enrichment__ sub-menu from __Apps__ from the menu bar.

```{r}
RCy3::commandsRun('string retrieve enrichment allNetSpecies=Homo sapiens')
```

Visit the [scNetViz website](https://www.cgl.ucsf.edu/cytoscape/scNetViz/#networks) for more information.
