-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AllergyIntolerance support to core study
- Adds core__allergyintolerance - Adds core__count_allergyintolerance_month
- Loading branch information
Showing
33 changed files
with
1,634 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
cumulus_library/studies/core/builder_allergyintolerance.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import cumulus_library | ||
from cumulus_library.studies.core.core_templates import core_templates | ||
from cumulus_library.template_sql import sql_utils | ||
|
||
expected_table_cols = { | ||
"allergyintolerance": { | ||
"id": [], | ||
"type": [], | ||
"category": [], | ||
"criticality": [], | ||
"recordedDate": [], | ||
"patient": sql_utils.REFERENCE, | ||
"encounter": sql_utils.REFERENCE, | ||
"reaction": ["severity"], | ||
} | ||
} | ||
|
||
|
||
class CoreAllergyIntoleranceBuilder(cumulus_library.BaseTableBuilder): | ||
display_text = "Creating AllergyIntolerance tables..." | ||
|
||
def prepare_queries(self, *args, config: cumulus_library.StudyConfig, **kwargs): | ||
code_sources = [ | ||
sql_utils.CodeableConceptConfig( | ||
source_table="allergyintolerance", | ||
column_hierarchy=[("clinicalStatus", dict)], | ||
target_table="core__allergyintolerance_dn_clinical_status", | ||
filter_priority=True, | ||
code_systems=[ | ||
# Restrict to just this required binding system | ||
"http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical", | ||
], | ||
), | ||
sql_utils.CodeableConceptConfig( | ||
source_table="allergyintolerance", | ||
column_hierarchy=[("verificationStatus", dict)], | ||
target_table="core__allergyintolerance_dn_verification_status", | ||
filter_priority=True, | ||
code_systems=[ | ||
# Restrict to just this required binding system | ||
"http://terminology.hl7.org/CodeSystem/allergyintolerance-verification", | ||
], | ||
), | ||
sql_utils.CodeableConceptConfig( | ||
source_table="allergyintolerance", | ||
column_hierarchy=[("code", dict)], | ||
target_table="core__allergyintolerance_dn_code", | ||
), | ||
sql_utils.CodeableConceptConfig( | ||
source_table="allergyintolerance", | ||
column_hierarchy=[("reaction", list), ("substance", dict)], | ||
target_table="core__allergyintolerance_dn_reaction_substance", | ||
expected={"substance": sql_utils.CODEABLE_CONCEPT}, | ||
), | ||
sql_utils.CodeableConceptConfig( | ||
source_table="allergyintolerance", | ||
column_hierarchy=[("reaction", list), ("manifestation", list)], | ||
target_table="core__allergyintolerance_dn_reaction_manifestation", | ||
expected={"manifestation": sql_utils.CODEABLE_CONCEPT}, | ||
), | ||
] | ||
self.queries += sql_utils.denormalize_complex_objects(config.db, code_sources) | ||
validated_schema = sql_utils.validate_schema(config.db, expected_table_cols) | ||
self.queries.append( | ||
core_templates.get_core_template("allergyintolerance", validated_schema) | ||
) |
138 changes: 138 additions & 0 deletions
138
cumulus_library/studies/core/core_templates/allergyintolerance.sql.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{% import 'core_utils.jinja' as utils %} | ||
{% import 'unnest_utils.jinja' as unnest_utils %} | ||
|
||
CREATE TABLE core__allergyintolerance AS | ||
WITH temp_allergyintolerance AS ( | ||
SELECT | ||
{{- | ||
utils.basic_cols( | ||
'allergyintolerance', | ||
'a', | ||
[ | ||
'id', | ||
] | ||
) | ||
}}, | ||
{{- | ||
utils.nullable_cols( | ||
'allergyintolerance', | ||
'a', | ||
[ | ||
'type', | ||
'category', | ||
'criticality', | ||
('patient', 'reference', 'patient_ref'), | ||
('encounter', 'reference', 'encounter_ref'), | ||
], | ||
schema | ||
) | ||
}}, | ||
{{- | ||
utils.date_cols_from_str( | ||
'allergyintolerance', | ||
'a', | ||
['recordedDate'], | ||
schema | ||
) | ||
}}, | ||
{{- | ||
utils.truncate_date_cols( | ||
'allergyintolerance', | ||
'a', | ||
[ | ||
('recordedDate', 'week'), | ||
('recordedDate', 'month'), | ||
('recordedDate', 'year'), | ||
], | ||
schema | ||
) | ||
}} | ||
FROM allergyintolerance AS a | ||
), | ||
|
||
temp_category AS ( | ||
SELECT | ||
a.id, | ||
t.category | ||
FROM | ||
allergyintolerance AS a, | ||
UNNEST(a.category) AS t (category) | ||
), | ||
|
||
flattened_reaction AS ({{ unnest_utils.flatten('allergyintolerance', 'reaction') }}), | ||
|
||
temp_reaction AS ( | ||
SELECT | ||
r.id, | ||
r.row, | ||
dn_subs.code AS substance_code, | ||
dn_subs.system AS substance_system, | ||
dn_subs.display AS substance_display, | ||
dn_man.code AS manifestation_code, | ||
dn_man.system AS manifestation_system, | ||
dn_man.display AS manifestation_display, | ||
{{- | ||
utils.nullable_cols( | ||
'allergyintolerance', | ||
'r', | ||
[ | ||
('reaction', 'severity', 'severity'), | ||
], | ||
schema | ||
) | ||
}} | ||
FROM flattened_reaction AS r | ||
LEFT JOIN core__allergyintolerance_dn_reaction_substance AS dn_subs | ||
ON r.id = dn_subs.id AND r.row = dn_subs.row | ||
LEFT JOIN core__allergyintolerance_dn_reaction_manifestation AS dn_man | ||
ON r.id = dn_man.id AND r.row = dn_man.row | ||
) | ||
|
||
SELECT | ||
ta.id, | ||
CONCAT('AllergyIntolerance/', ta.id) AS allergyintolerance_ref, | ||
|
||
{#- We don't expose system for these next two since we filter | ||
down to a single system in the denormalization table #} | ||
dn_cstat.code AS clinicalStatus_code, | ||
dn_vstat.code AS verificationStatus_code, | ||
|
||
{#- type, category, and criticality are not in US Core. | ||
But they are useful for clinical interpretation. #} | ||
ta.type, | ||
tcat.category, | ||
ta.criticality, | ||
|
||
dn_code.code AS code_code, | ||
dn_code.system AS code_system, | ||
dn_code.display AS code_display, | ||
|
||
ta.patient_ref, | ||
ta.encounter_ref, | ||
|
||
{#- recordedDate is not in US Core. | ||
But it's useful for looking at only a study period of data. #} | ||
ta.recordedDate, | ||
ta.recordedDate_week, | ||
ta.recordedDate_month, | ||
ta.recordedDate_year, | ||
|
||
{#- reaction.substance and reaction.severity are not in US Core. | ||
But they are useful for clinical interpretation. #} | ||
tr.row AS reaction_row, | ||
tr.substance_code AS reaction_substance_code, | ||
tr.substance_system AS reaction_substance_system, | ||
tr.substance_display AS reaction_substance_display, | ||
tr.manifestation_code AS reaction_manifestation_code, | ||
tr.manifestation_system AS reaction_manifestation_system, | ||
tr.manifestation_display AS reaction_manifestation_display, | ||
tr.severity AS reaction_severity | ||
|
||
FROM temp_allergyintolerance AS ta | ||
LEFT JOIN temp_reaction AS tr ON ta.id = tr.id | ||
LEFT JOIN core__allergyintolerance_dn_code AS dn_code ON ta.id = dn_code.id | ||
LEFT JOIN core__allergyintolerance_dn_clinical_status AS dn_cstat ON ta.id = dn_cstat.id | ||
LEFT JOIN core__allergyintolerance_dn_verification_status AS dn_vstat | ||
ON ta.id = dn_vstat.id | ||
LEFT JOIN temp_category AS tcat ON ta.id = tcat.id | ||
WHERE ta.recordedDate BETWEEN DATE('2016-01-01') AND CURRENT_DATE; |
Oops, something went wrong.