Skip to content

Commit

Permalink
Merge pull request #187 from darwin-eu/stratifyByRouteCategory
Browse files Browse the repository at this point in the history
add stratifyByRouteCategory
  • Loading branch information
edward-burn committed Jul 9, 2024
2 parents 00ba827 + 08f700e commit 83979b3
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(getVocabularies)
export(mockVocabRef)
export(restrictToCodesInUse)
export(sourceCodesInUse)
export(stratifyByRouteCategory)
export(subsetOnRouteCategory)
export(summariseAchillesCodeUse)
export(summariseCodeUse)
Expand Down
93 changes: 93 additions & 0 deletions R/stratifyByRoute.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright 2024 DARWIN EU®
#
# This file is part of CodelistGenerator
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#' Stratify a codelist by route category
#'
#' @param x A codelist
#' @param cdm A cdm reference
#'
#' @return A codelist
#' @export
#'
stratifyByRouteCategory <- function(x, cdm){

x <- omopgenerics::newCodelist(x)

if(isFALSE(inherits(cdm, "cdm_reference"))){
cli::cli_abort("cdm must be a cdm reference")
}

doseRouteData <- get0("doseFormToRoute", envir = asNamespace("CodelistGenerator"))

tableCodelist <- paste0(omopgenerics::uniqueTableName(),
omopgenerics::uniqueId())

result <- list()

for(i in seq_along(x)){
cdm <- omopgenerics::insertTable(cdm = cdm,
name = tableCodelist,
table = dplyr::tibble(concept_id = x[[i]]),
overwrite = TRUE,
temporary = FALSE)

workingName <- names(x)[i]

workingCodesWithRoute <- cdm[[tableCodelist]] |>
dplyr::inner_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") |>
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),
"unclassified route",
.data$route_category
)) |>
dplyr::select("concept_id", "route_category") |>
dplyr::distinct() |>
dplyr::collect()

workingCodesWithRoute <- split(
workingCodesWithRoute,
workingCodesWithRoute[, c("route_category")]
)

names(workingCodesWithRoute) <- paste0(workingName, "_",
names(workingCodesWithRoute))

for(j in seq_along(workingCodesWithRoute)){
workingCodesWithRoute[[j]] <- sort(workingCodesWithRoute[[j]] |>
dplyr::pull("concept_id"))
}

result[[i]] <- workingCodesWithRoute
}

result <- purrr:::list_flatten(result) |>
vctrs::list_drop_empty()

CDMConnector::dropTable(cdm = cdm, name = tableCodelist)

result

}
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ reference:
- matches("codesFromCohort|codesFromConceptSet")
- subtitle: Codelist utility functions
- contents:
- matches("codesInUse|compareCodelists|restrictToCodesInUse|subsetOnRouteCategory")
- matches("codesInUse|compareCodelists|restrictToCodesInUse|subsetOnRouteCategory|stratifyByRouteCategory")
- subtitle: Vocabulary utility functions
- contents:
- matches("getVocabVersion|getVocabularies|getConceptClassId|getDomains|getDescendants|getDoseForm|doseFormToRoute|getRouteCategories|getRoutes|getRelationshipId|getMappings|sourceCodesInUse")
Expand Down
19 changes: 19 additions & 0 deletions man/stratifyByRouteCategory.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 @@ -336,6 +336,12 @@ test_that("postgres", {
expect_true(length(drug_codes_subset) == 2)
expect_identical(drug_codes_subset, drug_codes2)

# can stratify by route
expect_no_error(drug_codes_stratified_by_route <- getDrugIngredientCodes(cdm,,
name = c("metformin","diclofenac")) |>
stratifyByRouteCategory(cdm = cdm))


# make sure no extra domains added to the results
codes <- getCandidateCodes(
cdm = cdm,
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-stratifyByRouteCategory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("stratify by route works", {
backends <- c("database", "data_frame")

for (i in seq_along(backends)) {
cdm <- mockVocabRef(backends[[i]])
ing_codes <- getDrugIngredientCodes(cdm)

ing_codes_str <- stratifyByRouteCategory(ing_codes, cdm)
# all will are unclassified
expect_true( all(stringr::str_detect(names(ing_codes_str),
"unclassified")))

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

if (backends[[i]] == "database") {
CDMConnector::cdm_disconnect(cdm)
}
}
})

0 comments on commit 83979b3

Please sign in to comment.