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
16 changes: 13 additions & 3 deletions R/import_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,29 @@ 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)
for(i in seq(length(idx))){
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
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-import_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

})