Skip to content

Commit d2a9479

Browse files
Fixed a bug in calculation functions that would name columns incorrectly in some data sets when the groups option was used.
relates to #233
1 parent 902efdf commit d2a9479

17 files changed

+24
-17
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: metacoder
22
Title: Tools for Parsing, Manipulating, and Graphing Taxonomic Abundance Data
3-
Version: 0.3.1.9001
3+
Version: 0.3.1.9002
44
Authors@R: c(person("Zachary", "Foster", email =
55
"zacharyfoster1989@gmail.com", role = c("aut", "cre")),
66
person("Niklaus", "Grunwald", email =

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export(complement)
1616
export(contains)
1717
export(counts_to_presence)
1818
export(diverging_palette)
19+
export(edge_shape_functions)
1920
export(ends_with)
2021
export(everything)
2122
export(filter_ambiguous_taxa)
@@ -27,6 +28,7 @@ export(make_dada2_asv_table)
2728
export(make_dada2_tax_table)
2829
export(matches)
2930
export(ncbi_taxon_sample)
31+
export(node_shape_functions)
3032
export(num_range)
3133
export(one_of)
3234
export(parse_dada2)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Development version
44

55
* calculation functions that use the `groups` option now warn if `groups` is used without `cols`.
6+
* Fixed a bug in calculation functions that would name columns incorrectly in some data sets when the `groups` option was used. (issue [#233](https://github.com/grunwaldlab/metacoder/issues/233)).
67

78
## metacoder 0.3.1
89

R/calculations--internal.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ get_numeric_cols <- function(obj, data, cols = NULL) {
7676
#' \item{Numeric vector:}{The indexes of columns to preserve}
7777
#' \item{Vector of TRUE/FALSE of length equal to the number of columns:}{Preserve the columns corresponding to \code{TRUE} values.}}
7878
#' @param out_names The names of count columns in the output. Must be the same
79-
#' length as \code{cols} (or \code{unique(groups)}, if \code{groups} is used).
79+
#' length and order as \code{cols} (or \code{unique(groups)}, if \code{groups} is used).
8080
#'
8181
#' @return A tibble
8282
#'
@@ -142,6 +142,7 @@ do_calc_on_num_cols <- function(obj, data, func, cols = NULL, groups = NULL,
142142
result <- NULL
143143
} else {
144144
result <- func(input[, cols], cols = cols, groups = groups)
145+
result <- result[, as.character(unique(groups)), drop = FALSE]
145146
colnames(result) <- out_names
146147
}
147148

R/calculations.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ calc_group_stat <- function(obj, data, func, groups = NULL, cols = NULL,
249249
do_calc_on_num_cols(obj, data, cols = cols, groups = groups,
250250
other_cols = other_cols, out_names = out_names,
251251
func = function(count_table, cols = cols, groups = groups) {
252-
as.data.frame(lapply(split(cols, groups), function(col_index) {
252+
tibble::as.tibble(lapply(split(cols, groups), function(col_index) {
253253
apply(count_table[, col_index], MARGIN = 1, FUN = func)
254254
}))
255255
}
@@ -418,6 +418,7 @@ zero_low_counts <- function(obj, data, min_count = 2, use_total = FALSE,
418418
}
419419
count_table[to_zero] <- 0
420420
}
421+
names(count_table) <- groups # needed because do_calc_on_num_cols assumes column named by groups even though this function does not use groups currently
421422
return(count_table)
422423
}
423424

@@ -488,7 +489,9 @@ rarefy_obs <- function(obj, data, sample_size = NULL, cols = NULL,
488489
sample_size <- min(colSums(count_table)) # Calculate minimum count if no sample size is given
489490
my_print("Rarefying to ", sample_size, " since that is the lowest sample total.")
490491
}
491-
as.data.frame(t(vegan::rrarefy(t(count_table), sample = sample_size)))
492+
output <- tibble::as.tibble(t(vegan::rrarefy(t(count_table), sample = sample_size)))
493+
names(output) <- groups # needed because do_calc_on_num_cols assumes column named by groups even though this function does not use groups currently
494+
return(output)
492495
}
493496
)
494497
}
@@ -771,7 +774,7 @@ calc_taxon_abund <- function(obj, data, cols = NULL, groups = NULL,
771774
}
772775
}, numeric(1))
773776
})
774-
output <- as.data.frame(output, stringsAsFactors = FALSE)
777+
output <- tibble::as.tibble(output, stringsAsFactors = FALSE)
775778

776779
return(output)
777780
}

man/calc_group_mean.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_group_median.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_group_rsd.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_group_stat.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_n_samples.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)