Search in sources :

Example 1 with UniversalLoinc2HPOAnnotation

use of org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation 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 UniversalLoinc2HPOAnnotation

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

the class WriteToFile method appendtoTSV.

public static void appendtoTSV(String path, Map<LoincId, UniversalLoinc2HPOAnnotation> annotationMap) throws IOException {
    StringBuilder builder = new StringBuilder();
    for (UniversalLoinc2HPOAnnotation annotation : annotationMap.values()) {
        builder.append("\n");
        builder.append(annotation.toString());
    }
    appendToFile(builder.toString(), path);
}
Also used : UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation)

Example 3 with UniversalLoinc2HPOAnnotation

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

the class WriteToFile method toTSV.

public static void toTSV(String path, Map<LoincId, UniversalLoinc2HPOAnnotation> annotationMap) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(path));
    writer.write(UniversalLoinc2HPOAnnotation.getHeaderAdvanced());
    for (UniversalLoinc2HPOAnnotation annotation : annotationMap.values()) {
        writer.newLine();
        writer.write(annotation.toString());
    }
    writer.close();
}
Also used : UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation)

Example 4 with UniversalLoinc2HPOAnnotation

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

the class WriteToFile method fromTSV.

/**
 * A method to deserialize annotation map from a TSV file.
 * @param path filepath to the TSV
 * @param hpoTermMap a HPO map from TermId to HpoTerm. Note: the key is TermId, instead of TermName
 * @return an annotation map
 * @throws FileNotFoundException
 */
public static Map<LoincId, UniversalLoinc2HPOAnnotation> fromTSV(String path, Map<TermId, HpoTerm> hpoTermMap) throws FileNotFoundException {
    Map<LoincId, UniversalLoinc2HPOAnnotation> deserializedMap = new LinkedHashMap<>();
    Map<LoincId, UniversalLoinc2HPOAnnotation.Builder> builderMap = new HashMap<>();
    Map<String, Code> internalCode = CodeSystemConvertor.getCodeContainer().getCodeSystemMap().get(Loinc2HPOCodedValue.CODESYSTEM);
    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]);
                LoincScale loincScale = LoincScale.string2enum(elements[1]);
                String codeSystem = elements[2];
                String codeId = elements[3];
                TermPrefix prefix = new ImmutableTermPrefix(elements[4].substring(0, 2));
                String id = elements[4].substring(3);
                HpoTerm hpoTerm = hpoTermMap.get(new ImmutableTermId(prefix, id));
                boolean inverse = Boolean.parseBoolean(elements[5]);
                String note = elements[6].equals(MISSINGVALUE) ? null : elements[6];
                boolean flag = Boolean.parseBoolean(elements[7]);
                double version = Double.parseDouble(elements[8]);
                LocalDateTime createdOn = elements[9].equals(MISSINGVALUE) ? null : LocalDateTime.parse(elements[9]);
                String createdBy = elements[10].equals(MISSINGVALUE) ? null : elements[10];
                LocalDateTime lastEditedOn = elements[11].equals(MISSINGVALUE) ? null : LocalDateTime.parse(elements[11]);
                String lastEditedBy = elements[12].equals(MISSINGVALUE) ? null : elements[12];
                if (!builderMap.containsKey(loincId)) {
                    UniversalLoinc2HPOAnnotation.Builder builder = new UniversalLoinc2HPOAnnotation.Builder().setLoincId(loincId).setLoincScale(loincScale).setNote(note).setFlag(flag).setVersion(version).setCreatedOn(createdOn).setCreatedBy(createdBy).setLastEditedOn(lastEditedOn).setLastEditedBy(lastEditedBy);
                    builderMap.put(loincId, builder);
                }
                Code code = Code.getNewCode().setSystem(codeSystem).setCode(codeId);
                HpoTermId4LoincTest hpoTermId4LoincTest = new HpoTermId4LoincTest(hpoTerm, inverse);
                if (code.equals(internalCode.get("L"))) {
                    builderMap.get(loincId).setLowValueHpoTerm(hpoTermId4LoincTest.getHpoTerm());
                }
                if (code.equals(internalCode.get("N"))) {
                    builderMap.get(loincId).setIntermediateValueHpoTerm(hpoTermId4LoincTest.getHpoTerm());
                    builderMap.get(loincId).setIntermediateNegated(hpoTermId4LoincTest.isNegated());
                }
                if (code.equals(internalCode.get("H"))) {
                    builderMap.get(loincId).setHighValueHpoTerm(hpoTermId4LoincTest.getHpoTerm());
                }
                if (code.equals(internalCode.get("A")) || code.equals(internalCode.get("P")) || code.equals(internalCode.get("NP"))) {
                    // currently, we neglect those codes
                    // it will be wrong to do so if the user has manually changed what map to them
                    logger.info("!!!!!!!!!!!annotation neglected. MAY BE WRONG!!!!!!!!!!!!!!!");
                } else {
                    builderMap.get(loincId).addAdvancedAnnotation(code, hpoTermId4LoincTest);
                }
            } 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();
    }
    builderMap.entrySet().forEach(b -> deserializedMap.put(b.getKey(), b.getValue().build()));
    return deserializedMap;
}
Also used : LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincScale(org.monarchinitiative.loinc2hpo.loinc.LoincScale) TermPrefix(com.github.phenomics.ontolib.ontology.data.TermPrefix) ImmutableTermPrefix(com.github.phenomics.ontolib.ontology.data.ImmutableTermPrefix) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) MalformedLoincCodeException(org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) HpoTerm(com.github.phenomics.ontolib.formats.hpo.HpoTerm) ImmutableTermId(com.github.phenomics.ontolib.ontology.data.ImmutableTermId) ImmutableTermPrefix(com.github.phenomics.ontolib.ontology.data.ImmutableTermPrefix)

