Search in sources :

Example 6 with HpoTermId4LoincTest

use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest 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);
}
Also used : ObservationResultInInternalCode(org.monarchinitiative.loinc2hpo.loinc.ObservationResultInInternalCode) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) WrongElementException(org.monarchinitiative.loinc2hpo.exception.WrongElementException) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) JsonNode(com.fasterxml.jackson.databind.JsonNode) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) BasicLabTestResultInHPO(org.monarchinitiative.loinc2hpo.testresult.BasicLabTestResultInHPO)

Example 7 with HpoTermId4LoincTest

use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest in project loinc2hpo by monarch-initiative.

the class WriteToFile method fromTSVAdvanced.

public static void fromTSVAdvanced(String path, Map<LoincId, UniversalLoinc2HPOAnnotation> deserializedMap, Map<TermId, HpoTerm> hpoTermMap) throws FileNotFoundException {
    BufferedReader reader = new BufferedReader(new FileReader(path));
    reader.lines().forEach(serialized -> {
        String[] elements = serialized.split("\\t");
        if (elements.length == 13 && !serialized.startsWith("loincId")) {
            try {
                LoincId loincId = new LoincId(elements[0]);
                String system = elements[2];
                String codeString = elements[3];
                TermId termId = convertToTermID(elements[4]);
                boolean inverse = Boolean.parseBoolean(elements[5]);
                UniversalLoinc2HPOAnnotation annotation = deserializedMap.get(loincId);
                Code code = Code.getNewCode().setSystem(system).setCode(codeString);
                annotation.addAdvancedAnnotation(code, new HpoTermId4LoincTest(hpoTermMap.get(termId), inverse));
            } catch (MalformedLoincCodeException e) {
                logger.error("Malformed loinc code line: " + serialized);
            }
        } else {
            if (elements.length != 13) {
                logger.error(String.format("line does not have 13 elements, but has %d elements. Line: %s", elements.length, serialized));
            } else {
                logger.info("line is header: " + serialized);
            }
        }
    });
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) MalformedLoincCodeException(org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException) TermId(com.github.phenomics.ontolib.ontology.data.TermId) ImmutableTermId(com.github.phenomics.ontolib.ontology.data.ImmutableTermId) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Aggregations

HpoTermId4LoincTest (org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest)7 Code (org.monarchinitiative.loinc2hpo.codesystems.Code)6 LoincId (org.monarchinitiative.loinc2hpo.loinc.LoincId)6 UniversalLoinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation)6 Loinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation)4 Map (java.util.Map)3 ImmutableTermId (com.github.phenomics.ontolib.ontology.data.ImmutableTermId)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 CodeableConcept (org.hl7.fhir.dstu3.model.CodeableConcept)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 CodeSystemConvertor (org.monarchinitiative.loinc2hpo.codesystems.CodeSystemConvertor)2 Loinc2HPOCodedValue (org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue)2 org.monarchinitiative.loinc2hpo.exception (org.monarchinitiative.loinc2hpo.exception)2 MalformedLoincCodeException (org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HpoTerm (com.github.phenomics.ontolib.formats.hpo.HpoTerm)1 ImmutableTermPrefix (com.github.phenomics.ontolib.ontology.data.ImmutableTermPrefix)1 TermId (com.github.phenomics.ontolib.ontology.data.TermId)1