---
title: "PvSeroApp Serological Assay Quality Control Report"
date: "`r format(Sys.Date(), '%d-%m-%y')`"
header-includes:
   - \usepackage[default]{sourcesanspro}
   - \usepackage[T1]{fontenc}
   - \usepackage{booktabs}
   - \usepackage{longtable}
   - \usepackage{array}
   - \usepackage{multirow}
   - \usepackage{graphicx}
   - \usepackage{float}
   - \usepackage{colortbl}
   - \usepackage{xcolor}
mainfont: SourceSansPro
output: 
  pdf_document: default
params:
  raw_data_filename: NA
  experiment_name: NA
  experiment_notes: NA
  platform: NA
  date: NA
  stdcurve_plot: NA
  plateqc_plot: NA
  blanks_plot: NA
  check_repeats_output: NA
  check_repats_table_pdf: NA
  model_plot: NA
  operator_output: NA
  volume_output: NA
  calibration_output: NA
  machine_output: NA
  plate_list_output: NA
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F, warning = FALSE, message = FALSE, tidy = TRUE, fig.width=8, fig.height=6, fig.align="center", df_print = "kable")
library(SeroTrackR)
library(dplyr)
library(ggplot2)
library(knitr)
library(kableExtra)
library(rmarkdown)
```

***
### QC Report for experiment: `r params$experiment_name` analysed on `r params$date`
***

#### Plot of Standard Curves

```{r standard curves plot}
params$stdcurve_plot
```
\newpage
#### Plot of Model Results

```{r model results plot, results='asis'}
first_plot <- TRUE                          # Flag to track the first plot
model_plot <- params$model_plot

# Loop through each plot in model_plot and print it
for (plot_name in names(model_plot)) {
  
  if (!first_plot){                         # Insert a page break before the plot but not for the first one 
    cat("\\newpage\n")                      # Page break before each plot
  }
  
  cat(paste0("##### ", plot_name, "\n\n"))  # Adds a title for each plot
  print(model_plot[[plot_name]])            # Render the actual plot
  
  first_plot <- FALSE                       # Update flag after first plot is printed 
}
```
\newpage

#### Plot of Bead Counts

Any wells with < 15 beads/well are indicated in red, and should be double-checked manually.

```{r bead counts plot}
plateqc_plot <- params$plateqc_plot
plateqc_plot + theme(legend.position = "bottom")
```

```{r check repeats table, echo=FALSE, results='asis'}
check_repeats_output <- params$check_repeats_output
check_repats_table_pdf <- params$check_repats_table_pdf

if (is.data.frame(check_repeats_output)) {
  # If it's a table, print it using kable
  tablel <- check_repats_table_pdf %>% 
    knitr::kable(
      format = "latex",
      align = "l",
      booktabs = TRUE,
      longtable = TRUE,
      linesep = "",
      ) %>%
    kableExtra::kable_styling(
        position = "center",
        latex_options = c("striped", "repeat_header"),
        stripe_color = "gray!15"
      )
  print(tablel)
} else {
  # If it's text, print it normally
  cat(check_repeats_output, "\n\n")
}
```
\newpage

#### Plot of Blank Samples

Blank samples should record MFI<50 for each protein. If any proteins are above the dashed line, they should be double-checked manually.

```{r blank samples plot}
blanks_plot <- params$blanks_plot
blanks_plot + theme(legend.position = "bottom")
```
\newpage

#### Experiment Information


**Experiment Notes**: `r params$experiment_notes`

**Platform Chosen**: `r params$platform`

**Raw Data Filename/s**: `r params$raw_data_filename`

**Operator**: `r params$operator_output`

**Acquisition volume**: `r params$volume_output`

**Recent Calibration and Verification results**: 
`r params$calibration_output`

**Machine Serial Number**: `r params$machine_output`

\newpage

#### Plate Layouts

\tiny 

```{r plate layouts, echo = FALSE, warning = FALSE, message = FALSE}
params$plate_list_output
```

