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

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

## ----setup--------------------------------------------------------------------
library(MiCARA)

## ----validate-step, eval = TRUE-----------------------------------------------
# Load example dataset included in MiCARA
data("example_taxa", package = "MiCARA")
data("example_pathways", package = "MiCARA")
data("example_metadata", package = "MiCARA")

# Validate inputs and filter unusable covariates
micara_obj <- validate_inputs(
    taxa = example_taxa,
    pathways = example_pathways,
    metadata = example_metadata,
    disease_col = "disease"
)

## ----validate-bioc, eval = FALSE----------------------------------------------
# # One (Tree)SummarizedExperiment for taxa and one for pathways
# micara_obj <- validate_inputs(taxa = tse_taxa, pathways = tse_pathways)
# 
# # A MultiAssayExperiment holding both: name its taxa and pathway experiments
# micara_obj <- validate_inputs(mae, experiments = c(taxa = "metaphlan", pathways = "humann"))

## ----impute-step, eval = TRUE-------------------------------------------------
# Optional: Impute remaining missing metadata values (<30%)
micara_obj <- impute_metadata(micara_obj)

## ----diff-step, eval = TRUE---------------------------------------------------
# Run differential abundance on both feature types
diff_results <- run_diff_abundance(
    micara_obj      = micara_obj,
    feature_type    = c("taxa", "pathways"),
    reference_level = "healthy",
    use_robust      = TRUE
)

# Inspect significant taxa
head(diff_results$taxa$significant)

# Inspect significant pathways
head(diff_results$pathways$significant)

## ----interactions, eval = TRUE------------------------------------------------
# Compute residual correlations per disease
interactions <- compute_residualCLR_correlations(
    micara_obj   = micara_obj,
    diffab_obj   = diff_results,
    covariates   = c("age_category", "BMI", "gender", "study_name"),
    q_cutoff     = 0.05,
    r_cutoff     = 0.20,
    verbose      = TRUE
)

# inspect available disease cohorts in the output list
names(interactions)

## ----inspect-diagnostics------------------------------------------------------
# View cohort-level analysis summary and covariate usage
attr(interactions, "summary")

# View analysis settings and CLR transformation diagnostics
attr(interactions, "settings")

# Find the first analyzed cohort dynamically
analyzed_cohorts <- names(interactions)[sapply(interactions, function(x) is.list(x) && x$status == "analysed")]

if (length(analyzed_cohorts) > 0) {
    first_cohort <- analyzed_cohorts[1]
    message("Displaying links for cohort: ", first_cohort)
    head(interactions[[first_cohort]]$links)
}

## ----plot-sankey, eval = TRUE-------------------------------------------------
# Interactive visualization. direction = "both" returns a list with one
# diagram per direction ($positive and/or $negative); print an element to show it.
sankeys <- plot_interaction_sankey(
    interactions      = interactions,
    disease           = names(interactions)[1], # Target disease cohort (e.g., "UC")
    direction         = "both", # Options: "positive", "negative", or "both"
    top_n_per_pathway = 5, # Top N correlated taxa per pathway
    font_size         = 12, # Text label size in pixels
    node_width        = 35 # Width of Sankey block nodes
)
names(sankeys)
sankeys$positive

# Optional: save interactive HTML and static PNG files. They go to a temporary folder.
invisible(plot_interaction_sankey(
    interactions      = interactions,
    disease           = names(interactions)[1],
    save_path         = file.path(tempdir(), "sankey_network")
))

## ----global interaction correction, eval = TRUE-------------------------------
# Apply global FDR correction across all disease cohorts
interactions_global <- compute_global_fdr(interactions)

# Plot Sankey network using global significance cutoffs
sankeys_global <- plot_interaction_sankey(
    interactions      = interactions_global,
    disease           = names(interactions_global)[1],
    significance      = "global",
    direction         = "both",
    top_n_per_pathway = 5
)

sankeys_global$positive

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

