generated from ohdsi-studies/StudyRepoTemplate
/
ScriptForCohortsInPhenotypeLibrary.R
529 lines (469 loc) · 20 KB
/
ScriptForCohortsInPhenotypeLibrary.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
remotes::install_github(repo = "OHDSI/PhenotypeLibrary", ref = "v3.29.0")
notPartOfV325 <- c(1120, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1117, 1118, 1119, 1216, 1217, 1218, 1219, 1220, 1221, 1222)
# Step 1: get all cohort definition in OHDSI PhenotypeLibrary ----
fullPhenotypeLog <- PhenotypeLibrary::getPhenotypeLog() |>
dplyr::filter(!cohortId %in% notPartOfV325)
# any overides
cohortsThatShouldBeRemovedBecauseTheySeemToCauseProblems <- c(344)
analysis2InputSpecifications <- readxl::read_excel("analysis_specifications/analysis2InputSpecifications.xlsx")
cohortsThatArePartOfAnlalysis2 <- c(analysis2InputSpecifications$tId,
analysis2InputSpecifications$oId) |> unique() |> sort()
# checks
setdiff(cohortsThatArePartOfAnlalysis2,
fullPhenotypeLog$cohortId)
intersect(cohortsThatArePartOfAnlalysis2,
fullPhenotypeLog$cohortId) |> length() == length(cohortsThatArePartOfAnlalysis2)
cohortsThatAreDuplicates <- c(794, 1077, #decided to remove
900, 726, #anaphylaxis replaced with 1076
986, 730, # pancreatitis using 251
1086, 692, 691, 296, 63, # duplicates of transverse myelitis
898, 725, # AKI
994, 410, # UTI
881, 71, 924, #MI
267, 964, #CKD
733, #DRESS
1085, 234, # Appendicitis
134, #ADHD
738, #Autoimmune hemolytic anemia
991, # Breast cancer
746, #CTEPH
992, #DKA
856, #Migraine
256, #Bells palsy
935, #Hemorrhagic stroke
979, #Heart failure
944, # ischemic stroke
215,216, #ITP
239, #narcolepsy
1000, #nausea
737, #febrile neutropenia
919, #cardiac arrhythmia
927, #dementia
947, #neutropenia
957, #Type 2 DM
950, #rhabdomyolysis
277 #sudden hearing loss
)
setdiff(cohortsThatAreDuplicates,
fullPhenotypeLog$cohortId)
cohortsThatWontAddValue <- c(325,
257,
976, 939, 998, #not useful
742, 945, 901, 917, #not useful from legend
918, 922, 882, 928, 943, 946, #not useful from legend
955, 1002, 948, #not useful from legend
982, 953, 954, #not useful from legend
1005, 1006, 963, #not useful from legend
1019, # wont work. pregnancy logic
1016, # nobody asked for it
1017 #rare event
)
#checking
setdiff(cohortsThatWontAddValue,
fullPhenotypeLog$cohortId)
# fullPhenotypeLog |>
# dplyr::filter(cohortId %in% c(
# intersect(cohortsThatWontAddValue,
# cohortsThatArePartOfAnlalysis2)
# )) |> View()
fullPhenotypeLog <- fullPhenotypeLog |>
dplyr::filter(!cohortId %in% c(cohortsThatShouldBeRemovedBecauseTheySeemToCauseProblems,
cohortsThatAreDuplicates,
cohortsThatWontAddValue))
# Note: HowOften has three types of analysis
## Analysis 1: Use all cohorts in PL that met some criteria as outcome, and use a baseCohort as Target
## Analysis 2: Community proposals
## Analysis 3: Compare incidence of certain outcomes after exposure to drug with and without subset to drugs indications
# Step 2: Identify and flag the cohortIds of the cohorts we want to use in HowOften ----
subsetOfCohorts <- c()
## analysis 1 base cohort. The cohort is 1071
subsetOfCohorts$baseCohort <- fullPhenotypeLog |>
dplyr::filter(cohortId %in% c(1071)) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonBaseCohort = 1)
## all cohorts that have been accepted to OHDSI PhenotypeLibrary after some review process
subsetOfCohorts$acceptedCohorts <- fullPhenotypeLog |>
dplyr::filter(stringr::str_length(string = addedVersion) > 0) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonAcceptToOhdsiPl = 1)
## Designated Medical Events
subsetOfCohorts$foundInLibraryOutcomeDme <- fullPhenotypeLog |>
dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#DME")) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonDme = 1)
## AESI cohorts built for covid analysis. These are mostly imported from the covid aesi studies
subsetOfCohorts$foundInLibraryOutcomeAesi <- fullPhenotypeLog |>
dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#AESI")) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonAesi = 1)
# LEGEND studies are imported from Legend Hypertension and LEGEND Diabetes. Unfortunately the cohorts may have duplicated.
subsetOfCohorts$foundInLibraryOutcomeLegend <- fullPhenotypeLog |>
dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#LEGEND")) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonLegend = 1)
# All service utilization cohorts.
subsetOfCohorts$foundInVisit <- fullPhenotypeLog |>
dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#VISIT")) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonVisit = 1) # debatable
# All cohorts flagged as symptoms.
# subsetOfCohorts$foundInSymptoms <- fullPhenotypeLog |>
# dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#SYMPTOMS")) |>
# dplyr::select(cohortId) |>
# dplyr::mutate(reasonSymptoms = 1)
# All cohorts that were submitted to the OHDSI PhenotypeLibrary on or after August 1st 2023
subsetOfCohorts$recentSubmission <- fullPhenotypeLog |>
dplyr::filter(createdDate > as.Date('2023-08-01')) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonRecentlyPosted = 1)
# These are hand picked cohorts that were used by Patrick to subset drug exposure cohorts
subsetOfCohorts$libraryIndicationCohorts <- fullPhenotypeLog |>
dplyr::filter(cohortId %in% c(770,
765,
71,
1032,
32,
749,
861,
19,
858,
860,
859,
748)) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonAnalysis3Indication = 1)
# These are cohorts that Patrick built for use in Analysis 3
subsetOfCohorts$howOften <- fullPhenotypeLog |>
dplyr::filter(stringr::str_detect(string = toupper(hashTag), pattern = "#HOWOFTEN")) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonHowOftenAnalysis3 = 1)
# These are cohorts that Patrick built for use in Analysis 3
subsetOfCohorts$jillHardinCohorts <- fullPhenotypeLog |>
dplyr::filter(cohortId %in% c(134,
1027, #replaces 470 is referrent cohort, it wont be used
383, # replace previously selected referrent cohort for atopic dermatitis
# 667, remove after talking to Jill
# 690, replace with 123
372, # replace jills selection of referent 533,
1151,
334, #replaces 521 which as referent
1151, #replacing previously selected Jills 591 which was referrent
383, # replacing referent selection of 466,
123)) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonJillHardin = 1)
subsetOfCohorts$cohortsThatArePartOfAnalysis <- fullPhenotypeLog |>
dplyr::filter(cohortId %in% c(cohortsThatArePartOfAnlalysis2)) |>
dplyr::select(cohortId) |>
dplyr::mutate(reasonAnalysis2 = 1)
## combine
allCohorts <- dplyr::bind_rows(subsetOfCohorts) |>
dplyr::select(cohortId) |>
dplyr::distinct() |>
dplyr::left_join(subsetOfCohorts$baseCohort) |>
dplyr::left_join(subsetOfCohorts$acceptedCohorts) |>
dplyr::left_join(subsetOfCohorts$foundInLibraryOutcomeDme) |>
dplyr::left_join(subsetOfCohorts$foundInLibraryOutcomeAesi) |>
dplyr::left_join(subsetOfCohorts$foundInLibraryOutcomeLegend) |>
dplyr::left_join(subsetOfCohorts$recentSubmission) |>
dplyr::left_join(subsetOfCohorts$libraryIndicationCohorts) |>
dplyr::left_join(subsetOfCohorts$howOften) |>
dplyr::left_join(subsetOfCohorts$foundInVisit) |>
# dplyr::left_join(subsetOfCohorts$foundInSymptoms) |>
dplyr::left_join(subsetOfCohorts$jillHardinCohorts) |>
dplyr::left_join(subsetOfCohorts$cohortsThatArePartOfAnalysis) |>
tidyr::replace_na(
replace = list(
reasonBaseCohort = 0,
reasonAcceptToOhdsiPl = 0,
reasonDme = 0,
reasonAesi = 0,
reasonLegend = 0,
reasonRecentlyPosted = 0,
reasonAnalysis3Indication = 0,
reasonHowOftenAnalysis3 = 0,
reasonVisit = 0,
reasonSymptoms = 0,
reasonJillHardin = 0,
reasonAnalysis2 = 0
)
) |>
dplyr::inner_join(
fullPhenotypeLog |>
dplyr::select(cohortId,
cohortName,
eventCohort,
exitDateOffSet,
exitPersistenceWindow,
collapseEraPad)
) |>
dplyr::relocate(cohortId,
cohortName,
eventCohort,
exitDateOffSet,
exitPersistenceWindow,
collapseEraPad)
# if we decide to filter
# toFilter <- allCohorts |>
# dplyr::filter(
# reasonBaseCohort == 1 |
# reasonDme == 1 |
# reasonAesi == 1 |
# reasonLegend == 1 |
# reasonRecentlyPosted == 1 |
# reasonAnalysis3Indication == 1 |
# reasonHowOftenAnalysis3 |
# reasonJillHardin == 1
# ) |>
# dplyr::select(cohortId) |>
# dplyr::distinct()
# Step 3: Assign clean window ----
## All cohorts get a default clean window ----
allCohorts <- allCohorts |>
dplyr::mutate(cleanWindow = 9999,
cleanWindowAssigned = 0,
cleanWindowRule = "")
## Only event cohorts need custom clean window. ----
## So we will assume that cleanWindowAssigned = 1 for non event cohorts
allCohorts <- allCohorts|>
dplyr::mutate(
cleanWindowAssigned = dplyr::if_else(
condition = !as.logical(eventCohort),
true = 1,
false = cleanWindowAssigned
),
cleanWindowRule = dplyr::if_else(
condition = !as.logical(eventCohort),
true = "Not an event cohort",
false = cleanWindowRule
)
)
allCohorts |>
dplyr::group_by(cleanWindowRule, cleanWindowAssigned, eventCohort) |>
dplyr::summarise(n = dplyr::n())
### We are using a combination of rule and heuristic to assign clean window.
### Rule based: use collapseEraPad ----
### explore the distribution of collapseEraPad among event cohorts
allCohorts |>
dplyr::filter(eventCohort == 1) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::group_by(collapseEraPad) |>
dplyr::select(collapseEraPad) |>
dplyr::summarise(n = dplyr::n()) |>
dplyr::arrange(collapseEraPad)
# Choice: If collapseEraPad >= 7, then we will use collapseEraPad
allCohorts <- allCohorts |>
dplyr::mutate(
cleanWindow = dplyr::if_else(
condition = (cleanWindowAssigned == 0) & (collapseEraPad > 7),
true = collapseEraPad,
false = cleanWindow
),
cleanWindowRule = dplyr::if_else(
condition = (eventCohort == 1) &
(cleanWindowAssigned == 0) & (collapseEraPad > 7),
true = "Has collapse era pad of greater than 7 in definition",
false = cleanWindowRule
),
cleanWindowAssigned = dplyr::if_else(
condition = (cleanWindowAssigned == 0) & (collapseEraPad > 7),
true = 1,
false = cleanWindowAssigned
)
)
allCohorts |>
dplyr::group_by(cleanWindowRule, cleanWindowAssigned, eventCohort) |>
dplyr::summarise(n = dplyr::n())
# Choice: If exitDateOffset >= 7 then the cohort exit was probably thoughtfully constructed
allCohorts |>
dplyr::filter(eventCohort == 1) |>
dplyr::filter(cleanWindowAssigned == 0) |>
dplyr::group_by(exitDateOffSet) |>
dplyr::select(exitDateOffSet) |>
dplyr::summarise(n = dplyr::n()) |>
dplyr::arrange(exitDateOffSet)
allCohorts <- allCohorts |>
dplyr::mutate(
cleanWindow = dplyr::if_else(
condition = (cleanWindowAssigned == 0) & (exitDateOffSet >= 7) & (is.na(exitPersistenceWindow)),
true = 0, # if exit date has been assigned in cohort definition ,then there is no need for clean window
false = cleanWindow
),
cleanWindowRule = dplyr::if_else(
condition = (cleanWindowAssigned == 0) & (exitDateOffSet >= 7) & (is.na(exitPersistenceWindow)),
true = "Has an exit date strategy in the definition",
false = cleanWindowRule
),
cleanWindowAssigned = dplyr::if_else(
condition = (cleanWindowAssigned == 0) & (exitDateOffSet >= 7) & (is.na(exitPersistenceWindow)),
true = 1,
false = cleanWindowAssigned
)
)
allCohorts |>
dplyr::group_by(cleanWindowRule, cleanWindowAssigned, eventCohort) |>
dplyr::summarise(n = dplyr::n())
ids <- allCohorts |>
dplyr::arrange(cohortId) |>
dplyr::filter(cleanWindowAssigned == 0) |>
dplyr::pull(cohortId) |>
sort()
### Heuristic based: assign clean window ----
### (Decided by Azza and Gowtham)
cleanWindow <- c()
cleanWindow$cleanWindow9999 <- c(334, 405, 999, 1071)
cleanWindow$cleanWindow365 <- c(32, 74, 276, 412, 693, 694, 729, 732, 734, 739,
1075, 1080, 1081, 1082, 1083, 1084, 1087,
1088, 1089, 1090, 1091)
cleanWindow$cleanWindow183 <- c(1078, 1079)
cleanWindow$cleanWindow180 <- c(218, 716, 727, 731, 785)
cleanWindow$cleanWindow30 <- c(19, 123, 362, 411, 743, 783, 784, 938, 980, 1076, 1104)
cleanWindow$cleanWindow90 <- c(251)
cleanWindow$cleanWindow1 <- c(24, 346, 347, 707)
cleanWindowToAssign <-
dplyr::bind_rows(
dplyr::tibble(cohortId = cleanWindow$cleanWindow9999,
cleanWindow = 9999),
dplyr::tibble(cohortId = cleanWindow$cleanWindow365,
cleanWindow = 365),
dplyr::tibble(cohortId = cleanWindow$cleanWindow183,
cleanWindow = 183),
dplyr::tibble(cohortId = cleanWindow$cleanWindow180,
cleanWindow = 180),
dplyr::tibble(cohortId = cleanWindow$cleanWindow90,
cleanWindow = 90),
dplyr::tibble(cohortId = cleanWindow$cleanWindow30,
cleanWindow = 30),
dplyr::tibble(cohortId = cleanWindow$cleanWindow14,
cleanWindow = 14),
dplyr::tibble(cohortId = cleanWindow$cleanWindow1,
cleanWindow = 1)
) |>
dplyr::group_by(cohortId) |>
dplyr::summarise(cleanWindow = max(cleanWindow)) |>
dplyr::ungroup() |>
dplyr::mutate(cleanWindowAssigned = 1,
cleanWindowRule = "Manual")
allCohorts <-
dplyr::bind_rows(
allCohorts |>
dplyr::filter(!cohortId %in% c(cleanWindowToAssign$cohortId)),
allCohorts |>
dplyr::select(-cleanWindow, -cleanWindowAssigned, -cleanWindowRule) |>
dplyr::inner_join(cleanWindowToAssign)
)
allCohorts |>
dplyr::group_by(cleanWindowRule, cleanWindowAssigned, eventCohort) |>
dplyr::summarise(n = dplyr::n())
# Step 4: Create the input format that Chris asked for ----
## Analysis 1 ----
targets <- allCohorts |>
dplyr::filter(cohortId %in% c(
allCohorts |>
dplyr::filter(reasonBaseCohort == 1) |>
dplyr::pull(cohortId)
)) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::select(cohortId,
cohortName) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
outcomes <- allCohorts |>
dplyr::filter(cohortId %in% c(
allCohorts |>
dplyr::filter(reasonBaseCohort == 0) |>
dplyr::pull(cohortId)
)) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::filter(!cohortId %in% c(subsetOfCohorts$howOften$cohortId)) |> #remove cohorts that are part of HowOften as Drug Cohorts
dplyr::select(cohortId,
cohortName,
cleanWindow) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
writexl::write_xlsx(list(targets = targets, outcomes = outcomes), "analysis_specifications/analysis1.xlsx")
## Analysis 2 ----
analysis2InputSpecifications <- readxl::read_excel("analysis_specifications/analysis2InputSpecifications.xlsx")
analysis2CombinationsUnique <- analysis2InputSpecifications |>
dplyr::select(from,
group) |>
dplyr::distinct() |>
dplyr::arrange(from,
group) |>
dplyr::group_by(from) |>
dplyr::mutate(id = dplyr::row_number()) |>
dplyr::ungroup()
for (i in (1:nrow(analysis2CombinationsUnique))) {
combi <- analysis2CombinationsUnique[i,]
targets <- allCohorts |>
dplyr::filter(
cohortId %in% c(analysis2InputSpecifications |>
dplyr::inner_join(combi) |>
dplyr::pull(tId) |>
unique())
) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::select(cohortId,
cohortName) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
outcomes <- allCohorts |>
dplyr::filter(
cohortId %in% c(analysis2InputSpecifications |>
dplyr::inner_join(combi) |>
dplyr::pull(oId) |>
unique())
) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::select(cohortId,
cohortName,
cleanWindow) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
writexl::write_xlsx(list(targets = targets, outcomes = outcomes), paste0("analysis_specifications/analysis2_",
combi$from,
"_",
combi$id,
".xlsx"))
}
## Analysis 3 ----
targets <- allCohorts |>
dplyr::filter(
cohortId %in% c(
subsetOfCohorts$howOften$cohortId,
subsetOfCohorts$libraryIndicationCohorts$cohortId
)
) |>
dplyr::filter(cohortId != 1071) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::select(cohortId,
cohortName) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
outcomes <- allCohorts |>
dplyr::filter(
cohortId %in% c(
subsetOfCohorts$foundInLibraryOutcomeDme$cohortId,
subsetOfCohorts$foundInLibraryOutcomeAesi$cohortId,
subsetOfCohorts$foundInLibraryOutcomeLegend$cohortId
)
) |>
dplyr::inner_join(fullPhenotypeLog) |>
dplyr::select(cohortId,
cohortName,
cleanWindow) |>
dplyr::arrange(cohortId) |>
dplyr::rename(cohortDefinitionId = cohortId,
cohortDefinitionName = cohortName) |>
SqlRender::camelCaseToSnakeCaseNames()
writexl::write_xlsx(list(targets = targets, outcomes = outcomes), "analysis_specifications/analysis3.xlsx")