diff --git a/R/parser.R b/R/parser.R index 9e38855..c2ca756 100644 --- a/R/parser.R +++ b/R/parser.R @@ -393,14 +393,14 @@ parser <- function(file, dec = ".") { #' @param condition_pattern Used to identify condition names by matching und removing given pattern with \code{\link[base]{grep}}. Ignored when condition_names is set. #' @param in_field_delimiter Delimiter for multi value fields. Default = ','. #' @param dec Decimal separator. Used in file reading and writing. -#' @param unique_id Whether the table contains an unique id column. If FALSE (default) will create one at first position. #' @param ... Used as header information. #' #' @details During conversion the parser will try to use the given config (if provided) to create the \href{https://github.molgen.mpg.de/loosolab/wilson-apps/wiki/CLARION-Format}{Clarion} metadata. In the case of insufficient config information it will try to approximate by referencing condition names issuing warnings in the process. +#' @details As the format requires an unqiue id the parser will create one if necessary. #' @details Factor grouping (metadata factor columns) is currently not implemented! #' #' @export -tobias_parser <- function(input, output, filter_columns = NULL, filter_pattern = NULL, config = system.file("extdata", "tobias_config.json", package = "wilson"), omit_NA = FALSE, condition_names = NULL, condition_pattern = "_bound$", in_field_delimiter = ",", dec = ".", unique_id = FALSE, ...) { +tobias_parser <- function(input, output, filter_columns = NULL, filter_pattern = NULL, config = system.file("extdata", "tobias_config.json", package = "wilson"), omit_NA = FALSE, condition_names = NULL, condition_pattern = "_bound$", in_field_delimiter = ",", dec = ".", ...) { ## filter data columns # check if filter columns is a file or a vector if (!is.null(filter_columns) && file.exists(filter_columns)) { @@ -432,14 +432,6 @@ tobias_parser <- function(input, output, filter_columns = NULL, filter_pattern = data <- stats::na.omit(data) } - # create id column - if (!unique_id) { - data[, "id" := seq_len(nrow(data))] - # move id column to first position - new_order <- c("id", names(data)[ names(data) != "id"]) - data <- data[, new_order, with = FALSE] - } - ##### metadata metadata <- data.table::data.table(names(data)) @@ -558,7 +550,22 @@ tobias_parser <- function(input, output, filter_columns = NULL, filter_pattern = # set unique_id fallback if (!any(metadata[["type"]] == "unique_id")) { - metadata[key == unique_id_fallback, "type"] <- "unique_id" + if (!is.null(unique_id_fallback)) { + metadata[key == unique_id_fallback, "type"] <- "unique_id" + } else { + # setup unique_id column if there is neither a defined column nor a fallback + + # create id column + data[, "id" := seq_len(nrow(data))] + # move id column to first position + new_order <- c("id", names(data)[ names(data) != "id"]) + data <- data[, new_order, with = FALSE] + + id_row <- data.table::data.table("id", level = "feature", type = "unique_id", label = "id", sub_label = "") + names(id_row)[1] <- "key" + # add meta entry + metadata <- rbind(id_row, metadata) + } } ##### header diff --git a/man/tobias_parser.Rd b/man/tobias_parser.Rd index c7cafdc..66ce2d6 100644 --- a/man/tobias_parser.Rd +++ b/man/tobias_parser.Rd @@ -8,7 +8,7 @@ tobias_parser(input, output, filter_columns = NULL, filter_pattern = NULL, config = system.file("extdata", "tobias_config.json", package = "wilson"), omit_NA = FALSE, condition_names = NULL, condition_pattern = "_bound$", - in_field_delimiter = ",", dec = ".", unique_id = FALSE, ...) + in_field_delimiter = ",", dec = ".", ...) } \arguments{ \item{input}{Path to input table} @@ -31,8 +31,6 @@ tobias_parser(input, output, filter_columns = NULL, \item{dec}{Decimal separator. Used in file reading and writing.} -\item{unique_id}{Whether the table contains an unique id column. If FALSE (default) will create one at first position.} - \item{...}{Used as header information.} } \description{ @@ -41,5 +39,7 @@ Click \href{https://github.molgen.mpg.de/loosolab/TOBIAS}{here} for more informa \details{ During conversion the parser will try to use the given config (if provided) to create the \href{https://github.molgen.mpg.de/loosolab/wilson-apps/wiki/CLARION-Format}{Clarion} metadata. In the case of insufficient config information it will try to approximate by referencing condition names issuing warnings in the process. +As the format requires an unqiue id the parser will create one if necessary. + Factor grouping (metadata factor columns) is currently not implemented! }