Skip to content

Commit

Permalink
updating two drug functions based on the comment
Browse files Browse the repository at this point in the history
  • Loading branch information
xihang-chen committed Jun 14, 2024
1 parent e18bdc6 commit a80bd38
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
45 changes: 16 additions & 29 deletions R/drugCodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,11 @@ getATCCodes <- function(cdm,
dplyr::select(!"ancestor_concept_id")
}

names(atc_descendants)[i] <- paste0(names(atc_descendants)[i], "_", workingName)
names(atc_descendants)[[i]] <- standardToConceptCode(cdm = cdm, names(atc_descendants)[[i]])
names(atc_descendants)[[i]] <- paste0(names(atc_descendants)[[i]], "_", workingName)
}
}

names(atc_descendants) <- nonDuplicateEntries(names(atc_descendants))

if(isFALSE(withConceptDetails)){
atc_descendants <- omopgenerics::newCodelist(atc_descendants)
}
Expand Down Expand Up @@ -190,9 +189,9 @@ getATCCodes <- function(cdm,
#'
#' @examples
#' \dontrun{
#'cdm <- mockVocabRef()
#'getDrugIngredientCodes(cdm = cdm, name = "Adalimumab")
#'CDMConnector::cdmDisconnect(cdm)
#' cdm <- mockVocabRef()
#' getDrugIngredientCodes(cdm = cdm, name = "Adalimumab")
#' CDMConnector::cdmDisconnect(cdm)
#'}
getDrugIngredientCodes <- function(cdm,
name = NULL,
Expand Down Expand Up @@ -280,7 +279,6 @@ getDrugIngredientCodes <- function(cdm,
workingName <- tidyWords(workingName)
workingName <- stringr::str_replace_all(workingName, " ", "_")


if(isFALSE(withConceptDetails)){
ingredientCodes[[i]] <- ingredientCodes[[i]] %>%
dplyr::select("concept_id") %>%
Expand All @@ -292,11 +290,10 @@ getDrugIngredientCodes <- function(cdm,
dplyr::select(!"ancestor_concept_id")
}

names(ingredientCodes)[[i]] <- standardToConceptCode(cdm = cdm, names(ingredientCodes)[[i]])
names(ingredientCodes)[[i]] <- paste0(names(ingredientCodes)[i], "_", workingName)
}

names(ingredientCodes) <- nonDuplicateEntries(names(ingredientCodes))

if(isFALSE(withConceptDetails)){
ingredientCodes <- omopgenerics::newCodelist(ingredientCodes)
}
Expand Down Expand Up @@ -336,26 +333,16 @@ fetchBatchedDescendants <- function(cdm,
return(descendants)
}

nonDuplicateEntries <- function(x){
standardToConceptCode <- function(cdm, x){
x_df <- as.data.frame(x)
names(x_df) <- c("name")
x_df <- x_df %>%
dplyr::mutate(name_tidy = gsub("^.*?_","",.data$name))

x_count <- x_df %>%
dplyr::group_by(.data$name_tidy) %>%
dplyr::tally()

name_fin <- x_df %>%
dplyr::left_join(x_count, by = "name_tidy") %>%
dplyr::mutate(
name_fin = dplyr::case_when(
(n>1) ~ .data$name,
T ~ .data$name_tidy
)
) %>%
dplyr::pull("name_fin")
y <- c()

return(name_fin)
}
for (i in (1:length(x))){
y_output <- cdm$concept %>%
dplyr::filter(.data$concept_id == !!x[[i]]) %>%
dplyr::pull("concept_code")
y <- c(y, y_output)
}

return(y)
}
39 changes: 23 additions & 16 deletions tests/testthat/test-drugCodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("getATCCodes working", {
atcCodes <- getATCCodes(cdm, level = "ATC 1st")
expect_true("codelist" %in% class(atcCodes))
expect_true(all(atcCodes[[1]] == c(12,13)))
expect_true(c("alimentary_tract_and_metabolism") %in%
expect_true(c("1234_alimentary_tract_and_metabolism") %in%
names(atcCodes))

atcCodes2 <- getATCCodes(cdm, level = "ATC 1st",
Expand Down Expand Up @@ -53,7 +53,7 @@ test_that("getDrugIngredientCodes working", {

ing_codes2 <- getDrugIngredientCodes(cdm, name = "Adalimumab")
expect_true(all(ing_codes2[[1]] == c(10,13)))
expect_true(names(ing_codes2) == "adalimumab")
expect_true(names(ing_codes2) == "1234_adalimumab")

ing_codes3 <- getDrugIngredientCodes(cdm,
name = "Adalimumab",
Expand Down Expand Up @@ -81,10 +81,10 @@ test_that("getDrugIngredientCodes working", {
ingredientRange = c(2,Inf))

expect_equal(ing_codes_all, ing_codes)
expect_true(all(c(10) %in% ing_codes_mono$adalimumab))
expect_null(ing_codes_mono$other_ingredient)
expect_true(all(c(13) %in% ing_codes_comb$adalimumab))
expect_true(all(c(13) %in% ing_codes_comb$other_ingredient))
expect_true(all(c(10) %in% ing_codes_mono$`1234_adalimumab`))
expect_null(ing_codes_mono$`1234_other_ingredient`)
expect_true(all(c(13) %in% ing_codes_comb$`1234_adalimumab`))
expect_true(all(c(13) %in% ing_codes_comb$`1234_other_ingredient`))

expect_error(getDrugIngredientCodes(cdm,
ingredientRange = c(3,2)))
Expand Down Expand Up @@ -176,7 +176,7 @@ test_that("no duplicate names example 1",{
"ICD10 Chapter", "ICD10 SubChapter",
"ICD Code","ICD Code", "Ingredient"
),
concept_code = "1234",
concept_code = as.character(c(2:20)),
valid_start_date = NA,
valid_end_date = NA,
invalid_reason =
Expand Down Expand Up @@ -403,8 +403,8 @@ test_that("no duplicate names example 1",{
2
)

expect_equal(names(ingredient_list) |>
unique() |>
expect_equal(names(ingredient_list) %>%
unique()|>
length() |>
as.numeric(),
2
Expand All @@ -414,15 +414,19 @@ test_that("no duplicate names example 1",{

expect_true(all(names(ingredient_list)
%in%
c("10_adalimumab", "19_adalimumab")))
c("11_adalimumab", "20_adalimumab")))

cdm <- mockVocabRef()

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

expect_true(all(startsWith(names(ingredient_list), "a")))
expect_false(all(startsWith(names(ingredient_list), "a")))

expect_true(all(names(ingredient_list)
%in%
c("1234_adalimumab")))
})

test_that("no duplicate names example 2",{
Expand Down Expand Up @@ -484,7 +488,7 @@ test_that("no duplicate names example 2",{
"ICD10 Chapter", "ICD10 SubChapter",
"ICD Code","ICD Code", "ATC 1st"
),
concept_code = "1234",
concept_code = as.character(c(2:20)),
valid_start_date = NA,
valid_end_date = NA,
invalid_reason =
Expand Down Expand Up @@ -711,24 +715,27 @@ test_that("no duplicate names example 2",{
2
)

expect_equal(names(atc_list) |>
unique() |>
expect_equal(names(atc_list) %>%
unique()|>
length() |>
as.numeric(),
2
)

expect_false(all(startsWith(names(atc_list), "a")))
expect_true(startsWith(names(atc_list)[1], "1"))
expect_true(startsWith(names(atc_list)[2], "2"))

expect_true(all(names(atc_list)
%in%
c("12_alimentary_tract_and_metabolism", "19_alimentary_tract_and_metabolism")))
c("13_alimentary_tract_and_metabolism", "20_alimentary_tract_and_metabolism")))

cdm <- mockVocabRef()

atc_list <- getATCCodes(
cdm = cdm, name = "ALIMENTARY TRACT AND METABOLISM"
)

expect_true(all(startsWith(names(atc_list), "a")))
expect_false(all(startsWith(names(atc_list), "a")))
expect_true(all(startsWith(names(atc_list), "1")))
})

0 comments on commit a80bd38

Please sign in to comment.