## ----include = [encoded data, 9983 characters]
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----reg-data, echo=FALSE, fig.align='center', out.width='100%'[encoded data, 9935 characters]
knitr::include_graphics("figures/registration_opt_diagram.png")

## ----load-greatR, [encoded data, 9980 characters]
# Load the package
library(greatR)
library(data.table)

## ----load-brapa-data, message=FALSE, [encoded data, 9961 characters]
# Load a data frame from the sample data
b_rapa_data <- system.file("extdata/brapa_arabidopsis_data.csv", package = "greatR") |>
  data.table::fread()

## ----brapa-data-kable-clean, [encoded data, 9969 characters]
# b_rapa_data |>
#   knitr::kable()

## ----brapa-data-kable, [encoded data, 9975 characters]
b_rapa_data[, .SD[1:6], by = accession][, .(gene_id, accession, timepoint, expression_value, replicate)] |>
  knitr::kable()

## ----register-data-raw, message=FALSE, warning=FALSE, [encoded data, 9944 characters]
# registration_results <- register(
#   b_rapa_data,
#   reference = "Ro18",
#   query = "Col0",
#   scaling_method = "z-score"
# )
# #> ── Validating input data ────────────────────────────────────────────────────────
# #> ℹ Will process 10 genes.
# #> ℹ Using estimated standard deviation, as no `exp_sd` was provided.
# #> ℹ Using `scaling_method` = "z-score".
# #>
# #> ── Starting registration with optimisation ──────────────────────────────────────
# #> ℹ Using L-BFGS-B optimisation method.
# #> ℹ Using computed stretches and shifts search space limits.
# #> ℹ Using `overlapping_percent` = 50% as a registration criterion.
# #> ✔ Optimising registration parameters for genes (10/10) [2s]

## ----register-data-raw-detect-cores, message=FALSE, warning=FALSE, [encoded data, 9931 characters]
# parallel::detectCores()
# #> 8

## ----register-data-raw-parallel, message=FALSE, warning=FALSE, [encoded data, 9935 characters]
# registration_results <- register(
#   b_rapa_data,
#   reference = "Ro18",
#   query = "Col0",
#   scaling_method = "z-score",
#   num_cores = 8
# )

## ----register-data, message=FALSE, warning=FALSE, [encoded data, 9948 characters]
# Load a data frame from the sample data
registration_results <- system.file("extdata/brapa_arabidopsis_registration.rds", package = "greatR") |>
  readRDS()

## ----get-model-summary-data, [encoded data, 9969 characters]
registration_results$model_comparison |>
  knitr::kable()

## ----load-input-vector, message=FALSE, [encoded data, 9959 characters]
# Define expression value vectors
ref_expressions <- c(1.9, 3.1, 7.8, 31.6, 33.7, 31.5, 131.4, 107.5, 116.7, 112.5, 109.7, 57.4, 50.9)
query_expressions <- c(14, 12.1, 15.9, 47, 30.9, 50.5, 80.1, 67.4, 72.9, 61.7)

list_vector <- list(
  reference = ref_expressions,
  query = query_expressions
)

## ----register-data-list-vectors, message=FALSE, [encoded data, 9950 characters]
registration_results_vectors <- register(
  list_vector,
  reference = "Ref",
  query = "Query",
  scaling_method = "z-score"
)
#> ── Validating input data ───────────────────────────────────────────────────────
#> ℹ Will process 1 gene.
#> ℹ Using estimated standard deviation, as no `exp_sd` was provided.
#> ℹ Using `scaling_method` = "z-score".
#> 
#> ── Starting registration with optimisation ─────────────────────────────────────
#> ℹ Using L-BFGS-B optimisation method.
#> ℹ Using computed stretches and shifts search space limits.
#> ℹ Using `overlapping_percent` = 50% as a registration criterion.
#> ✔ Optimising registration parameters for genes (1/1) [170ms]

## ----get-model-summary-data-vectors, [encoded data, 9961 characters]
registration_results_vectors$model_comparison |>
  knitr::kable()

