---
title: "Probabilistic Outlier Identification for RNA Sequencing Generalized Linear Models"
author: "Stefano Mangiola"
date: "`r Sys.Date()`"
package: ppcseq
output:
  BiocStyle::html_document:
    toc_float: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Overview of the ppcseq package}
  %\usepackage[UTF-8]{inputenc}
---

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

# Introduction

Relative transcript abundance has proven to be a valuable tool for understanding the function of genes in biological systems. For the differential analysis of transcript abundance using RNA sequencing data, the negative binomial model is by far the most frequently adopted. However, common methods that are based on a negative binomial model are not robust to extreme outliers, which we found to be abundant in public datasets. So far, no rigorous and probabilistic methods for detection of outliers have been developed for RNA sequencing data, leaving the identification mostly to visual inspection. Recent advances in Bayesian computation allow large-scale comparison of observed data against its theoretical distribution given in a statistical model. Here we propose ppcseq, a key quality-control tool for identifying transcripts that include outlier data points in differential expression analysis, which do not follow a negative binomial distribution. Applying ppcseq to analyse several publicly available datasets using popular tools, we show that from 3 to 10 percent of differentially abundant transcripts across algorithms and datasets had statistics inflated by the presence of outliers.

# Installation and use

The input data set is a tidy representation of a differential gene transcript abundance analysis

```{r}
library(dplyr)
library(ppcseq)
```

To install:

Before install, for linux systems, in order to exploit multi-threading, from R write:

```{r eval=FALSE}
fileConn<-file("~/.R/Makevars")
writeLines(c( "CXX14FLAGS += -O3","CXX14FLAGS += -DSTAN_THREADS", "CXX14FLAGS += -pthread"), fileConn)
close(fileConn)
```

Multi-threading allows the sampling or variational bayes to share the computation on multiple cores.

Then, install with

```{r eval=FALSE}
if(!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("ppcseq")
```

You can get the test dataset with

```{r}
data("counts")
counts 
```

You can identify anrtefactual calls from your differential transcribt anundance analysis, due to outliers.

```{r warning=FALSE, message=FALSE,results='hide'}
# Import libraries

if(Sys.info()[['sysname']] == "Linux")
counts.ppc = 
	counts %>%
	mutate(is_significant = FDR < 0.0001) %>%
	identify_outliers(
		formula = ~ Label,
		.sample = sample, 
		.transcript = symbol,
		.abundance = value,
		.significance = PValue,
		.do_check = is_significant,
		percent_false_positive_genes = 5, 
		approximate_posterior_inference = FALSE
	)
```

The new posterior predictive check has been added to the original data frame

```{r }
if(Sys.info()[['sysname']] == "Linux")
counts.ppc 
```

The new data frame contains plots for each gene

We can visualise the top five differentially transcribed genes

```{r }
if(Sys.info()[['sysname']] == "Linux")
counts.ppc_plots = 
	counts.ppc %>% 
	plot_credible_intervals() 
```

```{r }
if(Sys.info()[['sysname']] == "Linux")
counts.ppc_plots %>%
	pull(plot) %>% 
	.[seq_len(1)]
```


```{r softwareinfo}
sessionInfo()
```
