From ac63eeb673c410f07699435c8207aeaf586529ea Mon Sep 17 00:00:00 2001 From: "Marc Kohli (shadowdoc)" Date: Tue, 4 Apr 2017 08:55:26 -0700 Subject: [PATCH] first commit for DocumentReference creation --- create_mhd.rb | 104 ++++++++++++++++++++++++++++++ document_reference.json.erb | 122 ++++++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 create_mhd.rb create mode 100644 document_reference.json.erb diff --git a/create_mhd.rb b/create_mhd.rb new file mode 100644 index 00000000..58698cdb --- /dev/null +++ b/create_mhd.rb @@ -0,0 +1,104 @@ +# create_mhd.rb +# This script reads all of the JSON objects contained in the children of the execution directory + +require 'bundler' +require 'yaml' +require 'logger' +require 'erb' +require 'date' +require 'fileutils' + +log = Logger.new(STDOUT) + +DEBUG = true + +Bundler::require + +def generate_id + # Generates a 16 digit random number + Random.new.random_number(9999999999999999) +end + +if ARGV.length < 1 + raise "usage:\n\nruby create_mhd.rb path_for_patient\n\nnote: only a single patient is currently supported" +end + + +# Create directories to hold our objects +patient_root_directory = File.join(Dir.pwd, ARGV[0]) + +FileUtils.mkpath (File.join(patient_root_directory, "DocumentReference")) +FileUtils.mkpath (File.join(patient_root_directory, "DocumentManifest")) + + +# IHE XDS classCode Value Set +report_class_code = {} +report_class_code["system"] = "urn:oid:1.3.6.1.4.1.19376.1.2.6.1" +report_class_code["code"] = "REPORT" +report_class_code["display"] = "Report" + +images_class_code = {} +images_class_code["system"] = "urn:oid:1.3.6.1.4.1.19376.1.2.6.1" +images_class_code["code"] = "IMAGES" +images_class_code["display"] = "Images" + +fhir = {} + +Dir["#{ARGV[0]}/**/*.json"].each do |data| + + log.info("Processing: #{data}\n") + json = JSON.parse(data_string = File.read(data)) + + if fhir[json["resourceType"].to_s].nil? + fhir[json["resourceType"].to_s] = [] + end + + fhir[json["resourceType"].to_s] << json + +end + +if fhir["Patient"].length > 1 + raise "Multiple patients not currenlty supported" +end + + + +# Will use ERB templates to generate DocumentReference resources +# ERB template requires the following instance variables: +# @id - going to be used to uniquely identify this resource +# @div - human readable (html) rendering of the resource +# @patient_id - json +# @diagnostic_report_code + +@patient_id = fhir["Patient"][0]["id"] +@patient_identifier = fhir["Patient"][0]["identifier"].to_json + +fhir["DiagnosticReport"].each do |r| + + @parent_resource_type = r["resourceType"] + + @parent_resource_code = r["code"]["coding"][0].to_json + + @id = generate_id + + @parent_id = r["id"] + + @document_class_code = report_class_code.to_json + + @diagnostic_report_code = r["code"].to_json + + @created = r["issued"] + + b = binding + document_reference_template = ERB.new(File.read("document_reference.json.erb")) + + puts document_reference_template.result(b) if DEBUG + + #TODO - Create @div from the object itself + + mhd_hash = JSON.parse(document_reference_template.result(b)) + + File.write(File.join(patient_root_directory, "DocumentReference/document_reference." + @parent_id + ".json"), JSON.pretty_generate(mhd_hash)) + +end + diff --git a/document_reference.json.erb b/document_reference.json.erb new file mode 100644 index 00000000..82d12ac7 --- /dev/null +++ b/document_reference.json.erb @@ -0,0 +1,122 @@ +{ + "resourceType": "DocumentReference", + "id": "<%= @id %>", + "text": { + "status": "generated", + "div": "<%= @div %>" + }, + "identifier": [ + { + "system": "https://github.com/ImagingInformatics/hackathon-dataset/", + "value": "89573" + } + ], + "subject": { + "reference": "Patient/<%= @patient_id %>" + }, + "type": + <%= @diagnostic_report_code %>, + "class": { + "coding": [ + <%= @document_class_code %> + ] + }, + "author": [ + { + "reference": "Practitioner/siimmd" + }, + { + "reference": "#a2" + } + ], + "custodian": { + "reference": "Organization/siim" + }, + "authenticator": { + "reference": "Practitioner/siimmd" + }, + "created": "<%= @created %>", + "indexed": "<%= @created %>", + "status": "current", + "docStatus": { + "coding": [ + { + "system": "http://hl7.org/fhir/composition-status", + "code": "final", + "display": "Final" + } + ] + }, + "description": "Physical", + "securityLabel": [ + { + "coding": [ + { + "system": "http://hl7.org/fhir/v3/Confidentiality", + "code": "V", + "display": "very restricted" + } + ] + } + ], + "content": [ + { + "attachment": { + "contentType": "application/json-fhir", + "language": "en-US", + "url": "<%= @parent_resource_type + "/" + @parent_id %>" + }, + "format": [ + { + "system": "http://hl7.org/fhir/diagnosticreport.html", + "display": "FHIR DiagnosticReport Specification" + } + ] + } + ], + "context": { + "encounter": { + "reference": "Encounter/xcda" + }, + "event": [ + { + "coding": [ + <%= @parent_resource_code %> + ] + } + ], + "period": { + "start": "2004-12-23T08:00:00+11:00", + "end": "2004-12-23T08:01:00+11:00" + }, + "facilityType": { + "coding": [ + { + "system": "https://loinc.org/document-ontology/", + "code": "Outpatient", + "display": "Outpatient" + } + ] + }, + "practiceSetting": { + "coding": [ + { + "system": "https://loinc.org/document-ontology/", + "code": "Radiology", + "display": "Radiology" + } + ] + }, + "sourcePatientInfo": { + "reference": "Patient/<%= @patient_id %>" + }, + "related": [ + { + "identifier": <%= @patient_identifier %>, + "ref": { + "reference": "Patient/<%= @patient_id %>" + } + } + ] + } +} \ No newline at end of file