diff --git a/R/util.R b/R/util.R index 9fb13e9..3dbf143 100644 --- a/R/util.R +++ b/R/util.R @@ -29,17 +29,22 @@ full_locus_match <- function(sample_data, locus_name) { #' #' @return character vector of entry identifiers make_entry_id <- function(data) { - cols.names <- c("Dataset", "Sample", "Name", "Replicate", "Locus") - cols.idx <- match(cols.names, colnames(data)) - cols.idx <- cols.idx[!is.na(cols.idx)] - cols.idx <- cols.idx[unlist(lapply(cols.idx, function(x) { - !all(is.na(data[, x])) - }))] - data.names <- data[, cols.idx, drop = FALSE] - sapply(1:nrow(data.names), function(nr) { - entries <- lapply(data.names[nr, !is.na(data.names[nr, ])], as.character) - do.call(paste, as.list(c(entries, sep = "-"))) - }) + cols_names <- c("Dataset", "Sample", "Name", "Replicate", "Locus") + cols_idx <- match(cols_names, colnames(data)) + cols_idx <- cols_idx[! is.na(cols_idx)] + if (length(cols_idx)) { + cols_idx <- cols_idx[unlist(lapply(cols_idx, function(x) { + ! all(is.na(data[, x])) + }))] + data_names <- data[, cols_idx, drop = FALSE] + sapply(1:nrow(data_names), function(nr) { + entries <- lapply(data_names[nr, ! is.na(data_names[nr, ])], as.character) + do.call(paste, as.list(c(entries, sep = "-"))) + }) + } else { + warning("no recognized columns for entry id") + paste0("entry", 1:nrow(data)) + } } #' Create Row Names for STR Data diff --git a/tests/testthat/test_io.R b/tests/testthat/test_io.R index 905aee4..fc4a43d 100644 --- a/tests/testthat/test_io.R +++ b/tests/testthat/test_io.R @@ -73,6 +73,23 @@ with(test_data, { expect_true(all(locus_attrs == locus_attrs_test)) }) + test_that("load_csv parses CSV files with unknown columns", { + # For cases where our CSV file has none of the recognized column names, it + # should still work but just give us generic rownames and a + # warning. + fp <- tempfile() + data_expected <- data.frame( + Vec1 = c("A", "D"), + Vec2 = c("B", "E"), + Vec3 = c("C", "F"), + stringsAsFactors = FALSE, + row.names = c("entry1", "entry2")) + cat("Vec1,Vec2,Vec3\nA,B,C\nD,E,F\n", file = fp) + expect_warning(data <- load_csv(fp), "no recognized columns for entry id") + file.remove(fp) + expect_equal(data, data_expected) + }) + test_that("save_csv saves CSV files", { fp <- tempfile() @@ -86,6 +103,21 @@ with(test_data, { expect_true(all(locus_attrs == locus_attrs_test)) }) + test_that("save_csv saves CSV files with unknown columns", { + # save_csv shouldn't care about the columns but we'll make sure here. + fp <- tempfile() + data_expected <- data.frame( + Vec1 = c("A", "D"), + Vec2 = c("B", "E"), + Vec3 = c("C", "F"), + stringsAsFactors = FALSE) + save_csv(data_expected, fp) + expect_warning(data <- load_csv(fp)) + file.remove(fp) + rownames(data_expected) <- c("entry1", "entry2") + expect_equal(data, data_expected) + }) + # test load_locus_attrs ---------------------------------------------------