---
title: "INet for Network Integration"
author: "Valeria Policastro et al."
date: "`r Sys.Date()`"
vignette: >
  %\VignetteIndexEntry{INet}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output:
  knitr:::html_vignette:
    toc: yes
    toc_depth: 2
---

In the context of network integration, we propose the `INet` algorithm. `INet` assumes a similar network structure, representing latent variables in different network layers of the same system. Therefore, by combining individual edge weights and topological network structures, `INet` first constructs a `Consensus Network` that represents the shared information underneath the different layers to provide a global view of the entities that play a fundamental role in the phenomenon of interest. Then, it derives a `Case Specific Network` for each layer containing peculiar information of the single data type not present in all the others. 

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```

## Installation of INetTool

```{r, library}
# Install from CRAN 
# install.packages("INetTool")

# Install from GitHub
# install.packages("devtools")
#devtools::install_github("ValeriaPolicastro/INet-Tool")

```

# Example

Load the package and a list with the example datasets. In this case, columns of both datasets represent patients, while rows represent gene expression, methylation and mirna expression respectively.

```{r, data}
library(INetTool)
data("exampleL_data")
```

## Constuction of the Networks

If you do not have already the weighted networks, as in our example, you can construct them throughout this function:

```{r, construction}
net <- constructionGraph(exampleL_data)
```

*If you have already the networks skip this step.*

The function constructs similarity networks based on pearson correlation and the percentile parameter, in our example it constructs 3 patient similarity networks.

For this example we named:

**adjL** the list of weighted adjacency matrices needed for the next functions

```{r}
adjL <- net$Adj
```

**graphL** the list of graphs needed for the next functions

```{r}
graphL <- net$Graphs
```

## 1) Pre Analysis

To detect how similar are the networks you can use the Jaccard distance matrix function:

```{r}
JWmatrix(graphL)
```

or the *Mean Weighted Jaccard Distance for Multilayer Networks* a global distance function:

```{r}
JWmean(graphL)
```

### Networks measures

To calculate different measures on the different graphs you can easly use the following function:

```{r}
measures <- measuresNet(graphL, nodes.measures=F)
```

(default nodes.measures=T gives measures also about the nodes)

The measures of the first graph:

```{r}
measures[[1]] 
```

### Plot Networks

To plot all the layers in one plot use:

```{r, plot}
plotL(graphL,vertex.cex=.1,vertex.labels.cex=.1, vertex.color = 18)
```

different parameter can be specified for the plot.

## 2) Consensus Network:

To construct the *Consensus Network* use a list of weighted adjacency matrices:

***Note: `consensusNet` needs the same row and column names for all the matrices, if you do not have it use the `adj_rename`function before the consensus***

```{r, consensus-function}
Con <- consensusNet (adjL, theta=0.05) 

```

From our simulation study, we detected that the best theta for 500 nodes network is 0.04 and the best one for 100 nodes network is 0.06,thus for this example with 200 nodes we setted the parameter to 0.05.

The output are:

-   the *Consensus Network*

```{r}
Con$graphConsensus 
```

-   the *Mean Weighted Jaccard Distance for Multilayer Networks*

```{r}
Con$Comparison
```

-   the similar graphs before the thresholding

```{r}
Con$similarGraphs
```

The similar graphs are useful if it is needed to change the final thresholding, instead of computing again the algorithm you can use directly `thresholdNet` function.

Plotting the *Consensus Network* you can find the common informations present in all the layers:

```{r}
plotC(Con$graphConsensus,vertex.size=6, vertex.label.cex =0.5,vertex.color = 18)
```

## 3) Case Specific Networks:

To construct *Case Specific Networks* one for each layer to give information of the peculiar layer not present in the *Consensus*:

```{r, specific-function}
specificNet(graphL, Con$graphConsensus)
```

## 4) INet Plot

To plot a starting layer and the *Consensus* in one graph with different edge colours: red edges represent edges of the *Consensus* already present in the starting one, while light blue edges represent new edges constructed by the *Consensus*.

```{r}
plotINet(adjL[[1]], Con$graphConsensus, vertex.labels.cex=.01, vertex.size=2)
```
