Generate Comprehensive Longitudinal Microbiome Analysis Report for MicrobiomeStat
Source:R/mStat_generate_report_long.R
mStat_generate_report_long.Rd
This function performs extensive and in-depth longitudinal analysis of microbiome data using MicrobiomeStat package and generates a comprehensive PDF report. The report contains thorough statistical analysis along with data visualizations, statistical graphics, and result tables for microbial alpha diversity, beta diversity, taxonomic composition, and their temporal dynamics with MicrobiomeStat.
Usage
mStat_generate_report_long(
data.obj,
group.var,
vis.adj.vars = NULL,
test.adj.vars = NULL,
strata.var = NULL,
subject.var,
time.var,
t0.level = NULL,
ts.levels = NULL,
depth = NULL,
alpha.obj = NULL,
alpha.name = c("shannon", "observed_species"),
dist.obj = NULL,
dist.name = c("BC", "Jaccard"),
pc.obj = NULL,
prev.filter = 0.1,
abund.filter = 1e-04,
bar.area.feature.no = 30,
heatmap.feature.no = 30,
vis.feature.level = NULL,
test.feature.level = NULL,
feature.dat.type = c("count", "proportion", "other"),
feature.analysis.rarafy = TRUE,
feature.change.func = "relative change",
feature.mt.method = "none",
feature.sig.level = 0.1,
feature.box.axis.transform = c("sqrt"),
base.size = 16,
theme.choice = "bw",
custom.theme = NULL,
palette = NULL,
pdf = TRUE,
file.ann = NULL,
pdf.wid = 11,
pdf.hei = 8.5,
output.file
)
Arguments
- data.obj
A list object in a format specific to MicrobiomeStat, which can include components such as feature.tab (matrix), feature.ann (matrix), meta.dat (data.frame), tree, and feature.agg.list (list).
- group.var
Character, column name in metadata containing grouping variable, e.g. "treatment". Required if present in metadata.
- vis.adj.vars
Character vector, names of columns in the metadata containing covariates to visualize in plots, in addition to the primary variables of interest such as groups. For example, if sex is provided in vis.adj.vars, plots will display facets or colors for different sex groups. This allows visualization of effects across multiple covariates. Default is NULL, which indicates only the primary variables of interest will be visualized without additional covariates.
- test.adj.vars
Character vector, names of columns in the metadata containing covariates to be adjusted for in statistical tests and models, such as linear mixed effects models for longitudinal data analysis. This allows the user to account for the effects of additional variables in assessing the effects of primary variables of interest such as time and groups. Default is NULL, which indicates no covariates are adjusted for in statistical testing.
- strata.var
Character, column name in metadata containing stratification variable, e.g "sex". Optional.
- subject.var
Character, column name in metadata containing subject/sample IDs, e.g. "subject_id". Required.
- time.var
Character, column name in metadata containing time variable, e.g. "week". Required.
- t0.level
Character or numeric, baseline time point for longitudinal analysis, e.g. "week_0" or 0. Required.
- ts.levels
Character vector, names of follow-up time points, e.g. c("week_4", "week_8"). Required.
- depth
An integer specifying the sequencing depth for the "Rarefy" and "Rarefy-TSS" methods. If NULL, no rarefaction is performed.
- alpha.obj
An optional list containing pre-calculated alpha diversity indices. If NULL (default), alpha diversity indices will be calculated using mStat_calculate_alpha_diversity function from MicrobiomeStat package.
- alpha.name
The alpha diversity index to be plotted. Supported indices include "shannon", "simpson", "observed_species", "chao1", "ace", and "pielou".
- dist.obj
Distance matrix between samples, usually calculated using
mStat_calculate_beta_diversity
function. If NULL, beta diversity will be automatically computed fromdata.obj
usingmStat_calculate_beta_diversity
.- dist.name
A character vector specifying which beta diversity indices to calculate. Supported indices are "BC" (Bray-Curtis), "Jaccard", "UniFrac" (unweighted UniFrac), "GUniFrac" (generalized UniFrac), "WUniFrac" (weighted UniFrac), and "JS" (Jensen-Shannon divergence). If a name is provided but the corresponding object does not exist within dist.obj, it will be computed internally. If the specific index is not supported, an error message will be returned. Default is c('BC', 'Jaccard').
- pc.obj
A list containing the results of dimension reduction/Principal Component Analysis. This should be the output from functions like
mStat_calculate_PC
, containing the PC coordinates and other metadata. If NULL (default), dimension reduction will be automatically performed using metric multidimensional scaling (MDS) viamStat_calculate_PC
. The pc.obj list structure should contain:- points
A matrix with samples as rows and PCs as columns containing the coordinates.
- eig
Eigenvalues for each PC dimension.
- vectors
Loadings vectors for features onto each PC.
- Other metadata
like method, dist.name, etc.
See
mStat_calculate_PC
function for details on output format.- prev.filter
Numeric value specifying the minimum prevalence threshold for filtering taxa before analysis. Feature with prevalence below this value will be removed. Prevalence is calculated as the proportion of samples where the taxon is present. Default 0 removes no taxa by prevalence filtering.
- abund.filter
Numeric value specifying the minimum abundance threshold for filtering taxa before analysis. Feature with mean abundance below this value will be removed. Abundance refers to counts or proportions depending on
feature.dat.type
. Default 0 removes no taxa by abundance filtering.- bar.area.feature.no
A numeric value indicating the number of top abundant features to retain in both barplot and areaplot. Features with average relative abundance ranked below this number will be grouped into 'Other'. Default 20.
- heatmap.feature.no
A numeric value indicating the number of top abundant features to retain in the heatmap. Features with average relative abundance ranked below this number will be grouped into 'Other'. Default 20.
- vis.feature.level
The column name in the feature annotation matrix (feature.ann) of data.obj to use for visualization and plotting. This can be the taxonomic level like "Phylum", or any other annotation columns like "Genus" or "OTU_ID". Should be a character vector specifying one or more column names in feature.ann. Multiple columns can be provided, and data will be plotted separately for each column. Default is NULL, which defaults to all columns in feature.ann if `features.plot` is also NULL.
- test.feature.level
The column name in the feature annotation matrix (feature.ann) of data.obj to use for testing or analytical purposes. This can be the taxonomic level like "Phylum", or any other annotation columns like "Genus" or "OTU_ID". Should be a character vector specifying one or more column names in feature.ann. Multiple columns can be provided, and data will be analyzed separately for each column. Default is NULL, which defaults to all columns in feature.ann if `features.plot` is also NULL.
- feature.dat.type
The type of the feature data, which determines how the data is handled in downstream analyses. Should be one of: - "count": Raw count data, will be normalized by the function. - "proportion": Data that has already been normalized to proportions/percentages. - "other": Custom abundance data that has unknown scaling. No normalization applied. The choice affects preprocessing steps as well as plot axis labels. Default is "count", which assumes raw OTU table input.
- feature.analysis.rarafy
Logical, indicating whether to rarefy the data at the feature-level for analysis. If TRUE, the feature data will be rarefied before analysis. Default is TRUE.
- feature.change.func
A function or character string specifying how to calculate the change from baseline value. This allows flexible options: - If a function is provided, it will be applied to each row to calculate change. The function should take 2 arguments: value at timepoint t and value at baseline t0. - If a character string is provided, following options are supported: - 'relative change': (value_t - value_t0) / (value_t + value_t0) - 'absolute change': value_t - value_t0 - 'log fold change': log2(value_t + 1e-5) - log2(value_t0 + 1e-5) - Default is 'relative change'.
If none of the above options are matched, an error will be thrown indicating the acceptable options or prompting the user to provide a custom function.
- feature.mt.method
Character, multiple testing method for features, "fdr" or "none", default is "fdr".
- feature.sig.level
Numeric, significance level cutoff for highlighting features, default is 0.1.
- feature.box.axis.transform
A string indicating the transformation to apply to the y-axis of the feature's boxplot visualization before plotting. Options are: - "identity": No transformation (default). - "sqrt": Square root transformation. - "log": Logarithmic transformation. Zeros are replaced with half of the minimum non-zero value for each taxon before log transformation.
- base.size
Numeric, base font size for all plots, default is 16.
- theme.choice
Plot theme choice. Specifies the visual style of the plot. Can be one of the following pre-defined themes: - "prism": Utilizes the ggprism::theme_prism() function from the ggprism package, offering a polished and visually appealing style. - "classic": Applies theme_classic() from ggplot2, providing a clean and traditional look with minimal styling. - "gray": Uses theme_gray() from ggplot2, which offers a simple and modern look with a light gray background. - "bw": Employs theme_bw() from ggplot2, creating a classic black and white plot, ideal for formal publications and situations where color is best minimized. - "light": Implements theme_light() from ggplot2, featuring a light theme with subtle grey lines and axes, suitable for a fresh, modern look. - "dark": Uses theme_dark() from ggplot2, offering a dark background, ideal for presentations or situations where a high-contrast theme is desired. - "minimal": Applies theme_minimal() from ggplot2, providing a minimalist theme with the least amount of background annotations and colors. - "void": Employs theme_void() from ggplot2, creating a blank canvas with no axes, gridlines, or background, ideal for custom, creative plots. Each theme option adjusts various elements like background color, grid lines, and font styles to match the specified aesthetic. Default is "bw", offering a universally compatible black and white theme suitable for a wide range of applications.
- custom.theme
A custom ggplot theme provided as a ggplot2 theme object. This allows users to override the default theme and provide their own theme for plotting. Custom themes are useful for creating publication-ready figures with specific formatting requirements.
To use a custom theme, create a theme object with ggplot2::theme(), including any desired customizations. Common customizations for publication-ready figures might include adjusting text size for readability, altering line sizes for clarity, and repositioning or formatting the legend. For example:
“`r my_theme <- ggplot2::theme( axis.title = ggplot2::element_text(size=14, face="bold"), # Bold axis titles with larger font axis.text = ggplot2::element_text(size=12), # Slightly larger axis text legend.position = "top", # Move legend to the top legend.background = ggplot2::element_rect(fill="lightgray"), # Light gray background for legend panel.background = ggplot2::element_rect(fill="white", colour="black"), # White panel background with black border panel.grid.major = ggplot2::element_line(colour = "grey90"), # Lighter color for major grid lines panel.grid.minor = ggplot2::element_blank(), # Remove minor grid lines plot.title = ggplot2::element_text(size=16, hjust=0.5) # Centered plot title with larger font ) “`
Then pass `my_theme` to `custom.theme`. If `custom.theme` is NULL (the default), the theme is determined by `theme.choice`. This flexibility allows for both easy theme selection for general use and detailed customization for specific presentation or publication needs.
- palette
An optional parameter specifying the color palette to be used for the plot. It can be either a character string specifying the name of a predefined palette or a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). Available predefined palettes include 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired by various scientific publications and the `ggsci` package. If `palette` is not provided or an unrecognized palette name is given, a default color palette will be used. Ensure the number of colors in the palette is at least as large as the number of groups being plotted.
Logical, if TRUE save plots as PDF files, default is TRUE.
- file.ann
Character, annotation text to add to PDF plot filenames, default is NULL.
- pdf.wid
Numeric, width of PDF plots in inches, default is 11.
- pdf.hei
Numeric, height of PDF plots in inches, default is 8.5.
- output.file
Character, output PDF report filename (required).
Value
A PDF report containing:
- Summary statistics table: Sample size, covariates, time points
- Alpha diversity boxplots: Boxplots colored by time and groups
- Alpha diversity spaghetti plots: Line plots of trajectories
- Alpha diversity trends: Tables with LME model results
- Alpha volatility: Tables with LM model results
- Beta diversity PCoA plots: Ordination plots colored by time and groups
- Beta distance boxplots: Boxplots colored by time and groups
- Beta spaghetti plots: Individual line plots
- Beta diversity trends: Tables with LME results on distances
- Beta volatility: Tables with LM results on distances
- Feature area plots: Stacked area plots showing composition
- Feature heatmaps: Heatmaps colored by relative abundance
- Feature volcano plots: With trend/volatility significance
- Feature boxplots: Distribution by time and groups
- Feature spaghetti plots: Individual line plots
Details
This function generates a comprehensive longitudinal microbiome report with:
1. Summary Statistics - Table with sample size, number of timepoints, covariates
2. Alpha Diversity Analysis - Boxplots of alpha diversity vs time, colored by groups - Spaghetti plots of alpha trajectories for each subject - Linear mixed effects model results for alpha diversity trend - Linear model results for alpha diversity volatility
3. Beta Diversity Analysis - PCoA plots colored by time points and groups - Boxplots of PCoA coordinate 1 vs time, colored by groups - Spaghetti plots of distance from baseline vs time - Linear mixed effects models for beta diversity distance trend - Linear models for beta diversity volatility
4. Taxonomic Composition Analysis - Stacked area plots of average composition by time and groups - Heatmaps of relative abundance colored from low (blue) to high (red) - Volcano plots highlighting significant taxa in trend and volatility - Boxplots of significant taxa by time and groups - Spaghetti plots for significant taxa vs time
Author
Chen Yang cafferychen7850@email.com
Examples
if (FALSE) { # \dontrun{
data(subset_T2D.obj)
mStat_generate_report_long(
data.obj = subset_T2D.obj,
group.var = "subject_race",
strata.var = NULL,
test.adj.vars = NULL,
vis.adj.vars = NULL,
subject.var = "subject_id",
time.var = "visit_number_num",
t0.level = NULL,
ts.levels = NULL,
alpha.obj = NULL,
alpha.name = c("shannon","observed_species"),
dist.obj = NULL,
dist.name = c("BC",'Jaccard'),
pc.obj = NULL,
feature.mt.method = "none",
feature.sig.level = 0.3,
vis.feature.level = c("Family","Genus"),
test.feature.level = c("Family"),
feature.change.func = "relative change",
feature.dat.type = "count",
prev.filter = 0.1,
abund.filter = 1e-4,
bar.area.feature.no = 40,
heatmap.feature.no = 40,
feature.box.axis.transform = "sqrt",
theme.choice = "bw",
base.size = 20,
output.file = "/Users/apple/Research/MicrobiomeStat/result/per_time_test_report.pdf"
)
data(ecam.obj)
mStat_generate_report_long(
data.obj = ecam.obj,
group.var = "antiexposedall",
strata.var = NULL,
test.adj.vars = "delivery",
vis.adj.vars = "delivery",
subject.var = "subject.id",
time.var = "month_num",
t0.level = NULL,
ts.levels = NULL,
alpha.obj = NULL,
alpha.name = c("shannon","observed_species"),
dist.obj = NULL,
dist.name = c("BC",'Jaccard'),
pc.obj = NULL,
feature.mt.method = "none",
feature.sig.level = 0.3,
vis.feature.level = c("Family","Genus"),
test.feature.level = c("Family"),
feature.change.func = "relative change",
feature.dat.type = "proportion",
prev.filter = 0.1,
abund.filter = 1e-4,
bar.area.feature.no = 40,
heatmap.feature.no = 40,
feature.box.axis.transform = "sqrt",
theme.choice = "bw",
base.size = 20,
output.file = "/Users/apple/Research/MicrobiomeStat/result/ecam_obj_report_long.pdf"
)
} # }