use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest in project loinc2hpo by monarch-initiative.
the class ObservationAnalysisFromCodedValues method getHPOforObservation.
@Override
public HpoTermId4LoincTest getHPOforObservation() throws AmbiguousResultsFoundException, UnrecognizedCodeException, AnnotationNotFoundException {
if (annotationMap.get(loincId) == null)
throw new AnnotationNotFoundException();
Set<HpoTermId4LoincTest> results = new HashSet<>();
codedValue.getCoding().stream().filter(p -> annotationMap.get(loincId).getCodes().contains(new Code(p))).forEach(p -> results.add(annotationMap.get(loincId).loincInterpretationToHPO(new Code(p))));
if (results.size() > 1) {
throw new AmbiguousResultsFoundException();
}
if (results.size() == 1) {
return results.iterator().next();
} else {
throw new UnrecognizedCodeException();
}
}
use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest 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();
}
}
use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest 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;
}
use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest 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;
}
use of org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest in project loinc2hpo by monarch-initiative.
the class CurrentAnnotationController method populateTables.
private void populateTables() {
if (model == null) {
logger.error("model is null.");
return;
}
this.currentAnnotation = model.getCurrentAnnotation();
LoincEntry currentLoincEntry = model.getLoincEntryMap().get(currentAnnotation.getLoincId());
annotationTitle.setText(String.format("Annotations for Loinc: %s[%s]", currentLoincEntry.getLOINC_Number(), currentLoincEntry.getLongName()));
internalCodeAnnotations.clear();
currentAnnotation.getCandidateHpoTerms().entrySet().stream().filter(p -> p.getKey().getSystem().equals(Loinc2HPOCodedValue.CODESYSTEM)).map(p -> new Annotation(p.getKey(), p.getValue())).forEach(internalCodeAnnotations::add);
externalCodeAnnotations.clear();
currentAnnotation.getCandidateHpoTerms().entrySet().stream().filter(p -> !p.getKey().getSystem().equals(Loinc2HPOCodedValue.CODESYSTEM)).map(p -> new Annotation(p.getKey(), p.getValue())).forEach(externalCodeAnnotations::add);
interpretationCodeAnnotations.clear();
for (Map.Entry<Code, Code> entry : CodeSystemConvertor.getCodeConversionMap().entrySet()) {
logger.debug("key: " + entry.getKey() + "\nvalue: " + entry.getValue());
HpoTermId4LoincTest result = currentAnnotation.loincInterpretationToHPO(entry.getValue());
logger.debug("result is null? " + (result == null));
if (result != null) {
Annotation annotation = new Annotation(entry.getKey(), result);
interpretationCodeAnnotations.add(annotation);
logger.debug("interpretationCodeAnnotations size: " + interpretationCodeAnnotations.size());
}
}
logger.debug("internalTableview is null: " + (internalTableview == null));
logger.debug("internalCodeAnnotations is null: " + (internalTableview == null));
logger.debug("internal annotation size: " + internalCodeAnnotations.size());
internalCodeAnnotations.forEach(logger::info);
logger.trace("exit initInternalTableview()");
}
Aggregations