---
title: "ngsReports: Default FastQC Summary Report"
subtitle: "`r getwd()`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  html_document: 
    fig_caption: yes
    fig_height: 6
    fig_width: 10
    toc: yes
params:
    tgtsFile: "targets.csv"
    usePlotly: TRUE
    gcType: "Transcriptome"
    species: "Hsapiens"
    nOver: 30
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    echo = FALSE,
    results = "asis",
    message = FALSE, 
    warning = FALSE,
    error = FALSE
    )
```


```{r}
library(ngsReports)
library(dplyr)
library(ggplot2)
library(tibble)
library(kableExtra)
library(pander)
library(scales)
library(magrittr)
library(stringr)
library(readr)
```

```{r setGlobals}
globals <- list(
    usePlotly = params$usePlotly,
    cluster = TRUE,
    dendrogram = TRUE,
    theoreticalGC = TRUE,
    gcType = params$gcType,
    species = params$species
    )
```


```{r fastqcData}
fastqcFiles <- list.files(pattern = "(fastqc.zip|fastqc)$")
stopifnot(length(fastqcFiles) > 1)
message("FastQC files found. Loading FastQC data")
fastqcData <- tryCatch(FastqcDataList(fastqcFiles))
plotLabels <- structure(
    gsub(".(fastq|fastq.gz|bam)", "", fqName(fastqcData)),	
    names = fqName(fastqcData)
)
```

```{r outputParams}
n <- length(fastqcData)
fh <- max(0.25*n, 6)
```


```{r}
message("Checking for ", params$tgtsFile)
if (file.exists(params$tgtsFile)) {
	message("Found targets.csv...checking columns")
	targets <- read_csv(params$tgtsFile)
	fCol <- grep("[Ff]ile[Nn]ame", colnames(targets))
	lCol <- grep("[Ll]abel", colnames(targets))
	if (length(fCol) == 1 && length(lCol) == 1) {
		stopifnot(all(fqName(fastqcData) %in% targets[[fCol]]))
		message("Alternate labels found")
		plotLabels <- structure(targets[[lCol]], names = targets[[fCol]])
	}
	else{
		message("No valid labels found")
	}
}
if (!file.exists(params$tgtsFile)) {
	message(params$tgtsFile, " not found. Using default labels")
}
```


# FastQC Summary


```{r summaryTable}
bs <- getModule(fastqcData, "Basic_Statistics")
bs %>%
	mutate(
	    Sequence_length = paste(Shortest_sequence, Longest_sequence, sep = "-")
	) %>%
	dplyr::select(Filename, contains("sequence"), `%GC`, -contains("est")) %>%
	set_names(gsub("_", " ", names(.))) %>%
    set_names(str_to_title(names(.))) %>%
    set_names(gsub("Sequences", "", names(.))) %>%
    set_names(str_trim(names(.))) %>%
    dplyr::rename(`%GC` = `%Gc`) %>%
    kable(
        align = "lrrrr",
        caption = "Summary statistics for all libraries",
        format.args = list(big.mark = ",")
    ) %>%
    kable_styling(bootstrap_options = c("striped", "condensed")) %>%
    scroll_box(height = ifelse(n > 20, "650px", "100%")) 
```

## Read Totals

Library Sizes ranged between `r pander(comma(range(readTotals(fastqcData)$Total_Sequences)))` reads.

```{r plotReadTotals, fig.height=fh, fig.cap = "Read totals for each library. Duplicated reads are conventionally an high overestimate at this point."}
plotReadTotals(fastqcData, labels = plotLabels, usePlotly = globals$usePlotly)
```


## FastQC Summary

```{r plotSummary, fig.height=fh + 1, fig.cap="Summary of FastQC flags for each parameter"}
plotSummary(fastqcData, labels = plotLabels, usePlotly = globals$usePlotly)
```

## Per Base Sequence Quality

```{r plotBaseQuals, fig.height=fh, fig.cap = "Heatmap showing mean base qualities for each library"}
plotBaseQuals(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

## Per Sequence Quality Scores

```{r plotSeqQualsHeatmap, fig.height=fh, fig.cap = "Heatmap showing mean sequence qualities for each library"}
plotSeqQuals(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

## Per Base Sequence Content

```{r plotSeqContent, fig.height=fh, fig.cap = "Heatmap of summed base distributions along each read"}
plotSeqContent(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

## Per Sequence GC Content

```{r plotGCHeat, fig.height=fh, fig.cap = paste("GC Content Heatmap normalised to theoretical GC content in the", globals$species, globals$gcType)}
plotGcContent(fastqcData, labels = plotLabels, theoreticalGC = globals$theoreticalGC, gcType = globals$gcType, species = globals$species, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```


```{r plotGCLine,fig.cap = paste("GC Content Distributions for all reads showing theoretical GC content from the", globals$species, globals$gcType)}
plotGcContent(fastqcData, labels = plotLabels, theoreticalGC = globals$theoreticalGC, gcType = globals$gcType, species = globals$species, plotType = "line", usePlotly = globals$usePlotly)
```

## Sequence Length Distribution

```{r plotSeqLengthDistn, fig.height=fh}
plotSeqLengthDistn(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

```{r plotCumulativeLengthDistribution}
plotSeqLengthDistn(fastqcData, plotType = "cumulative", labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

## Sequence Duplication Levels

```{r plotDupLevels, fig.height=fh}
plotDupLevels(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly)
```

## Adapter Content

```{r plotAdapterContent, fig.cap="Universal Adapter Content"}
plotAdapterContent(fastqcData, labels = plotLabels, cluster = globals$cluster, dendrogram = globals$dendrogram, usePlotly = globals$usePlotly) 
```


## Overrepresented Summary

```{r Overrepresented_summary, fig.cap="Total overrepresented sequences for each library"}
plotOverrep(fastqcData, labels = plotLabels, usePlotly = globals$usePlotly)
```

## Overrepresented Sequences

```{r Overrepresented_sequences, echo=FALSE, results='asis', message=TRUE}
os <- getModule(fastqcData, "Overrepresented_sequences") 
if (length(os) > 0) {
    os %>% 
        mutate(Filename = plotLabels[Filename]) %>%
        group_by(Sequence, Possible_Source) %>%
        summarise(
            Total = sum(Count),
            Files = n(),
            Max_Percentage = max(Percentage/100)
        ) %>%
        ungroup() %>%
        mutate(
            `Overall Percentage` = percent(Total / sum(bs$Total_Sequences)),
            Max_Percentage = percent(Max_Percentage)
        ) %>%
        dplyr::arrange(desc(Total)) %>%
		dplyr::slice(1:params$nOver) %>%
        dplyr::select(
            Sequence, Total, `Present In` = Files, `Overall Percentage`, 
            Max_Percentage, Possible_Source
        ) %>%
        kable(
            align = "lrrrrl",
            caption = paste(
                "Summary of the most overrepresented sequences in all files.",
                "A maximum of", params$nOver, "sequences are shown"
            ),
            format.args = list(big.mark = ",")
        ) %>%
        kable_styling(bootstrap_options = c("striped", "condensed")) %>%
        scroll_box(width = "1200px",height = "100%")
}
if (length(os) == 0) {
	message("No overrepresented sequences were detected by FastQC")
}
```


# Session Information

```{r sessionInfo}
sessionInfo() %>% pander()
```

