Skip to content

Commit

Permalink
Issue #173 CWE Version & Alternate Coding support
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Cragun committed Aug 21, 2021
1 parent a66a0ee commit 946b2c1
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ public class SimpleCode {
private String system;
private String code;
private String display;
private String version;

/**
* Returns simple representation of Code.
*
* @param code
* @param system
* @param display
* @param version
*/

public SimpleCode(String code, String system, String display) {
this.code = code;
this.system = system;
this.display = display;
this.version = null;
}

public SimpleCode(String code, String system, String display, String version) {
this.code = code;
this.system = system;
this.display = display;
this.version = version;
}

public String getSystem() {
Expand All @@ -36,12 +46,18 @@ public String getCode() {
return code;
}



public String getDisplay() {
return display;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String toString() {
return ReflectionToStringBuilder.toString(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public static String getOriginalDisplayText(Object obj) {
return null;
}

public static String getVersion(Object obj) {
if (obj instanceof CWE) {
CWE id = (CWE) obj;
ST st = id.getCodingSystemVersionID();
if (st != null) {
String str = st.getValue();
if (str != null) {
return str.trim();
}
return null;
}
return null;
}
return null;
}

private static String getAssociatedtable(CWE id) {
ID val = id.getCwe3_NameOfCodingSystem();
if (val != null && StringUtils.startsWith(val.getValue(), "HL7")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum SimpleDataTypeMapper {
NAMED_UUID(SimpleDataValueResolver.NAMED_UUID),
OBJECT(SimpleDataValueResolver.OBJECT),
CODING_SYSTEM_V2(SimpleDataValueResolver.CODING_SYSTEM_V2),
CODING_SYSTEM_V2_ALTERNATE(SimpleDataValueResolver.CODING_SYSTEM_V2_ALTERNATE),
SYSTEM_URL(SimpleDataValueResolver.SYSTEM_URL),
SYSTEM_ID(SimpleDataValueResolver.SYSTEM_ID),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,28 @@ public class SimpleDataValueResolver {
return value;
};

public static final ValueExtractor<Object, SimpleCode> CODING_SYSTEM_V2_ALTERNATE = (Object value) -> {
// ensure we have a CWE
if (value instanceof CWE) {
CWE cwe = (CWE) value;
String table = Hl7DataHandlerUtil.getStringValue(cwe.getCwe6_NameOfAlternateCodingSystem());
String code = Hl7DataHandlerUtil.getStringValue(cwe.getCwe4_AlternateIdentifier());
String text = Hl7DataHandlerUtil.getStringValue(cwe.getCwe5_AlternateText());
String version = Hl7DataHandlerUtil.getStringValue(cwe.getCwe8_AlternateCodingSystemVersionID());
return commonCodingSystemV2(table, code, text, version);
}
return null;
};

public static final ValueExtractor<Object, SimpleCode> CODING_SYSTEM_V2 = (Object value) -> {
String table = Hl7DataHandlerUtil.getTableNumber(value);
String code = Hl7DataHandlerUtil.getStringValue(value);
String text = Hl7DataHandlerUtil.getOriginalDisplayText(value);
String version = Hl7DataHandlerUtil.getVersion(value);
return commonCodingSystemV2(table, code, text, version);
};

private static final SimpleCode commonCodingSystemV2 (String table, String code, String text, String version) {
if (table != null && code != null) {
// Found table and a code. Try looking it up.
SimpleCode coding = TerminologyLookup.lookup(table, code);
Expand All @@ -265,8 +283,9 @@ public class SimpleDataValueResolver {

if (display.isEmpty()) {
// We have a table, code, but unknown display, so we can't tell if it's good, use the original display text
coding = new SimpleCode(coding.getCode(), coding.getSystem(), text);
coding = new SimpleCode(coding.getCode(), coding.getSystem(), text, version);
}
coding.setVersion(version);
// We have a table, code, and display, so code was valid
return coding;
} else {
Expand All @@ -276,15 +295,15 @@ public class SimpleDataValueResolver {
}
} else {
// No success looking up the code, build our own fall-back system using table name
return new SimpleCode(code, "urn:id:"+table, text) ;
return new SimpleCode(code, "urn:id:"+table, text, version) ;
}
} else if (code != null) {
// A code but no system: build a simple systemless code
return new SimpleCode(code, null, null);
} else {
return null;
}
};
}

public static final ValueExtractor<Object, String> BUILD_IDENTIFIER_FROM_CWE = (Object value) -> {
CWE newValue = ((CWE) value);
Expand Down
11 changes: 4 additions & 7 deletions src/main/resources/hl7/datatype/CodeableConcept.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ coding_2:
vars:
code: ID |IS |TX

# Generalized CWE for alternate codes handles mismatched systems and codes
coding_3:
valueOf: datatype/Coding
valueOf: $coding
generateList: true
expressionType: resource
condition: $code NOT_NULL && $coding NULL
condition: $coding NOT_NULL
specs: CWE
vars:
code: CWE.4
system: CWE.6
display: CWE.5
version: CWE.8
coding: CODING_SYSTEM_V2_ALTERNATE, CWE

# Generalized CWE through CODING_SYSTEM_V2 handles mismatched systems and codes
coding_4:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,37 +303,56 @@ void testCodeableConceptForNDC() {
}

@Test
public void testCodeableConceptForI9() {
public void testCodeableConceptForLoincAltenativeI9WithVersions() {

// With a valid (to us) but unregistered (to FHIR) system, we should produce:
// With a valid (to us) but unregistered (to FHIR) system, and with a version, we should produce:
// "code": {
// "coding": [ {
// "system": <known-to-us-system-via-our-lookup>,
// "system": <known-to-us-system-via-our-lookup>, << FIRST CODING
// "display": <original-display-value>
// "code": <code-from-input>
// "version": <version>
// },
// {
// "system": <known-to-us-system-via-our-lookup>, << SECOND (ALTERNATE CODING)
// "display": <original-display-value>
// "code": <code-from-input>
// "version": <version>
// } ],
// "text": <original-display-value>
// },

String conditionWithCodeableConcept = "MSH|^~\\&|||||20040629164652|1|PPR^PC1|331|P|2.3.1||\n"
String conditionWithVersionedAlternateCodeableConcept = "MSH|^~\\&|||||20040629164652|1|PPR^PC1|331|P|2.3.1||\n"
+ "PID|||1234^^^^MR||DOE^JANE^|||F||||||||||||||||||||||\n"
+ "PRB|AD|2004062916460000|596.5^BLADDER DYSFUNCTION^I9||||20040629||||||ACTIVE|||20040629";
+ "PRB|AD|2004062916460000|2148-5^CREATININE^LN^F-11380^CREATININE^I9^474747^22222||||20040629||||||ACTIVE|||20040629";

Condition condition = ResourceUtils.getCondition(conditionWithCodeableConcept);
Condition condition = ResourceUtils.getCondition(conditionWithVersionedAlternateCodeableConcept);

assertThat(condition.hasCode()).isTrue();
CodeableConcept condCC = condition.getCode();
assertThat(condCC.hasText()).isTrue();
assertThat(condCC.getText()).isEqualTo("BLADDER DYSFUNCTION");
assertThat(condCC.getText()).isEqualTo("CREATININE");
assertThat(condCC.hasCoding()).isTrue();

Coding condCoding = condCC.getCoding().get(0);
assertThat(condCoding.hasSystem()).isTrue();
assertThat(condCoding.getSystem()).isEqualTo("http://loinc.org");
assertThat(condCoding.hasCode()).isTrue();
assertThat(condCoding.getCode()).isEqualTo("2148-5");
assertThat(condCoding.hasDisplay()).isTrue();
assertThat(condCoding.getDisplay()).isEqualTo("CREATININE");
assertThat(condCoding.hasVersion()).isTrue();
assertThat(condCoding.getVersion()).isEqualTo("474747");

condCoding = condCC.getCoding().get(1);
assertThat(condCoding.hasSystem()).isTrue();
assertThat(condCoding.getSystem()).isEqualTo("http://terminology.hl7.org/CodeSystem/icd9");
assertThat(condCoding.hasCode()).isTrue();
assertThat(condCoding.getCode()).isEqualTo("596.5");
assertThat(condCoding.getCode()).isEqualTo("F-11380");
assertThat(condCoding.hasDisplay()).isTrue();
assertThat(condCoding.getDisplay()).isEqualTo("BLADDER DYSFUNCTION");
assertThat(condCoding.getDisplay()).isEqualTo("CREATININE");
assertThat(condCoding.hasVersion()).isTrue();
assertThat(condCoding.getVersion()).isEqualTo("22222");
}

}

0 comments on commit 946b2c1

Please sign in to comment.