Search in sources :

Example 1 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project loinc2hpo by monarch-initiative.

the class ObservationAnalysisFromInterpretation method getHPOforObservation.

@Override
public HpoTermId4LoincTest getHPOforObservation() throws UnsupportedCodingSystemException, AmbiguousResultsFoundException, AnnotationNotFoundException, UnrecognizedCodeException {
    // here we use a map to store the results: since there could be more than one interpretation coding system,
    // we try them all and store the results in a map <external code, result in internal code>
    Map<Code, Code> results = new HashMap<>();
    // get the annotation class for this loinc code
    UniversalLoinc2HPOAnnotation annotationForLoinc = annotationMap.get(this.loincId);
    if (annotationForLoinc == null)
        throw new AnnotationNotFoundException();
    // all interpretation codes in different coding systems. Expect one in most cases.
    Set<Code> interpretationCodes = getInterpretationCodes();
    interpretationCodes.stream().filter(p -> CodeSystemConvertor.getCodeContainer().getCodeSystemMap().containsKey(p.getSystem())).forEach(p -> {
        Code internalCode = null;
        try {
            internalCode = CodeSystemConvertor.convertToInternalCode(p);
            results.put(p, internalCode);
        } catch (InternalCodeNotFoundException e) {
            e.printStackTrace();
        }
    });
    List<Code> distinct = results.values().stream().distinct().collect(Collectors.toList());
    if (distinct.size() == 1) {
        HpoTermId4LoincTest hpoTermId4LoincTest = annotationForLoinc.loincInterpretationToHPO(distinct.get(0));
        if (hpoTermId4LoincTest == null)
            throw new UnrecognizedCodeException();
        return hpoTermId4LoincTest;
    } else {
        throw new AmbiguousResultsFoundException();
    }
}
Also used : java.util(java.util) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) CodeSystemConvertor(org.monarchinitiative.loinc2hpo.codesystems.CodeSystemConvertor) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) org.monarchinitiative.loinc2hpo.exception(org.monarchinitiative.loinc2hpo.exception) Collectors(java.util.stream.Collectors) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Example 2 with Annotation

use of org.hl7.fhir.r4b.model.Annotation 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)

Example 3 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project gpconnect-demonstrator by nhsconnect.

the class WarningCodeExtHelper method addWarningCodeExtensions.

/**
 * confidential items are per record but the other two are global values per
 * patient
 *
 * @param warningCodes
 * @param list
 * @param patientRepository
 * @param medicationStatementRepository
 * @param structuredAllergySearch
 */
public static void addWarningCodeExtensions(Set<String> warningCodes, ListResource list, PatientRepository patientRepository, MedicationStatementRepository medicationStatementRepository, StructuredAllergySearch structuredAllergySearch) {
    String NHS = list.getSubject().getIdentifier().getValue();
    PatientEntity patientEntity = patientRepository.findByNhsNumber(NHS);
    List<MedicationStatementEntity> medicationStatements = medicationStatementRepository.findByPatientId(patientEntity.getId());
    for (MedicationStatementEntity medicationStatement : medicationStatements) {
        setFlags(medicationStatement.getWarningCode());
    }
    List<StructuredAllergyIntoleranceEntity> allergies = structuredAllergySearch.getAllergyIntollerence(NHS);
    for (StructuredAllergyIntoleranceEntity allergy : allergies) {
        setFlags(allergy.getWarningCode());
    }
    // check medication_statements for either of the global flags
    if (dataInTransit) {
        warningCodes.add(DATA_IN_TRANSIT);
    }
    if (dataAwaitingFiling) {
        warningCodes.add(DATA_AWAITING_FILING);
    }
    StringBuilder sb = new StringBuilder();
    warningCodes.forEach(warningCode -> {
        if (warningCode != null) {
            String warningCodeDisplay = "";
            Annotation annotation = new Annotation();
            switch(warningCode) {
                case CONFIDENTIAL_ITEMS:
                    warningCodeDisplay = "Confidential Items";
                    // #266
                    annotation.setText(CONFIDENTIAL_ITEMS_NOTE);
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                case DATA_IN_TRANSIT:
                    warningCodeDisplay = "Data in Transit";
                    Calendar cal = new GregorianCalendar();
                    Date now = new Date();
                    cal.setTime(now);
                    // a week before now
                    cal.add(Calendar.DAY_OF_YEAR, -7);
                    // #266
                    annotation.setText(String.format(DATA_IN_TRANSIT_NOTE, DATE_FORMAT.format(cal.getTime())));
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                case DATA_AWAITING_FILING:
                    warningCodeDisplay = "Data Awaiting Filing";
                    // #266
                    annotation.setText(DATA_AWAITING_FILING_NOTE);
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                default:
                    break;
            }
            // #182
            Extension warningExt = new Extension(SystemURL.WARNING_CODE, new CodeType(warningCode));
            list.addExtension(warningExt);
        }
    });
    // cardinality of note 0..1 #266
    if (sb.length() > 0) {
        Annotation annotation = null;
        if (list.getNote().size() > 0) {
            annotation = list.getNote().get(0);
            annotation.setText(annotation.getText());
            annotation.setText(annotation.getText() + sb.toString());
        } else {
            annotation = new Annotation();
            list.addNote(annotation);
            annotation.setText(sb.toString().replaceFirst("^\r\n", ""));
        }
    }
}
Also used : StructuredAllergyIntoleranceEntity(uk.gov.hscic.patient.structuredAllergyIntolerance.StructuredAllergyIntoleranceEntity) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Annotation(org.hl7.fhir.dstu3.model.Annotation) Date(java.util.Date) PatientEntity(uk.gov.hscic.patient.details.PatientEntity) MedicationStatementEntity(uk.gov.hscic.medication.statement.MedicationStatementEntity) Extension(org.hl7.fhir.dstu3.model.Extension) CodeType(org.hl7.fhir.dstu3.model.CodeType)

