def parseHL7(hl7_msg=""): """Parses an HL7 message into a JSON-LD document suitable for persisting into MongoDB hl7_msg -- a string containing the HL7 message to be parsed Returns a python dictionary representing the document to be stored """ msg = parse_segments(hl7_msg) MSH = msg[0] PID = msg[1] OBX = msg[2] OBR = msg[3] MRN = PID.pid_3.pid_3_1.to_er7() SEX = PID.pid_8.to_er7() dob = PID.pid_7.to_er7() DOB = str(datetime.strptime(dob, '%Y%m%d').date()) TestValue = OBX.obx_5.to_er7() LOINC_Code = OBR.obr_4.obr_4_1.to_er7() DES = OBR.obr_4.obr_4_2.to_er7() patient = {"resourceType": "Patient", "identifier": [ {"use": "usual", "label": "MRN", "system": "urn:oid:2.16.840.1.113883.19.5", "value": MRN}], "gender": {"coding":[ {"system": "http://hl7.org/fhir/v3/AdministrativeGender", "code":SEX}]}, "birthDate": DOB, "managingOrganization": {"reference": "Organization/2.16.840.1.113883.19.5","display": "MIMIC2"},} observation = {"resourceType": "Observation", "name": { "coding": [ { "system" : "http://loinc.org", "code" : LOINC_Code, "display": DES}]}, "valueQuantity": { "value" : TestValue,}, "issued": "2013-04-03T15:30:10+01:00", "status": "final", "subject": { "reference": "Patient/" + MRN, "display" : "P. van de Heuvel"},} return [patient,observation]
def test_parse_segments(self): msh = 'MSH|^~\&|SEND APP|SEND FAC|REC APP|REC FAC|20080115153000||ADT^A01^ADT_A01|0123456789|P|2.5||||AL\r' pid = 'PID|1||123-456-789^^^HOSPITAL^MR||SURNAME^NAME^A|||M|||1111 SOMEWHERE^^SOMEWHERE^^^USA||555~444|||M' segments_str = msh + pid segments = parse_segments(segments_str) self.assertEqual(msh.replace('\r', ''), segments[0].to_er7()) self.assertEqual(pid, segments[1].to_er7())
def test_parse_segments(self): msh = 'MSH|^~\&|SENDING APP|SENDING FAC|REC APP|REC FAC|20080115153000||ADT^A01^ADT_A01|0123456789|P|2.5||||AL\r' pid = 'PID|1||123-456-789^^^HOSPITAL^MR||SURNAME^NAME^A|||M|||1111 SOMEWHERE STREET^^SOMEWHERE^^^USA||555-555-2004~444-333-222|||M' segments_str = msh+pid segments = parse_segments(segments_str) self.assertEqual(msh.replace('\r',''), segments[0].to_er7()) self.assertEqual(pid, segments[1].to_er7())
def test_wrong_group(self): """ Tests that if there is an unexpected segment the message in not validated The message used has an unexpected OML_O33_PATIENT """ msg = self._create_message(self.adt_a01) oml_o33_patient = Group('OML_O33_PATIENT') segments = parse_segments('PID|||1010110909194822^^^GATEWAY_IL&1.3.6.1.4.1.21367.2011.2.5.17&ISO^PK||PIPPO^PLUTO^^^^^L||19790515|M|||VIA DI TOPOLINO^CAGLIARI^CAGLIARI^^09100^100^H^^092009~^^^^^^L|||||||PPPPPP79E15B354I^^^CF|||||CAGLIARI|||100\rPV1||O|||||||||||||||||1107080001^^^LIS') oml_o33_patient.children = segments msg.add(oml_o33_patient) self.assertFalse(msg.validate())
def test_having_more_groups(self): """ Tests that when a group occurs more than the allowed times the message is not validated The message used has 2 occurrence of oml_o33_group """ msg = self._create_message(self.oml_o33) oml_o33_patient = Group('OML_O33_PATIENT') segments = parse_segments('PID|||1010110909194822^^^GATEWAY_IL&1.3.6.1.4.1.21367.2011.2.5.17&ISO^PK||PIPPO^PLUTO^^^^^L||19790515|M|||VIA DI TOPOLINO^CAGLIARI^CAGLIARI^^09100^100^H^^092009~^^^^^^L|||||||PPPPPP79E15B354I^^^CF|||||CAGLIARI|||100\rPV1||O|||||||||||||||||1107080001^^^LIS') oml_o33_patient.children = segments msg.add(oml_o33_patient) self.assertFalse(msg.validate())
def test_wrong_group(self): """ Tests that if there is an unexpected segment the message in not validated The message used has an unexpected OML_O33_PATIENT """ msg = self._create_message(self.rsp_k21) oml_o33_patient = Group('OML_O33_PATIENT') segments = parse_segments('PID|||1010110909194822^^^GATEWAY_IL&1.3.6.1.4.1.21367.2011.2.5.17&ISO^PK||PIPPO^PLUTO^^^^^L||19790515|M|||VIA DI TOPOLINO^CAGLIARI^CAGLIARI^^09100^100^H^^092009~^^^^^^L|||||||PPPPPP79E15B354I^^^CF|||||CAGLIARI|||100\rPV1||O|||||||||||||||||1107080001^^^LIS') oml_o33_patient.children = segments msg.add(oml_o33_patient) self.assertRaises(ValidationError, msg.validate, report_file=self.report_file) self._test_report_file('ERROR')
def test_having_more_groups(self): """ Tests that when a group occurs more than the allowed times the message is not validated The message used has 2 occurrence of oml_o33_group """ msg = self._create_message(self.oml_o33) oml_o33_patient = Group('OML_O33_PATIENT') segments = parse_segments('PID|||1010110909194822^^^GATEWAY_IL&1.3.6.1.4.1.21367.2011.2.5.17&ISO^PK||PIPPO^PLUTO^^^^^L||19790515|M|||VIA DI TOPOLINO^CAGLIARI^CAGLIARI^^09100^100^H^^092009~^^^^^^L|||||||PPPPPP79E15B354I^^^CF|||||CAGLIARI|||100\rPV1||O|||||||||||||||||1107080001^^^LIS') oml_o33_patient.children = segments msg.add(oml_o33_patient) self.assertRaises(ValidationError, msg.validate, report_file=self.report_file) self._test_report_file('ERROR')