---
output:
  pdf_document:
    fig_caption: yes
    includes:
        in_header: header.tex 
date: "`r format(Sys.time(), '%d %B, %Y')`"
params:
  sample: NA
  primers: NA
  templates: NA
  direction: NA
  settings: NA
  used_settings: NA

title: "`r paste0('Report for ', params$sample)`"
---

<!-- Vignette options and disclaimer-->

```{r child = 'disclaimer.Rmd'}
``` 

<!--dont show caption heading, this would clear the first page ... Primer Sets-->

```{r primer_table, results = 'asis'}
for (i in seq_along(params$primers)) {
    primer.df <- params$primers[[i]]
    template.df <- params$templates[[i]]
    id <- primer.df$Run[1]
    caption <-  paste0("Overview of the *", id, "* primer set.")
    out.df <- openPrimeR:::view.primers.report(primer.df, template.df)
    tab <- knitr::kable(out.df, row.names = FALSE, caption = caption,
                escape = TRUE, longtable = FALSE)
    print(tab)
    # pandoc doesn't support multi-column/row tables
    #row.idx <- which(colnames(out.df) == "Mismatches")
    #if (length(row.idx) != 0) {
        #tab <- kableExtra::add_header_above(tab, c("" = 1, "aa" = 2)
        #c("Mismatches" = row.idx[1], "" = tail(row.idx, n = 1)+1))
    #}
}
```

# Primer Set Summary

```{r primer_summary, results = 'asis', echo = FALSE}
#summary(cars)
tab <- openPrimeR:::get_comparison_table(params$templates, params$primers, params$sample)
caption <- paste0("Summary of the primer sets. Brackets indicate inter-quartile ranges.")
# show fewer columns to remain in the boundary of the page, don't show template (2nd column)
#knitr::kable(tab[,c(1,seq(3,5))], row.names = FALSE, caption = caption,
             #escape = TRUE, longtable = FALSE)
# use pander to split up the table instead:
pander::pander(tab[,c(1,seq(3,ncol(tab)))], caption = caption,
                split.table = 120, style = 'rmarkdown')

#########
# this is how a long table works (still too long ..)
#cat(paste0(
#"\\begin{table}[ht]\\centering\\rotatebox{90}{",
    #paste0(capture.output(
      #print(xtable(tab), floating=FALSE, comment=FALSE)),
      #collapse="\n"),
    #"}\\caption{Landscape table.}\\end{table}"))
############

```

# Coverage

```{r cvg_table, results = 'asis'}
cvg.stats <- openPrimeR::get_cvg_stats(params$primers, params$templates, for.viewing = TRUE)
knitr::kable(cvg.stats, row.names = FALSE, caption = "Percentage of covered template sequences per primer set and group of template sequences.", longtable = TRUE)
```

```{r coverage_plot, fig.show='hold', fig.width = 5, fig.height = 5, fig.cap = 'Coverage of each primer set.'}
openPrimeR:::plot_template_cvg(params$primers, params$templates)
```


```{r binding_plot_fw, fig.show='hold', fig.width = 6, fig.height = 6, fig.cap = 'The binding position of the primers relative to the target binding region of forward primers. The rectangles stretching along the x-axis indicate the target binding region of forward primers and the following region, respectively.'}
idx <- which(sapply(params$direction, function(x) x %in% c("fw", "both")))
if (length(idx) != 0) {
    openPrimeR::plot_primer_binding_regions(params$primers[idx], 
                params$templates[idx], relation = "fw")
}
```

```{r binding_plot_rev, fig.show='hold', fig.width = 6, fig.height = 6, fig.cap = 'The binding position of the primers relative to the target binding region of reverse primers. The rectangles stretching along the x-axis indicate the target binding region of reverse primers and the following region, respectively.'}
idx <- which(sapply(params$direction, function(x) x %in% c("rev", "both")))
if (length(idx) != 0) {
    openPrimeR::plot_primer_binding_regions(params$primers[idx], 
                    params$templates[idx], relation = "rev")
}
```

```{r cvg_vs_size, fig.show='hold', fig.width = 7, fig.height = 7, fig.cap = 'Coverage versus number of primers. Primer sets offering a good trade-off between coverage and set size will appear in the top left corner of the plot. The radius of each dot is correlated with the ratio at which the corresponding primer set fulfills the constraints.'}
openPrimeR::plot_cvg_vs_set_size(params$primers, params$templates)
```

# Physicochemical properties

```{r plot_constraints_fulfilled_overall, fig.show='hold', fig.width = 5, fig.height = 5, fig.cap = 'Percentage of constraints that are fulfilled by each primer set.'}
# not enough space for p-vals
#openPrimeR::plot_constraint_signature(params$primers, params$templates,  # TODO: does not work in package yet.
openPrimeR::plot_constraint_fulfillment(params$primers,
                                params$settings, 
                                plot.p.vals = FALSE) 
```

```{r plot_constraints_binding, fig.show='hold', fig.width = 5, fig.height = 5, fig.cap = 'Binding properties of the primers. The horizontal dashed lines indicate the desired property value ranges.'}
constraints <- c("primer_coverage", "primer_efficiency", "primer_specificity")
constraints <- constraints[constraints %in% names(openPrimeR::constraints(params$settings))]
openPrimeR::plot_constraint(params$primers, params$settings,
    active.constraints = c("primer_coverage", "primer_efficiency", 
                    "primer_specificity"))
```

```{r plot_constraints_structure, fig.show='hold', fig.cap = 'Structural properties of the primers. The horizontal dashed lines indicate the desired property value ranges.'}
constraints <- c("self_dimerization", "cross_dimerization", "secondary_structure")
constraints <- constraints[constraints %in% names(openPrimeR::constraints(params$settings))]
openPrimeR::plot_constraint(params$primers, params$settings,
      active.constraints = constraints)
```

```{r plot_constraints_composition, fig.show='hold', fig.cap = 'Properties relating to the nucleotide composition of the primers. The horizontal dashed lines indicate the desired property value ranges.'}
constraints <- c("primer_length", "no_runs",
                "no_repeats", "gc_ratio", 
                "gc_clamp", "melting_temp_range")
constraints <- constraints[constraints %in% names(openPrimeR::constraints(params$settings))]
openPrimeR::plot_constraint(params$primers, params$settings, 
    active.constraints = constraints)
```

<!-- Input parameters -->

```{r child = 'parameters.Rmd'}
```

```{r vignette_options_end, echo = FALSE}
# reset theme to default setting
ggplot2::theme_set(old.theme)
```
