From c8fc9d6b254a0a2f5f5fcad26ee3dce6d53efe26 Mon Sep 17 00:00:00 2001 From: Enola Knezevic Date: Wed, 30 Oct 2024 18:01:36 +0100 Subject: [PATCH] fixed cql generation filter string --- resources/test/result_current.cql | 89 +++++++++++++++++++++++++++++++ src/cql.rs | 16 +++--- 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 resources/test/result_current.cql diff --git a/resources/test/result_current.cql b/resources/test/result_current.cql new file mode 100644 index 0000000..ca99e6a --- /dev/null +++ b/resources/test/result_current.cql @@ -0,0 +1,89 @@ +library Retrieve +using FHIR version '4.0.0' +include FHIRHelpers version '4.0.0' + +codesystem icd10: 'http://hl7.org/fhir/sid/icd-10' +codesystem SampleMaterialType: 'https://fhir.bbmri.de/CodeSystem/SampleMaterialType' +codesystem icd10gm: 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' +codesystem icd10gmnew: 'http://fhir.de/CodeSystem/bfarm/icd-10-gm' +codesystem StorageTemperature: 'https://fhir.bbmri.de/CodeSystem/StorageTemperature' + + +context Patient + +define AgeClass: +if (Patient.birthDate is null) then 'unknown' else ToString((AgeInYears() div 10) * 10) + +define Gender: +if (Patient.gender is null) then 'unknown' else Patient.gender + +define Custodian: + First(from Specimen.extension E + where E.url = 'https://fhir.bbmri.de/StructureDefinition/Custodian' + return (E.value as Reference).identifier.value) + +define function SampleType(specimen FHIR.Specimen): + case FHIRHelpers.ToCode(specimen.type.coding.where(system = 'https://fhir.bbmri.de/CodeSystem/SampleMaterialType').first()) + when Code 'plasma-edta' from SampleMaterialType then 'blood-plasma' + when Code 'plasma-citrat' from SampleMaterialType then 'blood-plasma' + when Code 'plasma-heparin' from SampleMaterialType then 'blood-plasma' + when Code 'plasma-cell-free' from SampleMaterialType then 'blood-plasma' + when Code 'plasma-other' from SampleMaterialType then 'blood-plasma' + when Code 'plasma' from SampleMaterialType then 'blood-plasma' + when Code 'tissue-formalin' from SampleMaterialType then 'tissue-ffpe' + when Code 'tumor-tissue-ffpe' from SampleMaterialType then 'tissue-ffpe' + when Code 'normal-tissue-ffpe' from SampleMaterialType then 'tissue-ffpe' + when Code 'other-tissue-ffpe' from SampleMaterialType then 'tissue-ffpe' + when Code 'tumor-tissue-frozen' from SampleMaterialType then 'tissue-frozen' + when Code 'normal-tissue-frozen' from SampleMaterialType then 'tissue-frozen' + when Code 'other-tissue-frozen' from SampleMaterialType then 'tissue-frozen' + when Code 'tissue-paxgene-or-else' from SampleMaterialType then 'tissue-other' + when Code 'derivative' from SampleMaterialType then 'derivative-other' + when Code 'liquid' from SampleMaterialType then 'liquid-other' + when Code 'tissue' from SampleMaterialType then 'tissue-other' + when Code 'serum' from SampleMaterialType then 'blood-serum' + when Code 'cf-dna' from SampleMaterialType then 'dna' + when Code 'g-dna' from SampleMaterialType then 'dna' + when Code 'blood-plasma' from SampleMaterialType then 'blood-plasma' + when Code 'tissue-ffpe' from SampleMaterialType then 'tissue-ffpe' + when Code 'tissue-frozen' from SampleMaterialType then 'tissue-frozen' + when Code 'tissue-other' from SampleMaterialType then 'tissue-other' + when Code 'derivative-other' from SampleMaterialType then 'derivative-other' + when Code 'liquid-other' from SampleMaterialType then 'liquid-other' + when Code 'blood-serum' from SampleMaterialType then 'blood-serum' + when Code 'dna' from SampleMaterialType then 'dna' + when Code 'buffy-coat' from SampleMaterialType then 'buffy-coat' + when Code 'urine' from SampleMaterialType then 'urine' + when Code 'ascites' from SampleMaterialType then 'ascites' + when Code 'saliva' from SampleMaterialType then 'saliva' + when Code 'csf-liquor' from SampleMaterialType then 'csf-liquor' + when Code 'bone-marrow' from SampleMaterialType then 'bone-marrow' + when Code 'peripheral-blood-cells-vital' from SampleMaterialType then 'peripheral-blood-cells-vital' + when Code 'stool-faeces' from SampleMaterialType then 'stool-faeces' + when Code 'rna' from SampleMaterialType then 'rna' + when Code 'whole-blood' from SampleMaterialType then 'whole-blood' + when Code 'swab' from SampleMaterialType then 'swab' + when Code 'dried-whole-blood' from SampleMaterialType then 'dried-whole-blood' + when null then 'Unknown' + else 'Unknown' + end +define Specimen: + if InInitialPopulation then [Specimen] S where (((((FHIRHelpers.ToDateTime(S.collection.collected) between @1900-01-01 and @2024-10-25) )) and (((((S.extension.where(url='https://fhir.bbmri.de/StructureDefinition/StorageTemperature').value.coding.code contains 'temperature2to10'))))))) else {} as List + +define Diagnosis: +if InInitialPopulation then [Condition] else {} as List + +define function DiagnosisCode(condition FHIR.Condition): +condition.code.coding.where(system = 'http://fhir.de/CodeSystem/bfarm/icd-10-gm').code.first() + +define function DiagnosisCode(condition FHIR.Condition, specimen FHIR.Specimen): +Coalesce( + condition.code.coding.where(system = 'http://hl7.org/fhir/sid/icd-10').code.first(), + condition.code.coding.where(system = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm').code.first(), + condition.code.coding.where(system = 'http://fhir.de/CodeSystem/bfarm/icd-10-gm').code.first(), + specimen.extension.where(url='https://fhir.bbmri.de/StructureDefinition/SampleDiagnosis').value.coding.code.first() + ) + +define InInitialPopulation: +((((((Patient.gender = 'male')))) and ((((((exists[Condition: Code 'C61' from icd10]) or (exists[Condition: Code 'C61' from icd10gm]) or (exists[Condition: Code 'C61' from icd10gmnew])) or (exists from [Specimen] S where (S.extension.where(url='https://fhir.bbmri.de/StructureDefinition/SampleDiagnosis').value.coding.code contains 'C61')))))) and (( AgeInYears() between Ceiling(10) and Ceiling(90)))) or (((exists from [Specimen] S +where FHIRHelpers.ToDateTime(S.collection.collected) between @1900-01-01 and @2024-10-25 )) and ((((exists from [Specimen] S where (S.extension.where(url='https://fhir.bbmri.de/StructureDefinition/StorageTemperature').value.coding contains Code 'temperature2to10' from StorageTemperature) )))))) \ No newline at end of file diff --git a/src/cql.rs b/src/cql.rs index 76cec5f..fc9f2b2 100644 --- a/src/cql.rs +++ b/src/cql.rs @@ -346,13 +346,9 @@ pub fn process( } } } - let position = filter_cond.rfind(')'); - match position{ - None => {} - Some(pos) => { - _ = filter_cond.split_off(pos); - } - }; + if let Some(pos) = filter_cond.rfind(')') { + _ = filter_cond.split_off(pos + 1); + } } } @@ -404,7 +400,6 @@ mod test { } #[test] - #[ignore] #[cfg(feature = "bbmri")] fn test_bbmri() { use crate::projects::{self, bbmri::Bbmri}; @@ -449,7 +444,10 @@ mod test { include_str!("../resources/test/result_empty.cql").to_string() ); - println!("{}",generate_cql(serde_json::from_str(CURRENT).unwrap()).unwrap()); + pretty_assertions::assert_eq!( + generate_cql(serde_json::from_str(CURRENT).unwrap()).unwrap(), + include_str!("../resources/test/result_current.cql").to_string() + ); }