forked from CaseyTa/ehr_prevalence
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcohd_omop_export_sql_server.sql
More file actions
335 lines (308 loc) · 15 KB
/
cohd_omop_export_sql_server.sql
File metadata and controls
335 lines (308 loc) · 15 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
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
-- Export condition, drug, procedure, and demographics data from SQL Server Management Studio for EHR prevalence analysis
--
-- Setup:
-- Set file output format to tab delimited:
-- In SQL Server Management Studio: Tools > Options > Query Results > SQL Server > Results to Text >
-- Output format: tab delimited
-- Include column headers in the result set: enabled
-- Restart SSMS for new settings to take effect
-- Prevent the count from showing up in the text file results
SET NOCOUNT ON;
-- Load the iatrogenic codes into temporary tables: #iatrogenic_codes and #iatrogenic_codes_with_desc
-- Iatrogenic codes and their descendants will be excluded from the analysis
:r D:\cohd\ehr_prevalence\load_iatrogenic_tsql.sql
-- -------------------------------------------------------------
-- Export conditions, drugs, and procedures, excluding iatrogenic codes
-- -------------------------------------------------------------
-- Export person ID, start date, and concept IDs for conditions, drugs, and procedures
:OUT D:\cohd\data\unique_patient_concept_pairs_date.txt
SELECT DISTINCT co.person_id, YEAR(co.condition_start_date) AS date, co.condition_concept_id AS concept_id
FROM condition_occurrence co
JOIN concept c ON co.condition_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE condition_concept_id != 0
AND c.domain_id = 'Condition' -- Make sure we only get conditions from the condition_occurrence table
AND i.concept_id IS NULL -- Make sure condition is not an iatrogenic code
UNION ALL
SELECT DISTINCT de.person_id, YEAR(de.drug_exposure_start_date) AS date, de.drug_concept_id AS concept_id
FROM drug_exposure de
JOIN concept c ON de.drug_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE drug_concept_id != 0
AND c.domain_id = 'Drug' -- Make sure we only get conditions from the condition_occurrence table
AND i.concept_id IS NULL -- Make sure condition is not an iatrogenic code
UNION ALL
SELECT DISTINCT po.person_id, YEAR(po.procedure_date) AS date, po.procedure_concept_id AS concept_id
FROM procedure_occurrence po
JOIN concept c ON po.procedure_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE procedure_concept_id != 0
AND c.domain_id = 'Procedure' -- Make sure we only get conditions from the condition_occurrence table
AND i.concept_id IS NULL -- Make sure condition is not an iatrogenic code;
-- -------------------------------------------------------------
-- Demographics
-- Two options:
-- 1) export raw demographics
-- 2) use custom defined race ancestor concepts to get higher level race concepts
-- -------------------------------------------------------------
-- Option 1) Export demographics from the person table
:OUT D:\cohd\data\person.txt
SELECT person_id, gender_concept_id, race_concept_id, ethnicity_concept_id
FROM person;
-- Option 2) Export demographics from the person table
:OUT D:\cohd\data\person.txt
SELECT person_id, gender_concept_id, r.ancestor_concept_id AS race_concept_id, ethnicity_concept_id
FROM person p
JOIN user_schema.dbo.race_ancestor_concepts r ON p.race_concept_id = r.race_concept_id;
-- -------------------------------------------------------------
-- Concept hierarchy
-- Get observed concepts and their ancestors from the desired vocabularies
-- -------------------------------------------------------------
-- Get observed concepts from conditions, drugs, and procedures
SELECT DISTINCT condition_concept_id AS concept_id
INTO #observed_condition_concepts
FROM condition_occurrence o
JOIN concept c ON o.condition_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE c.domain_id = 'Condition' AND i.concept_id IS NULL;
SELECT DISTINCT drug_concept_id AS concept_id
INTO #observed_drug_concepts
FROM drug_exposure o
JOIN concept c ON o.drug_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE c.domain_id = 'Drug' AND i.concept_id IS NULL;
SELECT DISTINCT procedure_concept_id AS concept_id
INTO #observed_procedure_concepts
FROM procedure_occurrence o
JOIN concept c ON o.procedure_concept_id = c.concept_id
LEFT JOIN #iatrogenic_codes_with_desc i ON c.concept_id = i.concept_id
WHERE c.domain_id = 'Procedure' AND i.concept_id IS NULL;
-- Get condition concepts and their ancestors
SELECT *
INTO #hierarchical_condition_concepts
FROM
((SELECT *
FROM #observed_condition_concepts)
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_condition_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.domain_id = 'Condition' AND c.vocabulary_id = 'SNOMED')
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_condition_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
LEFT JOIN concept_relationship cr ON (cr.concept_id_1 = c.concept_id AND cr.relationship_id = 'MedDRA - SNOMED eq')
WHERE c.domain_id = 'Condition' AND c.vocabulary_id = 'MedDRA' AND cr.relationship_id IS NULL)) y
;
-- Get drug concepts and their ancestors
SELECT *
INTO #hierarchical_drug_concepts
FROM
((SELECT *
FROM #observed_drug_concepts)
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_drug_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.domain_id = 'Drug' AND c.vocabulary_id = 'RxNorm' AND c.concept_class_id IN ('Ingredient', 'Clinical Drug Form', 'Clinical Drug Comp', 'Clinical Drug'))
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_drug_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
LEFT JOIN concept_relationship cr ON (cr.concept_id_1 = c.concept_id AND cr.relationship_id IN ('ATC - RxNorm', 'ATC - RxNorm name'))
WHERE c.domain_id = 'Drug' AND c.vocabulary_id = 'ATC' AND cr.relationship_id IS NULL)) y
;
-- Get procedure concepts and their ancestors
SELECT *
INTO #hierarchical_procedure_concepts
FROM
((SELECT *
FROM #observed_procedure_concepts)
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_procedure_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.domain_id = 'Procedure' AND c.vocabulary_id IN ('SNOMED', 'ICD10PCS'))
UNION
(SELECT DISTINCT ca.ancestor_concept_id
FROM #observed_procedure_concepts x
JOIN concept_ancestor ca ON ca.descendant_concept_id = x.concept_id
JOIN concept c ON ca.ancestor_concept_id = c.concept_id
LEFT JOIN concept_relationship cr ON (cr.concept_id_1 = c.concept_id AND cr.relationship_id = 'MedDRA - SNOMED eq')
WHERE c.domain_id = 'Procedure' AND c.vocabulary_id = 'MedDRA' AND cr.relationship_id IS NULL)) y
;
-- Get the vocabularies used in the condition/drug/procedure tables and for their hierarchy
declare @condition_vocabs TABLE (vocabulary_id varchar(20));
declare @drug_vocabs TABLE (vocabulary_id varchar(20));
declare @procedure_vocabs TABLE (vocabulary_id varchar(20));
INSERT INTO @condition_vocabs (vocabulary_id)
(SELECT *
FROM
-- Vocabularies used in hierarchy
(values ('SNOMED'), ('MedDRA')) v(vocabulary_id))
UNION
-- Vocabularies in observational tables
(SELECT DISTINCT vocabulary_id
FROM condition_occurrence co
JOIN concept c ON co.condition_concept_id = c.concept_id
WHERE c.domain_id = 'Condition');
INSERT INTO @drug_vocabs (vocabulary_id)
(SELECT *
FROM
-- Vocabularies used in hierarchy
(values ('RxNorm'), ('ATC')) v(vocabulary_id))
UNION
-- Vocabularies in observational tables
(SELECT DISTINCT vocabulary_id
FROM drug_exposure de2
JOIN concept c2 ON de2.drug_concept_id = c2.concept_id
WHERE c2.domain_id = 'Drug');
INSERT INTO @procedure_vocabs (vocabulary_id)
(SELECT *
FROM
-- Vocabularies used in hierarchy
(values ('SNOMED'), ('MedDRA'), ('ICD10PCS')) v(vocabulary_id))
UNION
-- Vocabularies in observational tables
(SELECT DISTINCT vocabulary_id
FROM procedure_occurrence po
JOIN concept c ON po.procedure_concept_id = c.concept_id
WHERE c.domain_id = 'Procedure');
-- Export the hierarchical concepts and their direct descendants from the desired vocabularies
-- Note: some concepts can be descendants with 0 levels of separation from the ancestor
-- Note 2: Using variables to store the vocabs instead of using a subquery because the
-- subquery was extremely slow despite being non-correlated.
:OUT D:\cohd\data\concept_descendants_direct.txt
SELECT DISTINCT concept_id, descendant_concept_id
FROM
((SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_condition_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts or its ancestors
JOIN #hierarchical_condition_concepts hc2 ON ca.descendant_concept_id = hc2.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only immediate descendants (or 0 level descendants)
AND ca.min_levels_of_separation <= 1
-- Want only descendants from the same domain
AND c.domain_id = 'Condition'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @condition_vocabs))
UNION
(SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_drug_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts or its ancestors
JOIN #hierarchical_drug_concepts hc2 ON ca.descendant_concept_id = hc2.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only immediate descendants (or 0 level descendants)
AND ca.min_levels_of_separation <= 1
-- Want only descendants from the same domain
AND c.domain_id = 'Drug'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @drug_vocabs))
UNION
(SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_procedure_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts or its ancestors
JOIN #hierarchical_procedure_concepts hc2 ON ca.descendant_concept_id = hc2.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only immediate descendants (or 0 level descendants)
AND ca.min_levels_of_separation <= 1
-- Want only descendants from the same domain
AND c.domain_id = 'Procedure'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @procedure_vocabs))) tmp
ORDER BY concept_id, descendant_concept_id
;
-- Export all observed descendants of each each hierarchical concept
-- Note: some concepts can be descendants with 0 levels of separation from the ancestor
-- Note 2: Using variables to store the vocabs instead of using a subquery because the
-- subquery was extremely slow despite being non-correlated.
:OUT D:\cohd\data\concept_descendants_all_observed.txt
SELECT concept_id, descendant_concept_id
FROM
((SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_condition_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts
JOIN #observed_condition_concepts oc ON ca.descendant_concept_id = oc.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only descendants from the same domain
AND c.domain_id = 'Condition'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @condition_vocabs))
UNION
(SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_drug_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts
JOIN #observed_drug_concepts oc ON ca.descendant_concept_id = oc.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only descendants from the same domain
AND c.domain_id = 'Drug'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @drug_vocabs))
UNION
(SELECT ca.ancestor_concept_id AS concept_id, ca.descendant_concept_id
FROM #hierarchical_procedure_concepts hc
JOIN concept_ancestor ca ON ca.ancestor_concept_id = hc.concept_id
JOIN concept c ON ca.descendant_concept_id = c.concept_id
-- Make sure the descendant concept is one of the observed concepts
JOIN #observed_procedure_concepts oc ON ca.descendant_concept_id = oc.concept_id
WHERE
-- Don't include the self-ancestor relationship
ca.ancestor_concept_id != ca.descendant_concept_id
-- Want only descendants from the same domain
AND c.domain_id = 'Procedure'
-- Want only descendants from the specified vocabs
AND c.vocabulary_id IN
(SELECT * FROM @procedure_vocabs))) tmp
ORDER BY concept_id, descendant_concept_id
;
-- Export the concept definitions
-- Note: use union instead of union all because 0 is in each domain
:OUT D:\cohd\data\concepts.txt
SELECT *
FROM
(SELECT concept_id FROM #hierarchical_condition_concepts
UNION
SELECT concept_id FROM #hierarchical_drug_concepts
UNION
SELECT concept_id FROM #hierarchical_procedure_concepts
UNION
SELECT DISTINCT gender_concept_id AS concept_id FROM person
UNION
SELECT DISTINCT race_concept_id AS concept_id FROM person
UNION
SELECT DISTINCT ethnicity_concept_id AS concept_id FROM person) concept_ids
LEFT JOIN concept ON concept_ids.concept_id = concept.concept_id
ORDER BY concept.concept_id ASC;
-- Return to normal settings
SET NOCOUNT OFF;