---
title: "Jupyter Bridge and RCy3"
author: "Yihang Xin, Alex Pico"
output: html_notebook
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
*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/jupyter-bridge-rcy3.Rmd).*

<hr />
# Why use Jupyter Bridge
* Users do not need to worry about dependencies and environment.
* Easily share notebook-based workflows and data sets
* Workflows can reside in the cloud, access cloud resources, and yet still use Cytoscape features.

# How Jupyter Bridge works

Jupyter-Bridge enables a workflow running on remote Jupyter to execute functions on a PC-local Cytoscape -- the remote Jupyter runs the request through Jupyter-Bridge, where it is picked up by Javascript code running on the Jupyter web page in the PC-local browser, which in turn calls Cytoscape. The Cytoscape response travels the reverse route.



![Jupyter-Bridge allows a remote Jupyter Notebook to communicate with a workstation-based Cytoscape as if the Notebook were running on the Cytoscape workstation. A Jupyter Notebook passes a Cytoscape call to an independent Jupyter-Bridge server where it’s picked up by the Jupyter-Bridge browser component and is passed to Cytoscape. The Cytoscape response is returned via the opposite flow. As a result, workflows can reside in the cloud, access cloud resources, and yet still leverage Cytoscape features. Jupyter Bridge supports py4cytoscape (Python library for communicating with Cytoscape) first, and now RCy3 also support Jupyter-Bridge.](https://raw.githubusercontent.com/cytoscape/jupyter-bridge/master/docs/images/Figure%202.svg)


Visit the [source code of Juputer Bridge](https://github.com/cytoscape/jupyter-bridge) for more information.

# Prerequisites (Local machine)
RCy3 works by connecting with Cytoscape. You will need to install and launch Cytoscape in your local machine:

* Download the latest Cytoscape from http://www.cytoscape.org/download.php
* Complete installation wizard
* Launch Cytoscape
* Install the filetransfer app from https://apps.cytoscape.org/apps/filetransfer

# Prerequisites (Cloud server)
There are a lot of cloud computing services online, such as Google Colab, Amazon EMR Notebook, Microsoft Azure, CoCalc and your own JupyterHub. You can choose your favorite one.

Here we use Google Colab to demonstrate. Visit this [link](https://colab.to/r) to create a new empty R Notebook, and make sure to run code below in the cloud.

<span style="color:red">Copy codes below to build connection between Jupyter notebook (cloud) and Cytoscape (local).</span>

<span style="color:red"> Make sure to run code below in the cloud!!!</span>

# Installation
Install the latest version of RCy3 from Bioconductor.
```{r eval=FALSE}
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("RCy3")
library(RCy3)
```




# Connect to local Cytoscape
First, build connection between the jupyter notebook and local Cytoscape.

```{r eval=FALSE}
browserClientJs <- getBrowserClientJs()
IRdisplay::display_javascript(browserClientJs)
```

# Check connection
Then, launch Cytoscape and keep it running whenever using RCy3 and Jupyter Bridge. Confirm that you have everything installed and running:

```{r eval=FALSE}
cytoscapeVersionInfo()
cytoscapePing()
```

Done! Now you can execute a workflow in a remote server-based Jupyter Notebook to leverage your workstation’s Cytoscape. You can also easily share notebook-based workflows and data sets.

# Use case: Run differentially expressed genes network analysis in the cloud
Visit the [Jupyter Bridge RCy3 and Differentially Expressed Genes Network Analysis](https://colab.research.google.com/drive/1iiYtjVrc0qFjzDAAdccgwByZWH1DPp2U?usp=sharing) for the detailed workflow.
