---
title: "Supplement to: Algorithms for Automatized Detection of Hook Effect-bearing Amplification Curves"
author: "Stefan R&ouml;diger, Micha&#322; Burdukiewicz, Konstantin A. Blagodatskikh, Werner Lehmann, Peter Schierack, Andrej-Nikolai Spiess"
date: "`r Sys.Date()`"
output: 
  rmarkdown::pdf_document:
    number_sections: true
    toc: true
bibliography: "literature.bib"
vignette: >
  %\VignetteIndexEntry{Hook Effect}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Introduction

This is a supplement document for the study *Algorithms for Automatized 
Detection of Hook Effect-bearing Amplification Curves*. The functions and data 
presented in the paper are available from <https://github.com/devSJR/PCRedux>. 
The data, including the RDML file are part of the `PCRedux` package and are 
made available in the CSV or RDML format [@rodiger_enabling_2017] for a vendor 
independent analysis.

All analysis were implemented and done with the **R** statistical computing 
language [@R_language, @rodiger_r_2015] and dedicated integrated development 
environments such as **RKWard** [@rodiger_rkward:_2012]. Further documentation 
can be found in the help files of the **R** packages.

We added the source code for the generation of the figures in the main text at 
the end of the document.

> Note: Some rows in the tables below appear to be empty. This expected 
behaviour may occur in cases where the corresponding functions were not able to 
calculate the coefficients due to a failed model fit or violation of truncation 
criterion.

## Results for the analysis of the hookreg.rdml data set by human rater

All calculations in the following sections were done with `hookreg.rdml` data 
set, which is part of the `PCRedux` package. The data were transfered to the 
**R** environment by the `RDML` package [@rodiger_enabling_2017].

```{r, echo=TRUE, message=FALSE, warning=FALSE}
# Load the RDML package for reading of the hookreg.rdml-file from the PCRedux
# package. The magrittr package is used for pipes.

library(RDML)
library(PCRedux)
library(magrittr)

# A comprehensive description of the RDML-file import can be found in Rödiger 
# et al. (2017) Bioinformatics

raw_data <- RDML$new(filename=system.file("hookreg.rdml", package="PCRedux"))

raw_data_tab <- raw_data$AsTable(name.pattern=paste(
  react$position,
  react$sample$id,
  # run id added to names
  sep="~"))
data <- as.data.frame(raw_data$GetFData(raw_data_tab, long.table=FALSE))

# Plot all data of the hookreg.rdml-file
matplot(data[, 1], data[, -1], type="l", lty=1, lwd=2, ylab="RFU", xlab="Cycle")
```

Give the first three and last three entires of the data for data evaluation

```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)
print(xtable(t(rbind(head(data, 10), tail(data, 10)))), scalebox='0.55', floating=FALSE)
```

Printout of all measured samples, their rating by two humans (rater 1 and rater 
2) with their dichotomous ratings (0, no hook; 1, hook) and their sources.

- The boggy data (qpcR::boggy) set was taken from the `qpcR` package 
[@Ritz2008, @spiess_highly_2008].
- The C127EGHP data (chipPCR::C127EGHP) set was taken from the `chipPCR` package 
[@rodiger_chippcr:_2015].
- The testdat data (qpcR::testdat) set was taken from the `qpcR` package 
[@Ritz2008, @spiess_highly_2008].
- Other data were prepared by Evrogen laboratory experiments.


```{r eval=TRUE,echo=FALSE,results="asis"}
library(readxl)
library(xtable)
options(xtable.comment=FALSE)

Table_human_rated <- read_xlsx(path=system.file("Table_human_rated.xlsx", package="PCRedux"))

print(xtable(Table_human_rated, digits=0),
      size = "normalsize",
      include.rownames = FALSE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE,
      table.placement = "!ht", scalebox='0.55', floating=FALSE
)
```

## Results for the analysis of the hookreg.rdml data set with hookreg


```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(PCRedux)
library(magrittr)
suppressMessages(library(qpcR))

res_hookreg <- sapply(2L:ncol(data), function(i) {
  hookreg(x=data[, 1], y=data[, i])
}) %>% t %>% data.frame(sample=colnames(data)[-1],.)


res_hookreg_table <- data.frame(sample=as.character(res_hookreg[["sample"]]),
                                intercept=signif(res_hookreg[["intercept"]], 2),
                                slope=signif(res_hookreg[["slope"]], 1),
                                hook.start=signif(res_hookreg[["hook.start"]], 0),
                                hook.delta=signif(res_hookreg[["hook.delta"]], 0),
                                p.value=signif(res_hookreg[["p.value"]], 4),
                                CI.low=signif(res_hookreg[["CI.low"]], 2),
                                CI.up=signif(res_hookreg[["CI.up"]], 2),
                                hook.fit=res_hookreg[["hook.fit"]],
                                hook.CI=res_hookreg[["hook.CI"]],
                                hook=res_hookreg[["hook"]]
)
```

