diff --git a/R/import_data.R b/R/import_data.R index 0b64a7c..f215d86 100644 --- a/R/import_data.R +++ b/R/import_data.R @@ -97,12 +97,12 @@ import_data <- function(peak_table, meta_data, format = "none") { } -unique_compounds <- function(peak_table_list) { +unique_compounds <- function(peak_table_list, show_message = TRUE) { peak_table <- peak_table_list$peak_table duplicates <- names(which(table(peak_table$Compound) > 1)) - if(length(duplicates) > 0){ + if(length(duplicates) > 0 && show_message){ cli::cli_inform("Found duplicate compound values, will add a suffix to unique - the value.") + the value.") } for(name in duplicates) { idx <- which(peak_table$Compound == name) @@ -110,6 +110,16 @@ unique_compounds <- function(peak_table_list) { peak_table$Compound[[idx[[i]]]] <- paste0(peak_table$Compound[[idx[[i]]]], "_", i) } } + + # Recursive call to ensure there are no duplicates after fix + duplicates <- names(which(table(peak_table$Compound) > 1)) + + if(length(duplicates) > 0){ + peak_table <- unique_compounds(list( + "peak_table" = peak_table, + "raw_table" = peak_table + ), FALSE)$peak_table + } return(list( "peak_table" = peak_table, "raw_table" = peak_table diff --git a/tests/testthat/test-import_data.R b/tests/testthat/test-import_data.R index f21c77a..40e9cbe 100644 --- a/tests/testthat/test-import_data.R +++ b/tests/testthat/test-import_data.R @@ -47,6 +47,14 @@ test_that("unique_compounds annotate duplicates properly", { length(uniqued_list$raw_table$Compound)) expect_false(length(unique(df$Compound)) == length(df$Compound)) + + df <- data.frame(Compound = c("1", "1", "1_1", "1_1_1")) + ls <- list(peak_table = df, raw_table = df) + uniqued_list <- unique_compounds(ls) + + expect_true(uniqued_list$peak_table$Compound[[4]] == "1_1_1_2") + expect_true(uniqued_list$peak_table$Compound[[1]] == "1_1_1_1") + })