use of com.github.phenomics.ontolib.ontology.data.ImmutableTermId 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 com.github.phenomics.ontolib.ontology.data.ImmutableTermId in project loinc2hpo by monarch-initiative.
the class FromFile method getHpoTermId.
TermId getHpoTermId(String termString) throws MalformedHpoTermIdException {
logger.trace("getHpoTermId=" + termString);
if (termString.equalsIgnoreCase("NA"))
return null;
int i = termString.indexOf(":");
if (i != 2)
throw new MalformedHpoTermIdException("Malformed HPO String: " + termString);
TermId id = new ImmutableTermId(HPPREFIX, termString.substring(3));
if (ontology.getTermMap().containsKey(id))
return id;
else
throw new MalformedHpoTermIdException("Could not find id " + termString);
}
use of com.github.phenomics.ontolib.ontology.data.ImmutableTermId in project loinc2hpo by monarch-initiative.
the class WriteToFile method convertToTermID.
private static TermId convertToTermID(String record) {
TermPrefix prefix = new ImmutableTermPrefix("HP");
if (!record.startsWith(prefix.getValue()) || record.length() <= 3) {
logger.error("Non HPO termId is detected from TSV");
return null;
}
String id = record.substring(3);
return new ImmutableTermId(prefix, id);
}
Aggregations