Skip to content

Commit 1893127

Browse files
long
1 parent dbd9009 commit 1893127

File tree

67 files changed

+1852
-402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1852
-402
lines changed

.DS_Store

0 Bytes
Binary file not shown.

NAMESPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export(generate_alpha_spaghettiplot_long)
1111
export(generate_alpha_test_long)
1212
export(generate_alpha_test_pair)
1313
export(generate_alpha_test_single)
14+
export(generate_alpha_trend_test_long)
15+
export(generate_alpha_volatility_test_long)
1416
export(generate_beta_change_boxplot_pair)
1517
export(generate_beta_change_spaghettiplot_long)
1618
export(generate_beta_change_test_pair)
@@ -19,9 +21,13 @@ export(generate_beta_ordination_pair)
1921
export(generate_beta_ordination_single)
2022
export(generate_beta_pc_boxplot_long)
2123
export(generate_beta_pc_change_boxplot_pair)
24+
export(generate_beta_pc_trend_test_long)
25+
export(generate_beta_pc_volatility_test_long)
2226
export(generate_beta_test_long)
2327
export(generate_beta_test_pair)
2428
export(generate_beta_test_single)
29+
export(generate_beta_trend_test_long)
30+
export(generate_beta_volatility_test_long)
2531
export(generate_taxa_areaplot_long)
2632
export(generate_taxa_barplot_long)
2733
export(generate_taxa_barplot_pair)
@@ -48,6 +54,8 @@ export(generate_taxa_spaghettiplot_long)
4854
export(generate_taxa_test_long)
4955
export(generate_taxa_test_pair)
5056
export(generate_taxa_test_single)
57+
export(generate_taxa_trend_test_long)
58+
export(generate_taxa_volatility_test_long)
5159
export(is_categorical)
5260
export(is_continuous_numeric)
5361
export(is_rarefied)
@@ -56,6 +64,7 @@ export(linda.plot)
5664
export(mStat_aggregate_by_taxonomy)
5765
export(mStat_aggregate_data)
5866
export(mStat_calculate_PC)
67+
export(mStat_calculate_adjusted_distance)
5968
export(mStat_calculate_alpha_diversity)
6069
export(mStat_calculate_beta_diversity)
6170
export(mStat_combine_data)

R/generate_alpha_boxplot_long.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @param palette An optional color palette for the plot. If not provided, a default color palette will be used. The palette should be a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). The number of colors in the palette should be at least as large as the number of groups being plotted.
1414
#' @param group.var An optional variable in the metadata table that represents the grouping factor.
1515
#' @param strata.var An optional variable in the metadata table that represents the stratification factor.
16+
#' @param adj.vars A character vector of variable names to be used for adjustment.
1617
#' @param base.size The base font size for the plot.
1718
#' @param theme.choice A character string indicating the choice of pre-defined ggplot2 theme for the plot. Supported choices include "prism" (default), "classic", "gray", and "bw".
1819
#' @param custom.theme An optional custom ggplot2 theme. If provided, this theme will be used instead of the pre-defined themes.
@@ -220,7 +221,7 @@ generate_alpha_boxplot_long <- function (data.obj,
220221

221222
# 使用mutate和residuals来添加残差
222223
alpha_df <- alpha_df %>%
223-
mutate(!!sym(index) := residuals(lm(formula_obj, data = alpha_df)))
224+
dplyr::mutate(!!sym(index) := stats::residuals(lm(formula_obj, data = alpha_df)))
224225

225226
message("Alpha diversity has been adjusted for the following covariates: ", paste(adj.vars, collapse = ", "), ".")
226227
}

