---
title: "Cytoscape and igraph"
author: "by Alexander Pico"
package: RCy3
date: "`r Sys.Date()`"
output: 
  BiocStyle::html_document:
    toc_float: true
#  pdf_document:
#    toc: true  
vignette: >
  %\VignetteIndexEntry{02. Cytoscape and igraph ~5 min}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
  eval=FALSE
)
```

This vignette will show you how to convert networks between igraph and Cytoscape.

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

# Required Software
The whole point of RCy3 is to connect 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()
```

# From igraph to Cytoscape
The igraph package is a popular network tool among R users. With RCy3, you can easily translate igraph networks to Cytoscape networks!

Here is a basic igraph network construction from the graph_from_data_frame docs, http://igraph.org/r/doc/graph_from_data_frame.html:
```{r}
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                            "Esmeralda"),
                     age=c(48,33,45,34,21),
                     gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                        same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
                        friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
ig <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)

# if function not found, then you need to install igraph. Try library(igraph)
```

You now have an igraph network, g.  In order to translate the network together with all vertex (node) and edge attributes over to Cytoscape, simply use:

```{r}
createNetworkFromIgraph(ig,"myIgraph")
```

# From Cytoscape to igraph 
Inversely, you can use createIgraphFromNetwork() in RCy3 to retrieve vertex (node) and edge data.frames to construct an igraph network. 

```{r}
ig2 <- createIgraphFromNetwork("myIgraph")
```

Compare the round-trip result for yourself...
```{r, eval=F}
ig
ig2
```
Note that a few additional attributes are present which are used by Cytoscape to
support node/edge selection and network collections.

**Also note: All networks in Cytoscape are implicitly modeled as *directed*. This means that if you start with an *undirected* network in igraph and then convert it round-trip (like described above), then you will end up with a *directed* network.**