Nothing
#' This function integrates pathway name/description annotations, ten of the most advanced differential abundance (DA) methods, and visualization of DA results.
#'
#' @param file A character string representing the file path of the input file containing KO abundance data in picrust2 export format. The input file should have KO identifiers in the first column and sample identifiers in the first row. The remaining cells should contain the abundance values for each KO-sample pair.
#' @param data An optional data.frame or matrix containing KO/pathway abundance
#' data. Data frames may use the same format as the input file, with feature
#' identifiers in the first non-numeric column and samples in the remaining
#' columns. Already-normalized matrix/data.frame inputs with feature identifiers
#' in row names and samples in columns are also accepted. If provided, the
#' function will use this data instead of reading from the file. By default,
#' this parameter is set to NULL.
#' @param metadata A tibble, consisting of sample information
#' @param group A character, name of the group
#' @param pathway A character, consisting of "EC", "KO", "MetaCyc"
#' @param daa_method a character specifying the method for differential abundance analysis, default is "ALDEx2", choices are:
#' - "ALDEx2": ANOVA-Like Differential Expression tool for high throughput sequencing data
#' - "DESeq2": Differential expression analysis based on the negative binomial distribution using DESeq2
#' - "edgeR": Exact test for differences between two groups of negative-binomially distributed counts using edgeR
#' - "limma voom": Limma-voom framework for the analysis of RNA-seq data
#' - "metagenomeSeq": Fit logistic regression models to test for differential abundance between groups using metagenomeSeq
#' - "LinDA": Linear models for differential abundance analysis of microbiome compositional data
#' - "Maaslin2": Multivariate Association with Linear Models (MaAsLin2) for differential abundance analysis
#' - "Lefser": Linear discriminant analysis effect size using the lefser package
#' @param ko_to_kegg Logical or logical-like string controlling conversion of
#' KO abundance to KEGG pathway abundance.
#' @param filter_for_prokaryotes Logical. If TRUE (default), filters out KEGG pathways
#' that are specific to eukaryotes (e.g., human diseases, organismal systems) when
#' ko_to_kegg = TRUE. Set to FALSE to include all KEGG pathways.
#' @param p_adjust_method A character specifying the method for p-value adjustment,
#' default is "BH".
#' @param p.adjust Deprecated alias for \code{p_adjust_method}. Do not supply
#' both parameters with different values.
#' @param order A character to control the order of the main plot rows
#' @param p_values_bar A character to control if the main plot has the p_values bar
#' @param x_lab A character to control the x-axis label name, you can choose from "feature","pathway_name" and "description"
#' @param select A vector consisting of pathway names to be selected
#' @param reference A character, a reference group level for several DA methods
#' @param colors A vector consisting of colors number
#' @param p_values_threshold A numeric value specifying the threshold for statistical
#' significance of differential abundance. Pathways with adjusted p-values below this
#' threshold will be displayed in the plot. Default is 0.05. Must be in the
#' range (0, 1].
#'
#' @return A list containing:
#' \itemize{
#' \item Numbered elements (1, 2, ...): Sub-lists for each DA method, each containing:
#' \itemize{
#' \item \code{plot}: A ggplot2 error bar plot visualizing the differential abundance results
#' \item \code{results}: A data frame of differential abundance results for that method
#' }
#' \item \code{abundance}: The processed abundance data (KEGG pathway or original) for downstream analysis
#' \item \code{metadata}: The metadata data frame
#' \item \code{group}: The group variable name used in the analysis
#' \item \code{daa_results_df}: The complete annotated DAA results data frame
#' \item \code{ko_to_kegg}: Logical indicating whether KO to KEGG conversion was performed
#' }
#' These additional fields allow seamless integration with \code{\link{pathway_pca}} and
#' \code{\link{pathway_heatmap}} for further visualization without re-preparing data.
#' @export
#' @examples
#' \dontrun{
#' # Requires MicrobiomeStat and KEGGREST; KEGG annotation uses the internet.
#' data("ko_abundance")
#' data("metadata")
#' results <- ggpicrust2(
#' data = ko_abundance, metadata = metadata, group = "Environment",
#' pathway = "KO", daa_method = "LinDA", ko_to_kegg = TRUE,
#' x_lab = "pathway_name"
#' )
#' results[[1]]$plot
#' head(results[[1]]$results)
#'
#' # Reuse the aligned abundance and metadata for exploratory plots.
#' pathway_pca(results$abundance, results$metadata, results$group)
#' sig_features <- unique(results$daa_results_df$feature[
#' !is.na(results$daa_results_df$p_adjust) &
#' results$daa_results_df$p_adjust < 0.05
#' ])
#' if (length(sig_features) > 0) {
#' pathway_heatmap(results$abundance[sig_features, , drop = FALSE],
#' results$metadata, results$group)
#' }
#' # For your own files, replace data = ko_abundance with file = "your_file.tsv"
#' # and supply matching metadata. See vignette("using_ggpicrust2").
#' }
ggpicrust2 <- function(file = NULL,
data = NULL,
metadata,
group,
pathway,
daa_method = "ALDEx2",
ko_to_kegg = FALSE,
filter_for_prokaryotes = TRUE,
p_adjust_method = "BH",
order = "group",
p_values_bar = TRUE,
x_lab = NULL,
select = NULL,
reference = NULL,
colors = NULL,
p_values_threshold = 0.05,
p.adjust = NULL) {
p_adjust_method <- resolve_deprecated_p_adjust_method(
p_adjust_method,
p.adjust,
p_adjust_method_missing = missing(p_adjust_method)
)
ko_to_kegg <- normalize_logical_flag(ko_to_kegg, "ko_to_kegg")
filter_for_prokaryotes <- normalize_logical_flag(filter_for_prokaryotes, "filter_for_prokaryotes")
validate_p_adjust_method(p_adjust_method)
validate_probability_threshold(p_values_threshold, "p_values_threshold")
# Input validation
if (is.null(file) && is.null(data)) {
stop("Error: Please provide either a 'file' path (character string) or a 'data' data frame.")
}
if (!is.null(file) && !is.null(data)) {
warning("Both 'file' and 'data' provided. Using 'data' and ignoring 'file'.")
file <- NULL # Set file to NULL to use data
}
# Validate file parameter if provided
if (!is.null(file)) {
if (!is.character(file) || length(file) != 1) {
stop("Error: 'file' parameter must be a character string representing a file path. If you have already loaded your data into R, please use the 'data' parameter instead.")
}
if (!file.exists(file)) {
stop("Error: File does not exist: ", file)
}
}
# Validate data parameter if provided
if (!is.null(data)) {
if (!is.data.frame(data) && !is.matrix(data)) {
stop("'data' must be a data frame or matrix. If you want to provide a file path, use the 'file' parameter instead.",
call. = FALSE)
}
}
# `ko_to_kegg = TRUE` aggregates KO abundances into KEGG pathways, so it is
# only meaningful when the input is KO abundance (pathway = "KO"). Pairing it
# with EC/MetaCyc produces an empty matrix and a cryptic downstream error.
if (ko_to_kegg && !identical(pathway, "KO")) {
stop(sprintf(
"`ko_to_kegg = TRUE` requires `pathway = \"KO\"`, but got pathway = \"%s\".\n - For EC or MetaCyc data, set `ko_to_kegg = FALSE`.\n - For KO -> KEGG pathway analysis, set `pathway = \"KO\"`.",
pathway))
}
# Step 1: Load abundance data
if (ko_to_kegg) {
message("Converting KO to KEGG...\n")
abundance <- if (!is.null(file)) {
ko2kegg_abundance(file, filter_for_prokaryotes = filter_for_prokaryotes)
} else {
ko2kegg_abundance(data = data, filter_for_prokaryotes = filter_for_prokaryotes)
}
} else {
message("Reading input data...\n")
abundance <- if (!is.null(file)) {
read_abundance_file(file)
} else {
data
}
abundance <- as.data.frame(abundance)
if (ncol(abundance) > 1 && !is.numeric(abundance[[1]])) {
abundance <- clean_ko_abundance(abundance)
} else if (!is.null(rownames(abundance))) {
rownames(abundance) <- gsub("^ko:", "", rownames(abundance))
}
abundance <- normalize_abundance_feature_ids(
abundance,
context = "ggpicrust2() abundance"
)
}
validate_feature_rownames(abundance, "ggpicrust2() abundance")
# Align once at the wrapper boundary. Both DAA and plotting consume this
# same sample order, so pathway_daa() is called with an explicit
# pre-aligned contract below instead of redoing the same alignment.
aligned <- align_samples(abundance, metadata, verbose = FALSE)
abundance <- aligned$abundance
metadata <- aligned$metadata
# Step 2: Differential abundance analysis
#
# Do NOT forward `select` to pathway_daa() here. In this wrapper `select`
# is documented as pathway/feature names (see @param select -- "A vector
# consisting of pathway names to be selected"), whereas pathway_daa()'s
# own `select` argument means sample names. Passing pathway names into
# pathway_daa() immediately failed with "Some selected samples not in
# abundance data". The user-supplied `select` is forwarded to
# pathway_errorbar() below, which is where feature-level filtering for
# the plot actually happens; pathway_daa() runs on the full sample set.
daa_results_df <- pathway_daa(
abundance = abundance,
metadata = metadata,
group = group,
daa_method = daa_method,
p_adjust_method = p_adjust_method,
reference = reference,
.pre_aligned = TRUE,
.sample_col = aligned$sample_col
)
if ("p_adjust" %in% colnames(daa_results_df)) {
validate_probability_values(daa_results_df$p_adjust,
"p_adjust",
"daa_results_df")
}
# Check for significant biomarkers
num_significant <- sum(daa_results_df$p_adjust < p_values_threshold, na.rm = TRUE)
if (num_significant == 0) {
warning(sprintf("No statistically significant biomarkers found (p_adjust < %g). ", p_values_threshold),
"Results will still be returned; significance-filtered plots will be skipped.",
call. = FALSE)
}
# Step 3: Pathway annotation
message("Annotating pathways...\n")
daa_results_df <- pathway_annotation(
pathway = pathway,
ko_to_kegg = ko_to_kegg,
daa_results_df = daa_results_df,
p_adjust_threshold = p_values_threshold
)
# Step 4: Create plots for each method
message("Creating pathway error bar plots...\n")
plot_result_list <- list()
Group_vec <- metadata[[group]]
names(Group_vec) <- colnames(abundance)
canonical_methods <- canonicalize_daa_method_names(daa_results_df$method)
method_names <- unique(canonical_methods)
for (i in seq_along(method_names)) {
method_name <- method_names[i]
daa_sub_method_results_df <- daa_results_df[canonical_methods == method_name, , drop = FALSE]
has_significant_results <- any(
!is.na(daa_sub_method_results_df$p_adjust) &
daa_sub_method_results_df$p_adjust < p_values_threshold
)
combination_bar_plot <- NULL
if (has_significant_results) {
combination_bar_plot <- pathway_errorbar(
abundance = abundance,
daa_results_df = daa_sub_method_results_df,
Group = Group_vec,
ko_to_kegg = ko_to_kegg,
p_value_bar = p_values_bar,
order = order,
colors = colors,
select = select,
x_lab = x_lab,
p_values_threshold = p_values_threshold
)
}
if (is.null(combination_bar_plot)) {
message(sprintf("Plot %d skipped (no data for method: %s)\n", i, method_name))
} else {
message(sprintf("Plot %d created.\n", i))
}
plot_result_list[[i]] <- list(plot = combination_bar_plot, results = daa_sub_method_results_df)
}
message("ggpicrust2 analysis completed.\n")
# Step 5: Add data for downstream analysis
plot_result_list$abundance <- abundance
plot_result_list$metadata <- metadata
plot_result_list$group <- group
plot_result_list$daa_results_df <- daa_results_df
plot_result_list$ko_to_kegg <- ko_to_kegg
return(plot_result_list)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.