Skip to content

Commit

Permalink
Add checks for missing alpha diversity indices in multiple functions
Browse files Browse the repository at this point in the history
This GitHub push updates several functions in the codebase to include checks for missing alpha diversity indices:

1. In the `generate_alpha_change_boxplot_pair` function:
   - Added a check to verify if all specified `alpha.name` values are present in `alpha.obj`.
   - If any missing alpha diversity indices are found, an error is thrown with a message listing the missing indices.

2. Similar checks for missing alpha diversity indices were added to the following functions:
   - `generate_alpha_change_test_long`
   - `generate_alpha_change_test_pair`
   - `generate_alpha_test_long`
   - `generate_alpha_test_pair`

3. In the `generate_alpha_test_single` function:
   - Updated the example code to include "ace" in the `alpha.name` parameter.
   - Modified the code to use `subset_data.obj` instead of `data.obj` when subsetting data based on `time.var` and `t.level`.
   - Added a check for missing alpha diversity indices in `alpha.obj`, similar to the other functions.
   - Updated the test list generation to use `alpha.name` instead of indexing `alpha.obj`.

These changes ensure that the functions gracefully handle cases where the specified alpha diversity indices are not available in the provided `alpha.obj`. By adding these checks, the code becomes more robust and informative, throwing an error with a clear message when missing indices are encountered.
  • Loading branch information
cafferychen777 committed Apr 9, 2024
1 parent 45f0d78 commit 9006226
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
11 changes: 11 additions & 0 deletions R/generate_alpha_change_boxplot_pair.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ generate_alpha_change_boxplot_pair <-
otu_tab <- data.obj$feature.tab
alpha.obj <-
mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

meta_tab <-
Expand Down
11 changes: 11 additions & 0 deletions R/generate_alpha_change_test_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ generate_alpha_change_test_long <-
}
otu_tab <- data.obj$feature.tab
alpha.obj <- mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

meta_tab <-
Expand Down
11 changes: 11 additions & 0 deletions R/generate_alpha_change_test_pair.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ generate_alpha_change_test_pair <-
}
otu_tab <- data.obj$feature.tab
alpha.obj <- mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

meta_tab <-
Expand Down
11 changes: 11 additions & 0 deletions R/generate_alpha_test_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ generate_alpha_test_long <- function(data.obj,
otu_tab <- data.obj$feature.tab
alpha.obj <-
mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

meta_tab <-
Expand Down
11 changes: 11 additions & 0 deletions R/generate_alpha_test_pair.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ generate_alpha_test_pair <-
otu_tab <- data.obj$feature.tab
alpha.obj <-
mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

meta_tab <-
Expand Down
23 changes: 18 additions & 5 deletions R/generate_alpha_test_single.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#' c("shannon", "observed_species"))
#' generate_alpha_test_single(data.obj = subset_T2D.obj,
#' alpha.obj = alpha.obj,
#' alpha.name = c("shannon", "observed_species"),
#' alpha.name = c("shannon", "observed_species", "ace"),
#' time.var = "visit_number",
#' t.level = NULL,
#' group.var = "subject_race",
Expand Down Expand Up @@ -68,8 +68,10 @@ generate_alpha_test_single <-
}

if (!is.null(time.var) & !is.null(t.level)) {
condition <- paste(time.var, "== '", t.level, "'", sep = "")
data.obj <- mStat_subset_data(data.obj, condition = condition)
subset.ids <- rownames(data.obj$meta.dat %>%
filter(!!sym(time.var) %in% c(t.level)))

subset_data.obj <- mStat_subset_data(data.obj, samIDs = subset.ids)
}

if (is.null(alpha.obj)) {
Expand All @@ -81,12 +83,23 @@ generate_alpha_test_single <-
}
otu_tab <- data.obj$feature.tab
alpha.obj <- mStat_calculate_alpha_diversity(x = otu_tab, alpha.name = alpha.name)
} else {
# Verify that all alpha.name are present in alpha.obj
if (!all(alpha.name %in% unlist(lapply(alpha.obj, function(x)
colnames(x))))) {
missing_alphas <- alpha.name[!alpha.name %in% names(alpha.obj)]
stop(
"The following alpha diversity indices are not available in alpha.obj: ",
paste(missing_alphas, collapse = ", "),
call. = FALSE
)
}
}

# Generate tests
test.list <- lapply(seq_along(alpha.obj), function(i) {
test.list <- lapply(alpha.name, function(alpha.name) {

df <- alpha.obj[[i]]
df <- alpha.obj[[alpha.name]]
# Join the alpha diversity index with metadata
merged_df <-
dplyr::inner_join(
Expand Down

0 comments on commit 9006226

Please sign in to comment.