001package org.hl7.fhir.convertors.misc; 002 003 004import java.io.File; 005import java.io.FileInputStream; 006import java.io.FileNotFoundException; 007import java.io.FileOutputStream; 008import java.io.IOException; 009import java.sql.Connection; 010import java.sql.DriverManager; 011import java.sql.SQLException; 012import java.sql.Statement; 013import java.util.Date; 014import java.util.List; 015import java.util.ArrayList; 016import java.util.Scanner; 017 018import org.hl7.fhir.exceptions.FHIRException; 019import org.hl7.fhir.r5.formats.IParser.OutputStyle; 020import org.hl7.fhir.r5.formats.JsonParser; 021import org.hl7.fhir.r5.model.CodeSystem; 022import org.hl7.fhir.r5.model.BooleanType; 023import org.hl7.fhir.r5.model.StringType; 024import org.hl7.fhir.r5.model.CodeType; 025import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent; 026import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionDesignationComponent; 027import org.hl7.fhir.r5.model.CodeSystem.ConceptPropertyComponent; 028import org.hl7.fhir.r5.model.CodeSystem.PropertyType; 029import org.hl7.fhir.r5.model.Coding; 030import org.hl7.fhir.r5.model.Enumerations.CodeSystemContentMode; 031import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; 032import org.hl7.fhir.r5.terminologies.CodeSystemUtilities; 033import org.hl7.fhir.utilities.CSVReader; 034import org.hl7.fhir.utilities.Utilities; 035import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 036 037public class CPTImporter { 038 039 public static void main(String[] args) throws FHIRException, FileNotFoundException, IOException, ClassNotFoundException, SQLException { 040 new CPTImporter().doImport(args[0], args[1], args[2]); 041 042 } 043 044 045 private void doImport(String src, String version, String dst) throws FHIRException, FileNotFoundException, IOException, ClassNotFoundException, SQLException { 046 047 CodeSystem cs = new CodeSystem(); 048 cs.setId("cpt"); 049 cs.setUrl("http://www.ama-assn.org/go/cpt"); 050 cs.setVersion(version); 051 cs.setName("AmaCPT"); 052 cs.setTitle("AMA CPT"); 053 cs.setStatus(PublicationStatus.ACTIVE); 054 cs.setDate(new Date()); 055 cs.setContent(CodeSystemContentMode.COMPLETE); 056 cs.setCompositional(true); 057 cs.setPublisher("AMA"); 058 cs.setValueSet("http://hl7.org/fhir/ValueSet/cpt-all"); 059 cs.setCopyright("CPT © Copyright 2019 American Medical Association. All rights reserved. AMA and CPT are registered trademarks of the American Medical Association."); 060 cs.addProperty().setCode("modifier").setDescription("Whether code is a modifier code").setType(PropertyType.BOOLEAN); 061 cs.addProperty().setCode("modified").setDescription("Whether code has been modified (all base codes are not modified)").setType(PropertyType.BOOLEAN); 062 cs.addProperty().setCode("orthopox").setDescription("Whether code is one of the Pathology and Laboratory and Immunization Code(s) for Orthopoxvirus").setType(PropertyType.BOOLEAN); 063 cs.addProperty().setCode("telemedicine").setDescription("Whether code is appropriate for use with telemedicine (and the telemedicine modifier)").setType(PropertyType.BOOLEAN); 064 cs.addProperty().setCode("kind").setDescription("Kind of Code (see metadata)").setType(PropertyType.CODE); 065 066 defineMetadata(cs); 067 068 System.out.println("LONGULT: "+readCodes(cs, Utilities.path(src, "LONGULT.txt"), false, null, null, null)); 069 System.out.println("LONGUT: "+readCodes(cs, Utilities.path(src, "LONGUT.txt"), false, "upper", null, null)); 070 System.out.println("MEDU: "+readCodes(cs, Utilities.path(src, "MEDU.txt"), false, "med", null, null)); 071 System.out.println("SHORTU: "+readCodes(cs, Utilities.path(src, "SHORTU.txt"), false, "short", null, null)); 072 System.out.println("ConsumerDescriptor: "+readCodes(cs, Utilities.path(src, "ConsumerDescriptor.txt"), true, "consumer", null, null)); 073 System.out.println("ClinicianDescriptor: "+readCodes(cs, Utilities.path(src, "ClinicianDescriptor.txt"), true, "clinician", null, null)); 074 System.out.println("OrthopoxvirusCodes: "+readCodes(cs, Utilities.path(src, "OrthopoxvirusCodes.txt"), false, null, null, "orthopox")); 075 076 System.out.println("modifiers: "+processModifiers(cs, Utilities.path(src, "modifiers.csv"))); 077 System.out.println("appendix P: "+processAppendixP(cs)); 078 079 080 System.out.println("-------------------"); 081 System.out.println(cs.getConcept().size()); 082 int c = 0; 083 int k = 0; 084 for (ConceptDefinitionComponent cc: cs.getConcept()) { 085 c = Integer.max(c, cc.getProperty().size()); 086 if (cc.getProperty().size() > 3) { 087 k++; 088 } 089 } 090 System.out.println(c); 091 System.out.println(k); 092 093 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(dst), cs); 094 produceDB(Utilities.changeFileExt(dst, ".db"), cs); 095 096 cs.setContent(CodeSystemContentMode.FRAGMENT); 097 cs.getConcept().removeIf(cc -> !Utilities.existsInList(cc.getCode(), "metadata-kinds", "metadata-designations", "99202", "99203", "0001A", "99252", "25", "P1", "1P", "F1", "95")); 098 new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(Utilities.changeFileExt(dst, "-fragment.json")), cs); 099 produceDB(Utilities.changeFileExt(dst, "-fragment.db"), cs); 100 } 101 102 private String processAppendixP(CodeSystem cs) { 103 List<String> tcodes = new ArrayList<>(); 104 tcodes.add("90785"); 105 tcodes.add("90791"); 106 tcodes.add("90792"); 107 tcodes.add("90832"); 108 tcodes.add("90833"); 109 tcodes.add("90834"); 110 tcodes.add("90836"); 111 tcodes.add("90837"); 112 tcodes.add("90838"); 113 tcodes.add("90839"); 114 tcodes.add("90840"); 115 tcodes.add("90845"); 116 tcodes.add("90846"); 117 tcodes.add("90847"); 118 tcodes.add("92507"); 119 tcodes.add("92508"); 120 tcodes.add("92521"); 121 tcodes.add("92522"); 122 tcodes.add("92523"); 123 tcodes.add("92524"); 124 tcodes.add("96040"); 125 tcodes.add("96110"); 126 tcodes.add("96116"); 127 tcodes.add("96160"); 128 tcodes.add("96161"); 129 tcodes.add("97802"); 130 tcodes.add("97803"); 131 tcodes.add("97804"); 132 tcodes.add("99406"); 133 tcodes.add("99407"); 134 tcodes.add("99408"); 135 tcodes.add("99409"); 136 tcodes.add("99497"); 137 tcodes.add("99498"); 138 139 for (String c : tcodes) { 140 ConceptDefinitionComponent cc = CodeSystemUtilities.findCode(cs.getConcept(), c); 141 if (cc == null) { 142 throw new Error("unable to find tcode "+c); 143 } 144 cc.addProperty().setCode("telemedicine").setValue(new BooleanType(true)); 145 } 146 return String.valueOf(tcodes.size()); 147 } 148 149 150 private void produceDB(String path, CodeSystem cs) throws ClassNotFoundException, SQLException, IOException { 151 Connection con = connect(path); 152 153 Statement stmt = con.createStatement(); 154 stmt.execute("insert into Information (name, value) values ('version', "+cs.getVersion()+")"); 155 for (ConceptDefinitionComponent cc: cs.getConcept()) { 156 if (!cc.getCode().startsWith("metadata")) { 157 stmt.execute("insert into Concepts (code, modifier) values ('"+cc.getCode()+"', "+isModifier(cc)+")"); 158 int i = 0; 159 if (cc.hasDisplay()) { 160 stmt.execute("insert into Designations (code, type, sequence, value) values ('"+cc.getCode()+"', 'display', 0, '"+Utilities.escapeSql(cc.getDisplay())+"')"); 161 i++; 162 } 163 for (ConceptDefinitionDesignationComponent d : cc.getDesignation()) { 164 stmt.execute("insert into Designations (code, type, sequence, value) values ('"+cc.getCode()+"', '"+d.getUse().getCode()+"', "+i+", '"+Utilities.escapeSql(d.getValue())+"')"); 165 i++; 166 } 167 i = 0; 168 for (ConceptPropertyComponent p : cc.getProperty()) { 169 if (!Utilities.existsInList(p.getCode(), "modified", "modifier")) { 170 stmt.execute("insert into Properties (code, name, sequence, value) values ('"+cc.getCode()+"', '"+p.getCode()+"', "+i+", '"+p.getValue().primitiveValue()+"')"); 171 i++; 172 } 173 } 174 } 175 } 176 177 } 178 179 private String isModifier(ConceptDefinitionComponent cc) { 180 for (ConceptPropertyComponent p : cc.getProperty()) { 181 if (p.getCode().equals("modifier")) { 182 return p.getValue().primitiveValue().equals("true") ? "1" : "0"; 183 } 184 } 185 return "0"; 186 } 187 188 189 private Connection connect(String dest) throws SQLException, ClassNotFoundException, IOException { 190 // Class.forName("com.mysql.jdbc.Driver"); 191 // con = DriverManager.getConnection("jdbc:mysql://localhost:3306/omop?useSSL=false","root",{pwd}); 192 ManagedFileAccess.file(dest).delete(); 193 Connection con = DriverManager.getConnection("jdbc:sqlite:"+dest); 194 makeMetadataTable(con); 195 makeConceptsTable(con); 196 makeDesignationsTable(con); 197 makePropertiesTable(con); 198 return con; 199 } 200 201 private void makeDesignationsTable(Connection con) throws SQLException { 202 Statement stmt = con.createStatement(); 203 stmt.execute("CREATE TABLE Designations (\r\n"+ 204 "`code` varchar(15) NOT NULL,\r\n"+ 205 "`type` varchar(15) NOT NULL,\r\n"+ 206 "`sequence` int NOT NULL,\r\n"+ 207 "`value` text NOT NULL,\r\n"+ 208 "PRIMARY KEY (`code`, `type`, `sequence`))\r\n"); 209 } 210 211 212 private void makePropertiesTable(Connection con) throws SQLException { 213 214 Statement stmt = con.createStatement(); 215 stmt.execute("CREATE TABLE Properties (\r\n"+ 216 "`code` varchar(15) NOT NULL,\r\n"+ 217 "`name` varchar(15) NOT NULL,\r\n"+ 218 "`sequence` int NOT NULL,\r\n"+ 219 "`value` varchar(15) NOT NULL,\r\n"+ 220 "PRIMARY KEY (`code`, `name`, `sequence`))\r\n"); 221 222 } 223 224 225 private void makeConceptsTable(Connection con) throws SQLException { 226 227 Statement stmt = con.createStatement(); 228 stmt.execute("CREATE TABLE Concepts (\r\n"+ 229 "`code` varchar(15) NOT NULL,\r\n"+ 230 "`modifier` int DEFAULT NULL,\r\n"+ 231 "PRIMARY KEY (`code`))\r\n"); 232 233 } 234 235 236 private void makeMetadataTable(Connection con) throws SQLException { 237 238 Statement stmt = con.createStatement(); 239 stmt.execute("CREATE TABLE Information (\r\n"+ 240 "`name` varchar(64) NOT NULL,\r\n"+ 241 "`value` varchar(64) DEFAULT NULL,\r\n"+ 242 "PRIMARY KEY (`name`))\r\n"); 243 244 } 245 246 247 private void defineMetadata(CodeSystem cs) { 248 ConceptDefinitionComponent pc = mm(cs.addConcept().setCode("metadata-kinds")); 249 mm(pc.addConcept()).setCode("code").setDisplay("A normal CPT code"); 250 mm(pc.addConcept()).setCode("cat-1").setDisplay("CPT Level I Modifiers"); 251 mm(pc.addConcept()).setCode("cat-2").setDisplay("A Category II code or modifier"); 252 mm(pc.addConcept()).setCode("physical-status").setDisplay("Anesthesia Physical Status Modifiers"); 253 mm(pc.addConcept()).setCode("general").setDisplay("A general modifier"); 254 mm(pc.addConcept()).setCode("hcpcs").setDisplay("Level II (HCPCS/National) Modifiers"); 255 mm(pc.addConcept()).setCode("metadata").setDisplay("A kind of code or designation"); 256 257 ConceptDefinitionComponent dc = mm(cs.addConcept().setCode("metadata-designations")); 258 mm(dc.addConcept()).setCode("upper").setDisplay("Uppercase variant of the display"); 259 mm(dc.addConcept()).setCode("med").setDisplay("Medium length variant of the display (all uppercase)"); 260 mm(dc.addConcept()).setCode("short").setDisplay("Short length variant of the display (all uppercase)"); 261 mm(dc.addConcept()).setCode("consumer").setDisplay("Consumer Friendly representation for the concept"); 262 mm(dc.addConcept()).setCode("clinician").setDisplay("Clinician Friendly representation for the concept (can be more than one per concept)"); 263 } 264 265 private ConceptDefinitionComponent mm(ConceptDefinitionComponent cc) { 266 cc.addProperty().setCode("kind").setValue(new CodeType("metadata")); 267 return cc; 268 } 269 270 private int processModifiers(CodeSystem cs, String path) throws FHIRException, FileNotFoundException, IOException { 271 CSVReader csv = new CSVReader(ManagedFileAccess.inStream(path)); 272 csv.readHeaders(); 273 274 int res = 0; 275 while (csv.line()) { 276 String code = csv.cell("Code"); 277 String general = csv.cell("General"); 278 String physicalStatus = csv.cell("PhysicalStatus"); 279 String levelOne = csv.cell("LevelOne"); 280 String levelTwo = csv.cell("LevelTwo"); 281 String hcpcs = csv.cell("HCPCS"); 282 String defn = csv.cell("Definition"); 283 284 res = Integer.max(res, defn.length()); 285 ConceptDefinitionComponent cc = cs.addConcept().setCode(code); 286 cc.setDisplay(defn); 287 cc.addProperty().setCode("modified").setValue(new BooleanType(false)); 288 cc.addProperty().setCode("modifier").setValue(new BooleanType(true)); 289 if ("1".equals(general)) { 290 cc.addProperty().setCode("kind").setValue(new CodeType("general")); 291 } 292 if ("1".equals(physicalStatus)) { 293 cc.addProperty().setCode("kind").setValue(new CodeType("physical-status")); 294 } 295 if ("1".equals(levelOne)) { 296 cc.addProperty().setCode("kind").setValue(new CodeType("cat-1")); 297 } 298 if ("1".equals(levelTwo)) { 299 cc.addProperty().setCode("kind").setValue(new CodeType("cat-2")); 300 } 301 if ("1".equals(hcpcs)) { 302 cc.addProperty().setCode("kind").setValue(new CodeType("hcpcs")); 303 } 304 } 305 return res; 306 } 307 308 private int readCodes(CodeSystem cs, String path, boolean hasConceptId, String use, String type, String boolProp) throws IOException { 309 int res = 0; 310 FileInputStream inputStream = null; 311 Scanner sc = null; 312 try { 313 inputStream = ManagedFileAccess.inStream(path); 314 sc = new Scanner(inputStream, "UTF-8"); 315 while (sc.hasNextLine()) { 316 String line = sc.nextLine(); 317 if (hasConceptId) { 318 line = line.substring(7).trim(); 319 } 320 String code = line.substring(0, 5); 321 String desc = line.substring(6); 322 if (desc.contains("\t")) { 323 desc = desc.substring(desc.indexOf("\t")+1); 324 } 325 res = Integer.max(res, desc.length()); 326 ConceptDefinitionComponent cc = CodeSystemUtilities.getCode(cs, code); 327 if (cc == null) { 328 cc = cs.addConcept().setCode(code); 329 cc.addProperty().setCode("modifier").setValue(new BooleanType(false)); 330 cc.addProperty().setCode("modified").setValue(new BooleanType(false)); 331 if (type == null) { 332 if (Utilities.isInteger(code)) { 333 cc.addProperty().setCode("kind").setValue(new CodeType("code")); 334 } else { 335 cc.addProperty().setCode("kind").setValue(new CodeType("cat-2")); 336 } 337 } else { 338 cc.addProperty().setCode("kind").setValue(new CodeType(type)); 339 } 340 } else if (type != null) { 341 cc.addProperty().setCode("kind").setValue(new CodeType(type)); 342 } 343 if (boolProp != null) { 344 cc.addProperty().setCode(boolProp).setValue(new BooleanType(true)); 345 } 346 if (use == null) { 347 if (cc.hasDisplay()) { 348 System.err.println("?"); 349 } 350 cc.setDisplay(desc); 351 } else { 352 cc.addDesignation().setUse(new Coding("http://www.ama-assn.org/go/cpt", use, null)).setValue(desc); 353 } 354 } 355 // note that Scanner suppresses exceptions 356 if (sc.ioException() != null) { 357 throw sc.ioException(); 358 } 359 } finally { 360 if (inputStream != null) { 361 inputStream.close(); 362 } 363 if (sc != null) { 364 sc.close(); 365 } 366 } 367 return res; 368 } 369 370 371}