Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions R/report.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ normalize_alleles <- function(data) {
#'
#' @export
tabulate_allele_names <- function(data, extra_cols=NULL) {
if (length(extra_cols)) {
badcols <- extra_cols[! extra_cols %in% colnames(data)]
if (length(badcols)) {
stop(
paste("undefined extra columns in tabulate_allele_names: ",
badcols, collapse = " "))
}
}
# Order and replicate (for homozygous) the allele names
nms <- normalize_alleles(data[, c("Allele1Name", "Allele2Name")])
# Create unique (aside from Locus) identifiers for each entry
Expand Down Expand Up @@ -69,8 +77,8 @@ tabulate_allele_names <- function(data, extra_cols=NULL) {
sep = "_")
colnames(tbl) <- c("ID", extra_cols, allele_cols)
# If extra columns were given, re-order output rows using those
if (! is.null(extra_cols)) {
tbl <- tbl[order_entries(tbl[, extra_cols]), ]
if (length(extra_cols)) {
tbl <- tbl[order_entries(tbl[, extra_cols, drop = FALSE]), ]
}
tbl
}
Expand Down
19 changes: 17 additions & 2 deletions tests/testthat/test_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,25 @@ with(test_data, {
Replicate = integer(3) * NA,
stringsAsFactors = FALSE)
with(results_summary_data, {
# The basic case: sample and replicate columns
tbl <- tabulate_allele_names(results$summary,
extra_cols = c("Sample", "Replicate"))
tbl <- tbl[, 1:3]
expect_equivalent(tbl, tbl_known)
expect_true(all(c("Sample", "Replicate") %in% colnames(tbl)))
expect_equivalent(tbl[, 1:3], tbl_known)
# What about just one column name? (We'd better be using drop=FALSE!)
tbl <- tabulate_allele_names(results$summary,
extra_cols = "Sample")
expect_true("Sample" %in% colnames(tbl))
expect_false("Replicate" %in% colnames(tbl))
expect_equivalent(tbl[, 1:2], tbl_known[, 1:2])
# Unknown column name
expect_error({
tabulate_allele_names(results$summary,
extra_cols = "DoesNotExist")
}, "undefined extra columns")
# Empty column name vector -> same as not given
tbl <- tabulate_allele_names(results$summary, extra_cols = character())
expect_true(all(! c("Sample", "Replicate") %in% colnames(tbl)))
})
})

Expand Down