Example 5 with UniversalLoinc2HPOAnnotation

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

the class WriteToFile method fromTSVBasic.

public static Map<LoincId, UniversalLoinc2HPOAnnotation> fromTSVBasic(String path, Map<TermId, HpoTerm> hpoTermMap) throws FileNotFoundException {
    Map<LoincId, UniversalLoinc2HPOAnnotation> deserializedMap = new LinkedHashMap<>();
    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]);
                LoincScale loincScale = LoincScale.string2enum(elements[1]);
                TermId low = convertToTermID(elements[2]);
                TermId intermediate = convertToTermID(elements[3]);
                TermId high = convertToTermID(elements[4]);
                boolean inverse = Boolean.parseBoolean(elements[5]);
                String note = elements[6].equals(MISSINGVALUE) ? null : elements[6];
                boolean flag = Boolean.parseBoolean(elements[7]);
                double version = Double.parseDouble(elements[8]);
                LocalDateTime createdOn = elements[9].equals(MISSINGVALUE) ? null : LocalDateTime.parse(elements[9]);
                String createdBy = elements[10].equals(MISSINGVALUE) ? null : elements[10];
                LocalDateTime lastEditedOn = elements[11].equals(MISSINGVALUE) ? null : LocalDateTime.parse(elements[11]);
                String lastEditedBy = elements[12].equals(MISSINGVALUE) ? null : elements[12];
                if (!deserializedMap.containsKey(loincId)) {
                    UniversalLoinc2HPOAnnotation.Builder builder = new UniversalLoinc2HPOAnnotation.Builder();
                    builder.setLoincId(loincId).setLoincScale(loincScale).setLowValueHpoTerm(hpoTermMap.get(low)).setIntermediateValueHpoTerm(hpoTermMap.get(intermediate)).setHighValueHpoTerm(hpoTermMap.get(high)).setIntermediateNegated(inverse).setCreatedOn(createdOn).setCreatedBy(createdBy).setLastEditedOn(lastEditedOn).setLastEditedBy(lastEditedBy).setVersion(version).setNote(note).setFlag(flag);
                    deserializedMap.put(loincId, builder.build());
                }
            } 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();
    }
    return deserializedMap;
}
Also used : LocalDateTime(java.time.LocalDateTime) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) MalformedLoincCodeException(org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException) LinkedHashMap(java.util.LinkedHashMap) TermId(com.github.phenomics.ontolib.ontology.data.TermId) ImmutableTermId(com.github.phenomics.ontolib.ontology.data.ImmutableTermId) LoincScale(org.monarchinitiative.loinc2hpo.loinc.LoincScale)

Aggregations

UniversalLoinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation)8 LoincId (org.monarchinitiative.loinc2hpo.loinc.LoincId)4 ImmutableTermId (com.github.phenomics.ontolib.ontology.data.ImmutableTermId)3 Code (org.monarchinitiative.loinc2hpo.codesystems.Code)3 MalformedLoincCodeException (org.monarchinitiative.loinc2hpo.exception.MalformedLoincCodeException)3 HpoTermId4LoincTest (org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest)3 TermId (com.github.phenomics.ontolib.ontology.data.TermId)2 LocalDateTime (java.time.LocalDateTime)2 LinkedHashMap (java.util.LinkedHashMap)2 LoincScale (org.monarchinitiative.loinc2hpo.loinc.LoincScale)2 HpoTerm (com.github.phenomics.ontolib.formats.hpo.HpoTerm)1 ImmutableTermPrefix (com.github.phenomics.ontolib.ontology.data.ImmutableTermPrefix)1 TermPrefix (com.github.phenomics.ontolib.ontology.data.TermPrefix)1 java.util (java.util)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 CodeableConcept (org.hl7.fhir.dstu3.model.CodeableConcept)1 CodeSystemConvertor (org.monarchinitiative.loinc2hpo.codesystems.CodeSystemConvertor)1 org.monarchinitiative.loinc2hpo.exception (org.monarchinitiative.loinc2hpo.exception)1 Loinc2HPOAnnotation (org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation)1