-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from darwin-eu/stratifyByConcept
stratifyByConcept
- Loading branch information
Showing
11 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# 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 the concepts included within it | ||
#' | ||
#' @param x A codelist | ||
#' @param cdm A cdm reference | ||
#' @param keepOriginal Whether to keep the original codelist and append the | ||
#' stratify (if TRUE) or just return the stratified codelist (if FALSE). | ||
#' | ||
#' @return A codelist | ||
#' @export | ||
#' | ||
stratifyByConcept <- function(x, | ||
cdm, | ||
keepOriginal = FALSE){ | ||
|
||
x_start <- x | ||
|
||
if(inherits(x_start, "codelist")){ | ||
x <- addDetails(x, cdm = cdm) | ||
} | ||
|
||
for(i in seq_along(x)){ | ||
x[[i]] <- x[[i]] |> | ||
dplyr::mutate(c_name = names(x[i])) |> | ||
dplyr::mutate(new_c_name = paste0(.data$c_name, "_", | ||
omopgenerics::toSnakeCase(.data$concept_name))) | ||
} | ||
|
||
x <- purrr::list_rbind(x) | ||
|
||
if(any(is.na(x$concept_name))){ | ||
nMissingConceptName <- sum(is.na(x$concept_name)) | ||
cli::cli_warn("Dropping {nMissingConceptName} concepts that do not have a concept name") | ||
x <- x |> | ||
dplyr::filter(!is.na(.data$concept_name)) | ||
} | ||
|
||
x <- split(x, | ||
x[, c("new_c_name")] | ||
) | ||
|
||
if(inherits(x_start, "codelist")){ | ||
for(i in seq_along(x)){ | ||
x[[i]] <- x[[i]] |> | ||
dplyr::pull("concept_id") | ||
} | ||
} | ||
|
||
if(inherits(x_start, "codelist_with_details")){ | ||
for(i in seq_along(x)){ | ||
x[[i]] <- x[[i]] |> | ||
dplyr::select(!"c_name") |> | ||
dplyr::select(!"new_c_name") | ||
} | ||
} | ||
|
||
if(isTRUE(keepOriginal)){ | ||
x <- purrr::list_flatten(list(x_start, x)) | ||
} | ||
|
||
x <- x[order(names(x))] | ||
|
||
if(inherits(x_start, "codelist")){ | ||
x <- omopgenerics::newCodelist(x) | ||
} else{ | ||
x <- omopgenerics::newCodelistWithDetails(x) | ||
} | ||
|
||
x | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
test_that("multiplication works", { | ||
|
||
cdm <- mockVocabRef() | ||
cl <- omopgenerics::newCodelist(list(a = c(1,2,3), | ||
b = c(3,4,5))) | ||
|
||
cl_s1 <- stratifyByConcept(cl, cdm, keepOriginal = FALSE) | ||
expect_true(length(cl_s1) == 6) | ||
|
||
|
||
cl_s2 <- stratifyByConcept(cl, cdm, keepOriginal = TRUE) | ||
expect_true(length(cl_s2) == 8) | ||
expect_true(all(sort(names(cl)) == sort(setdiff(names(cl_s2), names(cl_s1))))) | ||
|
||
|
||
|
||
cl <- omopgenerics::newCodelistWithDetails(list(a = data.frame(concept_id = c(1,2,3), | ||
concept_name = c("a", "b", "c")), | ||
b = data.frame(concept_id = c(1,2,3), | ||
concept_name = c("c", "d", "e")))) | ||
|
||
cl_s1 <- stratifyByConcept(cl, cdm, keepOriginal = FALSE) | ||
expect_true(length(cl_s1) == 6) | ||
|
||
|
||
cl_s2 <- stratifyByConcept(cl, cdm, keepOriginal = TRUE) | ||
expect_true(length(cl_s2) == 8) | ||
expect_true(all(sort(names(cl)) == sort(setdiff(names(cl_s2), names(cl_s1))))) | ||
|
||
# if concepts are not in the cdm | ||
cdm <- mockVocabRef() | ||
cl <- omopgenerics::newCodelist(list(a = c(1,2,3), | ||
b = c(3,4,5,99999))) | ||
expect_warning(cl_s1 <- stratifyByConcept(cl, cdm, keepOriginal = FALSE)) | ||
expect_true(length(cl_s1) == 6) # concept 99999 will have been dropped | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
test_that("stratifyByDoseUnit in mock", { | ||
cdm <- mockVocabRef() | ||
ing <- getDrugIngredientCodes(cdm = cdm) | ||
|
||
# no dose units in the mock | ||
expect_no_error(stratifyByDoseUnit(x = ing, cdm = cdm)) | ||
|
||
|
||
# expected errors | ||
expect_error(stratifyByDoseUnit(x = ing, cdm = "a")) | ||
expect_error(stratifyByDoseUnit(x = "a", cdm = cdm)) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
test_that("multiplication works", { | ||
expect_equal(2 * 2, 4) | ||
}) | ||
test_that("subsetOnDoseUnit in mock", { | ||
cdm <- mockVocabRef() | ||
ing <- getDrugIngredientCodes(cdm = cdm) | ||
|
||
# no dose units in the mock | ||
expect_no_error(subsetOnDoseUnit(x = ing, cdm = cdm, | ||
doseUnit = "milligram")) | ||
|
||
|
||
# expected errors | ||
expect_error(subsetOnDoseUnit(x = ing, cdm = cdm, | ||
doseUnit = 1)) | ||
expect_error(subsetOnDoseUnit(x = ing, cdm = "a", | ||
doseUnit = "milligram")) | ||
expect_error(subsetOnDoseUnit(x = "a", cdm = "a", | ||
doseUnit = "milligram")) | ||
|
||
}) |