Search in sources :

Example 1 with Loinc2HPOCodedValue

use of org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue in project loinc2hpo by monarch-initiative.

the class ObservationAnalysisFromQnValue method getHPOforObservation.

@Override
public HpoTermId4LoincTest getHPOforObservation() throws ReferenceNotFoundException, AmbiguousReferenceException, UnrecognizedCodeException {
    HpoTermId4LoincTest hpoTermId4LoincTest = null;
    // find applicable reference range
    List<Observation.ObservationReferenceRangeComponent> references = this.references.stream().filter(p -> withinAgeRange(p)).collect(Collectors.toList());
    if (references.size() < 1) {
        throw new ReferenceNotFoundException();
    } else if (references.size() == 1) {
        Observation.ObservationReferenceRangeComponent targetReference = references.get(0);
        double low = targetReference.hasLow() ? targetReference.getLow().getValue().doubleValue() : Double.MIN_VALUE;
        double high = targetReference.hasHigh() ? targetReference.getHigh().getValue().doubleValue() : Double.MAX_VALUE;
        double observed = valueQuantity.getValue().doubleValue();
        Loinc2HPOCodedValue result;
        if (observed < low) {
            result = Loinc2HPOCodedValue.fromCode("L");
        } else if (observed > high) {
            result = Loinc2HPOCodedValue.fromCode("H");
        } else {
            result = Loinc2HPOCodedValue.fromCode("N");
        }
        Code resultCode = Code.getNewCode().setSystem(Loinc2HPOCodedValue.CODESYSTEM).setCode(result.toCode());
        hpoTermId4LoincTest = annotationMap.get(loincId).loincInterpretationToHPO(resultCode);
    } else if (references.size() == 2) {
        // what does it mean with multiple references
        throw new AmbiguousReferenceException();
    } else if (references.size() == 3) {
    // it can happen when there is actually one range but coded in three ranges
    // e.g. normal 20-30
    // in this case, one range ([20, 30]) is sufficient;
    // however, it is written as three ranges: ( , 20) [20, 30] (30, )
    // We should handle this case
    } else {
        throw new AmbiguousReferenceException();
    }
    // if we can still not find an answer, it is probably that we did not have the annotation
    if (hpoTermId4LoincTest == null)
        throw new UnrecognizedCodeException();
    return hpoTermId4LoincTest;
}
Also used : AgeCalculator(org.monarchinitiative.loinc2hpo.util.AgeCalculator) Date(java.util.Date) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) org.monarchinitiative.loinc2hpo.exception(org.monarchinitiative.loinc2hpo.exception) Collectors(java.util.stream.Collectors) BigDecimal(java.math.BigDecimal) List(java.util.List) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) Year(java.time.Year) LocalDate(java.time.LocalDate) Map(java.util.Map) org.hl7.fhir.dstu3.model(org.hl7.fhir.dstu3.model) FHIRException(org.hl7.fhir.exceptions.FHIRException) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Aggregations

BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1 Year (java.time.Year)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 org.hl7.fhir.dstu3.model (org.hl7.fhir.dstu3.model)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1 Code (org.monarchinitiative.loinc2hpo.codesystems.Code)1 Loinc2HPOCodedValue (org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue)1 org.monarchinitiative.loinc2hpo.exception (org.monarchinitiative.loinc2hpo.exception)1 HpoTermId4LoincTest (org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest)1 Loinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation)1 LoincId (org.monarchinitiative.loinc2hpo.loinc.LoincId)1 UniversalLoinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation)1 AgeCalculator (org.monarchinitiative.loinc2hpo.util.AgeCalculator)1