-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmockConcept.R
132 lines (117 loc) · 4.56 KB
/
mockConcept.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' Adds mock concept data to a concept table within a Common Data Model (CDM) object.
#'
#' This function inserts new concept entries into a specified domain within
#' the concept table of a CDM object.It supports four domains: Condition, Drug,
#' Measurement, and Observation. Existing entries with the same concept IDs
#' will be overwritten, so caution should be used when adding data to prevent
#' unintended data loss.
#'
#'
#' @param cdm A CDM object that represents a common data model containing at
#' least a concept table.This object will be modified in-place to
#' include the new or updated concept entries.
#'
#' @param conceptSet A numeric vector of concept IDs to be added or updated in
#' the concept table.These IDs should be unique within the
#' context of the provided domain to avoid unintended
#' overwriting unless that is the intended effect.
#'
#' @param domain A character string specifying the domain of the concepts being
#' added.Only accepts "Condition", "Drug", "Measurement", or
#' "Observation". This defines under which category the concepts
#' fall and affects which vocabulary is used for them.
#'
#' @param seed An optional integer value used to set the random seed for
#' generating reproducible concept attributes like `vocabulary_id`
#' and `concept_class_id`. Useful for testing or when consistent
#' output is required.
#'
#' @return Returns the modified CDM object with the updated concept table
#' reflecting the newly added concepts.The function directly modifies
#' the provided CDM object.
#' @export
#'
#' @examples
#' library(omock)
#' library(dplyr)
#'
#' # Create a mock CDM reference and add concepts in the 'Condition' domain
#' cdm <- mockCdmReference() |> mockConcepts(
#' conceptSet = c(100, 200), domain = "Condition")
#'
#' # View the updated concept entries for the 'Condition' domain
#' cdm$concept |> filter(domain_id == "Condition")
#'
mockConcepts <- function(cdm,
conceptSet,
domain = "Condition",
seed = NULL) {
# initial checks
checkInput(
cdm = cdm,
conceptSet = conceptSet,
domain = domain,
seed = seed
)
if (!is.null(seed)) {
set.seed(seed = seed)
}
if (!domain %in% c("Condition", "Drug", "Measurement", "Observation")) {
cli::cli_abort("This function only supports concept in the Condtion,
Drug, Measurement and Observation domain.")
}
# check if concept table is empty
if (cdm$concept |> nrow() == 0) {
cli::cli_abort("concept table must exist and cannot be empty")
}
countConcept <- cdm$concept |>
dplyr::filter("concept_id" %in% conceptSet) |>
dplyr::tally() |>
dplyr::pull()
if (countConcept > 0) {
cli::cli_warn("The concept ID you are adding already exists in the concept
table. This will overwrite the existing entry.")
}
cdm$concept <- cdm$concept |> dplyr::filter(!"concept_id" %in% conceptSet)
# generate vocabulary_id
if (domain == "Condition") {
vocabulary_id <- sample("SNOMED", length(conceptSet), replace = TRUE)
concept_name <- paste0("Condition_", conceptSet)
concept_class_id <- sample(
"Clinical Finding", length(conceptSet), replace = TRUE)
}
if (domain == "Drug") {
vocabulary_id <- sample("RxNorm", length(conceptSet), replace = TRUE)
concept_name <- paste0("Drug_", conceptSet)
concept_class_id <- sample("Drug", length(conceptSet), replace = TRUE)
}
if (domain == "Measurement") {
vocabulary_id <- sample("RxNorm", length(conceptSet), replace = TRUE)
concept_name <- paste0("Measurement_", conceptSet)
concept_class_id <- sample(
"Measurement", length(conceptSet), replace = TRUE)
}
if (domain == "Observation") {
vocabulary_id <- sample("LOINC", length(conceptSet), replace = TRUE)
concept_name <- paste0("Observation_", conceptSet)
concept_class_id <- sample(
"Observation", length(conceptSet), replace = TRUE)
}
# generate domain_id
domain <- sample(domain, length(conceptSet), replace = TRUE)
# row to add
table <- dplyr::tibble(
concept_id = conceptSet,
concept_name = concept_name,
domain_id = domain,
vocabulary_id = vocabulary_id,
standard_concept = "S",
concept_class_id = concept_class_id,
concept_code = as.character(1234),
valid_start_date = NA,
valid_end_date = NA,
invalid_reason = NA
)
cdm$concept <- cdm$concept |> dplyr::add_row(table)
return(cdm)
}