001package org.hl7.fhir.r4.importers; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r4.importers 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023/** 024 * Development Notes: 025 * - ignore ICUStays and Transfers for now - low yield, based on discussons with Tom/Alistair 026 */ 027 028import java.io.FileInputStream; 029import java.io.FileNotFoundException; 030import java.io.FileOutputStream; 031import java.io.IOException; 032import java.math.BigDecimal; 033import java.nio.charset.Charset; 034import java.text.DateFormat; 035import java.text.ParseException; 036import java.text.SimpleDateFormat; 037import java.util.Date; 038import java.util.HashMap; 039import java.util.HashSet; 040import java.util.Map; 041import java.util.Set; 042import java.util.UUID; 043 044import org.fhir.ucum.UcumEssenceService; 045import org.fhir.ucum.UcumException; 046import org.fhir.ucum.UcumService; 047import org.hl7.fhir.exceptions.FHIRException; 048import org.hl7.fhir.r4.formats.IParser.OutputStyle; 049import org.hl7.fhir.r4.formats.JsonParser; 050import org.hl7.fhir.r4.model.Bundle; 051import org.hl7.fhir.r4.model.Bundle.BundleType; 052import org.hl7.fhir.r4.model.CodeableConcept; 053import org.hl7.fhir.r4.model.Coding; 054import org.hl7.fhir.r4.model.Condition; 055import org.hl7.fhir.r4.model.DateTimeType; 056import org.hl7.fhir.r4.model.DateType; 057import org.hl7.fhir.r4.model.DocumentReference; 058import org.hl7.fhir.r4.model.Encounter; 059import org.hl7.fhir.r4.model.Enumerations.AdministrativeGender; 060import org.hl7.fhir.r4.model.Enumerations.DocumentReferenceStatus; 061import org.hl7.fhir.r4.model.InstantType; 062import org.hl7.fhir.r4.model.MedicationRequest; 063import org.hl7.fhir.r4.model.MedicationRequest.MedicationRequestStatus; 064import org.hl7.fhir.r4.model.Observation; 065import org.hl7.fhir.r4.model.Observation.ObservationComponentComponent; 066import org.hl7.fhir.r4.model.Observation.ObservationStatus; 067import org.hl7.fhir.r4.model.Patient; 068import org.hl7.fhir.r4.model.Practitioner; 069import org.hl7.fhir.r4.model.PractitionerRole; 070import org.hl7.fhir.r4.model.Procedure; 071import org.hl7.fhir.r4.model.Procedure.ProcedureStatus; 072import org.hl7.fhir.r4.model.Quantity; 073import org.hl7.fhir.r4.model.Quantity.QuantityComparator; 074import org.hl7.fhir.r4.model.Range; 075import org.hl7.fhir.r4.model.Reference; 076import org.hl7.fhir.r4.model.Type; 077import org.hl7.fhir.utilities.CSVReader; 078import org.hl7.fhir.utilities.IniFile; 079import org.hl7.fhir.utilities.Utilities; 080 081public class Mimic14Importer { 082 083 public static class LabItem { 084 private int rowId; 085 private int itemId; 086 private String label; 087 private String fluid; 088 private String category; 089 private String loinc; 090 } 091 092 public static class Item { 093 private int rowId; 094 private int itemId; 095 private String label; 096 private String abbreviation; 097 private String dbSource; 098 private String linksTo; // where it's used... 099 private String category; 100 private String unitName; // uom for item, if it has one 101 private String paramType; // type of item 102 private int conceptId; 103 } 104 105 private static final String MRN_SYSTEM = null; 106 private Date date; 107 private Map<String, LabItem> labItems = new HashMap<>(); 108 private Map<String, Item> items = new HashMap<>(); 109 private Map<String, PractitionerRole> careGivers = new HashMap<>(); 110 private Map<String, Patient> patients = new HashMap<>(); 111 private Map<String, Encounter> encounters = new HashMap<>(); 112 private IniFile ini; 113 114 private UcumService ucum; 115 116 public static void main(String[] args) throws IOException, UcumException { 117 new Mimic14Importer().execute(args[0], args[1], args[2]); 118 } 119 120 public Mimic14Importer() { 121 super(); 122 date = new Date(); 123 } 124 125 126 private void execute(String src, String dest, String ucumSrc) throws IOException, UcumException { 127 System.out.println("Loading UCUM from "+ucumSrc); 128 129 ucum = new UcumEssenceService(ucumSrc); 130 ini = new IniFile(Utilities.path(src, "translations.ini")); 131 132 loadItems(Utilities.path(src, "d_items.csv")); 133 loadLabItems(Utilities.path(src, "d_labitems.csv")); 134 loadCareGivers(Utilities.path(src, "caregivers.csv"), Utilities.path(dest, "care-givers.json")); 135 136 Bundle patients = processPatients(Utilities.path(src, "patients.csv")); 137 Bundle encounters = processAdmissions(Utilities.path(src, "admissions.csv")); 138 processLabEvents(Utilities.path(src, "labevents.csv"), Utilities.path(dest, "lab-observations.json")); 139 processMicroEvents(Utilities.path(src, "microbiologyevents.csv"), Utilities.path(dest, "micro-observations.json")); 140 processNoteEvents(Utilities.path(src, "noteevents.csv"), Utilities.path(dest, "notes.json")); 141 processDiagnoses(Utilities.path(src, "diagnoses_icd.csv"), Utilities.path(dest, "diagnoses.json")); 142 processProcedures(Utilities.path(src, "procedures_icd.csv"), Utilities.path(dest, "procedures.json")); 143 processPrescriptions(Utilities.path(src, "prescriptions.csv"), Utilities.path(dest, "prescriptions.json")); 144 processProcedureEvents(Utilities.path(src, "procedureevents_mv.csv"), Utilities.path(dest, "procedure-events.json")); 145 processOutputEvents(Utilities.path(src, "outputevents.csv"), Utilities.path(dest, "outputs.json")); 146 processChartEvents(Utilities.path(src, "chartevents.csv"), Utilities.path(dest, "chartevents.json")); 147 148 System.out.println("saving"); 149 150 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "patients.json")), patients); 151 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "encounters.json")), encounters); 152 153 System.out.println("done"); 154 } 155 156 157 private void loadItems(String src) throws NumberFormatException, FHIRException, IOException { 158 System.out.print("Processing Items... "); 159 CSVReader csv = new CSVReader(new FileInputStream(src)); 160 int t = 0; 161 csv.readHeaders(); 162 while (csv.line()) { 163 Item item = new Item(); 164 t++; 165 item.rowId = Integer.parseInt(csv.cell("row_id")); 166 item.itemId = Integer.parseInt(csv.cell("itemid")); 167 item.label = csv.cell("label"); 168 item.abbreviation = csv.cell("abbreviation"); 169 item.dbSource = csv.cell("dbsource"); 170 item.linksTo = csv.cell("linksto"); 171 item.category = csv.cell("category"); 172 item.unitName = csv.cell("unitname"); 173 item.paramType = csv.cell("param_type"); 174 if (csv.has("conceptid")) { 175 item.conceptId = Integer.parseInt(csv.cell("conceptid")); 176 } 177 items.put(csv.cell("itemid"), item); 178 } 179 System.out.println(Integer.toString(t)+" found"); 180 csv.close(); 181 } 182 183 private void loadLabItems(String src) throws NumberFormatException, FHIRException, IOException { 184 System.out.print("Processing Lab Items... "); 185 CSVReader csv = new CSVReader(new FileInputStream(src)); 186 int t = 0; 187 csv.readHeaders(); 188 while (csv.line()) { 189 LabItem item = new LabItem(); 190 t++; 191 item.rowId = Integer.parseInt(csv.cell("row_id")); 192 item.itemId = Integer.parseInt(csv.cell("itemid")); 193 item.label = csv.cell("label"); 194 item.fluid = csv.cell("fluid"); 195 item.category = csv.cell("category"); 196 item.loinc = csv.cell("loinc_code"); 197 labItems.put(csv.cell("itemid"), item); 198 } 199 System.out.println(Integer.toString(t)+" found"); 200 csv.close(); 201 } 202 203 private void loadCareGivers(String src, String dest) throws NumberFormatException, FHIRException, IOException { 204 System.out.print("Processing Care Givers... "); 205 CSVReader csv = new CSVReader(new FileInputStream(src)); 206 int t = 0; 207 csv.readHeaders(); 208 Bundle bnd = new Bundle(); 209 bnd.setId("care-givers"); 210 bnd.setType(BundleType.COLLECTION); 211 bnd.setTimestamp(date); 212 while (csv.line()) { 213 PractitionerRole pr = new PractitionerRole(); 214 t++; 215 pr.setId(csv.cell("cgid")); 216 pr.addCode().addCoding().setSystem("http://mimic.physionet.org/fhir/CareGiverType").setCode(csv.cell("label")).setDisplay(csv.cell("description")); 217 careGivers.put(pr.getId(), pr); 218 bnd.addEntry().setResource(pr); 219 } 220 System.out.println(Integer.toString(t)+" found"); 221 csv.close(); 222 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 223 } 224 225 private void processLabEvents(String src, String dest) throws FileNotFoundException, IOException { 226 System.out.print("Processing Lab Events... "); 227 CSVReader csv = new CSVReader(new FileInputStream(src)); 228 Bundle bnd = new Bundle(); 229 bnd.setId("lab-observations"); 230 bnd.setType(BundleType.COLLECTION); 231 bnd.setTimestamp(date); 232 csv.readHeaders(); 233 int t = 0; 234 while (csv.line()) { 235 Observation obs = new Observation(); 236 t++; 237 238 Patient pat = patients.get(csv.cell("subject_id")); 239 Encounter enc = encounters.get(csv.cell("hadm_id")); 240 LabItem item = labItems.get(csv.cell("itemid")); 241 obs.setId(csv.cell("row_id")); 242 obs.setStatus(ObservationStatus.FINAL); 243 if (pat != null) { 244 obs.setSubject(new Reference("Patient/"+pat.getId())); 245 } 246 if (enc != null) { 247 obs.setEncounter(new Reference("Encounter/"+enc.getId())); 248 } 249 obs.setEffective(readDateTime(csv.cell("charttime"))); 250 if (item != null) { 251 if (!Utilities.noString(item.category)) { 252 obs.addCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode(item.category); 253 } 254 obs.getCode().setText(item.label); 255 if (!Utilities.noString(item.fluid)) { 256 obs.getSpecimen().setDisplay(item.fluid); 257 } 258 if (!Utilities.noString(item.loinc)) { 259 obs.getCode().addCoding().setSystem("http://loinc.org").setCode(item.loinc); 260 } 261 } 262 263 if (csv.has("valuenum")) { 264 Quantity qty = parseQuantity(csv.cell("valuenum"), "valueuom"); 265 obs.setValue(qty); 266 } else { 267 obs.setValue(new CodeableConcept().setText(csv.cell("value"))); 268 } 269 if (csv.has("flag")) 270 obs.addInterpretation().setText(csv.cell("flag")); 271 272 bnd.addEntry().setResource(obs); 273 } 274 System.out.println(Integer.toString(t)+" found"); 275 csv.close(); 276 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 277 } 278 279 /** 280 * loinc COE 600-7 DATE TIME + CODE FOR OBSERVED - UPCODE TO snomed? 281 * 282 * Sensitivity = observation panel code of 283 * code = 284 * @param src 285 * @param dest 286 * @throws FileNotFoundException 287 * @throws IOException 288 */ 289 private void processMicroEvents(String src, String dest) throws FileNotFoundException, IOException { 290 System.out.print("Processing Micro Events... "); 291 CSVReader csv = new CSVReader(new FileInputStream(src)); 292 Bundle bnd = new Bundle(); 293 bnd.setId("lab-observations"); 294 bnd.setType(BundleType.COLLECTION); 295 bnd.setTimestamp(date); 296 csv.readHeaders(); 297 Map<String, Observation> cache = new HashMap<>(); 298 299 int t = 0; 300 while (csv.line()) { 301 String cacheId = csv.cell("hadm_id")+"|"+csv.cell("spec_itemid")+"|"+csv.cell("org_itemid")+"|"+(csv.has("charttime") ? csv.cell("charttime") : csv.cell("chartdate"))+"|"+csv.cell("isolate_num"); 302 303 Patient pat = patients.get(csv.cell("subject_id")); 304 Encounter enc = encounters.get(csv.cell("hadm_id")); 305 Item spec = items.get(csv.cell("spec_itemid")); 306 Item org = items.get(csv.cell("org_itemid")); 307 Item ab = items.get(csv.cell("ab_itemid")); 308 309 Observation obs; 310 boolean isCulture = true; 311 if (cache.containsKey(cacheId)) { 312 obs = cache.get(cacheId); 313 } else { 314 obs = new Observation(); 315 t++; 316 cache.put(cacheId, obs); 317 obs.setId(csv.cell("row_id")); 318 obs.setStatus(ObservationStatus.FINAL); 319 obs.addCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode("microbiology"); 320 bnd.addEntry().setResource(obs); 321 322 323 // todo: these are all cultures, but the codes state mainly what the culture is on 324 obs.getCode().setText(csv.cell("spec_type_desc")); 325 if ("70012".equals(csv.cell("spec_itemid"))) { 326 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("600-7").setDisplay("Bacteria identified in Blood by Culture"); 327 } else if ("70079".equals(csv.cell("spec_itemid"))) { 328 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("630-4").setDisplay("Bacteria identified in Urine by Culture"); 329 } else if ("70064".equals(csv.cell("spec_itemid"))) { 330 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("625-4").setDisplay("Bacteria identified in Stool by Culture"); 331 } else if ("70005".equals(csv.cell("spec_itemid"))) { 332 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("43411-8").setDisplay("Bacteria identified in Aspirate by Culture"); 333 } else if ("70045".equals(csv.cell("spec_itemid"))) { 334 isCulture = false; 335 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("16718-9").setDisplay("Cytomegalovirus Ag [Presence] in Blood"); 336 } else if ("70088".equals(csv.cell("spec_itemid"))) { 337 isCulture = false; 338 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("5002-1").setDisplay("Epstein Barr virus DNA [Presence] in Blood by NAA with probe detection"); 339 } else if ("70087".equals(csv.cell("spec_itemid"))) { 340 isCulture = false; 341 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("16712-2").setDisplay("Cytomegalovirus Ab [Units/volume] in Body fluid"); 342 } else if ("70093".equals(csv.cell("spec_itemid"))) { 343 isCulture = false; 344 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("49447-6").setDisplay("Toxoplasma gondii DNA [#/volume] in Blood by NAA with probe detection"); 345 } else if ("70053".equals(csv.cell("spec_itemid"))) { 346 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("619-7").setDisplay("Bacteria identified in Peritoneal fluid by Culture"); 347// } else if ("70013".equals(csv.cell("spec_itemid"))) { 348// obs.getCode().addCoding().setSystem("http://loinc.org").setCode().setDisplay(); 349 } else if ("70023".equals(csv.cell("spec_itemid"))) { 350 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("19128-8").setDisplay("Bacteria identified in Catheter tip by Culture"); 351 } else if ("70046".equals(csv.cell("spec_itemid"))) { 352 isCulture = false; 353 obs.getCode().setText("HIV-1 Viral Load/Ultrasensitive"); 354 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("20447-9").setDisplay("HIV 1 RNA [#/volume] (viral load) in Serum or Plasma by NAA with probe detection"); 355 } else if ("70051".equals(csv.cell("spec_itemid"))) { 356 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6463-4").setDisplay("Bacteria identified in Unspecified specimen by Culture"); 357 } else if ("70069".equals(csv.cell("spec_itemid"))) { 358 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6463-4").setDisplay("Bacteria identified in Unspecified specimen by Culture"); 359 } else if ("70040".equals(csv.cell("spec_itemid"))) { 360 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6463-4").setDisplay("Bacteria identified in Unspecified specimen by Culture"); 361 } else if ("70057".equals(csv.cell("spec_itemid"))) { 362 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6463-4").setDisplay("Bacteria identified in Unspecified specimen by Culture"); 363 } else if ("70027".equals(csv.cell("spec_itemid"))) { 364 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("88142-5").setDisplay("Bacteria identified in Cornea or Conjunctiva by Aerobe culture"); 365 } else if ("70062".equals(csv.cell("spec_itemid"))) { 366 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6460-0").setDisplay("Bacteria identified in Sputum by Culture"); 367 } else if ("70054".equals(csv.cell("spec_itemid"))) { 368 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("618-9").setDisplay("Bacteria identified in Pleural fluid by Culture"); 369 } else if ("70067".equals(csv.cell("spec_itemid"))) { 370 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6462-6").setDisplay("Bacteria identified in Wound by Culture"); 371 } else if ("70028".equals(csv.cell("spec_itemid"))) { 372 isCulture = false; 373 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("62873-5").setDisplay("PhenX - assay herpes simplex virus types 1 - 2 protocol"); 374 } else if ("70070".equals(csv.cell("spec_itemid"))) { // check... 375 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6462-6").setDisplay("Bacteria identified in Wound by Culture"); 376 } else if ("70091".equals(csv.cell("spec_itemid"))) { // check... 377 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("13317-3").setDisplay("Methicillin resistant Staphylococcus aureus [Presence] in Unspecified specimen by Organism specific culture"); 378 } else if ("70061".equals(csv.cell("spec_itemid"))) { // check... 379 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("620-5").setDisplay("Bacteria identified in Skin by Aerobe culture"); 380 } else if ("70030".equals(csv.cell("spec_itemid"))) { // check... 381 isCulture = false; 382 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("31982-2").setDisplay("Varicella zoster virus Ag [Presence] in Unspecified specimen"); 383 } else if ("70013".equals(csv.cell("spec_itemid"))) { // check... 384 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("611-4").setDisplay("Bacteria identified in Body fluid by Culture"); 385 } else if ("70011".equals(csv.cell("spec_itemid"))) { // check... 386 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("533-0").setDisplay("Mycobacterium sp identified in Blood by Organism specific culture"); 387 } else if ("70009".equals(csv.cell("spec_itemid"))) { // check... 388 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("53911-4").setDisplay("Bacteria identified in Bile fluid by Culture"); 389 } else if ("70019".equals(csv.cell("spec_itemid"))) { // check... 390 isCulture = false; 391 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("50659-2").setDisplay("Chromosome analysis.interphase [Interpretation] in Bone marrow by FISH Narrative"); 392 } else if ("70026".equals(csv.cell("spec_itemid"))) { // check... 393 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("606-4").setDisplay("Bacteria identified in Cerebral spinal fluid by Culture"); 394 } else if ("70081".equals(csv.cell("spec_itemid"))) { // check... 395 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("630-4").setDisplay("Bacteria identified in Urine by Culture"); 396 } else if ("70075".equals(csv.cell("spec_itemid"))) { // check... 397 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("626-2").setDisplay("Bacteria identified in Throat by Culture"); 398 } else if ("70069".equals(csv.cell("spec_itemid"))) { // check... 399 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("626-2").setDisplay("Bacteria identified in Throat by Culture"); 400 } else if ("70021".equals(csv.cell("spec_itemid"))) { // check... 401 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("88683-8").setDisplay("Bacteria identified in Bronchoalveolar lavage by Anaerobe culture"); 402 } else if ("70076".equals(csv.cell("spec_itemid"))) { // check... 403 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("43408-4").setDisplay("Bacteria identified in Tissue by Culture"); 404 } else if ("70037".equals(csv.cell("spec_itemid"))) { // check... 405 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("43408-4").setDisplay("Bacteria identified in Tissue by Culture"); 406 } else if ("70090".equals(csv.cell("spec_itemid"))) { // check... 407 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("88683-8").setDisplay("Bacteria identified in Bronchoalveolar lavage by Anaerobe culture"); 408 } else if ("70047".equals(csv.cell("spec_itemid"))) { // check... 409 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("621-3").setDisplay("Bacteria identified in Synovial fluid by Culture"); 410 } else if ("70017".equals(csv.cell("spec_itemid"))) { // check... 411 isCulture = false; 412 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("56874-1").setDisplay("Serology and blood bank studies (set)"); 413 } else if ("70042".equals(csv.cell("spec_itemid"))) { // check... 414 isCulture = false; 415 obs.getCode().addCoding().setSystem("http://loinc.org").setCode("6438-6").setDisplay("Influenza virus A+B Ag [Presence] in Unspecified specimen by Immunofluorescence"); 416 } else 417 throw new Error("Not coded yet: "+csv.cell("spec_itemid")); 418 // obs.getCode().addCoding().setSystem("http://mimic.physionet.org/fhir/TestType").setCode(csv.cell("spec_itemid")); 419 } 420 421 if (pat != null) { 422 obs.setSubject(new Reference("Patient/"+pat.getId())); 423 } 424 if (enc != null) { 425 obs.setEncounter(new Reference("Encounter/"+enc.getId())); 426 } 427 428 if (csv.has("charttime")) { 429 obs.setEffective(readDateTime(csv.cell("charttime"))); 430 } else { 431 obs.setEffective(readDateTime(csv.cell("chartdate"))); 432 } 433 if (org == null) { 434 obs.setValue(new CodeableConcept(new Coding().setSystem("http://mimic.physionet.org/fhir/TestValue").setCode("negative"))); 435 } else { 436 obs.setValue(new CodeableConcept(new Coding().setSystem("http://mimic.physionet.org/fhir/Organism").setCode(csv.cell("org_itemid"))).setText(csv.cell("org_name"))); 437 } 438 // todo: make this members not components 439 if (isCulture && ab != null) { 440 ObservationComponentComponent oc = obs.addComponent(); 441 if ("90015".equals(csv.cell("ab_itemid"))) { 442 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("19000-9").setDisplay("Vancomycin [Susceptibility]")).setText(csv.cell("ab_name"))); 443 } else if ("90012".equals(csv.cell("ab_itemid"))) { 444 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18928-2").setDisplay("Gentamicin [Susceptibility]")).setText(csv.cell("ab_name"))); 445 } else if ("90025".equals(csv.cell("ab_itemid"))) { 446 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("20629-2").setDisplay("levoFLOXacin [Susceptibility]")).setText(csv.cell("ab_name"))); 447 } else if ("90016".equals(csv.cell("ab_itemid"))) { 448 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18961-3").setDisplay("Oxacillin [Susceptibility]")).setText(csv.cell("ab_name"))); 449 } else if ("90011".equals(csv.cell("ab_itemid"))) { 450 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18993-6").setDisplay("Tetracycline [Susceptibility]")).setText(csv.cell("ab_name"))); 451 } else if ("90006".equals(csv.cell("ab_itemid"))) { 452 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18919-1").setDisplay("Erythromycin [Susceptibility]")).setText(csv.cell("ab_name"))); 453 } else if ("90002".equals(csv.cell("ab_itemid"))) { 454 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18964-7").setDisplay("Penicillin [Susceptibility]")).setText(csv.cell("ab_name"))); 455 } else if ("90004".equals(csv.cell("ab_itemid"))) { 456 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18864-9").setDisplay("Ampicillin [Susceptibility]")).setText(csv.cell("ab_name"))); 457 } else if ("90005".equals(csv.cell("ab_itemid"))) { 458 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18878-9").setDisplay("ceFAZolin [Susceptibility]")).setText(csv.cell("ab_name"))); 459 } else if ("90007".equals(csv.cell("ab_itemid"))) { 460 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18908-4").setDisplay("Clindamycin [Susceptibility]")).setText(csv.cell("ab_name"))); 461 } else if ("90008".equals(csv.cell("ab_itemid"))) { 462 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18997-7").setDisplay("Trimethoprim [Susceptibility]")).setText(csv.cell("ab_name"))); 463 } else if ("90010".equals(csv.cell("ab_itemid"))) { 464 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18955-5").setDisplay("Nitrofurantoin [Susceptibility]")).setText(csv.cell("ab_name"))); 465 } else if ("90020".equals(csv.cell("ab_itemid"))) { 466 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18932-4").setDisplay("Imipenem [Susceptibility]")).setText(csv.cell("ab_name"))); 467 } else if ("90021".equals(csv.cell("ab_itemid"))) { 468 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18969-6").setDisplay("Piperacillin [Susceptibility]")).setText(csv.cell("ab_name"))); 469 } else if ("90013".equals(csv.cell("ab_itemid"))) { 470 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18996-9").setDisplay("Tobramycin [Susceptibility]")).setText(csv.cell("ab_name"))); 471 } else if ("90017".equals(csv.cell("ab_itemid"))) { 472 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18893-8").setDisplay("cefTAZidime [Susceptibility]")).setText(csv.cell("ab_name"))); 473 } else if ("90018".equals(csv.cell("ab_itemid"))) { 474 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18895-3").setDisplay("cefTRIAXone [Susceptibility]")).setText(csv.cell("ab_name"))); 475 } else if ("90019".equals(csv.cell("ab_itemid"))) { 476 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18906-8").setDisplay("Ciprofloxacin [Susceptibility]")).setText(csv.cell("ab_name"))); 477 } else if ("90022".equals(csv.cell("ab_itemid"))) { 478 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18865-6").setDisplay("Ampicillin+Sulbactam [Susceptibility]")).setText(csv.cell("ab_name"))); 479 } else if ("90023".equals(csv.cell("ab_itemid"))) { 480 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("51724-3").setDisplay("Cefuroxime [Susceptibility]")).setText(csv.cell("ab_name"))); 481 } else if ("90026".equals(csv.cell("ab_itemid"))) { 482 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18969-6").setDisplay("Piperacillin [Susceptibility]")).setText(csv.cell("ab_name"))); 483 } else if ("90028".equals(csv.cell("ab_itemid"))) { 484 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18879-7").setDisplay("Cefepime [Susceptibility]")).setText(csv.cell("ab_name"))); 485 } else if ("90029".equals(csv.cell("ab_itemid"))) { 486 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18943-1").setDisplay("Meropenem [Susceptibility]")).setText(csv.cell("ab_name"))); 487 } else if ("90031".equals(csv.cell("ab_itemid"))) { 488 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("29258-1").setDisplay("Linezolid [Susceptibility]")).setText(csv.cell("ab_name"))); 489 } else if ("90027".equals(csv.cell("ab_itemid"))) { 490 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18974-6").setDisplay("rifAMPin [Susceptibility]")).setText(csv.cell("ab_name"))); 491 } else if ("90009".equals(csv.cell("ab_itemid"))) { 492 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18903-5").setDisplay("Chloramphenicol [Susceptibility]")).setText(csv.cell("ab_name"))); 493 } else if ("90003".equals(csv.cell("ab_itemid"))) { 494 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18965-4").setDisplay("Penicillin G [Susceptibility]")).setText(csv.cell("ab_name"))); 495 } else if ("90014".equals(csv.cell("ab_itemid"))) { 496 oc.setCode(new CodeableConcept(new Coding().setSystem("http://loinc.org").setCode("18860-7").setDisplay("Amikacin [Susceptibility]")).setText(csv.cell("ab_name"))); 497 } else 498 throw new Error("Not coded yet: "+csv.cell("ab_itemid")); 499 if (csv.has("dilution_text")) { 500 oc.setValue(parseQuantity(csv.cell("dilution_text"))); 501 } 502 if (csv.has("interpretation")) { 503 oc.addInterpretation().addCoding().setSystem("http://mimic.physionet.org/fhir/Interpretation").setCode(csv.cell("interpretation")); 504 } 505 } 506 } 507 System.out.println(Integer.toString(t)+" found"); 508 csv.close(); 509 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 510 } 511 512 private void processNoteEvents(String src, String dest) throws FileNotFoundException, IOException { 513 System.out.print("Processing Note Events... "); 514 CSVReader csv = new CSVReader(new FileInputStream(src)); 515 Bundle bnd = new Bundle(); 516 bnd.setId("notes"); 517 bnd.setType(BundleType.COLLECTION); 518 bnd.setTimestamp(date); 519 csv.readHeaders(); 520 Map<String, Observation> cache = new HashMap<>(); 521 522 int t = 0; 523 while (csv.line()) { 524 525 Patient pat = patients.get(csv.cell("subject_id")); 526 Encounter enc = encounters.get(csv.cell("hadm_id")); 527 528 DocumentReference dr = new DocumentReference(); 529 dr.setId(csv.cell("row_id")); 530 if (pat != null) { 531 dr.setSubject(new Reference("Patient/"+pat.getId())); 532 } 533 if (enc != null) { 534 dr.getContext().addEncounter(new Reference("Encounter/"+enc.getId())); 535 } 536 if ("1".equals(csv.cell("iserror"))) { 537 dr.setStatus(DocumentReferenceStatus.ENTEREDINERROR); 538 } else { 539 dr.setStatus(DocumentReferenceStatus.CURRENT); 540 } 541 if (csv.has("cgid")) { 542 dr.addAuthor().setReference("PractitionerRole/"+csv.cell("cgid")); 543 } 544 String cat = csv.cell("category"); 545 String desc = csv.cell("description"); 546 if ("Discharge summary".equals(cat) && "Report".equals(desc)) { 547 dr.getType().addCoding().setSystem("http://loinc.org").setCode("18842-5").setDisplay("Discharge summary"); 548 dr.addCategory().addCoding().setSystem("http://fhir.org/guides/argonaut/clinicalnotes/CodeSystem/documentreference-category").setCode("other"); 549 } else if ("Echo".equals(cat) && "Report".equals(desc)) { 550 dr.getType().addCoding().setSystem("http://loinc.org").setCode("59282-4").setDisplay("Stress cardiac echo study report US"); 551 dr.addCategory().addCoding().setSystem("http://fhir.org/guides/argonaut/clinicalnotes/CodeSystem/documentreference-category").setCode("other"); 552 } else if ("Radiology".equals(cat) && "CHEST (PORTABLE AP)".equals(desc)) { 553 dr.getType().addCoding().setSystem("http://loinc.org").setCode("59282-4").setDisplay("Chest X-ray AP portable single view"); 554 dr.addCategory().addCoding().setSystem("http://fhir.org/guides/argonaut/clinicalnotes/CodeSystem/documentreference-category").setCode("other"); 555 } else if ("Nursing/other".equals(cat) && "Report".equals(desc)) { 556 dr.getType().addCoding().setSystem("http://loinc.org").setCode("34119-8").setDisplay("Nursing facility Initial assessment note"); 557 dr.addCategory().addCoding().setSystem("http://fhir.org/guides/argonaut/clinicalnotes/CodeSystem/documentreference-category").setCode("other"); 558 } else if ("Physician".equals(cat) && "Physician Surgical Admission Note".equals(desc)) { 559 dr.getType().addCoding().setSystem("http://loinc.org").setCode("36589-0").setDisplay("Surgery Admission evaluation note"); 560 dr.addCategory().addCoding().setSystem("http://fhir.org/guides/argonaut/clinicalnotes/CodeSystem/documentreference-category").setCode("other"); 561 } else { 562 throw new FHIRException("Unhandled Note type '"+cat+"'/'"+desc+"'"); 563 } 564 dr.addContent().getAttachment().setContentType("text/plain; charset=UTF-8").setData(csv.cell("text").getBytes(Charset.forName("UTF-8"))); 565 bnd.addEntry().setResource(dr); 566 } 567 System.out.println(Integer.toString(t)+" found"); 568 csv.close(); 569 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 570 } 571 572 573 private void processDiagnoses(String src, String dest) throws FileNotFoundException, IOException { 574 System.out.print("Processing Diagnoses... "); 575 CSVReader csv = new CSVReader(new FileInputStream(src)); 576 Bundle bnd = new Bundle(); 577 bnd.setId("diagnoses"); 578 bnd.setType(BundleType.COLLECTION); 579 bnd.setTimestamp(date); 580 csv.readHeaders(); 581 int t = 0; 582 while (csv.line()) { 583 Condition cnd = new Condition(); 584 t++; 585 586 Patient pat = patients.get(csv.cell("subject_id")); 587 Encounter enc = encounters.get(csv.cell("hadm_id")); 588 589 cnd.setId(csv.cell("row_id")); 590 cnd.setVerificationStatus(new CodeableConcept(new Coding().setSystem("http://terminology.hl7.org/CodeSystem/condition-ver-status").setCode("confirmed"))); 591 if (pat != null) { 592 cnd.setSubject(new Reference("Patient/"+pat.getId())); 593 } 594 if (enc != null) { 595 cnd.setEncounter(new Reference("Encounter/"+enc.getId())); 596 enc.addDiagnosis().setCondition(new Reference("Condition/"+cnd.getId())).setRank(Integer.parseInt(csv.cell("seq_num"))); 597 } 598 cnd.setCode(new CodeableConcept(new Coding().setCode(csv.cell("icd9_code")).setSystem("http://hl7.org/fhir/sid/icd-9-cm"))); 599 600 bnd.addEntry().setResource(cnd); 601 } 602 System.out.println(Integer.toString(t)+" found"); 603 csv.close(); 604 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 605 } 606 607 private void processProcedures(String src, String dest) throws FileNotFoundException, IOException { 608 System.out.print("Processing Procedures... "); 609 CSVReader csv = new CSVReader(new FileInputStream(src)); 610 Bundle bnd = new Bundle(); 611 bnd.setId("procedures"); 612 bnd.setType(BundleType.COLLECTION); 613 bnd.setTimestamp(date); 614 csv.readHeaders(); 615 int t = 0; 616 while (csv.line()) { 617 Procedure prc = new Procedure(); 618 t++; 619 620 Patient pat = patients.get(csv.cell("subject_id")); 621 Encounter enc = encounters.get(csv.cell("hadm_id")); 622 623 prc.setId(csv.cell("row_id")); 624 prc.setStatus(ProcedureStatus.UNKNOWN); 625 prc.getCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode("diagnosis"); 626 if (pat != null) { 627 prc.setSubject(new Reference("Patient/"+pat.getId())); 628 } 629 if (enc != null) { 630 prc.setEncounter(new Reference("Encounter/"+enc.getId())); 631 enc.addDiagnosis().setCondition(new Reference("Procedure/"+prc.getId())).setRank(Integer.parseInt(csv.cell("seq_num"))); 632 } 633 prc.setCode(new CodeableConcept(new Coding().setCode(csv.cell("icd9_code")).setSystem("http://hl7.org/fhir/sid/icd-9-cm"))); 634 635 bnd.addEntry().setResource(prc); 636 } 637 System.out.println(Integer.toString(t)+" found"); 638 csv.close(); 639 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 640 } 641 642 private void processPrescriptions(String src, String dest) throws FHIRException, FileNotFoundException, IOException { 643 System.out.print("Processing Prescriptions... "); 644 CSVReader csv = new CSVReader(new FileInputStream(src)); 645 Bundle bnd = new Bundle(); 646 bnd.setId("prescriptions"); 647 bnd.setType(BundleType.COLLECTION); 648 bnd.setTimestamp(date); 649 csv.readHeaders(); 650 int t = 0; 651 while (csv.line()) { 652 MedicationRequest med = new MedicationRequest(); 653 t++; 654 655 Patient pat = patients.get(csv.cell("subject_id")); 656 Encounter enc = encounters.get(csv.cell("hadm_id")); 657 658 med.setId(csv.cell("row_id")); 659 med.setStatus(MedicationRequestStatus.COMPLETED); 660 if (pat != null) { 661 med.setSubject(new Reference("Patient/"+pat.getId())); 662 } 663 if (enc != null) { 664 med.setEncounter(new Reference("Encounter/"+enc.getId())); 665 } 666 med.getDispenseRequest().getValidityPeriod().setStartElement(readDateTime(csv.cell("startdate"))); 667 med.getDispenseRequest().getValidityPeriod().setEndElement(readDateTime(csv.cell("enddate"))); 668 med.addCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode(csv.cell("drug_type")); 669 CodeableConcept cc = new CodeableConcept(); 670 cc.setText(csv.cell("drug")); 671 if (csv.has("formulary_drug_cd")) { 672 cc.addCoding().setSystem("http://mimic.physionet.org/fhir/drugs").setCode(csv.cell("formulary_drug_cd")); 673 } 674 med.setMedication(cc); 675// if (csv.has("gsn")) { 676// cc.addCoding().setSystem("http://hl7.org/fhir/sid/GSN?").setCode(csv.cell("gsn")); 677// } 678 if (csv.has("ndc")) { 679 cc.addCoding().setSystem("http://hl7.org/fhir/sid/ndc").setCode(csv.cell("ndc")); 680 } 681 682 //PROD_STRENGTH, DOSE_VAL_RX, DOSE_UNIT_RX - ignore as denomormalizations 683 if (csv.has("dose_val_rx")) { 684 try { 685 med.getDosageInstructionFirstRep().getDoseAndRateFirstRep().setDose(parseQuantityRange(csv.cell("dose_val_rx"), csv.cell("dose_unit_rx"))); 686 } catch (Exception e) { 687 med.getDosageInstructionFirstRep().setText(csv.cell("dose_val_rx")); 688 } 689 } 690 if (csv.has("route")) { 691 med.getDosageInstructionFirstRep().getRoute().addCoding().setSystem("http://mimic.physionet.org/fhir/route").setCode(csv.cell("route")); 692 } 693 694 bnd.addEntry().setResource(med); 695 } 696 System.out.println(Integer.toString(t)+" found"); 697 csv.close(); 698 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 699 } 700 701 private void processProcedureEvents(String src, String dest) throws FileNotFoundException, IOException { 702 System.out.print("Processing Procedure Events... "); 703 CSVReader csv = new CSVReader(new FileInputStream(src)); 704 Bundle bnd = new Bundle(); 705 bnd.setId("procedure-events"); 706 bnd.setType(BundleType.COLLECTION); 707 bnd.setTimestamp(date); 708 csv.readHeaders(); 709 int t = 0; 710 while (csv.line()) { 711 Procedure prc = new Procedure(); 712 t++; 713 714 Patient pat = patients.get(csv.cell("subject_id")); 715 Encounter enc = encounters.get(csv.cell("hadm_id")); 716 717 prc.setId(csv.cell("row_id")); 718 prc.setStatus(ProcedureStatus.UNKNOWN); 719 prc.getCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode("diagnosis"); 720 if (pat != null) { 721 prc.setSubject(new Reference("Patient/"+pat.getId())); 722 } 723 if (enc != null) { 724 prc.setEncounter(new Reference("Encounter/"+enc.getId())); 725 } 726 prc.getPerformedPeriod().setStartElement(readDateTime(csv.cell("starttime"))); 727 prc.getPerformedPeriod().setEndElement(readDateTime(csv.cell("endtime"))); 728 Item spec = items.get(csv.cell("itemid")); 729 if (spec != null) 730 prc.setCode(new CodeableConcept(new Coding().setSystem("http://mimic.physionet.org/fhir/procedures").setCode(csv.cell("itemid")).setDisplay(spec.label))); 731 if (csv.has("location")) 732 prc.addBodySite().setText(csv.cell("location")); 733 bnd.addEntry().setResource(prc); 734 } 735 System.out.println(Integer.toString(t)+" found"); 736 csv.close(); 737 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 738 } 739 740 private void processOutputEvents(String src, String dest) throws FileNotFoundException, IOException { 741 System.out.print("Processing Output Events... "); 742 CSVReader csv = new CSVReader(new FileInputStream(src)); 743 Bundle bnd = new Bundle(); 744 bnd.setId("output-events"); 745 bnd.setType(BundleType.COLLECTION); 746 bnd.setTimestamp(date); 747 csv.readHeaders(); 748 int t = 0; 749 while (csv.line()) { 750 t++; 751 Patient pat = patients.get(csv.cell("subject_id")); 752 Encounter enc = encounters.get(csv.cell("hadm_id")); 753 Item spec = items.get(csv.cell("itemid")); 754 String loinc = ini.getStringProperty("outputs", csv.cell("itemid")); 755 if (loinc != null) { 756 if (!loinc.equals("n/a")) { 757 String value = null; 758 if (loinc.contains(";")) { 759 String[] p = loinc.split("\\;"); 760 value=p[1].trim(); 761 loinc = p[0]; 762 } 763 Observation obs = new Observation(); 764 obs.setId(csv.cell("row_id")); 765 obs.setStatus(ObservationStatus.FINAL); 766 obs.addCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode("output"); 767 768 if (pat != null) { 769 obs.setSubject(new Reference("Patient/"+pat.getId())); 770 } 771 if (enc != null) { 772 obs.setEncounter(new Reference("Encounter/"+enc.getId())); 773 } 774 obs.setEffective(readDateTime(csv.cell("charttime"))); 775 if (spec != null) { 776 obs.setCode(new CodeableConcept(loincCoding(loinc)).setText(spec.label)); 777 obs.getCode().addCoding().setSystem("http://mimic.physionet.org/fhir/outputs").setCode(csv.cell("itemid")); 778 } 779 if (value != null) { 780 obs.setValue(new CodeableConcept().setText(value)); 781 } else { 782 Quantity qty = parseQuantity(csv.cell("value"), "valueuom"); 783 obs.setValue(qty); 784 } 785 obs.setIssuedElement(readInstant(csv.cell("storetime"))); 786 787 PractitionerRole prr = careGivers.get(csv.cell("cgid")); 788 if (prr != null) 789 obs.addPerformer().setReference("PractitionerRole/"+prr.getId()); 790 791 bnd.addEntry().setResource(obs); 792 } 793 } else { 794 throw new Error("No LOINC code for output event "+csv.cell("itemid")+" ("+(spec == null ? "n/a" : spec.label)+"="+csv.cell("value")+")"); 795 } 796 } 797 System.out.println(Integer.toString(t)+" found"); 798 csv.close(); 799 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), bnd); 800 } 801 802 private void processChartEvents(String src, String dest) throws FileNotFoundException, IOException { 803 System.out.print("Processing Chart Events... "); 804 CSVReader csv = new CSVReader(new FileInputStream(src)); 805 Bundle bnd = new Bundle(); 806 bnd.setId("chart-events"); 807 bnd.setType(BundleType.COLLECTION); 808 bnd.setTimestamp(date); 809 csv.readHeaders(); 810 int t = 0; 811 int fc = 1; 812 while (csv.line()) { 813 t++; 814 Patient pat = patients.get(csv.cell("subject_id")); 815 Encounter enc = encounters.get(csv.cell("hadm_id")); 816 Item spec = items.get(csv.cell("itemid")); 817 if (spec != null) { 818 Observation obs = new Observation(); 819 obs.setId(csv.cell("row_id")); 820 obs.setStatus(ObservationStatus.FINAL); 821 obs.addCategory().addCoding().setSystem("http://mimic.physionet.org/fhir/categories").setCode("chart"); 822 823 if (pat != null) { 824 obs.setSubject(new Reference("Patient/"+pat.getId())); 825 } 826 if (enc != null) { 827 obs.setEncounter(new Reference("Encounter/"+enc.getId())); 828 } 829 obs.setEffective(readDateTime(csv.cell("charttime"))); 830 831 obs.getCode().setText(spec.label).addCoding().setSystem("http://mimic.physionet.org/fhir/outputs").setCode(csv.cell("itemid")); 832 String loinc = ini.getStringProperty("chartevents", csv.cell("itemid")); 833 String value = null; 834 if (!Utilities.noString(loinc)) { 835 if (loinc.contains(";")) { 836 String[] p = loinc.split("\\;"); 837 value=p[1].trim(); 838 loinc = p[0]; 839 } 840 obs.getCode().addCoding(loincCoding(loinc)); 841 } 842 if (value != null) { 843 obs.setValue(new CodeableConcept().setText(value)); 844 } else if (csv.has("valuenum")) { 845 Quantity qty = parseQuantity(csv.cell("valuenum"), "valueuom"); 846 obs.setValue(qty); 847 } else { 848 obs.setValue(new CodeableConcept().setText(csv.cell("value"))); 849 } 850 obs.setIssuedElement(readInstant(csv.cell("storetime"))); 851 852 PractitionerRole prr = careGivers.get(csv.cell("cgid")); 853 if (prr != null) 854 obs.addPerformer().setReference("PractitionerRole/"+prr.getId()); 855 856 bnd.addEntry().setResource(obs); 857 if (bnd.getEntry().size() > 100000) { 858 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.changeFileExt(dest, ""+Integer.toString(fc)+".json")), bnd); 859 fc++; 860 bnd = new Bundle(); 861 bnd.setId("chart-events"); 862 bnd.setType(BundleType.COLLECTION); 863 bnd.setTimestamp(date); 864 } 865 } 866 } 867 System.out.println(Integer.toString(t)+" found"); 868 csv.close(); 869 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.changeFileExt(dest, ""+Integer.toString(fc)+".json")), bnd); 870 } 871 872 private Coding loincCoding(String loinc) { 873 Coding c = new Coding(); 874 String[] p = loinc.split("\\#"); 875 c.setSystem(p[0]); 876 p = p[1].split("\\:"); 877 c.setCode(p[0]); 878 c.setDisplay(p[1].trim()); 879 return c; 880 } 881 882 private Type parseQuantityRange(String cell, String uom) { 883 if (!Utilities.noString(cell) && cell.contains("-")) { 884 String[] p = cell.split("\\-"); 885 Range rng = new Range(); 886 rng.setLow(parseQuantity(p[0], uom)); 887 rng.setHigh(parseQuantity(p[1], uom)); 888 return rng; 889 } else 890 return parseQuantity(cell, uom); 891 } 892 private Quantity parseQuantity(String cell, String uom) { 893 if (Utilities.noString(cell)) 894 return null; 895 Quantity qty = parseQuantity(cell); 896 if (!Utilities.noString(uom)) { 897 qty.setUnit(uom); 898 if (ucum.validate(uom) == null) { 899 qty.setCode(uom); 900 qty.setSystem("http://unitsofmeasure.org"); 901 } 902 } 903 return qty; 904 } 905 906 private Quantity parseQuantity(String cell) { 907 if (cell.startsWith("<=")) { 908 return new Quantity().setComparator(QuantityComparator.LESS_OR_EQUAL).setValue(new BigDecimal(cell.substring(2))); 909 } 910 if (cell.startsWith("<")) { 911 return new Quantity().setComparator(QuantityComparator.LESS_THAN).setValue(new BigDecimal(cell.substring(2))); 912 } 913 if (cell.startsWith(">=") || cell.startsWith("=>") ) { 914 return new Quantity().setComparator(QuantityComparator.GREATER_OR_EQUAL).setValue(new BigDecimal(cell.substring(2))); 915 } 916 if (cell.startsWith(">")) { 917 return new Quantity().setComparator(QuantityComparator.GREATER_THAN).setValue(new BigDecimal(cell.substring(2))); 918 } 919 if (!Utilities.isDecimal(cell, true)) { 920 throw new FHIRException("Not a valid decimal: "+cell); 921 } 922 923 return new Quantity().setValue(new BigDecimal(cell)); 924 } 925 926 private Bundle processAdmissions(String src) throws FileNotFoundException, IOException { 927 System.out.print("Processing Admissions... "); 928 CSVReader csv = new CSVReader(new FileInputStream(src)); 929 Bundle bnd = new Bundle(); 930 bnd.setId("encounters"); 931 bnd.setType(BundleType.COLLECTION); 932 bnd.setTimestamp(date); 933 csv.readHeaders(); 934 int t = 0; 935 while (csv.line()) { 936 Encounter enc = new Encounter(); 937 t++; 938 Patient pat = patients.get(csv.cell("subject_id")); 939 encounters.put(csv.cell("hadm_id"), enc); 940 enc.setId(csv.cell("hadm_id")); 941 enc.setSubject(new Reference("Patient/"+pat.getId())); 942 enc.getPeriod().setStartElement(readDateTime(csv.cell("admittime"))); 943 enc.getPeriod().setEndElement(readDateTime(csv.cell("dischtime"))); 944 enc.addType().addCoding().setSystem("http://mimic.physionet.org/fhir/AdmissionType").setCode(csv.cell("admission_type")); 945 if (!Utilities.noString(csv.cell("admission_location"))) { 946 enc.getHospitalization().getAdmitSource().addCoding().setSystem("http://mimic.physionet.org/fhir/AdmitSource").setCode(csv.cell("admission_location")); 947 } 948 if (csv.has("discharge_location")) { 949 enc.getHospitalization().getDischargeDisposition().addCoding().setSystem("http://mimic.physionet.org/fhir/DischargeLocation").setCode(csv.cell("discharge_location")); 950 } 951 // ignore insurance 952 if (csv.has("language")) { 953 pat.addCommunication().getLanguage().setText(csv.cell("language")); 954 } 955 if (csv.has("religion")) { 956 pat.addExtension().setUrl("http://hl7.org/fhir/StructureDefinition/patient-religion").setValue(new CodeableConcept().setText(csv.cell("religion"))); 957 } 958 if (csv.has("marital_status")) { 959 pat.getMaritalStatus().addCoding().setSystem("http://mimic.physionet.org/fhir/MaritalStatus").setCode(csv.cell("marital_status")); 960 } 961 if (csv.has("ethnicity")) { 962 pat.addExtension().setUrl("http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity").setValue(new CodeableConcept().setText(csv.cell("ethnicity"))); 963 } 964 if (!csv.has("diagnosis")) { 965 enc.addReasonCode().setText(csv.cell("diagnosis")); 966 } 967 if ("1".equals(csv.cell("hospital_expire_flag"))) { 968 enc.getHospitalization().getDischargeDisposition().addCoding().setSystem("http://terminology.hl7.org/CodeSystem/discharge-disposition").setCode("exp"); 969 } 970 bnd.addEntry().setResource(enc); 971 } 972 System.out.println(Integer.toString(t)+" found"); 973 csv.close(); 974 return bnd; 975 } 976 977 private Bundle processPatients(String src) throws FileNotFoundException, IOException { 978 System.out.print("Processing Patients... "); 979 CSVReader csv = new CSVReader(new FileInputStream(src)); 980 Bundle bnd = new Bundle(); 981 bnd.setId("patients"); 982 bnd.setType(BundleType.COLLECTION); 983 bnd.setTimestamp(date); 984 csv.readHeaders(); 985 int t = 0; 986 while (csv.line()) { 987 Patient pat = new Patient(); 988 t++; 989 patients.put(csv.cell("subject_id"), pat); 990 pat.setId(csv.cell("row_id")); 991 pat.addIdentifier().setSystem(MRN_SYSTEM).setValue(csv.cell("subject_id")); 992 if ("F".equals(csv.cell("gender"))) 993 pat.setGender(AdministrativeGender.FEMALE); 994 else if ("M".equals(csv.cell("gender"))) 995 pat.setGender(AdministrativeGender.MALE); 996 else 997 throw new Error("unknown gender: "+csv.cell("gender")); 998 pat.setBirthDateElement(readDate(csv.cell("dob"))); 999 pat.setDeceased(readDateTime(csv.cell("dod"))); 1000 bnd.addEntry().setResource(pat); 1001 } 1002 System.out.println(Integer.toString(t)+" found"); 1003 csv.close(); 1004 return bnd; 1005 } 1006 1007 1008 private DateType readDate(String cell) { 1009 if (Utilities.noString(cell)) 1010 return null; 1011 String src = cell.substring(0, 10); 1012 return new DateType(src); 1013 } 1014 1015 private DateTimeType readDateTime(String cell) { 1016 if (Utilities.noString(cell)) 1017 return null; 1018 String src = cell.replace(" ", "T"); 1019 if (src.endsWith("00:00:00")) 1020 src = cell.substring(0, 10); 1021 return new DateTimeType(src); 1022 } 1023 1024 private InstantType readInstant(String cell) { 1025 if (Utilities.noString(cell)) 1026 return null; 1027 String src = cell.replace(" ", "T"); 1028 return new InstantType(src); 1029 } 1030 1031}