---
author: "`r paste0('Biodose Tools', ' v', utils::packageVersion(pkg = 'biodosetools'))`"
date: "`r format(Sys.time(), '%d %B, %Y %H:%M:%S')`"
output: word_document

params:
  fit_results_list: NA
  aberr_module: NA
---

```{r setup, include=FALSE}
library(dplyr)
library(ggplot2)
```

```{r parsed-title, include=FALSE}
aberr_module <- params$aberr_module

parsed_title <- dplyr::case_when(
  aberr_module == "dicentrics" ~ "Dicentrics curve fitting report",
  aberr_module == "translocations" ~ "Translocation FISH curve fitting report",
  aberr_module == "micronuclei" ~ "Micronuclei curve fitting report"
)
```

---
title: `r parsed_title`
---

```{r render-table-fn, include=FALSE}
pander::panderOptions("table.style", "grid")
pander::panderOptions("table.split.table", Inf)
pander::panderOptions(
  "table.alignment.default",
  function(df) {
    ifelse(sapply(df, is.numeric), "center", "left")
  }
)

render_table <- function(x, ...) {
  pander::pander(x)
  # TODO: handle alignments via justify parameter
}
```

```{r load-data, echo=FALSE}
# General parameters
count_data <- params$fit_results_list[["fit_raw_data"]]
fit_model_statistics <- params$fit_results_list[["fit_model_statistics"]]
fit_model_summary <- params$fit_results_list[["fit_model_summary"]]
fit_coeffs <- params$fit_results_list[["fit_coeffs"]]
fit_var_cov_mat <- params$fit_results_list[["fit_var_cov_mat"]]
fit_cor_mat <- params$fit_results_list[["fit_cor_mat"]]
fit_formula_tex <- params$fit_results_list[["fit_formula_tex"]]
gg_curve <- params$fit_results_list[["gg_curve"]]
irr_conds <- params$fit_results_list[["irr_conds"]]
# detection_lims <- params$fit_results_list[["detection_lims"]]

# Translocations
genome_factor <- params$fit_results_list[["genome_factor"]]
chromosome_table <- params$fit_results_list[["chromosome_table"]]
trans_sex <- params$fit_results_list[["trans_sex"]]
frequency_select <- params$fit_results_list[["frequency_select"]]
```


`r if (aberr_module == "translocations") {"# Chromosome data"}`

`r if (aberr_module == "translocations") {
  paste("The analysed blood sample comes from a", trans_sex, "individual.")
}
`

```{r chromosome-table, echo=FALSE}
if (aberr_module == "translocations") {
  num_cols <- as.numeric(ncol(chromosome_table))

  chromosome_table %>%
    dplyr::mutate(
      dplyr::across(
        .cols = dplyr::everything(),
        .fns = function(x) {
          x <- ifelse(is.na(x) | x == "FALSE", "", x)
          x <- ifelse(x == "TRUE", "$\\checkmark$", x)
          return(x)
        }
      )
    ) %>%
    render_table(align =  "c")
}
```

`r if (aberr_module == "translocations") {
  if (num_cols == 1) {
    "where each chromosome was stained using M-FISH."
  }
}`


# Count data used

```{r count-data, echo=FALSE}
data <- count_data %>%
  biodosetools:::fix_count_data_names(type = "count", output = "kable")

data %>% 
  render_table(align = "c")
# TODO: handle u > 1.96 highlighting
```


# Results

## Fit formula

`r paste0("$$", fit_formula_tex, "$$")`

## Model

`r gsub("<=", "$\\\\leq$", fit_model_summary)`

`r if (aberr_module == "translocations") {"## Translocation frequency"}`

`r if (aberr_module == "translocations") {
  paste("The fitting was performed using the ")
}
`
`r if (aberr_module == "translocations") {
  if (frequency_select == "full_gen_freq") {
    paste("full genome translocation frequency.")
  } else if (frequency_select == "measured_freq") {
    paste("translocation frequency measured by FISH.")
  }
}
`

`r if (aberr_module == "translocations") {"## Genomic conversion factor"}`

`r if (aberr_module == "translocations") {
  paste0("The genomic conversion factor to full genome is ", as.character(round(genome_factor, 3)), ".")
}
`

## Coefficients

```{r fit-coeffs, echo=FALSE}
fit_coeffs %>%
  formatC(format = "e", digits = 3) %>%
  as.data.frame() %>%
  biodosetools:::fix_coeff_names(type = "rows", output = "kable") %>%
  render_table(align = "c")
```

## Model-level statistics

```{r fit-model-statistics, echo=FALSE}
fit_model_statistics %>%
  formatC(format = "f", digits = 3) %>%
  as.data.frame() %>%
  dplyr::mutate(df = as.integer(df)) %>%
  render_table(align = "c")
```

## Correlation matrix

```{r fit-cor-mat, echo=FALSE}
fit_cor_mat %>%
  biodosetools:::fix_coeff_names(type = "rows", output = "kable") %>%
  biodosetools:::fix_coeff_names(type = "cols", output = "kable") %>%
  formatC(format = "f", digits = 3) %>%
  as.data.frame() %>%
  render_table(align = "c")
```

## Variance-covariance matrix

```{r fit-var-cov-mat, echo=FALSE}
fit_var_cov_mat %>%
  biodosetools:::fix_coeff_names(type = "rows", output = "kable") %>%
  biodosetools:::fix_coeff_names(type = "cols", output = "kable") %>%
  formatC(format = "e", digits = 3) %>%
  as.data.frame() %>%
  render_table(align = "c")
```

## Irradiation conditions

```{r irr-conds, echo=FALSE}
data.frame(
  matrix(
    unlist(irr_conds),
    nrow = length(irr_conds),
    byrow = TRUE
  )
) %>%
  `colnames<-`(c("Characteristic", "Details")) %>%
  dplyr::mutate(
    Details = ifelse(Details == "", "Not specified", Details)
  ) %>%
  render_table(align = "l")
```

## Curve plot

```{r gg-curve, echo=FALSE, fig.width=6, fig.height=3}
gg_curve
```