R/generate_alpha_boxplot_single.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' @param t.level The specific time point to be analyzed.
1111
#' @param group.var An optional variable in the metadata table that represents the grouping factor.
1212
#' @param strata.var An optional variable in the metadata table that represents the stratification factor.
13+
#' @param adj.vars A character vector of variable names to be used for adjustment.
1314
#' @param base.size The base font size for the plot. Default is 16.
1415
#' @param theme.choice A character string indicating the choice of pre-defined ggplot2 theme for the plot. Supported choices include "prism" (default), "classic", "gray", and "bw".
1516
#' @param custom.theme An optional custom ggplot2 theme. If provided, this theme will be used instead of the pre-defined themes.
@@ -185,7 +186,7 @@ generate_alpha_boxplot_single <- function (data.obj,
185186

186187
# 使用mutate和residuals来添加残差
187188
alpha_df <- alpha_df %>%
188-
mutate(!!sym(index) := residuals(lm(formula_obj, data = alpha_df)))
189+
dplyr::mutate(!!sym(index) := stats::residuals(lm(formula_obj, data = alpha_df)))
189190

190191
message("Alpha diversity has been adjusted for the following covariates: ", paste(adj.vars, collapse = ", "), ".")
191192
}

R/generate_alpha_change_boxplot_pair.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#' @param time.var The variable in the metadata table that represents the time.
1616
#' @param group.var (Optional) The variable in the metadata table that represents the grouping factor.
1717
#' @param strata.var (Optional) The variable in the metadata table that represents the stratification factor.
18+
#' @param adj.vars A character vector of variable names to be used for adjustment.
1819
#' @param change.base The base time for calculating the change in alpha diversity.
1920
#' @param change.func (Optional) A function for calculating the change in alpha diversity; can be either "log" (default) or "absolute."
2021
#' @param pdf (Optional) A boolean indicating whether to save the output as a PDF file.
@@ -239,7 +240,7 @@ generate_alpha_change_boxplot_pair <-
239240

240241
# 使用mutate和residuals来添加残差
241242
combined_alpha <- combined_alpha %>%
242-
mutate(!!sym(paste0(index, "_diff")) := residuals(lm(formula_obj, data = combined_alpha)))
243+
dplyr::mutate(!!sym(paste0(index, "_diff")) := stats::residuals(lm(formula_obj, data = combined_alpha)))
243244

244245
message("Alpha diversity Change has been adjusted for the following covariates: ", paste(adj.vars, collapse = ", "), ".")
245246
}

R/generate_alpha_spaghettiplot_long.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#' @param time.var The name of the time variable.
1919
#' @param group.var The name of the group variable.
2020
#' @param strata.var The name of the strata variable (default is NULL).
21+
#' @param adj.vars A character vector of variable names to be used for adjustment.
2122
#' @param pdf Logical, whether to save the plot as a PDF (default is TRUE).
2223
#' @param file.ann The annotation to be added to the PDF file name (default is NULL).
2324
#' @param ... Additional arguments passed to ggplot().
@@ -182,7 +183,7 @@ generate_alpha_spaghettiplot_long <-
182183

183184
# 使用mutate和residuals来添加残差
184185
sub_alpha.df <- sub_alpha.df %>%
185-
mutate(!!sym(index) := residuals(lm(formula_obj, data = sub_alpha.df)))
186+
dplyr::mutate(!!sym(index) := stats::residuals(lm(formula_obj, data = sub_alpha.df)))
186187

187188
message("Alpha diversity has been adjusted for the following covariates: ", paste(adj.vars, collapse = ", "), ".")
188189
}

R/generate_alpha_trend_test_long.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ construct_formula <- function(index, group.var, time.var, subject.var, adj.vars)
4646
#' @param data.obj A list object that includes feature.tab (an OTU table with
4747
#' taxa as rows and samples as columns) and meta.dat (a metadata table with
4848
#' samples as rows and variables as columns).
49+
#' @param alpha.obj An object containing precomputed alpha diversity indices,
50+
#' typically calculated by the function `mStat_calculate_alpha_diversity`.
51+
#' If NULL (the default), alpha diversity will be calculated from the data.obj using
52+
#' `mStat_calculate_alpha_diversity`.
4953
#' @param alpha.name A string with the name of the alpha diversity index to compute.
5054
#' Options could include: "shannon", "simpson", "observed_species", "chao1", "ace", and "pielou".
5155
#' @param time.var A string representing the time variable's name in the
@@ -104,12 +108,14 @@ generate_alpha_trend_test_long <- function(data.obj,
104108
}
105109
}
106110

