---
title: "Pk/Pv/Pf Serology Tutorial"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pk/Pv/Pf Serology Tutorial}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(rmarkdown.html_vignette.check_title = FALSE)
setup <- function() {
  needed <- c("knitr", "rmarkdown", "tidyverse", "kableExtra")
  
  lapply(needed, function(pkg) {
    if (requireNamespace(pkg, quietly = TRUE)) {
      library(pkg, character.only = TRUE)
    }
  })
}

setup()
library(SeroTrackR)
```

For all of these analyses you can run as many plates as you wish. 

### Visualisation of the Pk/Pv/Pf Pipeline

![](../man/figures/SeroTrackR_Pk_Pf_Pv.jpeg)

```{r setup pk analysis, eval=FALSE}
library(SeroTrackR)
library(tidyverse)
```

### 5-Point Standard Curve

#### Step 1: Load your data!

Firstly, we will be using our example data that's in-built in the package. Here replace the `system.file()` argument with the file path for your package.

```{r}
your_raw_data_5std <- c(
  system.file("extdata", "example_MAGPIX_pk_5std_plate1.csv", package = "SeroTrackR"),
  system.file("extdata", "example_MAGPIX_pk_5std_plate2.csv", package = "SeroTrackR")
)
your_plate_layout_5std <- system.file("extdata", "example_platelayout_pk_5std.xlsx", package = "SeroTrackR")
```

#### Step 2: Read your data and process MFI to RAU

This function to (a) process raw Serological data and (b) convert MFI to RAU. The `runPlasmoPipeline()` function will output three data frames:

1. All_Results: All columns of every MFI to RAU conversion 
2. MFI_RAU: Just the SampleID, Plate, MFI and RAU values per antigen
3. MFI_RAU_long: SampleID, Plate, MFI, RAU, Antigen, Species (long-format df)

```{r}
results_5stdcurve <- runPlasmoPipeline(
  raw_data = your_raw_data_5std,
  platform = "magpix",
  plate_layout = your_plate_layout_5std,
  panel = "panel1",
  std_point = 5, 
  experiment_name = "5-point standard curve"
)
```

##### Standard Curve Plot

```{r}
results_5stdcurve$std_curve
```

##### Bead Counts QC Plot

```{r}
results_5stdcurve$bead_counts
```

##### Blanks QC Ploat

```{r}
results_5stdcurve$blanks
```

##### MFI to RAU Tables

All results: 

```{r}
results_5stdcurve$mfi_outputs$All_Results %>%
  head() %>% 
  kable()
```

MFI and RAU only: 

```{r}
results_5stdcurve$mfi_outputs$MFI_RAU %>%
  head() %>% 
  kable()
```

MFI and RAU long table: 

```{r}
results_5stdcurve$mfi_outputs$MFI_RAU_long %>%
  head() %>% 
  kable()
```

### 10-Point Standard Curve

These steps are very similar to the 5-point standard curve, except where indicated. 

#### Step 1: Load your data!

```{r}
your_raw_data_10std <- c(
  system.file("extdata", "example_MAGPIX_pk_10std_plate1.csv", package = "SeroTrackR"),
  system.file("extdata", "example_MAGPIX_pk_10std_plate2.csv", package = "SeroTrackR")
)
your_plate_layout_10std <- system.file("extdata", "example_platelayout_pk_10std.xlsx", package = "SeroTrackR")
```

#### Step 2: Read your data and process MFI to RAU

```{r}
results_10stdcurve <- runPlasmoPipeline(
  raw_data = your_raw_data_10std,
  platform = "magpix",
  plate_layout = your_plate_layout_10std,
  panel = "panel1",
  std_point = 10, ################################### here make sure you write 10! 
  experiment_name = "10-point standard curve"
)
```

##### Standard Curve Plot

```{r}
results_10stdcurve$std_curve
```

##### Bead Counts QC Plot

```{r}
results_10stdcurve$bead_counts
```

##### Blanks QC Ploat

```{r}
results_10stdcurve$blanks
```

##### MFI to RAU Tables

All results: 

```{r}
results_10stdcurve$mfi_outputs$All_Results %>%
  head() %>% 
  kable()
```

MFI and RAU only: 

```{r}
results_10stdcurve$mfi_outputs$MFI_RAU %>%
  head() %>% 
  kable()
```

MFI and RAU long table: 

```{r}
results_10stdcurve$mfi_outputs$MFI_RAU_long %>%
  head() %>% 
  kable()
```
