---
title: "Pathway fingerprint: a tool for biomarker discovery based on gene expression data and pathway knowledge"
output: 
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{Pathway fingerprint: a tool for biomarker discovery based on gene expression data and pathway knowledge}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  %\usepackage[utf8]{inputenc}
---

```{r, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center", 
  fig.show = "asis",
  eval = TRUE,
  tidy.opts = list(blank = FALSE, width.cutoff = 60),
  tidy = TRUE,
  message = FALSE,
  warning = FALSE
)
```

## Introduction

Traditional methods of analyzing gene expression data in the study of some 
disease usually compare the disease and normal control groups of samples and
find the most differentially expressed genes. But that is hard to discover 
the disease’s biomarkers and mechanism. To give a quantitative comparison of the 
complex disease, we achieve *PFP*, a good characterization for a person’s 
disease based on pathway on the open scientific computing platform **R**.
In this package, a pathway-fingerprint (*PFP*) method was introduced to evaluate 
the importance of a gene set in different pathways to help researchers focus on 
the most related pathways and genes.It will be used to visually compare and 
parse different diseases by generating a fingerprint overlay. We collected three 
types of gene expression data to perform the enrichment analysis in KEGG 
pathways and make some comparations with other methods. The result indicated 
that Pathway Fingerprint had better performance than other enrichment tools, 
which not only picked out the most relevant pathways but also showed strong 
stability when changing data. 
we propose a novel, general and systematic method called Pathway Fingerprint to 
help researchers focus on the fatal pathways and genes by considering the 
topology knowledge. 

The three main features of *PFP*:

- PFP class, the differential genes were mapped to the KEGG pathway and the PFP score was calculated.
- PFPRefnet class, the selected sample in KEGG with network information.
- visualization, a fingerprint overlay.

### Installation

*PFP* requires these packages: *graph*, *igraph*, *KEGGgraph*, *clusterProfiler*, *ggplot2*, *plyr*,*tidy*,*magrittr*, *stats*, *methods* and *utils*. To install *PFP*, some required packages are only available from [Bioconductor](http://bioconductor.org).
It also allows users to install the latest development version from github, which requires  *devtools* package has been installed on your system or can be installed using `install.packages("devtools")`. Note that devtools sometimes needs some extra non-R software on your system -- more specifically, an Rtools download for Windows or Xcode for OS X. There's more information about devtools [here](https://github.com/hadley/devtools).
You  can install *PFP* via Bioconductor.
```{r install-pkg-bioconductor, eval=FALSE}
## install PFP from github, require biocondutor dependencies package pre-installed 
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("PFP")
```
You  can also install *PFP* via github.
```{r install-pkg-github, eval=FALSE}
## install PFP from github, require biocondutor dependencies package pre-installed 
if (!require(devtools))
  install.packages("devtools")
devtools::install_github("aib-group/PFP")
```

During analysis, you need to install *org.Hs.eg.db*, the installation strategy is as follows.
```{r install-database-bioconductor, eval=FALSE}
## install PFP from github, require biocondutor dependencies package pre-installed 
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("org.Hs.eg.db")
```

After installation, the \*{PFP} is ready to load into the current workspace by the following codes to the current workspace by typing or pasting the following codes:

```{r load-pkg,eval=TRUE, include=TRUE}
library(PFP)
```

## Analysis Pipeline: Get pathway networks from KEGG

In our method, we choose KEGG([http://www.kegg.jp/](http://www.kegg.jp/)) pathway networks as a reference to generate a Pathway Fingerprint. 
KEGG provides KGML files of pathways for users, which enables automatic drawing of KEGG pathways and provides facilities for computational analysis and modeling of gene/protein networks and chemical networks.
We downloaded the latest (2020.11.8) KGML files of all the human pathways in KEGG and translated the KGML files to network.Then we got a total number of 338 pathway networks for further analysis.


### Identification of DEGs(differentially expressed genes)

There are different methods for the different three types of data to identify DEGs. We processed the microarray data by *limma*, besides, we also selected some cancer samples of the same type of cancer and compared them with the control group by *edgeR*.In both *limma* and *edgeR*, we only chose the genes whose log2 fold change (logFC) was greater than 1 and false discovery rate (FDR) was less than 0.05.


We defined a new S4 class `PFP` to store the score. *PFP* also provides six major methods for this S4 class:

1. `genes_score()`: Gene score, adding function to specified selection group/ pathway.
2. `sub_PFP()`: A portion of the PFP can be selected by group, slice, path name, and ID.
3. `show()`: Display the network group name, group size, and PFP score for each channel
4. `plot_PFP()`: Display PFP fingerprinting.
5. `refnet_names()`: Extract base network group names
6. `rank_PFP()`: To achieve the path weight ranking, the preferred P value, and then the PFP score.
Detailed instructions for this five methods refer to package function help.

We also defined a new S4 class `PFPRefnet` to store the reference pathway network information of KEGG,
it provides six  methods for this S4 class::

1. `network()`: Reference path network of KEGG.
2. `net_info()`: Pathway information.
3. `group()`: Group information.
4. `refnet_names()`: The access information of the reference network.
5. `subnet()`: A portion of the PFPRefnet can be selected by group, slice, path name, and ID.
6. `show()`: Show the number of pathways in each group of the reference network.


### PFP scores calculation

1. Input differential gene list
2. Extract the network information of KEGG base map.
3. The channel fingerprint score was calculated and converted to PFP format.

Then the PFP can be calculated as following:
```{r a general-pipline,eval=TRUE, include=TRUE}
#load the data -- gene list of human; the PFPRefnet object of human; the PFP 
#object to test; the list of different genes.
data("gene_list_hsa")
data("PFPRefnet_hsa")
data("PFP_test1")
data("data_std")
# Step1: calculate the similarity score of network.
PFP_test <- calc_PFP_score(genes = gene_list_hsa,PFPRefnet = PFPRefnet_hsa)
# Step2: rank the pathway by the PFP score.
rank1 <- rank_PFP(object = PFP_test,total_rank = TRUE,thresh_value=0.5)
```
We study the target pathway, the pathway with the highest score after ranking.Below is 
a simple example.
```{r a the_target_gene,eval=TRUE, include=TRUE}
# Step1: select the max score of pathway.
pathway_select <- refnet_info(rank1)[1,"id"]
gene_test <- pathways_score(rank1)$genes_score[[pathway_select]]$ENTREZID
# Step2: get the correlation coefficient score of the edge.
edges_coexp <- get_exp_cor_edges(gene_test,data_std)
# Step3: Find the difference genes that are of focus.
gene_list2 <- unique(c(edges_coexp$source,edges_coexp$target))
# Step4: Find the edge to focus on.
edges_kegg <- get_bg_related_kegg(gene_list2,PFPRefnet=PFPRefnet_hsa,
                                  rm_duplicated = TRUE)
# Step5: Find the associated network
require(org.Hs.eg.db)
net_test <- get_asso_net(edges_coexp = edges_coexp,
                         edges_kegg = edges_kegg,
                         if_symbol = TRUE,
                         gene_info_db = org.Hs.eg.db)
```

            
### Network fingerprint visualization

*PFP* provides the `plot_PFP()` function to visualize the network fingerprint of a single query network.
First we show an example of PFP score.
```{r a PFP example, fig.height=6, fig.width=7.2, warning=FALSE}
plot_PFP(PFP_test)
```
Plot the scores from high to low.
```{r a rank PFP, fig.height=6, fig.width=7.2, warning=FALSE}
plot_PFP(rank1)
```

## Session Information

The version number of R and packages loaded for generating the vignette were:

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