107-
message("The volatility calculation in generate_alpha_volatility_test_long relies on a numeric time variable.
108-
Please check that your time variable is coded as numeric.
109-
If the time variable is not numeric, it may cause issues in computing the results of the volatility test.
110-
You can ensure the time variable is numeric by mutating it in the metadata.")
111+
message(
112+
"The trend test in 'generate_alpha_trend_test_long' relies on a numeric time variable.\n",
113+
"Please ensure that your time variable is coded as numeric.\n",
114+
"If the time variable is not numeric, it may cause issues in computing the results of the trend test.\n",
115+
"The time variable will be converted to numeric within the function if needed."
116+
)
111117

112-
data.obj$meta.dat <- data.obj$meta.dat %>% mutate(!!sym(time.var) := as.numeric(!!sym(time.var)))
118+
data.obj$meta.dat <- data.obj$meta.dat %>% dplyr::mutate(!!sym(time.var) := as.numeric(!!sym(time.var)))
113119

114120
meta_tab <-
115121
load_data_obj_metadata(data.obj) %>% as.data.frame() %>% select(all_of(c(
@@ -123,8 +129,6 @@ generate_alpha_trend_test_long <- function(data.obj,
123129

124130
test.list <- lapply(alpha.name, function(index) {
125131

126-
# Calculate the volatility for each subject
127-
128132
formula <- construct_formula(index, group.var, time.var, subject.var, adj.vars)
129133

130134
model <- lmer(formula, data = alpha_df)

R/generate_alpha_volatility_test_long.R

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#' @param data.obj A list object that includes feature.tab (an OTU table with
1717
#' taxa as rows and samples as columns) and meta.dat (a metadata table with
1818
#' samples as rows and variables as columns).
19+
#' @param alpha.obj An object containing precomputed alpha diversity indices,
20+
#' typically calculated by the function `mStat_calculate_alpha_diversity`.
21+
#' If NULL (the default), alpha diversity will be calculated from the data.obj using
22+
#' `mStat_calculate_alpha_diversity`.
1923
#' @param alpha.name A string with the name of the alpha diversity index to compute.
2024
#' Options could include: "shannon", "simpson", "observed_species", "chao1", "ace", and "pielou".
2125
#' @param time.var A string representing the time variable's name in the
@@ -35,11 +39,12 @@
3539
#' subset_T2D.obj2$meta.dat$visit_number <- as.numeric(subset_T2D.obj2$meta.dat$visit_number)
3640
#' generate_alpha_volatility_test_long(
3741
#' data.obj = subset_T2D.obj2,
42+
#' alpha.obj = NULL,
3843
#' alpha.name = c("shannon","simpson"),
3944
#' time.var = "visit_number",
4045
#' subject.var = "subject_id",
4146
#' group.var = "subject_race",
42-
#' adj.vars = "subject_gender"
47+
#' adj.vars = "sample_body_site"
4348
#' )
4449
#' }
4550
#' @export
@@ -74,12 +79,14 @@ generate_alpha_volatility_test_long <- function(data.obj,
7479
}
7580
}
7681

77-
message("The volatility calculation in generate_alpha_volatility_test_long relies on a numeric time variable.
78-
Please check that your time variable is coded as numeric.
79-
If the time variable is not numeric, it may cause issues in computing the results of the volatility test.
80-
You can ensure the time variable is numeric by mutating it in the metadata.")
82+
message(
83+
"The volatility calculation in generate_alpha_volatility_test_long relies on a numeric time variable.\n",
84+
"Please check that your time variable is coded as numeric.\n",
85+
"If the time variable is not numeric, it may cause issues in computing the results of the volatility test.\n",
86+
"You can ensure the time variable is numeric by mutating it in the metadata."
87+
)
8188

82-
data.obj$meta.dat <- data.obj$meta.dat %>% mutate(!!sym(time.var) := as.numeric(!!sym(time.var)))
89+
data.obj$meta.dat <- data.obj$meta.dat %>% dplyr::mutate(!!sym(time.var) := as.numeric(!!sym(time.var)))
8390

8491
meta_tab <-
8592
load_data_obj_metadata(data.obj) %>% as.data.frame() %>% select(all_of(c(
@@ -95,7 +102,9 @@ generate_alpha_volatility_test_long <- function(data.obj,
95102

96103
if (!is.null(adj.vars)){
97104
# Obtain the residuals by fitting lm(alpha ~ adj.vars)
98-
residuals_lm <- lm(as.formula(paste0(index, "~", paste(adj.vars, collapse = "+"))), data = alpha_df)$residuals
105+
residuals_lm <- stats::lm(as.formula(paste0(index, "~", paste(adj.vars, collapse = "+"))), data = alpha_df)$residuals
106+
} else {
107+
residuals_lm <- alpha_df %>% dplyr::select(all_of(c(index))) %>% dplyr::pull()
99108
}
100109

101110
# Calculate the volatility for each subject
@@ -104,23 +113,24 @@ generate_alpha_volatility_test_long <- function(data.obj,
104113

105114
# Group data by subject and calculate volatility
106115
volatility_df <- alpha_df %>%
107-
arrange(!!sym(subject.var), !!sym(time.var)) %>%
108-
group_by(!!sym(subject.var)) %>%
109-
mutate(diff_residuals = abs(residuals - lag(residuals)),
110-
diff_time = !!sym(time.var) - lag(!!sym(time.var))) %>%
111-
filter(!is.na(diff_residuals), !is.na(diff_time)) %>%
112-
filter(diff_time != 0) %>%
113-
summarize(volatility = mean(diff_residuals / diff_time), .groups = 'drop')
116+
dplyr::group_by(!!sym(subject.var)) %>%
117+
dplyr::arrange(!!sym(time.var)) %>%
118+
dplyr::mutate(diff_residuals = abs(residuals - lag(residuals)),
119+
diff_time = !!sym(time.var) - dplyr::lag(!!sym(time.var))) %>%
120+
dplyr::filter(!is.na(diff_residuals), !is.na(diff_time)) %>%
121+
dplyr::filter(diff_time != 0) %>%
122+
dplyr::summarize(volatility = mean(diff_residuals / diff_time), .groups = 'drop')
114123

115124
test_df <- volatility_df %>%
116-
left_join(meta_tab %>%
125+
dplyr::left_join(meta_tab %>%
117126
select(all_of(c(subject.var, group.var))) %>%
118-
distinct(),
127+
dplyr::distinct(),
119128
by = subject.var)
120129

121130
# Test the association between the volatility and the grp.var
122131
formula <- as.formula(paste("volatility ~", group.var))
123-
test_result <- lm(formula, data = test_df)
132+
133+
test_result <- stats::lm(formula, data = test_df)
124134

125135
coef.tab <- extract_coef(test_result)
126136

R/generate_beta_change_boxplot_pair.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#' @param time.var A string specifying the name of the time variable
1212
#' @param group.var A string specifying the name of the group variable or NULL (default)
1313
#' @param strata.var A string specifying the name of the strata variable or NULL (default)
14+
#' @param adj.vars A character vector containing the names of the columns in data.obj$meta.dat to include as covariates in the PERMANOVA analysis. If no covariates are needed, use NULL (default).
1415
#' @param change.base A string or numeric value specifying the first time point used for computing the beta diversity change
1516
#' @param 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.
1617
#' @param base.size (Optional) Base font size for the plot (default is 16).
@@ -38,7 +39,7 @@
3839
#' dist.obj <- mStat_calculate_beta_diversity(peerj32.obj, dist.name = "BC")
3940
#' generate_beta_change_boxplot_pair(
4041
#' data.obj = peerj32.obj,
41-
#' dist.obj = dist.obj,
42+
#' dist.obj = NULL,
4243
#' subject.var = "subject",
4344
#' time.var = "time",
4445
#' group.var = "group",
@@ -81,6 +82,9 @@ generate_beta_change_boxplot_pair <-
8182
meta_tab <- load_data_obj_metadata(data.obj) %>% select(all_of(c(subject.var, time.var, group.var, strata.var, adj.vars)))
8283
dist.obj <-
8384
mStat_calculate_beta_diversity(data.obj = data.obj, dist.name = dist.name)
85+
if (!is.null(adj.vars)){
86+
dist.obj <- mStat_calculate_adjusted_distance(data.obj = data.obj, dist.obj = dist.obj, adj.vars = adj.vars, dist.name = dist.name)
87+
}
8488
} else {
8589
if (!is.null(data.obj) & !is.null(data.obj$meta.dat)){
8690
meta_tab <- load_data_obj_metadata(data.obj) %>% select(all_of(c(subject.var, time.var, group.var, strata.var, adj.vars)))
@@ -93,10 +97,6 @@ generate_beta_change_boxplot_pair <-
9397

9498
meta_tab <- meta_tab %>% rownames_to_column("sample")
9599

96-
if (!is.null(adj.vars)){
97-
dist.obj <- mStat_calculate_adjusted_distance(data.obj = data.obj, dist.obj = dist.obj, adj.vars = adj.vars, dist.name = dist.name)
98-
}
99-
100100
if (is.null(change.base)){
101101
change.base <- unique(meta_tab %>% select(all_of(c(time.var))))[1,]
102102
message("The 'change.base' variable was NULL. It has been set to the first unique value in the 'time.var' column of the 'alpha_df' data frame: ", change.base)

R/generate_beta_change_spaghettiplot_long.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' @param t0.level (Optional) The baseline time point level.
1010
#' @param ts.levels (Optional) Time point levels.
1111
#' @param group.var (Optional) The variable in the metadata table that represents the grouping factor.
12+
#' @param adj.vars A character vector containing the names of the columns in data.obj$meta.dat to include as covariates in the PERMANOVA analysis. If no covariates are needed, use NULL (default).
1213
#' @param strata.var (Optional) The variable in the metadata table that represents the stratification factor.
1314
#' @param 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.
1415
#' @param base.size (Optional) Base font size for the plot (default is 16).
@@ -104,7 +105,7 @@ generate_beta_change_spaghettiplot_long <-
104105
classic = theme_classic(),
105106
gray = theme_gray(),
106107
bw = theme_bw(),
107-
ggprism::theme_prism())
108+
ggprism::theme_prism())
108109

109110
theme_to_use <- if (!is.null(custom.theme)) custom.theme else theme_function
110111

@@ -122,6 +123,9 @@ generate_beta_change_spaghettiplot_long <-
122123
meta_tab <- load_data_obj_metadata(data.obj) %>% select(all_of(c(subject.var, time.var, group.var, strata.var)))
123124
dist.obj <-
124125
mStat_calculate_beta_diversity(data.obj = data.obj, dist.name = dist.name)
126+
if (!is.null(adj.vars)){
127+
dist.obj <- mStat_calculate_adjusted_distance(data.obj = data.obj, dist.obj = dist.obj, adj.vars = adj.vars, dist.name = dist.name)
128+
}
125129
} else {
126130
if (!is.null(data.obj) & !is.null(data.obj$meta.dat)){
127131
data.obj <-
@@ -135,10 +139,6 @@ generate_beta_change_spaghettiplot_long <-
135139
}
136140
}
137141

138-
if (!is.null(adj.vars)){
139-
dist.obj <- mStat_calculate_adjusted_distance(data.obj = data.obj, dist.obj = dist.obj, adj.vars = adj.vars, dist.name = dist.name)
140-
}
141-
142142
if (is.null(dist.obj[[dist.name]])) {
143143
message(paste("dist.obj does not contain", dist.name))
144144
} else {

0 commit comments

Comments
 (0)