forked from OHDSI/Vocabulary-v5.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_stage.sql
2084 lines (2002 loc) · 65.9 KB
/
load_stage.sql
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
* Copyright 2016 Observational Health Data Sciences and Informatics (OHDSI)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Maria Rogozhkina, Oleg Zhuk, Polina Talapova, Dmitry Dymshyts, Alexander Davydov, Timur Vakhitov, Christian Reich
* Date: 2021
**************************************************************************/
--1. Update a 'latest_update' field to a new date
DO $_$
BEGIN
PERFORM VOCABULARY_PACK.SetLatestUpdate(
pVocabularyName => 'LOINC',
pVocabularyDate => (SELECT vocabulary_date FROM sources.loinc LIMIT 1),
pVocabularyVersion => (SELECT vocabulary_version FROM sources.loinc LIMIT 1),
pVocabularyDevSchema => 'DEV_LOINC'
);
END $_$;
--2. Truncate all working tables
TRUNCATE TABLE concept_stage;
TRUNCATE TABLE concept_relationship_stage;
TRUNCATE TABLE concept_synonym_stage;
TRUNCATE TABLE pack_content_stage;
TRUNCATE TABLE drug_strength_stage;
--Prepare tables
--RUN PRELOAD_STAGE
--3. Load LOINC concepts indicating Measurements or Observations from a source table of 'sources.loinc' into the concept_stage
INSERT INTO concept_stage (
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT CASE
WHEN loinc_num = '66678-4'
AND property = 'Hx'
THEN 'History of Diabetes (regardless of treatment) [PhenX]'
WHEN loinc_num = '82312-0'
THEN 'History of ' || REPLACE(long_common_name, 'andor', 'and/or')
WHEN property = 'Hx'
AND long_common_name !~* 'hx|histor|reported|status|narrative|^do you|^have you|^does|^has |education|why you|timing|virtuoso|maestro|grade|received|cause|allergies|in the past'
THEN 'History of ' || long_common_name
ELSE long_common_name -- AVOF-819
END AS concept_name,
--TODO: fix wrong domains, more information in AVOF-2241
CASE
WHEN classtype IN (
'1',
'2'
)
AND (
survey_quest_text LIKE '%?%' -- manually defined source attributes indicating the 'Observation' domain
OR scale_typ = 'Set'
OR property IN (
'Hx',
'Addr',
'Anat',
'ClockTime',
'Date',
'DateRange',
'Desc',
'EmailAddr',
'Instrct',
'Loc',
'Pn',
'Tele',
'TmStp',
'TmStpRange',
'Txt',
'URI',
'Xad',
'Bib'
)
OR (
property = 'ID'
AND system IN (
'^BPU',
'^Patient',
'Vaccine'
)
)
OR system IN (
'^Family member',
'^Neighborhood',
'^Brother',
'^Daughter',
'^Sister',
'^Son',
'^CCD',
'^Census tract',
'^Clinical trial protocol',
'^Community',
'*',
'?',
'^Contact',
'^Donor',
'^Emergency contact',
'^Event',
'^Facility',
'Provider',
'Report',
'Repository',
'School',
'Surgical procedure'
)
OR (
system IN (
'^Patient',
'*^Patient'
)
AND (
scale_typ IN (
'Doc',
'Nar',
'Nom',
'Ord',
'OrdQn'
)
AND (
method_typ <> 'Apgar'
OR method_typ IS NULL
)
OR property IN (
'Arb',
'Imp',
'NRat',
'Num',
'PrThr',
'RelRto',
'Time',
'Type',
'Find'
)
AND class NOT IN (
'COAG',
'PULM'
)
)
)
OR (long_common_name ~* 'note|Note')
)
AND (
long_common_name !~* 'scale|score'
OR long_common_name ~* 'interpretation|rose dyspnea scale'
)
AND (
method_typ <> 'Measured'
OR method_typ IS NULL
)
AND loinc_num NOT IN (
'65712-2',
'65713-0'
)
THEN 'Observation' -- AVOF-1579
WHEN classtype = '1'
THEN 'Measurement'
WHEN classtype = '2'
THEN 'Measurement'
WHEN classtype = '3'
THEN 'Observation'
WHEN classtype = '4'
THEN 'Observation'
END AS domain_id,
v.vocabulary_id,
CASE
WHEN classtype IN (
'1',
'2'
)
AND (
survey_quest_text LIKE '%?%' -- manually defined source attributes indicating the 'Clinical Observation' concept class
OR scale_typ = 'Set'
OR property IN (
'Hx',
'Addr',
'Anat',
'ClockTime',
'Date',
'DateRange',
'Desc',
'EmailAddr',
'Instrct',
'Loc',
'Pn',
'Tele',
'TmStp',
'TmStpRange',
'Txt',
'URI',
'Xad',
'Bib'
)
OR (
property = 'ID'
AND system IN (
'^BPU',
'^Patient',
'Vaccine'
)
)
OR system IN (
'^Family member',
'^Neighborhood',
'^Brother',
'^Daughter',
'^Sister',
'^Son',
'^CCD',
'^Census tract',
'^Clinical trial protocol',
'^Community',
'*',
'?',
'^Contact',
'^Donor',
'^Emergency contact',
'^Event',
'^Facility',
'Provider',
'Report',
'Repository',
'School',
'Surgical procedure'
)
OR (
system IN (
'^Patient',
'*^Patient'
)
AND (
scale_typ IN (
'Doc',
'Nar',
'Nom',
'Ord',
'OrdQn'
)
AND (
method_typ NOT IN ('Apgar')
OR method_typ IS NULL
)
OR property IN (
'Arb',
'Imp',
'NRat',
'Num',
'PrThr',
'RelRto',
'Time',
'Type',
'Find'
)
AND class NOT IN (
'COAG',
'PULM'
)
)
)
)
AND (
long_common_name !~* 'scale|score'
OR long_common_name ~* 'interpretation|rose dyspnea scale'
)
AND (
method_typ <> 'Measured'
OR method_typ IS NULL
)
AND loinc_num NOT IN (
'65712-2',
'65713-0'
)
THEN 'Clinical Observation' -- AVOF-1579
WHEN classtype = '1'
THEN 'Lab Test'
WHEN classtype = '2'
THEN 'Clinical Observation'
WHEN classtype = '3'
THEN 'Claims Attachment'
WHEN classtype = '4'
THEN 'Survey'
END AS concept_class_id,
CASE
WHEN l.STATUS IN ('DEPRECATED')
THEN NULL
WHEN l.STATUS IN ('DISCOURAGED')
AND (
l.loinc_num = ANY (cj_1map.arr_loinc)
OR l.loinc_num = ANY (cj_part.arr_loincnumber)
OR l.class = 'PANEL.HEDIS'
OR l.classtype IN (
'3',
'4'
)
) --Discouraged concepts that shouldn't be Standard: 1) have only one link in the sources.map_to 2) have Mass or Substance Concentration Loinc property 3) have the class "PANEL.HEDIS" 4) have classtype 3 (Survey) or 4 (Claims Attachment)
THEN NULL
ELSE 'S'
END AS standard_concept,
LOINC_NUM AS concept_code,
v.latest_update AS valid_start_date,
CASE
WHEN l.STATUS IN ('DEPRECATED')
THEN CASE
WHEN c.valid_end_date > v.latest_update
OR c.valid_end_date IS NULL
THEN v.latest_update
ELSE c.valid_end_date
END
WHEN l.STATUS IN ('DISCOURAGED')
AND (
l.loinc_num = ANY (cj_1map.arr_loinc)
OR l.loinc_num = ANY (cj_part.arr_loincnumber)
OR l.class = 'PANEL.HEDIS'
OR l.classtype IN (
'3',
'4'
)
) --Discouraged concepts that shouldn't be Standard: 1) have only one link in the sources.map_to 2) have Mass or Substance Concentration Loinc property 3) have the class "PANEL.HEDIS" 4) have classtype 3 (Survey) or 4 (Claims Attachment)
THEN CASE
WHEN c.valid_end_date > v.latest_update
OR c.valid_end_date IS NULL
THEN v.latest_update
ELSE c.valid_end_date
END
ELSE TO_DATE('20991231', 'yyyymmdd')
END AS valid_end_date,
CASE
WHEN (
l.STATUS IN ('DISCOURAGED')
AND (
(
l.loinc_num = ANY (cj_map.arr_loinc)
AND (
l.class = 'PANEL.HEDIS'
OR l.loinc_num = ANY (cj_part.arr_loincnumber)
)
) --Discouraged concepts that should be Updated: 1) have Mass or Substance Concentration Loinc property and with mapping in the sources.to_map 3) have the class "PANEL.HEDIS" and with mapping in the sources.to_map
OR l.loinc_num = ANY (cj_1map.arr_loinc)
)
) --Discouraged concepts that should be Updated: 1) have only one link in the sources.map_to
OR (
l.STATUS IN ('DEPRECATED')
AND l.loinc_num = ANY (cj_map.arr_loinc)
)
THEN 'U'
WHEN l.STATUS = 'DEPRECATED'
OR (
l.STATUS = 'DISCOURAGED'
AND (
l.class = 'PANEL.HEDIS'
OR l.loinc_num = ANY (cj_part.arr_loincnumber)
OR l.classtype IN (
'3',
'4'
)
)
) --Discouraged concepts that should be Deprecated: 1) have Mass or Substance Concentration Loinc property without mapping in the sources.map_to 2) have the class "PANEL.HEDIS" without mapping in the sources.map_to 3) have classtype 3 (Survey) or 4 (Claims Attachment) without mapping in the sources.map_to
THEN 'D'
ELSE NULL
END AS invalid_reason
FROM sources.loinc l
JOIN vocabulary v ON v.vocabulary_id = 'LOINC'
CROSS JOIN (
SELECT ARRAY(SELECT DISTINCT m.loinc FROM sources.map_to m) arr_loinc
) cj_map
CROSS JOIN (
SELECT ARRAY(SELECT m.loinc FROM sources.map_to m GROUP BY m.loinc HAVING COUNT(DISTINCT m.map_to) = 1) arr_loinc
) cj_1map
CROSS JOIN (
SELECT ARRAY(SELECT lp.loincnumber FROM sources.loinc_partlink_primary lp WHERE lp.partnumber = 'LP33032-1') arr_loincnumber
) cj_part
LEFT JOIN concept c ON c.concept_code = l.LOINC_NUM
AND c.vocabulary_id = 'LOINC';
--3.1. Update Domains for concepts representing Imaging procedures
UPDATE concept_stage cs
SET domain_id = 'Procedure'
FROM sources.loinc l
WHERE cs.concept_code = l.loinc_num
AND l.class = 'RAD' --Radiology concepts
--Concept code doesn't have parts like "Qn", "Densitometry", "Calcium score"
AND NOT EXISTS (
SELECT 1
FROM sources.loinc_partlink_primary lp
WHERE lp.partnumber IN (
'LP7753-9',
'LP200093-5',
'LP200395-4'
)
AND lp.loincnumber = cs.concept_code
);
--4. Add LOINC Classes from a manual table of 'sources.loinc_class' into the concept_stage
INSERT INTO concept_stage (
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT concept_name,
CASE
WHEN concept_name ~* 'history|report|document|miscellaneous|public health' -- manually defined word patterns indicating the 'Observation' domain
THEN 'Observation'
ELSE domain_id
END, -- AVOF-1579
vocabulary_id,
concept_class_id,
'C',
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
FROM sources.loinc_class;
--5. Add LOINC Attributes ('Parts') and LOINC Hierarchy concepts into the concept_stage
INSERT INTO concept_stage (
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
)
WITH s AS (
-- pick LOINC Parts of 6 classes (classes that have links in 'Primary' linktypename)
SELECT p.partnumber,
p.partdisplayname,
p.parttypename,
p.status
FROM sources.loinc_part p -- contains LOINC Parts and defines their validity ('status' field)
WHERE p.parttypename IN (
'SYSTEM',
'METHOD',
'PROPERTY',
'TIME',
'COMPONENT',
'SCALE'
) -- list of Primary LOINC Parts
UNION ALL
-- pick LOINC Hierarchy concepts (Attributive Panels, non-primary Parts and ~400 Undefined attributes)
SELECT DISTINCT code,
COALESCE(p.partdisplayname, code_text) AS partdisplayname,
'LOINC Hierarchy' AS parttypename,
CASE
WHEN p.status IS NOT NULL
THEN p.status
ELSE 'ACTIVE'
END AS status
FROM sources.loinc_hierarchy lh
LEFT JOIN sources.loinc_part p --to get a validity of concept (a 'status' field)
ON lh.code = p.partnumber -- LOINC Attribute
WHERE lh.code LIKE 'LP%' -- all LOINC Hierсrchy concepts have 'LP' at the beginning of the names (including ~400 undefined concepts and LOINC panels)
AND NOT EXISTS (
SELECT 1
FROM sources.loinc_part p_int
WHERE p_int.parttypename IN (
'SYSTEM',
'METHOD',
'PROPERTY',
'TIME',
'COMPONENT',
'SCALE'
)
AND p_int.partnumber = lh.code
) --excluding Primary LOINC Parts added above
)
SELECT DISTINCT TRIM(s.partdisplayname) AS concept_name,
CASE
WHEN partdisplayname ~* ('directive|^age\s+|lifetime risk|alert|attachment|\s+date|comment|\s+note|consent|identifier|\s+time|\s+number|' || 'date and time|coding system|interpretation|status|\s+name|\s+report|\s+id$|s+id\s+|version|instruction|known exposure|priority|ordered|available|requested|issued|flowsheet|\s+term|' || 'reported|not yet categorized|performed|risk factor|device|administration|\s+route$|suggestion|recommended|narrative|ICD code|reference|' || 'reviewed|information|intention|^Reason for|^Received|Recommend|provider|subject|summary|time\s+|document') -- manually defined word patterns indicating the 'Observation' domain
AND partdisplayname !~* ('thrombin time|clotting time|bleeding time|clot formation|kaolin activated time|closure time|protein feed time|Recalcification time|reptilase time|russell viper venom time|' || 'implanted device|dosage\.vial|isolate|within lymph node|cancer specimen|tumor|chromosome|inversion|bioavailable')
THEN 'Observation'
ELSE 'Measurement' --AVOF-1579 --will be corrected below (5.1) for 6 Primary LOINC Parts
END AS domain_id,
'LOINC' AS vocabulary_id,
CASE
WHEN s.parttypename = 'SYSTEM'
THEN 'LOINC System'
WHEN s.parttypename = 'METHOD'
THEN 'LOINC Method'
WHEN s.parttypename = 'PROPERTY'
THEN 'LOINC Property'
WHEN s.parttypename = 'TIME'
THEN 'LOINC Time'
WHEN s.parttypename = 'COMPONENT'
THEN 'LOINC Component'
WHEN s.parttypename = 'SCALE'
THEN 'LOINC Scale'
ELSE 'LOINC Hierarchy'
END AS concept_class_id,
CASE s.status
WHEN 'DEPRECATED'
THEN NULL
ELSE 'C' --will be corrected below (5.1) for 6 Primary LOINC Parts
END AS standard_concept,
s.partnumber AS concept_code, -- LOINC Attribute or Hierarchy concept
v.latest_update AS valid_start_date,
CASE
WHEN s.status = 'DEPRECATED'
THEN CASE
WHEN c.valid_end_date <= latest_update
THEN c.valid_end_date -- preserve valid_end_date for already existing DEPRECATED concepts
ELSE GREATEST(COALESCE(c.valid_start_date, v.latest_update), -- assign LOINC 'latest_update' as 'valid_end_date' for new concepts which have to be deprecated in the current release
latest_update - 1)
END -- assign LOINC 'latest_update-1' as 'valid_end_date' for already existing concepts, which have to be deprecated in the current release
ELSE TO_DATE('20991231', 'yyyymmdd')
END AS valid_end_date, -- default value of 31-Dec-2099 for the rest
CASE
WHEN s.status IN (
'ACTIVE',
'INACTIVE'
)
THEN NULL -- define concept validity according to the 'status' field
WHEN s.status = 'DEPRECATED'
THEN 'D'
ELSE 'X'
END AS invalid_reason --IF there are any changes in LOINC source we don't know about. GenericUpdate() will fail in case of 'X' in invalid_reason field
FROM s
JOIN vocabulary v ON v.vocabulary_id = 'LOINC'
LEFT JOIN concept c ON c.concept_code = s.partnumber -- already existing LOINC concepts
AND c.vocabulary_id = 'LOINC';
--prerelease fix
INSERT INTO concept_stage (
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date
)
SELECT long_common_name AS concept_name,
'Measurement' AS domain_id,
'LOINC' AS vocabulary_id,
'Lab Test' AS concept_class_id,
'S' AS standard_concept,
loinc AS concept_code,
created_on AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date
FROM vocabulary_pack.GetLoincPrerelease();
--5.1 Update Radiology Hierarchy Domains
UPDATE concept_stage cs
SET domain_id = 'Procedure'
FROM sources.loinc_hierarchy lh
WHERE (
lh.path_to_root LIKE '%LP29684-5%' --Radiology (LOINC Hierarchy)
AND cs.concept_class_id = 'LOINC Hierarchy'
AND cs.concept_name LIKE '%Radiology%'
AND lh.code = cs.concept_code
)
OR cs.concept_code = 'LP29684-5';
--6. Insert missing codes from manual extraction
DO $_$
BEGIN
PERFORM VOCABULARY_PACK.ProcessManualConcepts();
END $_$;
--7. Update Domain = 'Observation' and standard_concept = NULL for attributes that are not part of Hierarchy (AVOF-2222)
WITH hierarchy
AS (
SELECT lh.code
FROM sources.loinc_hierarchy lh
WHERE (
NOT EXISTS (
SELECT 1
FROM sources.loinc_partlink_primary lpp
WHERE lh.code = lpp.partnumber
AND lpp.parttypename <> 'CLASS'
)
AND NOT EXISTS (
SELECT 1
FROM sources.loinc_partlink_supplementary lps
WHERE lh.code = lps.partnumber
AND lps.parttypename <> 'CLASS'
)
)
AND lh.code !~ '^\d'
)
UPDATE concept_stage cs
SET domain_id = 'Observation',
standard_concept = NULL
WHERE NOT EXISTS (
SELECT 1
FROM hierarchy h
WHERE h.code = cs.concept_code
)
--currently hierarchy does not overlap with 6 LP classes, but this might be helpful in further development
AND cs.concept_class_id ~ 'LOINC (System|Method|Property|Time|Component|Scale)';
--8. Build 'Subsumes' relationships from LOINC Ancestors to Descendants using a source table of 'sources.loinc_hierarchy'
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT DISTINCT lh.immediate_parent AS concept_code_1, -- LOINC Ancestor
lh.code AS concept_code_2, -- LOINC Descendant
'Subsumes' AS relationship_id,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_hierarchy lh
WHERE lh.immediate_parent IS NOT NULL;-- when immediate parent is null then there is no Ancestor
--9. Build 'Has system', 'Has method', 'Has property', 'Has time aspect', 'Has component', and 'Has scale type' relationships from LOINC Measurements/Observations to Primary LOINC Parts (attributes)
--assign specific links using a TYPE of LOINC Part using 'sources.loinc_partlink_primary'
ANALYZE concept_stage;
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
valid_start_date,
valid_end_date,
invalid_reason
)
WITH s AS (
SELECT pl.loincnumber, -- LOINC Measurement/Observation
p.partnumber, -- Primary LOINC Part
p.status,
CASE
WHEN p.parttypename = 'SYSTEM'
THEN 'Has system'
WHEN p.parttypename = 'METHOD'
THEN 'Has method'
WHEN p.parttypename = 'PROPERTY'
THEN 'Has property'
WHEN p.parttypename = 'TIME'
THEN 'Has time aspect'
WHEN p.parttypename = 'COMPONENT'
THEN 'Has component'
WHEN p.parttypename = 'SCALE'
THEN 'Has scale type'
END AS relationship_id
FROM sources.loinc_partlink_primary pl
JOIN sources.loinc_part p ON pl.partnumber = p.partnumber -- Primary LOINC Part
WHERE pl.linktypename = 'Primary'
),
-- pick already existing relationships between LOINC Measurements/Observations and Primary LOINC Parts (it's needed to pull the validity dates from basic tables)
cr AS (
SELECT c1.concept_code AS concept_code_1, -- LOINC Measurement/Observation
c1.vocabulary_id,
relationship_id,
c2.concept_code AS concept_code_2, -- Primary LOINC Part ?
c2.vocabulary_id,
cr.valid_start_date,
cr.valid_end_date,
cr.invalid_reason
FROM concept_relationship cr
JOIN concept c1 ON c1.concept_id = cr.concept_id_1
JOIN concept c2 ON c2.concept_id = cr.concept_id_2
WHERE c1.vocabulary_id = 'LOINC'
AND c2.vocabulary_id = 'LOINC'
)
SELECT s.loincnumber AS concept_code_1,
partnumber AS concept_code_2,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
s.relationship_id AS relationship_id,
COALESCE(cr.valid_start_date, -- preserve valid_start_date for already existing relationships
LEAST(cs1.valid_end_date, cs2.valid_end_date, v.latest_update)) AS valid_start_date, -- compare and assign earliest date of 'valid_end_date' of a LOINC concept AS 'valid_start_date' for NEW relationships of concepts deprecated in the current release OR 'latest update' for the rest of the codes
CASE
WHEN cr.valid_end_date <= v.latest_update --preserve valid_end_date for already existing relationships
AND (cs1.invalid_reason IS NOT NULL OR cs2.invalid_reason IS NOT NULL OR cs1.concept_code IS NULL OR cs2.concept_code IS NULL) --only if they're still deprecated
THEN cr.valid_end_date
WHEN cs1.invalid_reason IS NOT NULL OR cs2.invalid_reason IS NOT NULL
THEN LEAST(cs1.valid_end_date, cs2.valid_end_date) -- compare and assign earliest date of 'valid_end_date' of a LOINC concept as 'valid_end_date' for NEW relationships of concepts deprecated in the current release
ELSE TO_DATE('20991231', 'yyyymmdd')
END AS valid_end_date, -- for the rest of the codes
CASE
WHEN cs1.invalid_reason IS NOT NULL
OR cs2.invalid_reason IS NOT NULL
OR cs1.concept_code IS NULL
OR cs2.concept_code IS NULL
THEN 'D'
ELSE NULL
END AS invalid_reason
FROM s
LEFT JOIN concept_stage cs1 -- to define deprecated LOINC Observations/Measurements in the current release
ON cs1.concept_code = s.loincnumber --LOINC Observation/Measurement in the current release
AND cs1.vocabulary_id = 'LOINC'
LEFT JOIN concept_stage cs2 -- to define deprecated LOINC Parts
ON cs2.concept_code = s.partnumber -- LOINC Part
LEFT JOIN cr ON (
cr.concept_code_1,
cr.relationship_id,
cr.concept_code_2
) = (
s.loincnumber,
s.relationship_id,
s.partnumber
) -- already existing relationships between LOINC concepts
JOIN vocabulary v ON v.vocabulary_id = 'LOINC';
--10. Build 'Subsumes' relationships between LOINC Classes using a source table of 'sources.loinc_class' and a similarity of a class name beginning (ancestor class_name LIKE descendant class_name || '%')
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT l2.concept_code AS concept_code_1, -- LOINC Class Ancestor
l1.concept_code AS concept_code_2, -- LOINC Class Descendant
'Subsumes' AS relationship_id,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_class l1,
sources.loinc_class l2
WHERE l1.concept_code LIKE l2.concept_code || '%'
AND l1.concept_code <> l2.concept_code;
--11. Build 'Subsumes' relationships from LOINC Classes to LOINC concepts indicating Measurements or Observations with the use of source tables of 'sources.loinc_class' and 'sources.loinc' to create Multiaxial Hierarchy
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT l.class AS concept_code_1, -- LOINC Class concept
l.loinc_num AS concept_code_2, -- LOINC Observation/Measurement concept
'Subsumes' AS relationship_id,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_class lc,
sources.loinc l
WHERE lc.concept_code = l.class;
--12. Delete wrong relationship between 'PANEL.H' class (History & Physical order set) and 38213-5 'FLACC pain assessment panel' (AVOF-352)
DELETE
FROM concept_relationship_stage
WHERE concept_code_1 = 'PANEL.H' || CHR(38) || 'P' -- '&' = CHR(38)
AND concept_code_2 = '38213-5'
AND relationship_id = 'Subsumes';
--13. Add to the concept_synonym_stage all synonymic names from a source table of 'sources.loinc'
-- NB! We do not add synonyms for LOINC Answers (a 'description' field) due to their vague formulation
INSERT INTO concept_synonym_stage (
synonym_concept_code,
synonym_name,
synonym_vocabulary_id,
language_concept_id
) (
--values of a 'RelatedNames2' field
SELECT l.loinc_num AS synonym_concept_code,
vocabulary_pack.CutConceptSynonymName(l.relatednames2) AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id -- English
FROM sources.loinc l WHERE l.relatednames2 IS NOT NULL
UNION
-- values of a 'consumer_name' field that were previously used as preferred name (in 195 cases)
SELECT l.loinc_num AS synonym_concept_code,
l.consumer_name AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id -- English
FROM sources.loinc l WHERE l.consumer_name IS NOT NULL
UNION
-- values of the 'ShortName' field
SELECT l.loinc_num AS synonym_concept_code,
l.shortname AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id -- English
FROM sources.loinc l WHERE l.shortname IS NOT NULL
UNION
--'long_common_name' field values which were changed ('History of')
SELECT l.loinc_num AS synonym_concept_code,
l.long_common_name AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id -- English
FROM sources.loinc l WHERE NOT EXISTS (
SELECT 1
FROM concept_stage cs_int
WHERE cs_int.concept_name = l.long_common_name
)
UNION
--'PartName' field values which are synonyms for 'partdisplayname' field values in sources.loinc_part
SELECT pl.partnumber AS synonym_concept_code,
p.partname AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id --English language
FROM sources.loinc_partlink_primary pl
JOIN sources.loinc_part p ON p.partnumber = pl.partnumber
WHERE EXISTS (
SELECT 1
FROM concept_stage cs_int
WHERE cs_int.concept_code = pl.partnumber
)
AND pl.partname <> p.partdisplayname
UNION
SELECT pl.partnumber AS synonym_concept_code,
p.partname AS synonym_name,
'LOINC' AS synonym_vocabulary_id,
4180186 AS language_concept_id --English language
FROM sources.loinc_partlink_supplementary pl
JOIN sources.loinc_part p ON p.partnumber = pl.partnumber
WHERE EXISTS (
SELECT 1
FROM concept_stage cs_int
WHERE cs_int.concept_code = pl.partnumber
)
AND pl.partname <> p.partdisplayname
);-- pick only different names
--14. Add LOINC Answers from 'sources.loinc_answerslist' and 'sources.loinc_answerslistlink' source tables to the concept_stage
INSERT INTO concept_stage (
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT DISTINCT TRIM(ans_l.displaytext) AS concept_name,
'Meas Value' AS domain_id,
'LOINC' AS vocabulary_id,
'Answer' AS concept_class_id,
'S' AS standard_concept,
ans_l.answerstringid AS concept_code,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_answerslist ans_l -- Answer containing table
JOIN sources.loinc_answerslistlink ans_l_l ON ans_l_l.answerlistid = ans_l.answerlistid -- 'AnswerListID' field unites Answers with Questions
JOIN sources.loinc l ON l.loinc_num = ans_l_l.loincnumber -- to confirm the connection of 'AnswerListID' with LOINC concepts indicating Measurements and Observations (currently all of them are connected)
WHERE ans_l.answerstringid IS NOT NULL;--'AnswerStringID' value may be null
--15. Build 'Has Answer' relationships from LOINC Questions to Answers with the use of such source tables as 'sources.loinc_answerslist' and 'sources.loinc_answerslistlink'
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT DISTINCT ans_l_l.loincnumber AS concept_code_1, -- LOINC Question code
ans_l.answerstringid AS concept_code_2, -- LOINC Answer code
'Has Answer' AS relationship_id,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_answerslist ans_l -- Answer containing table
JOIN sources.loinc_answerslistlink ans_l_l ON ans_l_l.answerlistid = ans_l.answerlistid -- 'AnswerListID' field unites Answers with Questions
WHERE ans_l.answerstringid IS NOT NULL;-- 'AnswerStringID' may be null
--16. Build 'Panel contains' relationships from LOINC Panels to their descendants with the use of 'sources.loinc_forms' table
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT DISTINCT lf.parentloinc AS concept_code_1, -- LOINC Panel code
lf.loinc AS concept_code_2, -- LOINC Descendant code
'Panel contains' AS relationship_id,
'LOINC' AS vocabulary_id_1,
'LOINC' AS vocabulary_id_2,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.loinc_forms lf -- Panel containing table
WHERE lf.loinc <> lf.parentloinc;-- to exclude cases when parents and children are represented by the same concepts
--17. Build temporary 'LOINC - SNOMED eq' relationships between LOINC Attributes and SNOMED Attributes (will be dropped in 20). Afterward 'Maps to' may be built instead.
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
valid_start_date,
valid_end_date,
invalid_reason
)
-- note, there are 39 LOINC Parts which have more than one link to SNOMED due to different representation of Systems and Components in vocabularies
SELECT DISTINCT s.maptarget AS concept_code_1, -- LOINC Attribute code
s.referencedcomponentid AS concept_code_2, -- SNOMED Attribute code
'LOINC' AS vocabulary_id_1,
'SNOMED' AS vocabulary_id_2,
'LOINC - SNOMED eq' AS relationship_id,
v.latest_update AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM sources.scccrefset_mapcorrorfull_int s
JOIN concept_stage cs ON cs.concept_code = s.maptarget --LOINC Attribute
AND cs.vocabulary_id = 'LOINC'
AND cs.invalid_reason IS NULL
JOIN concept c ON c.concept_code = s.referencedcomponentid --SNOMED Attribute
AND c.vocabulary_id = 'SNOMED'
AND c.invalid_reason IS NULL
JOIN vocabulary v ON cs.vocabulary_id = v.vocabulary_id --valid_start_date
WHERE s.attributeid IN (
'246093002',
'704319004',
'704327008',
'718497002'
);--'Component', 'Inheres in' (Component-like), 'Direct site' (System-like), 'Inherent location' (Component-like)
/* Excluded attribute IDs:
Process output - reduplicate a Component
Process agent - link from a LOINC Component to a possible SNOMED System, useless in mapping ('Kidney structure')
Property type - links from a LOINC Component to a possible SNOMED Property (useless, non-SNOMED logic)
Technique - link from a LOINC Component to SNOMED Technique (useless, non-SNOMED logic)
Characterizes - senseless 'Excretory process' */
--18. Build temporary relationships between LOINC Measurements and respective SNOMED attributes given by the table of 'sources.scccrefset_expressionassociation_int' (will be dropped in 20).
--Note, that some suggested by LOINC relationship_ids ('Characterizes', 'Units', 'Relative to', 'Process agent' 'Inherent location') are useless in the context of a mapping to SNOMED.
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
valid_start_date,
valid_end_date,
invalid_reason
)
-- extract LOINC Measurement codes, LOINC-to-SNOMED relationship_id identifiers and related SNOMED Attributes from sources.scccrefset_expressionassociation_int
WITH t1 AS (
SELECT s0.maptarget, -- LOINC Measurement code
s0.tuples [1] AS sn_key, -- LOINC to SNOMED relationship_id identifier
s0.tuples [2] AS sn_value -- related SNOMED Attribute
FROM (
SELECT ea.maptarget,
STRING_TO_ARRAY(UNNEST(STRING_TO_ARRAY(SUBSTRING(ea.expression, devv5.instr(ea.expression, ':') + 1), ',')), '=') AS tuples
FROM sources.scccrefset_expressionassociation_int ea