## ----vignette-setup, include = FALSE------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

has_graphics <- requireNamespace("ggplot2", quietly = TRUE) &&
  requireNamespace("RColorBrewer", quietly = TRUE)
has_tidy_se <- requireNamespace("tidySummarizedExperiment", quietly = TRUE)

## ----setup--------------------------------------------------------------------
library(growkar)
library(dplyr)
library(knitr)
data(yeast_growth_data)

## ----growth-experiment-construct----------------------------------------------
# Constructor (control over column-name resolution):
ge <- GrowthExperiment(yeast_growth_data)
ge

# Equivalent standard coercion:
ge2 <- as(yeast_growth_data, "GrowthExperiment")
identical(ge, ge2)

# It is a SummarizedExperiment, so it hands off cleanly to other packages:
se <- as(ge, "SummarizedExperiment")
is(ge, "SummarizedExperiment")

## ----summarized-experiment----------------------------------------------------
ge <- growth_metrics(ge, method = "rolling_window", average_replicates = TRUE)

S4Vectors::metadata(ge)$growth_metrics

## ----validate-se--------------------------------------------------------------
validate_growth_experiment(ge)

## ----se-accessors-------------------------------------------------------------
growth_assay(ge)[1:3, 1:3]
timepoints(ge)
sample_data(ge)

## ----tidy-conversion----------------------------------------------------------
tidy_growth <- as_tidy_growth_data(yeast_growth_data)

head(tidy_growth)

## ----validate-input-----------------------------------------------------------
validate_growth_data(tidy_growth)

## ----summarized-experiment-direct---------------------------------------------
GrowthExperiment(yeast_growth_data)

## ----tidyomics, eval = has_tidy_se--------------------------------------------
library(tidySummarizedExperiment)

# The SE prints and behaves as a tibble abstraction, without being converted.
se

se |>
  filter(condition == "Cg") |>
  filter(time <= 6) |>
  select(.feature, .sample, od, condition, replicate)

se |>
  group_by(condition) |>
  summarise(max_od = max(od), .groups = "drop")

## ----plot-averaged-curves, eval = has_graphics--------------------------------
plot_growth_curve(
  se,
  average_replicates = TRUE,
  colour_col = "condition",
  palette_name = "Dark2"
)

## ----plot-replicate-facets, eval = has_graphics-------------------------------
plot_growth_curve(
  se,
  average_replicates = FALSE,
  colour_col = "condition",
  facet_col = "replicate",
  palette_name = "Dark2"
)

## ----summarize-metrics--------------------------------------------------------
metrics <- summarize_growth_metrics(
  se,
  method = "rolling_window",
  average_replicates = TRUE
)

knitr::kable(metrics, digits = 3)

## ----single-sample-growth-rate------------------------------------------------
gr <- compute_growth_rate(se, method = "rolling_window")

knitr::kable(head(gr), digits = 3)

## ----doubling-time-helper-----------------------------------------------------
knitr::kable(
  tibble(
    sample = gr$sample,
    growth_rate = gr$mu,
    doubling_time = compute_doubling_time(gr$mu)
  ),
  digits = 3
)

## ----detect-phase-------------------------------------------------------------
phase_tbl <- detect_exponential_phase(se)

knitr::kable(head(phase_tbl), digits = 3)

## ----fit-logistic-model-------------------------------------------------------
sample_id <- unique(gr$sample)[1]
fit_input <- as_tidy_growth_data(se) |>
  filter(sample == sample_id)

cg_fit <- fit_growth_curve(fit_input, model = "logistic")

cg_fit
isVirtualClass("GrowthFit")
extract_params(cg_fit)
summary(cg_fit)

## ----growth-fit-accessors-----------------------------------------------------
fit_sample(cg_fit)
fit_model(cg_fit)
fit_status(cg_fit)
fit_converged(cg_fit)
coef(cg_fit)
head(fitted(cg_fit))
nobs(cg_fit)

## ----failed-fit-example-------------------------------------------------------
flat_fit <- fit_growth_curve(
  tibble(
    sample = "flat",
    time = 0:4,
    od = rep(0.2, 5)
  ),
  model = "logistic"
)

summary(flat_fit)

## ----fit-plate----------------------------------------------------------------
se <- fit_growth_models(se, model = "logistic")

S4Vectors::metadata(se)$growth_model_parameters

## ----plot-fitted-curve, eval = has_graphics-----------------------------------
plot_fitted_curve(cg_fit)

## ----session-information------------------------------------------------------
sessionInfo()

