-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-validateUsagiFile.R
More file actions
300 lines (237 loc) · 19.7 KB
/
test-validateUsagiFile.R
File metadata and controls
300 lines (237 loc) · 19.7 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
test_that("test validateUsagiFile returns no errors with a valid usagi file", {
pathToUsagiFile <- system.file("testdata/VOCABULARIES/ICD10fi/ICD10fi.usagi.csv", package = "ROMOPMappingTools")
pathToOMOPVocabularyDuckDBfile <- helper_createATemporaryCopyOfTheOMOPVocabularyDuckDB()
withr::defer(unlink(pathToOMOPVocabularyDuckDBfile))
vocabularyDatabaseSchema = "main"
sourceConceptIdOffset = 2000500000
pathToValidatedUsagiFile <- tempfile(fileext = ".csv")
# Create connection to test database
connection <- DatabaseConnector::connect(
dbms = "duckdb",
server = pathToOMOPVocabularyDuckDBfile
)
on.exit(DatabaseConnector::disconnect(connection))
validationsSummary <- validateUsagiFile(
pathToUsagiFile,
connection,
vocabularyDatabaseSchema,
pathToValidatedUsagiFile,
sourceConceptIdOffset
)
# all validations must be successful
validationsSummary |> dplyr::filter(type != "SUCCESS") |> nrow() |> expect_equal(0)
# Usagi file has not changed apart from mappingStatus and ADD_INFO:validationMessages
usagiTibble <- readUsagiFile(pathToUsagiFile)
validatedUsagiTibble <- readUsagiFile(pathToValidatedUsagiFile)
byNames <- usagiTibble |> names() |> setdiff(c("mappingStatus", "ADD_INFO:validationMessages"))
expect_equal(usagiTibble |> dplyr::select(dplyr::all_of(byNames)), validatedUsagiTibble |> dplyr::select(dplyr::all_of(byNames)))
})
test_that("test validateUsagiFile returns errors with the errored usagi file", {
pathToUsagiFile <- system.file("testdata/VOCABULARIES/ICD10fi/ICD10fi_with_errors.usagi.csv", package = "ROMOPMappingTools")
pathToOMOPVocabularyDuckDBfile <- helper_createATemporaryCopyOfTheOMOPVocabularyDuckDB()
withr::defer(unlink(pathToOMOPVocabularyDuckDBfile))
pathToValidatedUsagiFile <- tempfile(fileext = ".csv")
vocabularyDatabaseSchema = "main"
sourceConceptIdOffset = 2000500000
# Create connection to test database
connection <- DatabaseConnector::connect(
dbms = "duckdb",
server = pathToOMOPVocabularyDuckDBfile
)
on.exit(DatabaseConnector::disconnect(connection))
validationsSummary <- validateUsagiFile(
pathToUsagiFile,
connection,
vocabularyDatabaseSchema,
pathToValidatedUsagiFile,
sourceConceptIdOffset
)
validatedUsagiFile <- readUsagiFile(pathToValidatedUsagiFile)
# Missing default columns
# not implemented as Usagi wont open this file
# SourceCode is empty
validationsSummary |> dplyr::filter(step == "SourceCode is empty") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is empty")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is empty")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceCode is empty")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is empty")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceCode and conceptId are not unique
validationsSummary |> dplyr::filter(step == "SourceCode and conceptId are not unique") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode and conceptId are not unique")) |> nrow() |> expect_equal(2)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode and conceptId are not unique")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(rep("ERROR: SourceCode and conceptId are not unique", 2))
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode and conceptId are not unique")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("FLAGGED", 2))
# SourceName is empty
validationsSummary |> dplyr::filter(step == "SourceName is empty") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(is.na(sourceName)) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(is.na(sourceName)) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceName is empty")
validatedUsagiFile |> dplyr::filter(is.na(sourceName)) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceCode is more than 50 characters
validationsSummary |> dplyr::filter(step == "SourceCode is more than 50 characters") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is more than 50 characters")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is more than 50 characters")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceCode is more than 50 characters")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceCode is more than 50 characters")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceName is more than 255 characters
validationsSummary |> dplyr::filter(step == "SourceName is more than 255 characters") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceName is more than 255 characters")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceName is more than 255 characters")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceName is more than 255 characters")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceName is more than 255 characters")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceFrequency is empty
# not implemented as Usagi wont open this file
# MappingStatus is empty
# not implemented as Usagi wont open this file
# MappingStatus is not one of the following
# not implemented as Usagi wont open this file
# Concept_id is 0 for APPROVED mappingStatus
validationsSummary |> dplyr::filter(step == "APPROVED mappingStatus conceptId is 0") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "APPROVED mappingStatus conceptId is 0")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "APPROVED mappingStatus conceptId is 0")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: APPROVED mappingStatus conceptId is 0")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "APPROVED mappingStatus conceptId is 0")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# APPROVEDConceptIds not in vocabularies
validationsSummary |> dplyr::filter(step == "APPROVED mappingStatus with concepts outdated") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED mappingStatus with concepts outdated")) |> nrow() |> expect_equal(3)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED mappingStatus with concepts outdated")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(rep("ERROR OUTDATED conceptId: conceptId 1234 does not exist on the target vocabularies", 3))
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED mappingStatus with concepts outdated")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("APPROVED", 3))
# Not APPROVED ConceptIds not in vocabularies
validationsSummary |> dplyr::filter(step == "Not APPROVED mappingStatus with concepts outdated") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED mappingStatus with concepts outdated")) |> nrow() |> expect_equal(3)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED mappingStatus with concepts outdated")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(rep("WARNING OUTDATED conceptId: conceptId 1234 does not exist on the target vocabularies", 3))
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED mappingStatus with concepts outdated")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("UNCHECKED", 3))
#APPROVED ConceptIds outdated
validationsSummary |> dplyr::filter(step == "APPROVED mappingStatus with concepts outdated") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED ConceptIds outdated\\]")) |> nrow() |> expect_equal(7)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED ConceptIds outdated\\]"))|> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(
c("ERROR OUTDATED domainId: domainId for conceptId 4150516 is different in the target vocabularies",
"ERROR OUTDATED conceptName: conceptName for conceptId 4130374 is different in the target vocabularies",
"ERROR OUTDATED standardConcept: standardConcept for conceptId 320073 has changed to non-standard",
"ERROR OUTDATED standardConcept: standardConcept for conceptId 320073 has changed to non-standard",
"ERROR OUTDATED standardConcept: standardConcept for conceptId 3085666 has changed to non-standard",
"ERROR OUTDATED standardConcept: standardConcept for conceptId 3079174 has changed to non-standard",
"ERROR OUTDATED standardConcept: standardConcept for conceptId 30258 has changed to non-standard"
)
)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED ConceptIds outdated\\]")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("APPROVED", 7))
#Not APPROVED ConceptIds outdated
validationsSummary |> dplyr::filter(step == "Not APPROVED mappingStatus with concepts outdated") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED ConceptIds outdated")) |> nrow() |> expect_equal(7)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED ConceptIds outdated"))|> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(
c("WARNING OUTDATED domainId: domainId for conceptId 4150516 is different in the target vocabularies",
"WARNING OUTDATED conceptName: conceptName for conceptId 4130374 is different in the target vocabularies",
"WARNING OUTDATED standardConcept: standardConcept for conceptId 320073 has changed to non-standard",
"WARNING OUTDATED standardConcept: standardConcept for conceptId 320073 has changed to non-standard",
"WARNING OUTDATED standardConcept: standardConcept for conceptId 3085666 has changed to non-standard",
"WARNING OUTDATED standardConcept: standardConcept for conceptId 3079174 has changed to non-standard",
"WARNING OUTDATED standardConcept: standardConcept for conceptId 30258 has changed to non-standard"
)
)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Not APPROVED ConceptIds outdated")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("UNCHECKED", 7))
# Missing C&CR columns
#
# SourceConceptId is empty
validationsSummary |> dplyr::filter(step == "SourceConceptId is empty") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is empty")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is empty")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceConceptId is empty")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is empty")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceConceptId is not a number on the range
validationsSummary |> dplyr::filter(step == "SourceConceptId is not a number on the range") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is not a number on the range")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is not a number on the range")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceConceptId is not a number on the range")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptId is not a number on the range")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceConceptClass is empty
validationsSummary |> dplyr::filter(step == "SourceConceptClass is empty") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is empty")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is empty")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceConceptClass is empty")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is empty")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceConceptClass is more than 20 characters
validationsSummary |> dplyr::filter(step == "SourceConceptClass is more than 20 characters") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is more than 20 characters")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is more than 20 characters")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceConceptClass is more than 20 characters")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceConceptClass is more than 20 characters")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceDomain is empty
validationsSummary |> dplyr::filter(step == "SourceDomain is empty") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is empty")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is empty")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceDomain is empty")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is empty")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# SourceDomain is not a valid domain
validationsSummary |> dplyr::filter(step == "SourceDomain is not a valid domain") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is not a valid domain")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is not a valid domain")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceDomain is not a valid domain")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceDomain is not a valid domain")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# APPROVED Invalid domain combination
validationsSummary |> dplyr::filter(step == "APPROVED mappingStatus with valid domain combination") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED Invalid domain combination")) |> nrow() |> expect_equal(2)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED Invalid domain combination")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(rep("ERROR OUTDATED domainId: domainId for conceptId 4109415 is different in the target vocabularies | ERROR: this code is mapped to more than one domains that are not compatible: Condition noDomain", 2))
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[APPROVED Invalid domain combination")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("FLAGGED", 2))
# Not APPROVED Invalid domain combination
validationsSummary |> dplyr::filter(step == "Not APPROVED mappingStatus with valid domain combination") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[Not APPROVED Invalid domain combination")) |> nrow() |> expect_equal(2)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[Not APPROVED Invalid domain combination")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal(rep("WARNING OUTDATED domainId: domainId for conceptId 4109415 is different in the target vocabularies | WARNING: this code is mapped to more than one domains that are not compatible: Condition noDomain", 2))
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "\\[Not APPROVED Invalid domain combination")) |> dplyr::pull(mappingStatus) |>
expect_equal(rep("UNCHECKED", 2))
# Missing date columns
# SourceValidStartDate is after SourceValidEndDate
validationsSummary |> dplyr::filter(step == "SourceValidStartDate is after SourceValidEndDate") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceValidStartDate is after SourceValidEndDate")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceValidStartDate is after SourceValidEndDate")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: SourceValidStartDate is after SourceValidEndDate")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "SourceValidStartDate is after SourceValidEndDate")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# Invalid parent concept code
validationsSummary |> dplyr::filter(step == "Invalid parent concept code") |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code]")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code]")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: WWW is not a valid concept in this vocabulary | ERROR: OOO is not a valid concept in this vocabulary")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code]")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# Invalid parent concept code in other vocabulary
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code in other vocabulary")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code in other vocabulary")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: WWW is not a valid concept code in vocabulary ICD10 | ERROR: OOO is not a valid concept code in vocabulary ICD10")
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent concept code in other vocabulary")) |> dplyr::pull(mappingStatus) |>
expect_equal("FLAGGED")
# Missing parent columns
# Invalid parent vocabulary
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent vocabulary")) |> nrow() |> expect_equal(1)
validatedUsagiFile |> dplyr::filter(stringr::str_detect(sourceName, "Invalid parent vocabulary")) |> dplyr::pull(`ADD_INFO:validationMessages`) |>
expect_equal("ERROR: A06 is not a valid concept code in vocabulary INVENT | ERROR: A06.8 is not a valid concept code in vocabulary INVENT")
# it opens with Usagi
#file.copy(pathToValidatedUsagiFile, "~/Downloads/ICD10fi.usagi.csv", overwrite = TRUE)
# Usagi file has not changed apart from mappingStatus and ADD_INFO:validationMessages
usagiTibble <- readUsagiFile(pathToUsagiFile)
validatedUsagiTibble <- readUsagiFile(pathToValidatedUsagiFile)
byNames <- usagiTibble |> names() |> setdiff(c("mappingStatus", "ADD_INFO:validationMessages"))
expect_equal(usagiTibble |> dplyr::select(dplyr::all_of(byNames)), validatedUsagiTibble |> dplyr::select(dplyr::all_of(byNames)))
})