---
title: 'Report breakdown by group'
author: 'ampliCan'
date: '`r format(Sys.time(), "%d %B %Y")`'
output:
  html_document:
    toc: true
    theme: paper
    toc_float: true
    number_sections: true
params:
  alignments: !r system.file('extdata', 'results', 'alignments', 'events_filtered_shifted_normalized.csv', package = 'amplican')
  config_summary: !r system.file('extdata', 'results', 'config_summary.csv', package = 'amplican')
vignette: >
  %\VignetteIndexEntry{example group_report report}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r load data, message=F, warning=FALSE, include=FALSE}
library(amplican)
library(ggplot2)
alignments <- data.table::fread(params$alignments)
data.table::setDF(alignments)
config <- data.frame(data.table::fread(params$config_summary))
height <- plot_height(length(unique(config$Group)))
```

***

# Description  

***

**Read distribution plot** - plot shows number of reads assigned during read grouping  
**Filtered Reads** - plot shows percentage of assigned reads that have been recognized as PRIMER DIMERS or filtered based on low alignment score  
**Edit rates** - plot gives overview of percentage of reads (not filtered as PRIMER DIMER) that have edits  
**Frameshift** - plot shows what percentage of reads that have frameshift  
**Read heterogeneity plot** - shows what is the share of each of the unique reads in total count of all reads. The more yellow each row, the less heterogeneity in the reads, more black means reads don't repeat often and are unique  


***

# Group Summary  

***

## Read distribution  

```{r plot_total_reads, echo=FALSE, fig.height=height, fig.width=14, message=F, warning=FALSE}
ggplot(data = config, aes(x = as.factor(Group), y = log10(Reads + 1), order = Group, fill = Group)) +
  geom_boxplot() +
  ylab('Number of reads + 1, log10 scaled')  +
  xlab('Group') + 
  theme(legend.position = 'none',
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 14, face = 'bold')) +
  coord_flip()
```

## Filtered reads  

```{r plot_F_per, echo=FALSE, fig.height=height, fig.width=14, message=F, warning=FALSE}
config$F_percentage <- (config$PRIMER_DIMER + config$Low_Score) * 100/config$Reads
config$F_percentage[is.nan(config$F_percentage)] <- 0

ggplot(data = config, aes(x = as.factor(Group), y = F_percentage, 
                          order = Group, fill = Group)) +
  geom_boxplot() +
  xlab('Group') + 
  ylab('Percentage of filtered reads')  +
  theme(axis.text = element_text(size=12),
        axis.title = element_text(size=14, face = 'bold'),
        legend.position = 'none') +
  ylim(0, 100) +
  coord_flip()
``` 

## Edit rates  

```{r plot indel percentage, echo=FALSE, fig.height=height, fig.width=14, message=F, warning=FALSE}
config$edit_percentage <- config$Reads_Edited * 100/config$Reads_Filtered
config$edit_percentage[is.nan(config$edit_percentage)] <- 0  

ggplot(data = config, aes(x = as.factor(Group), y = edit_percentage, order = Group, fill = Group)) +
  geom_boxplot() +
  xlab('Group') + 
  ylab('Percentage of reads (not filtered) that have edits')  +
  theme(axis.text = element_text(size=12),
        axis.title = element_text(size=14, face = 'bold'),
        legend.position = 'None') +
  ylim(0,100) +
  coord_flip()
``` 

## Frameshift  

```{r plot_frameshift_per, echo=FALSE, fig.height=height, fig.width=14, message=F, warning=FALSE}
config$frameshift_percentage <- config$Reads_Frameshifted * 100/config$Reads_Filtered
config$frameshift_percentage[is.nan(config$frameshift_percentage)] <- 0  

ggplot(data = config, aes(x = as.factor(Group), y = frameshift_percentage, order = Group, fill = Group)) +
  geom_boxplot() +
  xlab('Group') + 
  ylab('Percentage of reads (not filtered) that have frameshift')  +
  theme(axis.text = element_text(size=12),
        axis.title = element_text(size=14,face = 'bold'),
        legend.position = 'None') +
  ylim(0, 100) +
  coord_flip()
``` 

## Heterogeneity of reads  

```{r plot read heterogeneity, echo=FALSE, fig.height=height + 1, fig.width=14, message=F, warning=FALSE}
plot_heterogeneity(alignments, config, level = 'Group')
``` 

***

# Alignments plots  

***

```{r plot_alignments, results='asis', echo=F, message=F, warning=F}
alignments <- alignments[alignments$consensus & alignments$overlaps, ]
alignments$strand <- "+" # strand does not matter after consensus filtering
src = sapply(unique(config$Group), function(i) {
  knitr::knit_expand(text = c(
    "## Group {{i}}  \n", 
    "### Deletions  \n", 
    paste('```{r del-{{i}}, echo = F, results = "asis", ',
          'message=F, warning=F}', collapse = ''), 
    'amplican::metaplot_deletions(alignments, config, "Group", "{{i}}")', 
    '```\n',
    "### Insertions", 
    paste('```{r ins-{{i}}, echo = F, results = "asis", ',
          'message=F, warning=F}', collapse = ''), 
    'amplican::metaplot_insertions(alignments, config, "Group", "{{i}}")',
    '```\n', 
    "### Mismatches", 
    paste('```{r mis-{{i}}, echo = F, results = "asis", ',
          'message=F, warning=F}', collapse = ''), 
    'amplican::metaplot_mismatches(alignments, config, "Group", "{{i}}")',
    '```\n'))
})
# knit the source
res = knitr::knit_child(text = src, quiet = TRUE)
cat(res, sep = '\n')
```

<script>
//add logo to upper right corner
$(document).ready(function() {
$head = $('#header');
$head.prepend('<img src="data:image/png;base64,[encoded data, 10148 characters]" style=\"float: right;width: 150px;\"/>')
});
</script>