Example 4 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project bunsen by cerner.

the class FhirEncodersTest method annotation.

@Test
public void annotation() throws FHIRException {
    Annotation original = medRequest.getNoteFirstRep();
    Annotation decoded = decodedMedRequest.getNoteFirstRep();
    Assert.assertEquals(original.getText(), medDataset.select(functions.expr("note[0].text")).head().get(0));
    Assert.assertEquals(original.getText(), decoded.getText());
    Assert.assertEquals(original.getAuthorReference().getReference(), decoded.getAuthorReference().getReference());
}
Also used : Annotation(org.hl7.fhir.dstu3.model.Annotation) Test(org.junit.Test)

Example 5 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project bunsen by cerner.

the class TestData method newMedRequest.

/**
 * Returns a FHIR medication request for testing purposes.
 */
public static MedicationRequest newMedRequest() {
    MedicationRequest medReq = new MedicationRequest();
    medReq.setId("test-med");
    // Medication code
    CodeableConcept med = new CodeableConcept();
    med.addCoding().setSystem("http://www.nlm.nih.gov/research/umls/rxnorm").setCode("582620").setDisplay("Nizatidine 15 MG/ML Oral Solution [Axid]");
    med.setText("Nizatidine 15 MG/ML Oral Solution [Axid]");
    medReq.setMedication(med);
    Annotation annotation = new Annotation();
    annotation.setText("Test medication note.");
    annotation.setAuthor(new Reference("Provider/example").setDisplay("Example provider."));
    medReq.addNote(annotation);
    return medReq;
}
Also used : MedicationRequest(org.hl7.fhir.dstu3.model.MedicationRequest) Reference(org.hl7.fhir.dstu3.model.Reference) Annotation(org.hl7.fhir.dstu3.model.Annotation) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept)

Aggregations

NotImplementedException (org.apache.commons.lang3.NotImplementedException)14 Trace (com.newrelic.api.agent.Trace)13 Operation (gov.cms.bfd.server.war.Operation)13 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 Annotation (org.hl7.fhir.r4.model.Annotation)12 NoResultException (javax.persistence.NoResultException)11 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)9 Annotation (org.hl7.fhir.dstu3.model.Annotation)9 Search (ca.uhn.fhir.rest.annotation.Search)8 OffsetLinkBuilder (gov.cms.bfd.server.war.commons.OffsetLinkBuilder)8 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)8 Read (ca.uhn.fhir.rest.annotation.Read)7 Beneficiary (gov.cms.bfd.model.rif.Beneficiary)7 Patient (org.hl7.fhir.dstu3.model.Patient)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)6 Resource (org.hl7.fhir.r4.model.Resource)6 Test (org.junit.jupiter.api.Test)6