Search in sources :

Example 11 with Observation

use of org.hl7.fhir.dstu3.model.Observation in project loinc2hpo by monarch-initiative.

the class Loinc2HpoConversionTabController method handleConvertButton.

@FXML
void handleConvertButton(ActionEvent event) {
    String path = model.getPathToJsonFhirFile();
    Observation observation = FhirResourceRetriever.parseJsonFile2Observation(path);
    FhirObservationAnalyzer.setObservation(observation);
    LabTestResultInHPO res = FhirObservationAnalyzer.getHPO4ObservationOutcome(model.getLoincIds(), model.getLoincAnnotationMap());
    ObservableList<String> items = FXCollections.observableArrayList();
    if (res == null) {
        items.add(observation.getId() + ": failed not interpret");
    } else {
        TermId id = res.getTermId();
        String name = model.termId2HpoName(id);
        String display = String.format("%s: %s [%s]", observation.getId(), name, id.getIdWithPrefix());
        if (res.isNegated()) {
            display = String.format("%s: NOT %s [%s]", observation.getId(), name, id.getIdWithPrefix());
        }
        items.add(display);
    }
    patientPhenotypeTableView.setItems(items);
}
Also used : Observation(org.hl7.fhir.dstu3.model.Observation) LabTestResultInHPO(org.monarchinitiative.loinc2hpo.testresult.LabTestResultInHPO) TermId(com.github.phenomics.ontolib.ontology.data.TermId) FXML(javafx.fxml.FXML)

Example 12 with Observation

use of org.hl7.fhir.dstu3.model.Observation 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;
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) BasicLabTestResultInHPO(org.monarchinitiative.loinc2hpo.testresult.BasicLabTestResultInHPO)

Example 13 with Observation

use of org.hl7.fhir.dstu3.model.Observation in project loinc2hpo by monarch-initiative.

the class FhirResourceRetriever method retrievePatientFromServer.

/**
 * Retrieve a patient from the reference field of an observation
 * @param subject
 * @return
 */
public static Patient retrievePatientFromServer(Reference subject) throws SubjectNotFoundException, AmbiguousSubjectException {
    List<Patient> patients = new ArrayList<>();
    if (subject.hasReference()) {
        String ref = subject.getReference();
        if (!ref.startsWith(BASEURL) && ref.startsWith("Patient")) {
            ref = BASEURL + "/" + ref;
        }
        Bundle patientBundle = client.search().byUrl(ref).returnBundle(Bundle.class).execute();
        while (true) {
            patientBundle.getEntry().forEach(p -> patients.add((Patient) p.getResource()));
            if (patientBundle.getLink(IBaseBundle.LINK_NEXT) != null) {
                patientBundle = client.loadPage().next(patientBundle).execute();
            } else {
                break;
            }
        }
    } else if (subject.hasIdentifier()) {
        Identifier identifier = subject.getIdentifier();
    // TODO: find patient through the identifier
    }
    if (patients.size() == 1) {
        return patients.iterator().next();
    } else if (patients.isEmpty()) {
        throw new SubjectNotFoundException("Expect one subject, but found none");
    } else {
        throw new AmbiguousSubjectException("Except one subject, but found multiple");
    }
}
Also used : SubjectNotFoundException(org.monarchinitiative.loinc2hpo.exception.SubjectNotFoundException) AmbiguousSubjectException(org.monarchinitiative.loinc2hpo.exception.AmbiguousSubjectException) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) ArrayList(java.util.ArrayList)

Example 14 with Observation

use of org.hl7.fhir.dstu3.model.Observation in project loinc2hpo by monarch-initiative.

the class FhirResourceRetriever method retrieveObservationFromServer.

/**
 * @TODO: implement it
 * retrieve a patient's observations from FHIR server
 * @param patient
 * @return
 */
public static List<Observation> retrieveObservationFromServer(Patient patient) {
    List<Observation> observationList = new ArrayList<>();
    String id = patient.getId();
    if (id != null) {
        Bundle observationBundle = client.search().forResource(Observation.class).where(new ReferenceClientParam("subject").hasId(id)).prettyPrint().returnBundle(Bundle.class).execute();
        while (true) {
            observationBundle.getEntry().forEach(p -> observationList.add((Observation) p.getResource()));
            if (observationBundle.getLink(IBaseBundle.LINK_NEXT) != null) {
                observationBundle = client.loadPage().next(observationBundle).execute();
            } else {
                break;
            }
        }
    }
    return observationList;
}
Also used : IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) ArrayList(java.util.ArrayList) ReferenceClientParam(ca.uhn.fhir.rest.gclient.ReferenceClientParam)

Example 15 with Observation

use of org.hl7.fhir.dstu3.model.Observation in project loinc2hpo by monarch-initiative.

the class FhirObservationRetrieverTest method testParseJsonFile2ObservationException.

@Test
public void testParseJsonFile2ObservationException() {
    String path = getClass().getClassLoader().getResource("json/malformedObservation.fhir").getPath();
    Observation observation = FhirResourceRetriever.parseJsonFile2Observation(path);
    assertNull(observation);
}
Also used : Observation(org.hl7.fhir.dstu3.model.Observation) Test(org.junit.Test)

Aggregations

Observation (org.hl7.fhir.dstu3.model.Observation)15 Outcome (org.monarchinitiative.loinc2hpocore.codesystems.Outcome)7 Test (org.junit.jupiter.api.Test)6 LoincId (org.monarchinitiative.loinc2hpocore.loinc.LoincId)6 ObservationDtu3 (org.monarchinitiative.loinc2hpofhir.fhir2hpo.ObservationDtu3)6 Uberobservation (org.monarchinitiative.loinc2hpofhir.fhir2hpo.Uberobservation)6 TermId (com.github.phenomics.ontolib.ontology.data.TermId)4 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)4 BeforeClass (org.junit.BeforeClass)4 HpoOntology (com.github.phenomics.ontolib.formats.hpo.HpoOntology)3 HpoTerm (com.github.phenomics.ontolib.formats.hpo.HpoTerm)3 HpoOboParser (com.github.phenomics.ontolib.io.obo.hpo.HpoOboParser)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 TestBase.importDstu3Observation (fhir.TestBase.importDstu3Observation)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Code (org.monarchinitiative.loinc2hpo.codesystems.Code)3 Test (org.junit.Test)2 ReferenceClientParam (ca.uhn.fhir.rest.gclient.ReferenceClientParam)1