use of org.monarchinitiative.loinc2hpo.testresult.BasicLabTestResultInHPO in project loinc2hpo by monarch-initiative.
the class FhirObservationAnalyzer method getHPO4ObservationOutcome.
/**
* A core function that tries three ways to return a LabTestResultInHPO object:
* first, it tries to return the result through the interpretation field. If it fails,
* second, it tries to return the result through the quantative value, or
* third, it tries to return the retult through the coded value (ordinal Loinc)
* @param loinc2HPOannotationMap
* @param loincIds
* @return
*/
public static LabTestResultInHPO getHPO4ObservationOutcome(HashSet<LoincId> loincIds, Map<LoincId, UniversalLoinc2HPOAnnotation> loinc2HPOannotationMap) {
// first make sure the observation has a valid loinc code; otherwise, we cannot handle it
if (!hasValidLoincCode(loincIds)) {
// TODO: consider handling this as a future project
return null;
}
LoincId loincId = null;
try {
loincId = getLoincIdOfObservation();
} catch (MalformedLoincCodeException e) {
logger.error("malformed loinc code; should never happen");
return null;
} catch (LoincCodeNotFoundException e) {
logger.error("No loinc code was found in the observation; should never happen");
return null;
} catch (UnsupportedCodingSystemException e) {
logger.error("coding system not recognized");
return null;
}
if (!loinc2HPOannotationMap.containsKey(loincId)) {
return null;
}
if (observation.hasInterpretation()) {
logger.debug("enter analyzer using the interpretation field");
try {
// return getHPOFromInterpretation(observation.getInterpretation(), loinc2HPOannotationMap);
HpoTermId4LoincTest hpoterm = new ObservationAnalysisFromInterpretation(getLoincIdOfObservation(), observation.getInterpretation(), loinc2HPOannotationMap).getHPOforObservation();
return new BasicLabTestResultInHPO(hpoterm, null);
} catch (UnrecognizedCodeException e) {
// this means the interpretation code is not recognized
logger.info("The interpretation codes for this loinc code is not annotated; system will try using raw values");
} catch (MalformedLoincCodeException e1) {
// not going to happen
logger.error("malformed loinc code.");
return null;
} catch (LoincCodeNotFoundException e2) {
// not going to happen
logger.error("no loinc code is found in the observation");
return null;
} catch (UnsupportedCodingSystemException e3) {
// not going to happen
logger.error("The interpretation coding system cannot be recognized.");
return null;
} catch (AmbiguousResultsFoundException e) {
logger.error("The observation has conflicting interpretation codes.");
return null;
} catch (AnnotationNotFoundException e) {
logger.error("There is no annotation for the loinc code used in the observation");
}
}
// Qn will have a value field
if (observation.hasValueQuantity()) {
try {
HpoTermId4LoincTest hpoterm = new ObservationAnalysisFromQnValue(loincId, observation, loinc2HPOannotationMap).getHPOforObservation();
if (hpoterm != null)
return new BasicLabTestResultInHPO(hpoterm, null);
} catch (ReferenceNotFoundException e) {
// if there is no reference
logger.error("The observation has no reference field.");
// TODO: make a list of our own references
} catch (AmbiguousReferenceException e) {
logger.info("There are two reference ranges or more");
} catch (UnrecognizedCodeException e) {
logger.error("uncognized coding system");
}
}
// Ord will have a ValueCodeableConcept field
if (observation.hasValueCodeableConcept()) {
try {
HpoTermId4LoincTest hpoterm = null;
hpoterm = new ObservationAnalysisFromCodedValues(loincId, observation.getValueCodeableConcept(), loinc2HPOannotationMap).getHPOforObservation();
if (hpoterm != null)
return new BasicLabTestResultInHPO(hpoterm, null);
} catch (AmbiguousResultsFoundException e) {
logger.error("multiple results are found");
} catch (UnrecognizedCodeException e) {
logger.error("unrecognized codes");
} catch (FHIRException e) {
// not going to happen
logger.error("Could not get HPO term from coded value");
} catch (AnnotationNotFoundException e) {
logger.error("There is no annotation for the loinc code used in the observation");
}
}
// if all the above fails, we cannot do nothing
logger.error("Could not return HPO for observation: " + observation.getId());
return null;
}
use of org.monarchinitiative.loinc2hpo.testresult.BasicLabTestResultInHPO in project loinc2hpo by monarch-initiative.
the class FhirObservationAnalyzer method getHPOFromInterpretation.
/**
* Convert a FHIR observation to HPO through the interpretation field
* @param interpretation
* @param testmap
* @return
* @throws MalformedLoincCodeException
* @throws LoincCodeNotFoundException
* @throws UnsupportedCodingSystemException
*/
public static LabTestResultInHPO getHPOFromInterpretation(CodeableConcept interpretation, Map<LoincId, UniversalLoinc2HPOAnnotation> testmap) throws MalformedLoincCodeException, LoincCodeNotFoundException, UnsupportedCodingSystemException, AnnotationNotFoundException {
// here we only look at interpretation code system defined by HL7
Code interpretationCode = null;
for (Coding coding : interpretation.getCoding()) {
if (coding.getSystem().equals("http://hl7.org/fhir/v2/0078")) {
interpretationCode = Code.getNewCode().setSystem(coding.getSystem()).setCode(coding.getCode());
break;
}
}
if (interpretationCode != null) {
// find result
try {
Code internalCode = CodeSystemConvertor.convertToInternalCode(interpretationCode);
// get the loinc code from the observation
LoincId loincId = getLoincIdOfObservation();
// get the annotation class for this loinc code
UniversalLoinc2HPOAnnotation annotationForLoinc = testmap.get(loincId);
if (annotationForLoinc == null)
throw new AnnotationNotFoundException();
HpoTermId4LoincTest hpoId = annotationForLoinc.loincInterpretationToHPO(internalCode);
return new BasicLabTestResultInHPO(hpoId, null, "?");
} catch (InternalCodeNotFoundException e) {
e.printStackTrace();
}
} else {
logger.error("Could not recognize the coding system for interpretation");
for (Coding coding : interpretation.getCoding()) {
logger.error("coding system: " + coding.getSystem() + " Value: " + coding.getCode());
}
throw new UnsupportedCodingSystemException("Coding system is not supported");
}
return null;
}
use of org.monarchinitiative.loinc2hpo.testresult.BasicLabTestResultInHPO in project loinc2hpo by monarch-initiative.
the class FhirResourceRetriever method fhir2testrest.
@Deprecated
public static LabTestResultInHPO fhir2testrest(JsonNode node, Map<LoincId, Loinc2HPOAnnotation> testmap) throws Loinc2HpoException {
boolean interpretationDetected = false;
String interpretationCode = null;
LoincId lid;
ObservationResultInInternalCode observation;
String comment;
System.out.println(node.toString());
String resourcetype = node.get("resourceType").asText();
if (!resourcetype.equals("Observation"))
throw new WrongElementException("Unexpected resource type " + resourcetype + "(expected: LabTestResultInHPO)");
JsonNode codeNode = node.get("code");
lid = getLoincId(codeNode);
JsonNode interpretationNode = node.get("interpretation");
observation = getInterpretationCode(interpretationNode);
if (observation != null && lid != null) {
Loinc2HPOAnnotation test = testmap.get(lid);
if (test == null) {
logger.error("Could not retrieve test for " + lid.toString());
debugPrint(testmap);
return null;
}
HpoTermId4LoincTest hpoId = test.loincInterpretationToHpo(observation);
return new BasicLabTestResultInHPO(hpoId, observation, "?");
}
logger.info("interpretation node: " + interpretationNode.asText());
if (interpretationNode.isMissingNode()) {
//
} else {
JsonNode codingNode = interpretationNode.get("coding");
logger.info("coding node: " + codingNode.asText());
if (codingNode.isMissingNode()) {
logger.info("coding node is not array");
} else {
if (!codingNode.isArray()) {
//
} else {
logger.info("coding node is array");
for (JsonNode loinc : codingNode) {
if (!loinc.path("code").isMissingNode()) {
interpretationDetected = true;
interpretationCode = loinc.path("code").asText();
logger.info("code is detected: " + interpretationCode);
// if (interpretationCode.equalsIgnoreCase("H")) {
// return new BasicLabTestResultInHPO()
// }
}
/**
* String system = loinc.path("system").asText();
* logger.info("system: " + system);
* if (!system.equals("http://loinc.org")) {
* System.err.println("[ERROR] Non Loinc code detected...");
* //continue;
* }
*
* String display = loinc.path("display").asText();
* logger.info("display: " + display);
*/
}
}
}
}
if (!interpretationDetected) {
double valueObserved = 9999.0;
JsonNode valueNode = node.get("valueQuantity");
// System.out.println("found valueNode: " + valueNode.asText());
try {
logger.info("value observed: " + valueNode.get("value").asText());
valueObserved = Double.parseDouble(valueNode.get("value").asText());
logger.info("value retrieved: " + valueObserved);
} catch (NumberFormatException e) {
logger.error("measured value is not a double");
}
if (Math.abs(valueObserved - 9999.0) > 0.00001) {
// we want to compare the observed value with the normal range
if (valueObserved < 0) {
interpretationCode = "L";
}
if (valueObserved > 1) {
interpretationCode = "H";
} else {
interpretationCode = "N";
}
}
}
return null;
// return LabTestResultInHPO(interpretationCode);
}
Aggregations