Advertisement
snafl

combos

Sep 10th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --1. ATC 4th
  2.   --1.1 ATC 4th, and
  3.   select
  4.     c1.concept_id as atc5_id, c1.concept_name as atc5_name,
  5.     c2.concept_id as atc4_id, c2.concept_name as atc4_name,
  6.     c4.concept_id as rxnorm_id, c4.concept_name as rxnorm_name
  7.   from concept c1
  8.   join concept_relationship r1 on r1.concept_id_1=c1.concept_id and r1.invalid_reason is null and r1.relationship_id='Is a'
  9.   join concept c2 on r1.concept_id_2=c2.concept_id
  10.   join concept_relationship r2 on r2.concept_id_1=c2.concept_id and r2.invalid_reason is null and r2.relationship_id='Subsumes'
  11.   join concept c3 on c3.concept_id = r2.concept_id_2
  12.   join relationship_to_concept rtc on rtc.concept_code_1 = c3.concept_name
  13.   join concept c4 on c4.concept_id = rtc.concept_id_2
  14.   where c1.concept_class_id='ATC 5th'  and c1.vocabulary_id  = 'ATC'
  15.   and lower(c1.concept_name) ~ ' and '
  16.   and not c2.concept_name ~* ' and |comb| with'-- take narrow ATC4
  17.   and not c3.concept_name ~* ' and |comb| with'
  18.   order by c2.concept_code;
  19.  
  20.   --1.2 ATC 3rd, and
  21.   select
  22.    -- c1.concept_id as atc5_id, c1.concept_name as atc5_name,
  23.     c3.concept_id as atc3_id, c3.concept_name as atc3_name,
  24.     c6.concept_id as rxnorm_id, c6.concept_name as rxnorm_name
  25.   from concept c1
  26.   join concept_relationship r1 on r1.concept_id_1=c1.concept_id and r1.invalid_reason is null and r1.relationship_id='Is a'
  27.   join concept c2 on r1.concept_id_2=c2.concept_id
  28.   join concept_relationship r2 on r2.concept_id_1=c2.concept_id and r2.invalid_reason is null and r2.relationship_id='Is a'
  29.   join concept c3 on r2.concept_id_2=c3.concept_id
  30.   join concept c4 on regexp_replace(c4.concept_code, '.$', '') = c3.concept_code -- siblings
  31.   join concept_relationship r3 on r3.concept_id_1=c4.concept_id and r3.invalid_reason is null and r3.relationship_id='Subsumes' --their descendants
  32.   join concept c5 on r3.concept_id_2=c5.concept_id
  33.   join relationship_to_concept rtc on rtc.concept_code_1 = c6.concept_name -- no need to exclude anything, will join only ingredients
  34.   join concept c6 on c6.concept_id = rtc.concept_id_2
  35.   where c1.concept_class_id='ATC 5th'  and c1.vocabulary_id  = 'ATC' and lower(c1.concept_name) ~ ' and '
  36.   and c2.concept_name ~* ' and |comb| with'
  37.   and not c3.concept_name ~* ' and |comb| with' -- take narrow ATC3
  38.   and not c4.concept_name ~* ' and |comb| with' -- only pure siblings
  39.   and c5.vocabulary_id  = 'ATC'
  40.   order by c1.concept_code
  41.   ;
  42.  
  43.   --1.3 ATC 2rd, and [there are cases where the first ATC group is good and where many are good]
  44.   select distinct
  45.    -- c1.concept_id as atc5_id, c1.concept_name as atc5_name,
  46.     c4.concept_id as atc2_name, c4.concept_name as atc2_name,
  47.     c8.concept_id as rxnorm_id, c8.concept_name as rxnorm_name
  48.   from concept c1
  49.   join concept_relationship r1 on r1.concept_id_1=c1.concept_id and r1.invalid_reason is null and r1.relationship_id='Is a'
  50.   join concept c2 on r1.concept_id_2=c2.concept_id
  51.   join concept_relationship r2 on r2.concept_id_1=c2.concept_id and r2.invalid_reason is null and r2.relationship_id='Is a'
  52.   join concept c3 on r2.concept_id_2=c3.concept_id
  53.   join concept_relationship r3 on r3.concept_id_1=c3.concept_id and r3.invalid_reason is null and r3.relationship_id='Is a'
  54.   join concept c4 on r3.concept_id_2=c4.concept_id
  55.   join concept_relationship r4 on r4.concept_id_1=c4.concept_id and r4.invalid_reason is null and r4.relationship_id='Subsumes' -- back to ATC 3
  56.   join concept c5 on r4.concept_id_2=c5.concept_id
  57.   join concept_relationship r5 on r5.concept_id_1=c5.concept_id and r5.invalid_reason is null and r5.relationship_id='Subsumes' -- back to ATC 4
  58.   join concept c6 on r5.concept_id_2=c6.concept_id
  59.   join concept_relationship r6 on r6.concept_id_1=c6.concept_id and r6.invalid_reason is null and r6.relationship_id='Subsumes' -- back to ATC 5
  60.   join concept c7 on r6.concept_id_2=c7.concept_id
  61.   join relationship_to_concept rtc on rtc.concept_code_1 = c7.concept_name -- no need to exclude anything, will join only ingredients
  62.   join concept c8 on c8.concept_id = rtc.concept_id_2
  63.   where c1.concept_class_id='ATC 5th'  and c1.vocabulary_id  = 'ATC' and lower(c1.concept_name) ~ ' and '
  64.   and c2.concept_name ~* ' and |comb| with'
  65.   and c3.concept_name ~* ' and |comb| with'
  66.   and not c4.concept_name ~* ' and |comb| with' -- take narrow ATC2
  67.   --order by c1.concept_code
  68.   ;
  69.  
  70.   -- 2. ATC for other combinations
  71.   --2.1 ATC 4th
  72.   select
  73.     c1.concept_id as atc5_id, c1.concept_name as atc5_name,
  74.     c2.concept_id as atc4_id, c2.concept_name as atc4_name,
  75.     c4.concept_id as rxnorm_id, c3.concept_name as rxnorm_name
  76.   from concept c1
  77.   join concept_relationship r1 on r1.concept_id_1=c1.concept_id and r1.invalid_reason is null and r1.relationship_id='Is a'
  78.   join concept c2 on r1.concept_id_2=c2.concept_id
  79.   join concept_relationship r2 on r2.concept_id_1=c2.concept_id and r2.invalid_reason is null and r2.relationship_id='Subsumes'
  80.   join concept c3 on r2.concept_id_2=c3.concept_id
  81.   join relationship_to_concept rtc on rtc.concept_code_1 = c3.concept_name -- no need to exclude anything, will join only ingredients
  82.   join concept c4 on c4.concept_id = rtc.concept_id_2
  83.   where c1.concept_class_id='ATC 5th'  and c1.vocabulary_id  = 'ATC'
  84.   and lower(c1.concept_name) ~ 'comb| with '
  85.   and not lower(c2.concept_name) ~ 'comb| with '
  86.   order by c2.concept_code;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement