---
title: "FooDMe - Food DNA Metabarcoding - analysis report"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
theme: default
---

<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r setup, include=FALSE}
# logging
log = file(snakemake@log[[1]], open="wt")
sink(log)
sink(log, type = "message")

knitr::opts_chunk$set(out.width = '80%',fig.asp= 0.5,fig.align='center',echo=FALSE, warning=FALSE, message=FALSE)
options(markdown.HTML.header = system.file("misc", "datatables.html", package = "knitr"))

library(DT, quietly = T)
library(tidyverse, quietly = T)

executor <- Sys.info()["user"]
```

---
author: `r paste0(executor)`
---

```{r init}
workdir <- snakemake@params[["workdir"]]

overview <- snakemake@input[["summary"]]
fastp <- snakemake@input[["fastp"]]
qc_filtering <- snakemake@input[["qc_filtering"]]
clustering <- snakemake@input[["clustering"]]
blast_rep <- snakemake@input[["blast_rep"]]
taxonomy <- snakemake@input[["taxonomy"]]
result <- snakemake@input[["result"]]
db <- snakemake@input[["db"]]
soft <- snakemake@input[["soft"]]

OTU_bool <- snakemake@params[["method"]] == "otu" # store True if using OTU 

# infer run name from workdir
run <- basename(workdir)
#head(tail(strsplit(workdir,"/")[[1]],2),1)

reportAll <- snakemake@params[["sample"]] == "all"

# Number of samples
nsamples <- nrow(read.csv(file = overview, sep = "\t", check.names=FALSE))
```

# Analysis report for run: `r run` {.tabset}

## Overview

* Run name: `r run`

* Number of samples: `r nsamples`

* Run directory: `r workdir`

* config file: <a href=../config.yaml>`r file.path(workdir,"config.yaml")`<a/>

* log files in folder: `r file.path(workdir,"logs")`

* snakemake logs in folder: `r file.path(workdir,".snakemake")`

* FooDMe version: `r snakemake@params[["version"]]`

## Quality summary

```{r summary}
data_table <- read.csv(file = overview, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

## Trimming statistics

Click on 'file' in the links column to access the full fastp html report.

```{r trimming}
data_table <- read.csv(file = fastp, sep = "\t", check.names=FALSE)

# Create hyperlinks
data_table$links <- paste0("<a href=", data_table$link_to_report, ">file</a>")
data_table$link_to_report = NULL

datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

```{r filtering_header, results='asis', eval=OTU_bool}
cat("## Read filtering statistics\n")
```
```{r filtering_table, eval=OTU_bool}
data_table <- read.csv(file = qc_filtering, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

```{r clustering_header, results='asis', eval=OTU_bool}
cat("## Clustering statistics\n")
```
```{r clustering_table, eval=OTU_bool}
data_table <- read.csv(file = clustering, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

```{r denoising_header, results='asis', eval=!OTU_bool}
cat("## Denoising statistics\n")
```
```{r denoising_table, eval=!OTU_bool}
data_table <- read.csv(file = clustering, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

## Taxonomic assignment

```{r blast_rep}
data_table <- read.csv(file = blast_rep, sep = "\t", check.names=FALSE)
#Process links
data_table$blast_report <- paste0("<a href=", data_table$link_report, ">file</a>")
data_table$link_report = NULL
data_table$filtered_report <- paste0("<a href=", data_table$link_filtered, ">file</a>")
data_table$link_filtered = NULL
						
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						),
						deferRender = TRUE,
						scroller = TRUE
						))
```

## Taxonomic assignment statistics

```{r taxonomy}
data_table <- read.csv(file = taxonomy, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						)
						))
```

## Metabarcoding results

```{r results}
data_table <- read.csv(file = result, sep = "\t", check.names=FALSE)
datatable(data_table, filter = 'top', rownames = FALSE, escape = FALSE,
		extensions = list("ColReorder" = NULL, "Buttons" = NULL),
		options = list(	
					dom = 'BRrltpi',
					autoWidth=FALSE,
					scrollX = TRUE,
					lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
					ColReorder = TRUE,
					buttons =
					list(
						'copy',
						'print',
						list(
						extend = 'collection',
						buttons = c('csv', 'excel', 'pdf'),
						text = 'Download'
						),
						I('colvis')
						),
						deferRender = TRUE,
						scroller = TRUE
						))
```

## Graphical results

```{r iframe}
if (snakemake@params[["sample"]] == "all") {
	krona_source <- "krona_chart.html"
} else {
	krona_source <- paste0(snakemake@params[["sample"]], "_krona_chart.html")
}

htmltools::tags$iframe(title = "Krona chart", src = krona_source, width ="100%", height="800px") 
```

Link to chart: <a href=`r krona_source`>`r krona_source`<a/>

## Versions

### Databases

```{r db_versions}
db_table <- read.csv(file = db, sep = "\t", check.names=FALSE)
knitr::kable(db_table)
```

### Softwares

```{r soft_versions}
soft_table <- read.csv(file = soft, sep = "\t", check.names=FALSE)
knitr::kable(soft_table)
```

## Help

[Link to the repository](https://github.com/CVUA-RRW/FooDMe)


[Report a bug or ask a question](https://github.com/CVUA-RRW/FooDMe/issues/new)


[Last release](https://github.com/CVUA-RRW/FooDMe/releases)
