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

## ----input-format-overview, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Five input sources converge on the common `proteomics_data` structure used by the NADIA workflow.", fig.alt = "Workflow diagram showing four report formats and a SummarizedExperiment entering five dedicated importers. Spectronaut supplies the design in its report; DIA-NN, TMT and LFQ use column suffixes or annot_path; SummarizedExperiment uses colData. All five produce a proteomics_data object containing metadata, protein_id and protein_quant for process_proteomics."----
knitr::include_graphics("figures/Input_Formats_Workflow.svg")

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

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

## ----spectronaut-input-structure, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Structure of the long-format Spectronaut report accepted by `preprocess_spectronaut()`.", fig.alt = "Diagram of a long-format Spectronaut report. Each row contains one LC-MS run and one protein group. R.FileName, R.Condition and R.Replicate identify the sample and experimental design; PG.ProteinGroups identifies the protein; PG.Quantity contains its abundance; and further columns contain run-level annotations."----
knitr::include_graphics("figures/Spectronaut_Input_Structure.svg")

## ----spectronaut-defaults, eval = FALSE-----------------------------------------------------------
# dia <- preprocess_spectronaut(
#     # Required input and experimental conditions
#     file_path = "spectronaut_report.tsv",
#     condition_order = c("Control", "Treatment"),
# 
#     # Optional export; NULL keeps the result in memory
#     export_dir = NULL,
# 
#     # Aggregation of annotations reported for multiple runs
#     agg_coverage_run = "max",
#     agg_coverage_global = "max",
#     agg_mw = "max",
#     agg_cscore_runwise = "mean",
# 
#     # Exported file names and progress messages
#     timestamp_suffix = TRUE,
#     verbose = TRUE
# )

## ----dia------------------------------------------------------------------------------------------
dia_file <- system.file("extdata", "nadia_dia_report.tsv.gz", package = "NADIA")
dia <- preprocess_spectronaut(dia_file, condition_order = c("A", "B", "D"),
                              verbose = FALSE)
dia

## ----dia-md---------------------------------------------------------------------------------------
dia$metadata

## ----dia-agg, eval = FALSE------------------------------------------------------------------------
# dia_median <- preprocess_spectronaut(
#     dia_file, condition_order = c("A", "B", "D"),
#     agg_coverage_run = "median",   # default "max"
#     agg_cscore_runwise = "median", # default "mean"
#     verbose = FALSE)

## ----diann-input-structure, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Structure of the wide DIA-NN protein-group matrix accepted by `preprocess_diann()`.", fig.alt = "Diagram of a wide DIA-NN protein-group matrix. Each row represents one protein group. Protein annotations occupy the first columns, followed by one Abundance column for each run. The experimental design is obtained from Condition_Replicate suffixes or from annot_path."----
knitr::include_graphics("figures/DIANN_Input_Structure.svg")

## ----diann-defaults, eval = FALSE-----------------------------------------------------------------
# diann <- preprocess_diann(
#     # Required input and conditions to retain
#     file_path = "report.pg_matrix.tsv",
#     condition_order = c("Control", "Treatment"),
# 
#     # Optional sample annotation file; NULL uses the column suffixes
#     annot_path = NULL,
# 
#     # Optional export; NULL keeps the result in memory
#     export_dir = NULL,
#     timestamp_suffix = TRUE,
# 
#     # Progress messages
#     verbose = TRUE
# )

## ----diann----------------------------------------------------------------------------------------
diann_file <- system.file("extdata", "nadia_diann_report.tsv.gz",
                          package = "NADIA")
diann <- preprocess_diann(diann_file, condition_order = c("A", "B", "D"),
                          verbose = FALSE)
diann

## ----diann-annot, eval = FALSE--------------------------------------------------------------------
# preprocess_diann(
#     "report.pg_matrix.tsv",
#     condition_order = c("A", "B", "D"),
#     annot_path = "design.tsv")

## ----diann-md-------------------------------------------------------------------------------------
diann$metadata

## ----tmt-input-structure, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Structure of the wide Proteome Discoverer TMT report accepted by `preprocess_tmt()`.", fig.alt = "Diagram of a wide TMT report from Proteome Discoverer. Each row represents one protein group. Global protein and identification annotations occupy the first columns, followed by one Abundance column for each TMT channel. The design is obtained from Condition_Replicate suffixes or from annot_path."----
knitr::include_graphics("figures/TMT_Input_Structure.svg")

