## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.5,
  out.width = "100%"
)

## ----setup--------------------------------------------------------------------
library(ambre)
set.seed(2024)

## ----libs---------------------------------------------------------------------
library(dplyr)
library(purrr)

## ----install, eval = FALSE----------------------------------------------------
#  remotes::install_git(
#    "https://forge.inrae.fr/reversaal/reut/ambre-package.git"
#  )

## ----example-files------------------------------------------------------------
list.files(system.file(package = "ambre"), pattern = "\\.xlsx$")

## ----scenario-----------------------------------------------------------------
scenario <- create_scenario(
  system.file("input_1culture_2pop.xlsx", package = "ambre")
)
dim(scenario)

## ----scenario-glimpse---------------------------------------------------------
dplyr::glimpse(
  scenario[, c("CropName", "PopulationName", "PathName",
               "CropID", "PopulationID", "PathID", "config")]
)

## ----run, results = "hide"----------------------------------------------------
library(dplyr)
scenario <- create_scenario(system.file("input_1culture_2pop.xlsx", package = "ambre"))
regulation_reduction <- config_ambre$regulation$regulation_value |> filter(Country == "France") |>
                          select(-c(Concentration, Country, RegulationID))
regulation_concentration <- config_ambre$regulation$regulation_value |> filter(Country == "France") |>
                          select(-c(Country, RegulationID, Reduction))
plots <- run_qmra_initial_situation(scenario = scenario, 
                     pathogen = c("Campylobacter jejuni", "Norovirus"),
                     regulationLog = regulation_reduction,
                     regulationConcentration = regulation_concentration)

## ----run-names----------------------------------------------------------------
names(plots)

## ----fig-logreduction---------------------------------------------------------
plots$logreduction

## ----fig-dalys----------------------------------------------------------------
plots$dalys

## ----fig-matrix-log-----------------------------------------------------------
plots$regul_matrix_log

## ----fig-matrix-concentration-------------------------------------------------
plots$regul_matrix_concentration

## ----plot-inflow--------------------------------------------------------------
scenario_with_inflow <- inflow_concentration(scenario = scenario,
 pathogenName = c("Campylobacter jejuni", "Norovirus"))
plot_inflow(scenario_with_inflow)

## ----plot-volume--------------------------------------------------------------
scenario_volume <-
  scenario_with_inflow |>                     
  mutate(
    volume = map(
      .x = config,                                         
      .f = ~ simulate_exposure(config = .x)              
    )
  )

plot_volumes(scenario_volume)

## ----plot-infection-probability-----------------------------------------------
scenario_dose_ini <- initial_dose_calculation(scenario_volume)

scenario_scheme <- update_treatment_scheme(scenario_dose_ini)

scenario_with_logreduc_and_co <- scenario_scheme |> 
  mutate( log_reduction = map(config, simulate_treatment))

scenario_final_dose_and_co_test <- final_dose_calculation(scenario_with_logreduc_and_co)

scenario_inf_proba_and_co <- infection_probability_calculation(scenario_final_dose_and_co_test)

scenario_illness_proba_and_co <- illness_probability_calculation(scenario_inf_proba_and_co)

scenario_dalys_and_co <- dalys_calculation(scenario_illness_proba_and_co)

scenario_risk_total_and_co <-get_risk_total(scenario_dalys_and_co)

plot_infection_probability(scenario_risk_total_and_co)