Printout of all measured samples and their sources.


```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)
print(xtable(res_hookreg_table),
      size = "normalsize",
      include.rownames = FALSE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE,
      table.placement = "!ht", scalebox='0.55', floating=FALSE
)
```

## Results for the analysis of the hookreg.rdml data set with hookregNL


```{r, echo=TRUE, message=FALSE, warning=FALSE}
res_hookregNL <- suppressMessages(sapply(2L:ncol(data), function(i) {
  hookregNL(x=data[, 1], y=data[, i])
}) %>% t %>% data.frame(sample=colnames(data)[-1],.))

res_hookregNL_table <- data.frame(sample=as.character(res_hookregNL[["sample"]]),
                                  slope=signif(as.numeric(res_hookregNL[["slope"]]), 1),
                                  CI.low=signif(as.numeric(res_hookregNL[["CI.low"]]), 2),
                                  CI.up=signif(as.numeric(res_hookregNL[["CI.up"]]), 2),
                                  hook.CI=unlist(res_hookregNL[["hook"]])
)
```

```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)

print(xtable(res_hookregNL_table),
      size = "normalsize",
      include.rownames = FALSE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE,
      table.placement = "!ht", scalebox='0.55', floating=FALSE
)
```


## Comparison of the methods

```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(readxl)
library(dplyr)

Table_human_rated <- read_xlsx(path=system.file("Table_human_rated.xlsx", package="PCRedux"))

res <- data.frame(sample=Table_human_rated[, "Sample"],
                  hr=Table_human_rated[, "Hook effect-like\nRater 2"],
                  hookreg=res_hookreg_table[, "hook"],
                  hookregNL=data.frame(unlist(res_hookregNL[, "hook"]))
)
colnames(res) <- c("Sample",
                   "Human rater",
                   "hookreg",
                   "hookregNL"
                )
```

```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)

print(xtable(res, digits=0),
      size = "normalsize",
      include.rownames = FALSE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE,
      table.placement = "!ht", scalebox='0.55', floating=FALSE
)
```


```{r, echo=FALSE, message=FALSE, warning=FALSE}
meta_hookreg <- sapply(1:nrow(res), function(i){
  ifelse(res[i, "hookreg"] == 1 || res[i, "hookregNL"] == 1, 1, 0)
})

res_out <- data.frame(Sample=res[["Sample"]], res[["Human rater"]], res_hookreg[["hook"]], res_hookregNL_table[["hook.CI"]], meta_hookreg)

colnames(res_out) <- c("Sample",
                       "Human rater",
                       "hookreg",
                       "hookregNL",
                       "hookreg and hoohkreNL combined"
)
```

```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)

print(xtable(res_out, digits=0), scalebox='0.55', floating=FALSE)
```

# Performance analysis

The performance of the **hookreg()** function and **hookregNL()** function was analyzed with 
the **performeR()** function of the `PCRedux` package. The methods were adopted from @brenner_variation_1997 and @kuhn_building_2008.

```{r echo=TRUE, message=FALSE, warning=FALSE}
res_performeR <- rbind(
  hookreg=performeR(res_out[["hookreg"]], res_out[["Human rater"]]),
  hookregNL=performeR(res_out[["hookregNL"]], res_out[["Human rater"]]),
  combined_hookreg=performeR(res_out[["hookreg and hoohkreNL combined"]], 
                               res_out[["Human rater"]])
) %>% t %>% signif(4)

colnames(res_performeR) <- c("hookreg", "hookregNL", "hookreg and hookregNL")
```

```{r results='asis', echo=TRUE, message=FALSE, warning=FALSE}
library(xtable)
options(xtable.comment=FALSE)

print(xtable(res_performeR, digits=0),
      size = "normalsize",
      include.rownames = TRUE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE,
      table.placement = "!ht", scalebox='0.75', floating=FALSE
)
```

# Hook effect analysis with **rdmlEdit** GUI

One can analyze data with GUI by installing `RDML` package and run **rdmlEdit** as local server, or open [rdmlEdit app](http://shtest.evrogen.net/rdmlEdit/) at Evrogen server.
After opening **rdmlEdit** you can see several tabs at the top of the screen. Inside *Files* tab click *Browse...* button to select your file. After, the file is opened you can see its structure at right of the screen presented as dendrogramm. 

Click to the `qPCR` tab to start analysis. Select a hook detection method at *Hook Detection Method* selector and wait for analysis. Results can be viewed inside the table (*hook* column) or by color and shape of PCR curves (select *Hook* at *Color by* and *Line Type by* selectors).

# References