## ----tmt-defaults, eval = FALSE-------------------------------------------------------------------
# tmt <- preprocess_tmt(
#     # Required input and conditions to retain
#     file_path = "tmt_report.tsv",
#     condition_order = c("Control", "Treatment"),
# 
#     # Optional channel annotation file; NULL uses the column suffixes
#     annot_path = NULL,
# 
#     # Optional export; NULL keeps the result in memory
#     export_dir = NULL,
#     timestamp_suffix = TRUE,
# 
#     # Progress messages
#     verbose = TRUE
# )

## ----tmt-annot, eval = FALSE----------------------------------------------------------------------
# preprocess_tmt(
#     "tmt_report.tsv",
#     condition_order = c("A", "B", "D"),
#     annot_path = "design.tsv")

## ----tmt-run--------------------------------------------------------------------------------------
tmt_file <- system.file("extdata", "nadia_tmt_report.tsv.gz", package = "NADIA")
tmt <- preprocess_tmt(tmt_file, condition_order = c("A", "B", "D"),
                      verbose = FALSE)
tmt

## ----tmt-md---------------------------------------------------------------------------------------
head(tmt$metadata)
table(tmt$metadata$R.Condition)

## ----lfq-input-structure, echo = FALSE, fig.align = "center", out.width = "100%", fig.cap = "Structure of the wide Proteome Discoverer LFQ report accepted by `preprocess_lfq()`.", fig.alt = "Diagram of a wide label-free report from Proteome Discoverer. Each row represents one protein group. Protein annotations are followed by repeated families of Score Mascot, PSM count, peptide count and Abundance columns for every sample. NADIA matches these families by sample name."----
knitr::include_graphics("figures/LFQ_Input_Structure.svg")

## ----lfq-defaults, eval = FALSE-------------------------------------------------------------------
# lfq <- preprocess_lfq(
#     # Required input
#     file_path = "lfq_report.tsv",
# 
#     # Optional design file and condition filtering/order
#     annot_path = NULL,
#     condition_order = NULL,
# 
#     # Optional export; NULL keeps the result in memory
#     export_dir = NULL,
#     timestamp_suffix = TRUE,
# 
#     # Progress messages
#     verbose = TRUE
# )

## ----lfq-annot, eval = FALSE----------------------------------------------------------------------
# preprocess_lfq(
#     "lfq_report.tsv",
#     condition_order = c("Control", "Treatment"),
#     annot_path = "design.tsv")

## ----lfq-run--------------------------------------------------------------------------------------
lfq_file <- system.file("extdata", "nadia_lfq_report.tsv.gz", package = "NADIA")
lfq <- preprocess_lfq(lfq_file, verbose = FALSE)
lfq

## ----lfq-md---------------------------------------------------------------------------------------
lfq$metadata

## ----from-se--------------------------------------------------------------------------------------
quant_cols <- grep("^PG.Quantity_", names(dia$protein_quant))
log2_intensities <- log2(as.matrix(dia$protein_quant[, quant_cols]))
rownames(log2_intensities) <- dia$protein_quant$PG.ProteinGroups

se <- SummarizedExperiment::SummarizedExperiment(
    assays  = list(log2 = log2_intensities),
    colData = data.frame(Condition = dia$metadata$R.Condition))

from_se <- preprocess_se(se, log2_input = TRUE, verbose = FALSE)
from_se
head(from_se$metadata[, c("R.FileName", "R.Condition", "R.Replicate", "Coding")])

## ----names-test-----------------------------------------------------------------------------------
candidates <- c("Ctrl", "Ct.1", "Ct_a", "Ct-1", "Ct 1", "1Ct")
data.frame(name  = candidates,
           valid = make.names(candidates) == candidates,
           what_r_would_do = make.names(candidates))

## ----classes--------------------------------------------------------------------------------------
inputs <- list(Spectronaut = dia, DIA_NN = diann, TMT = tmt, LFQ = lfq,
               SummarizedExperiment = from_se)
vapply(inputs, function(x) class(x)[1], character(1))
vapply(inputs, function(x) inherits(x, "proteomics_data"), logical(1))

## ----elements-------------------------------------------------------------------------------------
lapply(inputs, names)

## ----md-cols--------------------------------------------------------------------------------------
lapply(inputs, function(x) colnames(x$metadata))

## ----score-labels---------------------------------------------------------------------------------
lapply(inputs, function(x) attr(x$protein_quant, "nadia_labels"))

## ----pipeline-------------------------------------------------------------------------------------
processed <- lapply(inputs, process_proteomics, verbose = FALSE)
lapply(processed, function(x) x$comparisons)

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

