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

## ----paths--------------------------------------------------------------------
# Load package
library(ProteinBatcher)

# Input files shipped with the package
path_annotation <- system.file("extdata", "annotation_HaCaT.tsv",
                                package = "ProteinBatcher")
path_pgmatrix   <- system.file(
  "extdata", "2024MK017_HaCaT_Stimulation_1to24_Astral_report.pg_matrix.tsv",
  package = "ProteinBatcher")
path_output <- tempdir()

# Inspect annotation structure (base R, sin readr)
ann <- utils::read.delim(path_annotation, check.names = FALSE)

## ----show-annotation----------------------------------------------------------
dim(ann)
head(ann)

## ----design-table-------------------------------------------------------------
table(condition = ann$condition, batch = ann$batch)

## ----ex1-params---------------------------------------------------------------
experiment <- "HaCaT"
percent_missing <- 50

formula <- formula(~ 0 + condition + batch + condition:batch)
tests <- c(
  "IL13_vs_NoTreated",
  "IL22_vs_NoTreated",
  "COMBO_vs_NoTreated"
)
tests_interaction <- c(
  "IL13.batchday2",
  "IL22.batchday2",
  "COMBO.batchday2"
)
reference_condition <- "NoTreated"

## ----ex1-run------------------------------------------------------------------
res <- run_proteomics_pipeline(
  path_pgmatrix        = path_pgmatrix,
  path_annotation      = path_annotation,
  path_output          = path_output,
  tests                = tests,
  tests_interaction    = tests_interaction,
  formula              = formula,
  reference_condition  = reference_condition,
  percent_missing      = percent_missing,
  ldv_source           = "per-condition",
  experiment           = experiment,
  plots                = FALSE
)

## ----ex1-structure------------------------------------------------------------
names(res)
res$se_imp

## ----ex1-effects--------------------------------------------------------------
# Contrasts available, and direct access with `$`
names(res$effects)
il13 <- res$effects$IL13_vs_NoTreated
names(il13)

# Each slot is a SummarizedExperiment; statistics live in rowData()
il13$all_common_effect
head(colnames(SummarizedExperiment::rowData(il13$all_common_effect)))

## ----ex1-tables---------------------------------------------------------------
path_filtered <- system.file(
  "extdata", "HaCaT_filtered_out_proteins.csv",
  package = "ProteinBatcher"
)
path_before <- system.file(
  "extdata", "HaCaT_Imputation_before.csv",
  package = "ProteinBatcher"
)
path_after <- system.file(
  "extdata", "HaCaT_Imputation_after.csv",
  package = "ProteinBatcher"
)

if (nzchar(path_filtered)) {
  filtered_df <- read.csv(path_filtered, check.names = FALSE,
                          stringsAsFactors = FALSE)
  head(filtered_df)
}

## ----ex1-tables-imp-----------------------------------------------------------
if (nzchar(path_before) && nzchar(path_after)) {
  before_df <- read.csv(path_before, check.names = FALSE,
                        stringsAsFactors = FALSE)
  after_df  <- read.csv(path_after,  check.names = FALSE,
                        stringsAsFactors = FALSE)
  # First proteins, first few columns, before vs after imputation
  head(before_df[, seq_len(min(5, ncol(before_df)))])
  head(after_df[, seq_len(min(5, ncol(after_df)))])
}

## ----ex1-fig-main, echo = FALSE, out.width = "80%"----------------------------
p_main <- system.file(
  "extdata", "HaCaT_MainEffect_AllProteins_IL13_vs_NoTreated.png",
  package = "ProteinBatcher"
)
if (nzchar(p_main)) knitr::include_graphics(p_main)

## ----ex1-fig-common, echo = FALSE, out.width = "80%"--------------------------
p_common <- system.file(
  "extdata", "HaCaT_CommonEffect_IL13_vs_NoTreated.png",
  package = "ProteinBatcher"
)
if (nzchar(p_common)) knitr::include_graphics(p_common)

## ----ex1-fig-interaction, echo = FALSE, out.width = "80%"---------------------
p_inter <- system.file(
  "extdata", "HaCaT_InteractionEffect_IL13.batchday2.png",
  package = "ProteinBatcher"
)
if (nzchar(p_inter)) knitr::include_graphics(p_inter)

## ----ex1-fig-dereg, echo = FALSE, out.width = "80%"---------------------------
p_dereg <- system.file(
  "extdata", "HaCaT_Deregulogram_IL13_vs_NoTreated_batch.png",
  package = "ProteinBatcher"
)
if (nzchar(p_dereg)) knitr::include_graphics(p_dereg)

## ----ex2-params---------------------------------------------------------------
experiment <- "HaCaT"
percent_missing <- 50

formula <- ~ 0 + condition
tests <- c(
  "IL13_vs_NoTreated",
  "IL22_vs_NoTreated",
  "COMBO_vs_NoTreated"
)
tests_interaction <- "NA"
reference_condition <- "NoTreated"

## ----ex2-run------------------------------------------------------------------
res_no_int <- run_proteomics_pipeline(
  path_pgmatrix        = path_pgmatrix,
  path_annotation      = path_annotation,
  path_output          = path_output,
  tests                = tests,
  tests_interaction    = tests_interaction,
  formula              = formula,
  reference_condition  = reference_condition,
  percent_missing      = percent_missing,
  ldv_source           = "per-condition",
  experiment           = experiment,
  plots                = FALSE
)

## ----ex2-structure------------------------------------------------------------
names(res_no_int$effects)
res_no_int$effects$IL13_vs_NoTreated$all_common_effect

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

