## ----setup, include = FALSE-----------------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", crop = NULL)
options(width = 100)

## ----install, eval = FALSE------------------------------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# BiocManager::install("NADIA")

## ----packages, message = FALSE--------------------------------------------------------------------
library(NADIA)
library(SummarizedExperiment)

data(nadia_dia)

## ----norm-run, message = FALSE--------------------------------------------------------------------
se_norm <- nm_prepare_se(nadia_dia, verbose = FALSE)
se_norm <- nm_run_normalizations(
    se_norm,
    methods = c("log2Norm", "cycloess", "Rlr", "quantile"),
    verbose = FALSE)

assayNames(se_norm)

## ----norm-metrics, message = FALSE, warning = FALSE-----------------------------------------------
# Two diagnostics (Hopkins and the PERMANOVA p-value) use random numbers.
set.seed(42)
nm <- normalization_metrics(
    se_norm,
    output_dir = NULL,
    verbose = FALSE)

nm$final_rank

## ----norm-ranking, fig.width = 7, fig.height = 4.5, fig.alt = "Final ranking of candidate normalization methods"----
nm$final_ranking

## ----norm-pca, fig.width = 8, fig.height = 6, fig.alt = "PCA of samples after each candidate normalization method"----
nm$pca

## ----norm-winner----------------------------------------------------------------------------------
norm_winner <- nm$final_rank$Method[1]
norm_winner

## ----mar-mnar-diagram, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Practical distinction between MAR and MNAR missing values in proteomics. MAR values are reconstructed from observed data structure, whereas MNAR values call for a low-value or detection-aware model.", fig.alt = "Two-panel diagram comparing MAR and MNAR missing values. MAR shows a missing replicate among similar observed intensities and recommends structure-based imputation. MNAR shows low values falling below a detection limit and recommends low-value or detection-aware imputation. A note states that an individual missing value does not reveal its cause and that both mechanisms can coexist."----
knitr::include_graphics("figures/MAR_MNAR_Missingness.svg")

## ----imp-run, message = FALSE, warning = FALSE----------------------------------------------------
se_imp <- im_prepare_se(
    nadia_dia,
    norm_method = norm_winner,
    verbose = FALSE)

# The cells masked to build the ground truth are drawn at random.
set.seed(42)
im <- imputation_metrics(
    se_imp,
    assay_name = norm_winner,
    methods = c("Impseqrob", "knn", "MinDet", "QRILC", "min"),
    combo_methods = list(
        Impseqrob_min = list(
            mar_method = "Impseqrob",
            mnar_method = "min")),
    na_prop = 0.20,
    pattern = "random",
    output_dir = NULL,
    verbose = FALSE)

im$metrics_table[, c(
    "Method", "NRMSE", "SOR", "PSS", "ACC_OI", "Rank_Mean")]

## ----imp-metrics-plot, fig.width = 8, fig.height = 6, fig.alt = "Imputation quality metrics for the candidate methods"----
im$metrics

## ----imp-ranking, fig.width = 7.5, fig.height = 5, fig.alt = "Heatmap of imputation method ranks"----
im$ranking

## ----imp-winner-----------------------------------------------------------------------------------
imp_winner <- im$metrics_table$Method[1]
imp_winner

## ----apply-winners, message = FALSE---------------------------------------------------------------
res_selected <- process_proteomics(
    nadia_dia,
    norm_method = norm_winner,
    imp_method = imp_winner,
    verbose = FALSE)

table(res_selected$DEPs_results$Change)

## ----selected-assays------------------------------------------------------------------------------
assayNames(res_selected$se_proc)

## ----sensitivity, message = FALSE, warning = FALSE------------------------------------------------
variants <- list(
    "cycloess + Impseqrob" = list(
        norm_method = "cycloess", imp_method = "Impseqrob"),
    "cycloess + combo" = list(
        norm_method = "cycloess", imp_method = "combo"),
    "cycloess + knn" = list(
        norm_method = "cycloess", imp_method = "knn"),
    "quantile + combo" = list(
        norm_method = "quantile", imp_method = "combo"),
    "log2 + min" = list(
        norm_method = "log2", imp_method = "min"))

# Some imputation methods print progress on stdout; capture.output keeps it out.
invisible(capture.output(
    counts <- do.call(rbind, lapply(names(variants), function(label) {
        fit <- do.call(
            process_proteomics,
            c(list(nadia_dia, verbose = FALSE), variants[[label]]))
        tab <- table(factor(
            fit$DEPs_results$Change,
            levels = c("Up", "Down", "No Change")))
        data.frame(
            Pipeline = label,
            Up = tab[["Up"]],
            Down = tab[["Down"]],
            No_Change = tab[["No Change"]])
    }))))

counts

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

