-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_xml.R
370 lines (320 loc) · 10.8 KB
/
parse_xml.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# https://stackoverflow.com/questions/48324165/scraping-table-from-xml
# workspace ----
library(tidyverse)
library(stringi)
library(xml2)
library(readxl)
library(glue)
library(vroom)
# requires 'year' variable from R/download-and-unzip-files.R
# year <- 2014
message("working on ", year, ' - - - - - - -') # print heading
xml_path <- paste0("file-history/icd10_", year, ".xml")
# bring in xml
load_xml <- read_xml(xml_path)
# chapters ----
chapter_nodes <- xml_find_all(load_xml, ".//chapter")
icd_chapters <-
tibble(
chapter = xml_text(xml_find_first(chapter_nodes, ".//name")),
chapter_desc = xml_text(xml_find_first(chapter_nodes, ".//desc"))
) %>%
mutate(
chapter = str_remove_all(chapter_desc, ".*\\(|\\)"),
category = chapter,
#chapter_desc = trimws(str_extract(chapter_desc, "[^\\(]+"))
) %>%
separate_rows(category)
# sections ----
section_nodes <- xml_find_all(load_xml, ".//section")
icd_sections <-
tibble(
section = xml_attr(section_nodes, "id"),
section_desc = xml_text(xml_find_first(section_nodes, ".//desc"))
) %>%
mutate(
section = str_remove_all(section_desc, ".*\\(|\\)"),
category = section
) %>%
separate_rows(category) %>%
group_by(category) %>%
slice_tail(n = 1) %>%
ungroup()
# dx ----
parse_dx <- function(n, title, join_field) {
# need to grab //diag/diag/diag x # of times
rep_n <- ifelse(n == 3, 1, n - 2)
xpath <-
paste0(
".//",
paste(rep("diag", rep_n), collapse = "/")
)
# pulls back description etc
dx_nodes <- xml_find_all(load_xml, xpath)
tibble(
dx = xml_find_first(dx_nodes, "name") %>% xml_text(),
desc = xml_find_first(dx_nodes, "desc") %>% xml_text(),
join =
str_sub(dx, 1, -2) %>% # remove last character
str_remove("\\.$") # remove trailing period
) %>%
# dx_nodes brings back all dx below this level
# this will ensure we just have the codes of length n
filter(nchar(str_remove(dx, "\\.")) == n) %>%
# rename columns
select(
"{title}" := dx,
"{title}_desc" := desc,
"{join_field}" := join # will be used in full_joins()
)
}
all_sub_category <-
parse_dx(7, "extension", "subcategory_3") %>%
full_join(parse_dx(6, "subcategory_3", "subcategory_2"), by = "subcategory_3") %>%
full_join(parse_dx(5, "subcategory_2", "subcategory_1"), by = "subcategory_2") %>%
full_join(parse_dx(4, "subcategory_1", "category"), by = "subcategory_1") %>%
full_join(parse_dx(3, "category", "supercategory"), by = "category")
# as ascii ----
standardize_ascii <- function(x) {
x |>
tolower() |>
stringi::stri_trans_general("latin-ascii")
}
as_ascii <-
all_sub_category %>%
# filter(category == "S42") %>% select(icd10_code, matches("[y123]_desc")) %>%
mutate(across(matches("cat.*desc"), standardize_ascii))
icd_dx <-
as_ascii |>
mutate(
icd10_code =
coalesce(extension, subcategory_3, subcategory_2, subcategory_1, category)
) %>%
#filter(str_detect(icd10_code, "S42")) %>%
select(
icd10_code,
starts_with("category"),
matches("1"),
matches("2"),
matches("3"),
matches("extension")
) %>%
arrange(icd10_code)
# extensions ----
ext_node <- xml_find_all(load_xml, ".//diag/sevenChrDef/extension")
icd_extensions <-
tibble(
name = xml_find_first(ext_node, "../../name") %>% xml_text(),
note = xml_find_first(ext_node, "../../sevenChrNote/note") %>% xml_text(),
char = xml_attr(ext_node, "char"),
text = xml_text(ext_node) |> standardize_ascii()
)
# full dataset
prep_extensions <-
icd_extensions %>%
mutate( # extract referenced code patterns
applies_to =
note %>% # 'The appropriate 7th character is to be added to each code from category M1A, S42, T49.123' %>%
str_remove_all("O30") %>%
# expand to S12.1, S12.2, etc
str_replace("S12.0-S12.6", paste0("S12.", 0:6, collapse = ", ")) %>%
# pull out dx codes
str_extract_all("[A-Z](\\d\\w)([\\.A-Z0-9]+)?")
) %>%
unnest(applies_to) %>%
mutate( # create fields to join to in next step
applies_to = str_remove(applies_to, "\\.$"),
length = nchar(applies_to),
category = ifelse(length == 3, str_sub(applies_to, 1, 3), NA),
subcategory_1 = ifelse(length == 5, str_sub(applies_to, 1, 5), NA),
subcategory_2 = ifelse(length == 6, str_sub(applies_to, 1, 6), NA)
)
# see what it looks like
prep_extensions %>%
filter(str_detect(name, "S4")) %>%
group_by(note) %>%
summarise(
n = n_distinct(applies_to),
codes = paste(unique(applies_to), collapse = ", "),
chars = paste(unique(char), collapse = ", ")
) %>%
ungroup() #%>% filter(n > 1) #%>%
# mutate(
# note = str_remove_all(note, "The appropriate 7th character is to be added to|One of the following 7th characters is to be assigned to( each)? code(s in subcategory)?|to designate ((lateral|sever)ity|the stage) of (the disease|glaucoma)")
# )
join_dx <- function(df, join_var, loc) {
# df <- icd_dx %>% filter(icd10_code == "S42.271"); join_var <- quo(subcategory_2); loc <- 6;
# df <- icd_dx %>% filter(icd10_code == "S42.271"); join_var <- quo(subcategory_1); loc <- 5;
# df <- icd_dx %>% filter(icd10_code == "S42.271"); join_var <- quo(category); loc <- 3;
df %>%
# filter out any extension field that already has a value
filter(if_all(starts_with("x"), ~is.na(.x))) %>%
inner_join(
prep_extensions %>%
filter(length == loc) %>%
distinct(
{{join_var}},
"x{{loc}}" := char,
"x{{loc}}_desc" := text
) %>%
drop_na(),#, by = deparse(substitute(join_var)) # creates string
relationship = "many-to-many"
) %>%
select(icd10_code, tail(names(.), 2)) %>%
distinct() |>
suppressMessages()
}
# with extensions ----
with_extensions <-
icd_dx %>%
# odd but need to bring over the code & text each time so not overwritten
left_join(join_dx(., subcategory_2, 6), by = "icd10_code", relationship = "many-to-many") %>%
left_join(join_dx(., subcategory_1, 5), by = "icd10_code", relationship = "many-to-many") %>%
left_join(join_dx(., category, 3), by = "icd10_code", relationship = "many-to-many") %>%
#filter(icd10_code == "S42.261") %>%
mutate(
extension = coalesce(str_sub(extension, 8), x3, x5, x6),
extension_desc = coalesce(extension_desc, x3_desc, x5_desc, x6_desc),
icd10_code =
ifelse(
test = nchar(icd10_code) == 3 & !is.na(extension),
yes = paste0(icd10_code, "."),
no = icd10_code
)
) %>%
select(-starts_with("x")) %>%
mutate(
icd10_code = # recompile icd10_code
case_when(
# some codes are 8 digits long w/o extension, keep as-is
nchar(icd10_code) == 8 ~ icd10_code,
# pad code with Xs if less than 7 digits
!is.na(extension) ~ paste0(str_pad(icd10_code, 7, "right", "X"), extension),
TRUE ~ icd10_code
)
) |>
distinct()
paste(nrow(with_extensions) - n_distinct(with_extensions$icd10_code), "dupes")
#count(with_extensions, icd10_code, sort = TRUE)
# final join ----
final_joins <-
with_extensions %>%
left_join(icd_chapters, by = "category") %>%
# select(icd10_code, starts_with("x")) %>%
arrange(icd10_code) %>%
fill(chapter) %>%
fill(chapter_desc) %>%
left_join(icd_sections, by = "category") %>%
fill(section) %>%
fill(section_desc) %>%
mutate(
description =
paste(
coalesce(
subcategory_3_desc,
subcategory_2_desc,
subcategory_1_desc,
category_desc
),
replace_na(extension_desc, "")
) %>% tolower()
) %>%
select(
icd10_code, description, starts_with("chap"), starts_with("sect"), everything()
)
#' make hyphens, parentheses literal
#' @examples
#' preserve_punctuation('a-b#')
# preserve_punctuation <- function(x) {
# str_replace_all(
# string = x,
# pattern = "([[:punct:]])",
# replacement =
# str_c(
# "\\", "\\", # 2 escaped '\'
# "\\1" # capture group
# )
# )
# }
#' find text difference between two strings
#' @examples
#' find_difference(x = '1 2 3 4-6', ref = '1 3')
find_difference <- function(x, ref) {
clean_ref <-
coalesce(ref, "") |>
str_replace_all(',', " ") |>
trimws() |>
str_split(" ")
clean_x <-
coalesce(x, "") |>
str_replace_all(",", " ") |>
str_split(" ")
map2(clean_x, clean_ref, setdiff) |>
map_chr(paste, collapse = " ") |>
str_replace_all(" {2,}", " ") |>
trimws()
}
# final diagnoses ----
with_diff_cols <-
final_joins %>%
#filter(category == "S42") %>% select(icd10_code, matches("[y123]_desc")) %>%
mutate(
subcategory_1_diff = find_difference(subcategory_1_desc, category_desc),
subcategory_2_diff = find_difference(subcategory_2_desc, subcategory_1_desc),
subcategory_3_diff = find_difference(subcategory_3_desc, subcategory_2_desc)
) |>
mutate_all(trimws)#%>%select(ends_with("diff"))
# with_diff_cols |> filter(icd10_code == "S52.271S") |> gather() |> print(n = Inf)
append_higher_dx <- function(field, ...){
# TODO:
# Note: Using an external vector in selections is ambiguous.
# i Use `all_of(field)` instead of `field` to silence this message.
# i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
with_diff_cols |>
drop_na(field) |>
filter(!get(field) %in% with_diff_cols$icd10_code) |>
select(
icd10_code = field,
description = paste0(field, "_desc"),
chapter:field,
"{field}" := field,
"{field}_desc" := paste0(field, "_desc"),
...
) |>
distinct() |>
mutate(
# remove ending commas from description fields, ex dx: O10.10
across(matches("desc"), ~str_remove(.x, ",$"))
)
}
final_diagnosis <-
with_diff_cols |>
bind_rows(
append_higher_dx("subcategory_3", subcategory_1_diff, subcategory_2_diff, subcategory_3_diff),
append_higher_dx("subcategory_2", subcategory_1_diff, subcategory_2_diff),
append_higher_dx("subcategory_1", subcategory_1_diff),
append_higher_dx("category")
) |>
arrange(icd10_code) |>
mutate(
year = year,
derived_code_ind = as.integer(!icd10_code %in% with_diff_cols$icd10_code),
update_date = Sys.Date()
)
# check for missing dx codes
final_diagnosis |> count(derived_code_ind)
# check for dupes
paste(nrow(final_diagnosis) - n_distinct(final_diagnosis$icd10_code), "dupes")
# count(final_diagnosis, icd10_code, sort = TRUE)
# filter(final_diagnosis, icd10_code == "S42.271A") |> distinct()
# waldo::compare(.Last.value[1,], .Last.value[2,])
#filter(with_diff_cols, icd10_code == "C44.10")
#filter(final_diagnosis, icd10_code == "C44.10")
# write to csv
vroom::vroom_write(
final_diagnosis,
glue("output/icd10_diagnosis_hierarchy_{year}.csv"),
delim = ",",
na = ""
)
#write_csv(final_diagnosis, "~/github/Chop-Data-Blocks/data/lookup_diagnosis_icd10.csv", na = "")