---
title: "PERK-Walkthrough"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{PERK-Walkthrough}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r load_packages, message=FALSE, warning=FALSE, include=FALSE} 
library(fontawesome)
```

# Introduction

The aim of PERK is to predict and visualize concentrations of pharmaceuticals in the aqueous environment.

PERK acronym for **P**redicting **E**nvironmental concentration and **R**is**K**, is an R/Shiny application tool, aims to facilitate automated modelling and reporting of predicted environmental concentrations of a comprehensive set of pharmaceuticals derived from a wide range of therapeutic classes with different mode of action.

The tool helps users,

-   to input their measured concentration,
-   to compare the predicted and measured concentrations of the APIs by means of the PEC/MEC ratio,
-   to establish whether the predicted equations used tend to underestimate or overestimate measured values.
-   It provides a consistent interactive user interface in a familiar dashboard layout, enabling users to visualise predicted values and compare with their measured values without any hassles.
-   Users can download data and graphs generated using the tool in .csv or publication ready images (.pdf, .eps).

# Data sources:

## Prescription Data For England:

This tool uses the prescription data from [PrAna](https://bmcmedinformdecismak.biomedcentral.com/articles/10.1186/s12911-021-01727-z), an R package to calculate and visualize England NHS prescribing data.

The data used in PrAna are as follows,

-   Prescribing data and Practice information are from the monthly files published by the NHS Business Service Authority, used under the terms of the Open Government Licence.

-   BNF codes and names are also from the NHS Business Service Authority's Information Portal, used under the terms of the Open Government Licence.

-   dm+d weekly release data is also from the NHS Business Service Authority's Information Portal, used under the terms of the Open Government Licence.

## WWTP Data:

The following dataset are provided from WWTP collaborators,

-   Catchment map used to define the boundaries and capture the GP Practices inside the catchments for the prescription data calculations.

-   Daily flow data used to calculate the load and population equivalent.

-   Population Equivalent number of inhabitants per catchment zone.

-   Site information required to predict information such as recovery percentage.

-   Water quality parameters to predict population equivalent.

## API properties

-   Metabolites and Excretion factors collected from research articles and data repositories such as Drug bank.
-   Recovery percentage collected from research articles, calculated from measured concentration from previous experiments, predicted using WWTP site information.
-   Physio-chemical properties collected from research articles and data repositories.
-   Site information required to predict information such as recovery percentage.
-   Eco-toxicity data collected from research articles and data repositories.

# Workflow {#sec-workflow}

```{r, setup, include=FALSE}
knitr::opts_template$set(fullwidth = list(
  fig.width = 4, fig.height = 4, 
  fig.retina = 2, out.width = '50%', out.height = '50%'
))
```

The workflow in this tutorial consists of the following steps, as in the **Figure 1**.

-   Upload Data: Download template for the dataset and upload in the corresponding input holders.

-   Analysis and Visualisation (AV) Panel: Click on the relevant analysis and visualisation panel. PERK features three AV panels (1) Predicted, (2) Measured, (3) Predicted vs Measured.

-   Analysis and Visualisation settings (AVS): Click respective analysis and visualisation setting (AVS) tab, to select the option to analyse and visualise datatable/plot

-   Plot settings: Click on the plot settings such as, color and line width for the better/suitable visualisation.

-   Download data: Click on the download buttons to download generated plot/data in publication friendly .pdf/.eps or .csv files.

```{r fig-workflow, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 1: PERK Workflow",  out.width="100%"}
knitr::include_graphics("img/perk_workflow-min.png")
```


# PERK Features {#sec-features}

-   PERK consist of several features, broadly categorized as three panels

    (1) Upload Data
    (2) Predicted
    (3) Measured
    (4) Predicted vs Measured

-   Overview of the individual panels and their options can be found in **Figure 2** and will be discussed in the following sections.

```{r fig-features, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 2. PERK: Features",  out.width="100%"}
knitr::include_graphics("img/perk_features-min.png")
```

# Upload Data {#sec-upload-data}

```{r fig-upload-data, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 3. PERK: Upload Data",  out.width="100%"}
knitr::include_graphics("img/datainput-min.png")
```

| Part | Remarks                               |
|------|---------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel |
| 2    | Full screen                           |
| 3    | Dark and Light mode                   |
| 4    | Plot settings                         |
| 5    | Data selection Area                   |
| 6    | Upload File Button                    |
| 7    | Download Template for the file        |
| 8    | User Logout                           |

: Table 1. Upload Data: Data Input

-   In this panel, user can `Download template` for the dataset and upload in the corresponding input holders as in **Figure 3**.
-   User can click on the `Download template` button to generate the comma separated value (.csv) file.
-   Once the template is downloaded, user can add in or convert their dataset to corresponding template and upload it in the corresponding input holders to do the analysis and visualisation.

# Predicted (PC) {#sec-predicted}

-   Predicted (PC) panel, has two sub-panels
    (1) Prescription: to analyse and visualise the prescription trends and
    (2) Predicted Concentrations: to analyse and visualise the prediction trends based on user inputs.

# Predicted: Prescription {#sec-pred-presc}

```{r fig-predicted01, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 4. Predicted: Prescription - AV Panel.",  out.width="100%"}
knitr::include_graphics("img/predicted01-min.png")
```

-   Different parts of the `Predicted: Prescription` sub-panel and `PERK` dashboard is highlighted in the **Figure 4** and listed in the **Table 2**.

-   In Prescription sub-panel, user can select the period of their interest using the `Data Range` option, and select prescription type (raw or population normalized) value using `Target type` and the site using `Select the site` options in the analysis and visualisation settings (AVS) tab, as in **Figure 4**

| Part | Remarks                                                    |
|------|------------------------------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel                      |
| 2    | Full screen                                                |
| 3    | Dark and Light mode                                        |
| 4    | Plot settings                                              |
| 5    | Plot generated based on user selection                     |
| 6    | Analysis and Visualisation settings (AVS) panel            |
| 7    | User log-out                                               |
| 8    | Download buttons to download the generated plot as .pdf or .eps and data as .csv format |
| 9    | Show Datatable |

: Table 2. Predicted: Prescription Sub-Panel

-   Prescription trends in the Predicted (PC) panel, can generate long-term month wise raw prescription trends (kg/month), as in  **Figure 5** and population normalized daily loads based on prescription (PNDP) (mg/day/1000 inhabitants) as in  **Figure 6** .

-   User can download the generated plot as publication-friendly images in .pdf/.eps format, user can also download the images in .png format and data generated for the plot as .csv file using the download buttons present below the plot.

-   User can view the data table by checking the `Show Datatable` check box present below the download buttons.
```{r fig-prescraw, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 5. PC: kg/month.",  out.width="100%"}
knitr::include_graphics("img/prescplot01-1-min.png")
```

```{r fig-prescpndp, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 6. PC: PNDP.",  out.width="100%"}
knitr::include_graphics("img/prescplot02-1-min.png")
```

# Predicted: Predicted Concentration {#sec-pred-pec}

```{r fig-predicted02, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 7. Predicted: Predicted Concentrations - AV Panel.",  out.width="100%"}
knitr::include_graphics("img/predicted02-min.png")
```

-   Different parts of the `Predicted: Predicted Concentrations` sub-panel and `PERK` dashboard is highlighted in the  **Figure 7** and listed in the  **Table 3**.

| Part | Remarks                                                                                 |
|------------|------------------------------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel                                                   |
| 2    | Full screen                                                                             |
| 3    | Dark and Light mode                                                                     |
| 4    | Plot settings                                                                           |
| 5    | Plot generated based on user selection                                                  |
| 6    | Analysis and Visualisation settings (AVS) panel                                         |
| 7    | User log-out                                                                            |
| 8    | Download buttons to download the generated plot as .pdf or .eps and data as .csv format |
| 9    | Show Data table                                                                          |

: Table 3. Predicted: Predicted Concentrations Sub-Panel

-   In the predicted concentrations sub-panel, user can select the period of their interest using the `Data Range` option, and select prediction sample type (wastewater influent `INF`, wastewater effluent `EFF` and river `RDOWN`) using `Sample type` and the site using `Select the site` options in the analysis and visualisation settings (AVS) tab, as in  **Figure 7**

-   Two types of prediction values can be visualised in this panel based on the prescription data,

    -   `PEC_I`: This prediction considers prescription based on individual month,
    -   `PEC_II`: This prediction is based on the prescription per year.

::: {layout-ncol="2"}
```{r fig-predicted03, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 8. PC: concentration/month.",  out.width="100%"}
knitr::include_graphics("img/predplot01-1-min.png")
```

```{r fig-predicted04, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 9. PC: concentration/period.",  out.width="100%"}
knitr::include_graphics("img/predplot02-1-min.png")
```
:::

-   This panel visualise prediction per month for the selected period `(ng/L)` as in the **Figure 8**, and total prediction per selected period `(ng/L)`, as in the **Figure 9**

-   In addition, this panel also enables to compare month wise and total predicted concentration of selected pharmaceuticals over different environmental matrices, such as, `INF`, `EFF` and `RDOWN` and compare over different WWTPs in the study.

-   User can download the generated plot as publication-friendly images in .pdf/.eps format, user can also download the images in .png format and data generated for the plot as .csv file using the download buttons present below the plot.

-   User can view the data table by checking the `Show Datatable` check box present below the download buttons.

## Measured (MC) {#sec-measured}

```{r fig-measured01, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 10. Measured: Measured Concentrations - AV Panel.",  out.width="100%"}
knitr::include_graphics("img/measured01-min.png")
```

-   Different parts of the `Measured` tab and `PERK` dashboard is highlighted in the **Figure 10** and listed in the **Table 4**.

| Part | Remarks                                                                                 |
|------|------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel                                                   |
| 2    | Full screen                                                                             |
| 3    | Dark and Light mode                                                                     |
| 4    | Plot settings                                                                           |
| 5    | Plot generated based on user selection                                                  |
| 6    | Analysis and Visualisation settings (AVS) panel                                         |
| 7    | User log-out                                                                            |
| 8    | Download buttons to download the generated plot as .pdf or .eps and data as .csv format |
| 9    | Show Datatable                                                                          |

: Table 4. Measured Panel

-   In `Measured` panel as in the **Figure 10**, user can select the period of their interest using the `Data Range` option, and select sample matrix type (`INF` - wastewater influent, `EFF` - wastewater effluent, `RDOWN` - River Downstream, `RUP` - River upstream, `SPM` - Solids) using `Sample type`, based on the user input dataset.

-   User can select the measurement type (Concentration, DL - Daily Load, PNDL - Population normalised daily load) using `Measurement Type`, and the site by `Select the site` options in the analysis and visualisation settings (AVS) tab, as in **Figure 10**

-   User can download the generated plot as publication-friendly images in .pdf/.eps format, user can also download the images in .png format and data generated for the plot as .csv file using the download buttons present below the plot.

-   User can view the data table by checking the `Show Datatable` check box present below the download buttons.

::: {layout-ncol="2"}
```{r fig-mcmonth, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 11. MC: concentration/month.",  out.width="100%"}
knitr::include_graphics("img/mcplot01-1-min.png")
```

```{r fig-mctotal, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 12. MC: concentration/period.",  out.width="100%"}
knitr::include_graphics("img/mcplot02-1-min.png")
```
:::

-   Three types of measured values can be visualised in this panel based on the measurement data uploaded by the user,

    -   `Concentration (ng/L)`: This is the raw concentration values based on individual measurements.\
    -   `DL (mg/day)`: This is the Daily Load (DL) values based on measurments normalised with the daily flow of wastewater for the `INF`and `EFF`, and river for the `RDOWN` and `RUP`.
    -   `PNDL (mg/day/1000 inhabitants)`: This is the Population Normalised Daily Load values calculated based on the population in the WWTP catchment and the daily flow.

-   This panel visualise measurement per month for the selected period `(ng/L)` as in the **Figure 11**, and total measurement per selected period `(ng/L)`, as in the **Figure 12**

# Predicted vs Measured {#sec-pcvsmc}

-   Predicted vs Measured (PC vs MC) panel, has two sub-panels
    (1) Predicted vs Measured: to analyse and visualise the predicted trends vs measured trends and
    (2) Prediction Accuracy - PA: to analyse and visualise the prediction accuracy.

## Predicted vs Measured: Predicted vs Measured {#sec-pcvsmc-pcvsmc}

```{r fig-pcsvsmc01, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 13. Predicted vs Measured: Predicted vs Measured - AV Panel.",  out.width="100%"}
knitr::include_graphics("img/pcvsmc01-min.png")
```

-   Different parts of the `Predicted vs Measured: Predicted vs Measured` sub-panel and `PERK` dashboard is highlighted in the **Figure 13** and listed in the **Table 5**.

| Part | Remarks                            |
|------|------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel                                                   |
| 2    | Full screen                                                                             |
| 3    | Dark and Light mode                                                                     |
| 4    | Plot settings                                                                           |
| 5    | Plot generated based on user selection                                                  |
| 6    | Analysis and Visualisation settings (AVS) panel                                         |
| 7    | User log-out                                                                            |
| 8    | Download buttons to download the generated plot as .pdf or .eps and data as .csv format |
| 9    | Show Datatable                                                                          |

: Table 5. Predicted vs Measured: Predicted vs Measured Sub-Panel

-   In `Predicted vs Measured` sub-panel, user can select the period of their interest using the `Select Period` option, and select sample type (wastewater influent `INF`, wastewater effluent `EFF` and river `RDOWN`) using `Select Sample type` and the site using `Select the site` options in the analysis and visualisation settings (AVS) tab, as in **Figure 13**

-   Predicted vs Measured trends in the `Predicted vs Measured` (PCvsMC) panel, can generate measured concentration vs `PEC_I` and `PEC_II`, predictions based on monthly prescription as in **Figure 14** and prediction based on the prescription per year **Figure 15** respectively.

::: {layout-ncol="2"}

```{r fig-pcvsmcplot1, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 14. PCvsMC: PEC-I.",  out.width="100%"}
knitr::include_graphics("img/pcvsmcplot01-1-min.png")
```


```{r fig-pcvsmcplot2, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 15. PCvsMC: PEC-II.",  out.width="100%"}
knitr::include_graphics("img/pcvsmcplot02-1-min.png")
```

:::

-   User can download the generated plot as publication-friendly images in .pdf/.eps format, user can also download the images in .png format and data generated for the plot as .csv file using the download buttons present below the plot.

-   User can view the data table by checking the `Show Datatable` check box present below the download buttons.

## Predicted vs Measured: Prediction Accuracy {#sec-pcvsmc-pa}

-   Different parts of the `Predicted vs Measured: Prediction Accuracy` sub-panel and `PERK` dashboard is highlighted in the **Figure 16** and listed in the **Table 6**.
```{r fig-pcsvsmc02, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 16. Predicted vs Measured: Prediction Accuracy - AV Panel.",  out.width="100%"}
knitr::include_graphics("img/pcvsmc02-min.png")
```

| Part | Remarks                            |
|------|------------------------------------|
| 1    | Analysis and Visualisation (AV) Panel                                                   |
| 2    | Full screen                                                                             |
| 3    | Dark and Light mode                                                                     |
| 4    | Plot settings                                                                           |
| 5    | Plot generated based on user selection                                                  |
| 6    | Analysis and Visualisation settings (AVS) panel                                         |
| 7    | User log-out                                                                            |
| 8    | Download buttons to download the generated plot as .pdf or .eps and data as .csv format |
| 9    | Show Datatable                                                                          |

: Table 6. Predicted vs Measured: Prediction Accuracy Sub-Panel

-   In `Prediction Accuracy` sub-panel, user can select the period of their interest using the `Select Period` option, and select sample type (wastewater influent `INF`, wastewater effluent `EFF` and river `RDOWN`) using `Select Sample type` and the site using `Select the site` options in the analysis and visualisation settings (AVS) tab, as in **Figure 16**
-   Prediction Accuracy trends in the `Prediction Accuracy` (PA) panel, can generate trends in `PA_I` and `PA_II`, predictions based on monthly prescription as in **Figure 17** and prediction based on the prescription per year respectively.

```{r fig-paplot01, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 17. PCvsMC: PA-I.",  out.width="100%"}
knitr::include_graphics("img/paplot01-1-min.png")
```

-   User can download the generated plot as publication-friendly images in .pdf/.eps format, user can also download the images in .png format and data generated for the plot as .csv file using the download buttons present below the plot.

-   User can view the data table by checking the `Show Datatable` check box present below the download buttons.

# Plot Settings {#settings}

- Plot settings `r fa("screwdriver-wrench", fill = "grey")`, feature offers various options for controlling and customising the user interface using the `skin` tab; and plot elements using the `plot` tab.
- The `skin` tab as in the **Figure 18 A** provides settings for changing the theme of the `Navigation Bar`, `Sidebar`, and `Accents` in the user interface. This tab also offers the option to switch between `Dark and 'Light' mode for the Control Bar.
- The `plot` tab as in the **Figure 18 B** provides options for changing the fill colours in the generated plots.
- The `plot` tab also provides options for changing the plot line width in the X and Y axis and the point outline colour.


```{r fig-plotsetting, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure 18. Plot Settings, A: `Skin` Tab; B: `Plot` Tab.",  out.width="100%"}
knitr::include_graphics("img/plot-settings.png")
```

# Acknowledgements {#acknowledgements}

This package was built as a part of the **Wastewater Fingerprinting for Public Health Assessment (ENTRUST)** and **Innovative Pathway Control (IPC)** project funded by Wessex Water and EPSRC IAA (grant no. EP/R51164X/1).

## Disclaimer

We accept no liability for any errors in the data or its publication here: use this data at your own risk. You should not use this data to make individual prescribing decisions.
