Skip to content

Commit

Permalink
Merge pull request #203 from darwin-eu/cran_submission
Browse files Browse the repository at this point in the history
3.1.0
  • Loading branch information
edward-burn authored Jul 12, 2024
2 parents 4bb5cb9 + eaa1c45 commit dcaec80
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 10 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.0.0.900
Version: 3.1.0
Authors@R: c(
person("Edward", "Burn", email = "[email protected]",
role = c("aut", "cre"),
Expand Down
11 changes: 8 additions & 3 deletions R/stratifyByDoseUnit.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ stratifyByDoseUnit <- function(x, cdm, keepOriginal = FALSE){
workingName <- names(x)[i]

workingCodesWithDoseUnit <- cdm[[tableCodelist]] |>
dplyr::left_join(cdm$concept,
by = "concept_id")|>
dplyr::left_join(drugStrengthNamed,
by = c("concept_id" = "drug_concept_id")
) |>
dplyr::select("concept_id",
dplyr::select("concept_id", "domain_id",
"amount_concept_name",
"numerator_concept_name") |>
dplyr::distinct() |>
Expand All @@ -83,9 +85,10 @@ stratifyByDoseUnit <- function(x, cdm, keepOriginal = FALSE){
unit_group = dplyr::case_when(
!is.na(.data$amount_concept_name) ~ omopgenerics::toSnakeCase(.data$amount_concept_name),
!is.na(.data$numerator_concept_name) ~ omopgenerics::toSnakeCase(.data$numerator_concept_name),
.default = "unkown_dose_unit"
tolower(.data$domain_id) == "drug" ~ "unkown_dose_unit"
)
)
) |>
dplyr::filter(!is.na(.data$unit_group))

if(isTRUE(withDetails)){
workingCodesWithDoseUnit <- x_original[[i]] |>
Expand All @@ -98,8 +101,10 @@ stratifyByDoseUnit <- function(x, cdm, keepOriginal = FALSE){
workingCodesWithDoseUnit[, c("unit_group")]
)

if(length(workingCodesWithDoseUnit)>0){
names(workingCodesWithDoseUnit) <- paste0(workingName, "_",
names(workingCodesWithDoseUnit))
}

if(isFALSE(withDetails)){
for(j in seq_along(workingCodesWithDoseUnit)){
Expand Down
12 changes: 10 additions & 2 deletions R/stratifyByRoute.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ stratifyByRouteCategory <- function(x, cdm, keepOriginal = FALSE){
withDetails <- TRUE
x <- codelistFromCodelistWithDetails(x)
} else {
omopgenerics::newCodelist(x)
withDetails <- FALSE
}

Expand Down Expand Up @@ -59,23 +60,28 @@ stratifyByRouteCategory <- function(x, cdm, keepOriginal = FALSE){
workingName <- names(x)[i]

workingCodesWithRoute <- cdm[[tableCodelist]] |>
dplyr::left_join(cdm$concept |>
dplyr::select("concept_id", "domain_id"),
by = "concept_id") |>
dplyr::left_join(cdm$concept_relationship |>
dplyr::filter(.data$relationship_id == "RxNorm has dose form"),
by = c("concept_id" = "concept_id_1")
) |>
dplyr::select("concept_id",
"concept_id_2") |>
"concept_id_2",
"domain_id") |>
dplyr::collect() |>
dplyr::left_join(
doseRouteData, by = c("concept_id_2" = "dose_form_concept_id")
) |>
dplyr::mutate(route_category = dplyr::if_else(
is.na(.data$route_category),
is.na(.data$route_category) & (tolower(.data$domain_id) == "drug"),
"unclassified_route",
.data$route_category
)) |>
dplyr::select("concept_id", "route_category") |>
dplyr::distinct() |>
dplyr::filter(!is.na(.data$route_category)) |>
dplyr::collect()

if(isTRUE(withDetails)){
Expand All @@ -89,8 +95,10 @@ stratifyByRouteCategory <- function(x, cdm, keepOriginal = FALSE){
workingCodesWithRoute[, c("route_category")]
)

if(length(workingCodesWithRoute) > 0){
names(workingCodesWithRoute) <- paste0(workingName, "_",
names(workingCodesWithRoute))
}

if(isFALSE(withDetails)){
for(j in seq_along(workingCodesWithRoute)){
Expand Down
5 changes: 3 additions & 2 deletions R/summariseOrphanCodes.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' Find orphan codes related to a codelist
#' Find orphan codes related to a codelist using achilles counts and, if
#' available, PHOEBE concept recommendations
#'
#' @param x A codelist for which to find related codes used in the database
#' @param cdm cdm_reference via CDMConnector
Expand Down Expand Up @@ -68,7 +69,7 @@ summariseOrphanCodes <- function(x,
} else {
phoebe <- FALSE
cli::cli_inform(c("PHOEBE results not available",
"i" = "The concept_recommened table is not present in the cdm."))
"i" = "The concept_recommended table is not present in the cdm."))
}

orphanCodes <- list()
Expand Down
6 changes: 4 additions & 2 deletions man/summariseOrphanCodes.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-dbms.R
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ test_that("postgres", {
codes <- getDrugIngredientCodes(cdm, "metformin")
codes[["asthma"]] <- 317009

expect_no_error(stratifyByDoseUnit(codes, cdm))
expect_no_error(stratifyByDoseUnit(codes, cdm, keepOriginal = TRUE))
expect_no_error(stratifyByRouteCategory(codes, cdm))
expect_no_error(stratifyByRouteCategory(codes, cdm, keepOriginal = TRUE))


drug_codes <- getDrugIngredientCodes(cdm, name = c("metformin",
"diclofenac"))

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-stratifyByDoseUnit.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ test_that("stratifyByDoseUnit in mock", {
# no dose units in the mock
expect_no_error(stratifyByDoseUnit(x = ing, cdm = cdm))

# if concepts are not from the drug domain we should get empty codelist back
oa <- getCandidateCodes(cdm = cdm, "osteoarthritis")
oa_str <- stratifyByDoseUnit(list(oa = oa$concept_id),
cdm, keepOriginal = FALSE)
expect_true(length(oa_str)==0)

oa_str <- stratifyByDoseUnit(omopgenerics::newCodelistWithDetails(list(oa = oa)),
cdm, keepOriginal = FALSE)
expect_true(length(oa_str)==0)

# expected errors
expect_error(stratifyByDoseUnit(x = ing, cdm = "a"))
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-stratifyByRouteCategory.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ test_that("stratify by route works", {
keepOriginal = TRUE)
expect_true(length(ing_codes_str_all) == 4)


# if concepts are not from the drug domain we should get empty codelist back
oa <- getCandidateCodes(cdm = cdm, "osteoarthritis")
oa_str <- stratifyByRouteCategory(list(oa = oa$concept_id),
cdm, keepOriginal = FALSE)
expect_true(length(oa_str)==0)

oa_str <- stratifyByRouteCategory(omopgenerics::newCodelistWithDetails(list(oa = oa)),
cdm, keepOriginal = FALSE)
expect_true(length(oa_str)==0)

# expected errors
expect_error(stratifyByRouteCategory("a", cdm))
expect_error(stratifyByRouteCategory(ing_codes, "a"))
Expand Down

0 comments on commit dcaec80

Please sign in to comment.