---
title: "QC Report"
date: "`r Sys.Date()`"
author: "`r paste0('gimap (v', utils::packageVersion('gimap'), ')')`"
params:
  dataset: NULL
  plots_dir: ./plots
  filter_zerocount_target_col: NULL
  filter_plasmid_target_col: NULL
  filter_replicates_target_col: NULL
output: 
  html_document:
    theme: spacelab
    toc: true
    toc_float: TRUE
    df_print: kable
    code_folding: show
  pdf_document:
    toc: true
  always_allow_html: yes
  editor_options:
    chunk_output_type:console
---

<style>
  tbody tr:nth-child(odd){
    background-color: #D6E0F5;
}
</style>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
knitr::opts_knit$set(progress = TRUE, verbose = FALSE)
library(gimap)
library(ggplot2)
library(purrr)
```

# Unfiltered/raw data

## Counts CDF

```{r}
qc_cdf(gimap_dataset)
```

## Histogram of cpm values by sample

```{r}
qc_sample_hist(gimap_dataset)
```


## Sample Correlation heatmap (unfiltered data)

```{r}
qc_cor_heatmap(gimap_dataset)
```

## Variance within replicates

```{r}
qc_variance_hist(gimap_dataset, filter_replicates_target_col = filter_replicates_target_col)
```

## Plasmid expression (log 2 cpm values for Day 0 sample/time point)

```{r}
qc_plasmid_histogram(gimap_dataset, filter_plasmid_target_col = filter_plasmid_target_col)
```

# Applying potential filters

## Filter pgRNAs where there is a count of 0 for any sample/time point

### Replicates with a pgRNA count of 0 

```{r}
qc_constructs_countzero_bar(gimap_dataset, filter_zerocount_target_col = filter_zerocount_target_col, filter_replicates_target_col = filter_replicates_target_col)
```

If this filter is applied, this is the number of pgRNAs that would be filtered out

```{r}
potentialFilter1 <- qc_filter_zerocounts(gimap_dataset, filter_zerocount_target_col = filter_zerocount_target_col)

potentialFilter1$reportdf
```

### Filter pgRNAs where there is a low log2 CPM value for the plasmid sample/time point

If this filter is applied, this is the number of pgRNAs that would be filtered out

```{r}
potentialFilter2 <- qc_filter_plasmid(gimap_dataset, filter_plasmid_target_col = filter_plasmid_target_col)

potentialFilter2$reportdf
```

### If both filters are applied

```{r}
combined_filters <- reduce(list(potentialFilter1$filter, potentialFilter2$filter), cbind)
```

| Which Filter(s) | Number of pgRNAs flagged for removal | Percent of total pgRNA constructs |
|:---------------|:------------------------------------|:----------|
| Zero count, but not low plasmid CPM | `r sum(combined_filters[,1] == TRUE & combined_filters[,2] == FALSE)` | `r round(sum(combined_filters[,1] == TRUE & combined_filters[,2] == FALSE) / nrow(combined_filters) * 100, 2)`|
| Low plasmid CPM, but not zero count | `r sum(combined_filters[,2] == TRUE & combined_filters[,1] == FALSE)` | `r round(sum(combined_filters[,2] == TRUE & combined_filters[,1] == FALSE) / nrow(combined_filters) * 100, 2)` |
| Either Zero count or Low plasmid CPM or both | `r sum(rowSums(combined_filters) >= 1)`| `r round(sum(rowSums(combined_filters) >= 1) / nrow(combined_filters) * 100, 2)`  |
| Both Zero count and Low plasmid CPM | `r sum(rowSums(combined_filters) == 2)` | `r round(sum(rowSums(combined_filters) == 2) / nrow(combined_filters) * 100, 2)` |
| Remaining pgRNAs flagged by no filters | `r sum(rowSums(combined_filters) == 0)` |  `r round(sum(rowSums(combined_filters) == 0) / nrow(combined_filters) * 100, 2)` |

# Session Info

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