Skip to content

Commit

Permalink
nameStyle argument for drug ingredients
Browse files Browse the repository at this point in the history
  • Loading branch information
edward-burn committed Oct 14, 2024
1 parent 6b19b33 commit 877c5fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: CodelistGenerator
Title: Identify Relevant Clinical Codes and Evaluate Their Use
Version: 3.2.0
Version: 3.2.0.900
Authors@R: c(
person("Edward", "Burn", email = "[email protected]",
role = c("aut", "cre"),
Expand Down
15 changes: 11 additions & 4 deletions R/drugCodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ getATCCodes <- function(cdm,
#' @param name Names of ingredients of interest. For example, c("acetaminophen",
#' "codeine"), would result in a list of length two with the descendant
#' concepts for these two particular drug ingredients.
#' @param nameStyle Name style to apply to returned list. Can be one of
#' "{concept_code}_{concept_name}", "{concept_code}", or "{concept_name}".
#' @param doseForm Only descendants codes with the specified dose form
#' will be returned. If NULL, descendant codes will be returned regardless
#' of dose form.
Expand All @@ -214,11 +216,13 @@ getATCCodes <- function(cdm,
#' @examples
#' \dontrun{
#' cdm <- mockVocabRef()
#' getDrugIngredientCodes(cdm = cdm, name = "Adalimumab")
#' getDrugIngredientCodes(cdm = cdm, name = "Adalimumab",
#' nameStyle = "{concept_name}")
#' CDMConnector::cdmDisconnect(cdm)
#'}
getDrugIngredientCodes <- function(cdm,
name = NULL,
nameStyle = "{concept_code}_{concept_name}",
doseForm = NULL,
doseUnit = NULL,
routeCategory = NULL,
Expand All @@ -236,6 +240,10 @@ getDrugIngredientCodes <- function(cdm,
add = errorMessage,
null.ok = TRUE
)
checkmate::assert_choice(x = nameStyle,
choices = c("{concept_code}_{concept_name}",
"{concept_code}",
"{concept_name}"))
checkmate::assertCharacter(type, len = 1)
checkmate::reportAssertions(collection = errorMessage)

Expand Down Expand Up @@ -303,14 +311,13 @@ getDrugIngredientCodes <- function(cdm,
f = as.factor(ingredientCodes$ancestor_concept_id),
drop = TRUE
)

names(ingredientCodes) <- dplyr::tibble(concept_id = names(ingredientCodes)) |>
dplyr::mutate(seq = dplyr::row_number()) |>
dplyr::left_join(ingredientConcepts |>
dplyr::mutate(concept_id = as.character(.data$concept_id)),
by= "concept_id") |>
dplyr::mutate(new_name = paste0(.data$concept_code, "_",
omopgenerics::toSnakeCase(.data$concept_name))) |>
dplyr::mutate(concept_name = paste0(omopgenerics::toSnakeCase(.data$concept_name)),
new_name = glue::glue(nameStyle)) |>
dplyr::arrange(seq) |>
dplyr::pull("new_name")

Expand Down
3 changes: 3 additions & 0 deletions man/getDrugIngredientCodes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion tests/testthat/test-dbms.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,22 @@ test_that("redshift", {
expect_true(nrow(asthma) > 0)

# drug ingredients
expect_no_error(metformin <- getDrugIngredientCodes(cdm, "metformin"))
expect_no_error(metformin <- getDrugIngredientCodes(cdm, "metformin",
nameStyle = "{concept_name}"))
expect_true(inherits(metformin, "codelist"))
expect_true("metformin" %in% names(metformin))

expect_no_error(metformin_2 <- getDrugIngredientCodes(cdm, "metformin",
nameStyle = "{concept_code}"))
expect_true("6809" %in% names(metformin_2))

expect_no_error(metformin_3 <- getDrugIngredientCodes(cdm, "metformin",
nameStyle = "{concept_code}_{concept_name}"))
expect_true("6809_metformin" %in% names(metformin_3))

expect_error(getDrugIngredientCodes(cdm, "metformin",
nameStyle = "something else"))


# achilles
cdm$achilles_results <- cdm$condition_occurrence %>%
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-drugCodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ test_that("no duplicate names example 1",{
overwrite = TRUE)
attr(cdm, "write_schema") <- "main"

expect_error(getDrugIngredientCodes(
cdm = cdm, name = "Adalimumab", nameStyle = "{concept_name}"
))

expect_no_error(getDrugIngredientCodes(
cdm = cdm, name = "Adalimumab", nameStyle = "{concept_code}"
))

ingredient_list <- getDrugIngredientCodes(
cdm = cdm, name = "Adalimumab"
)
Expand Down

0 comments on commit 877c5fe

Please sign in to comment.