Skip to content

Commit

Permalink
Merge pull request #473 from EBISPOT/mapping-fixes2
Browse files Browse the repository at this point in the history
Mapping fixes2
  • Loading branch information
henrietteharmse authored Sep 27, 2023
2 parents b2708a4 + 477d0bd commit ca030ef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 50 deletions.
15 changes: 8 additions & 7 deletions dataload/extras/json2sssom/src/main/java/CurieMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ public CurieMapping mapEntity(JsonObject entityOrLinkedEntity) {
String curieLocalPart = curie.split(":")[1];

if(iriOrUrl == null || !iriOrUrl.endsWith(curieLocalPart)) {
System.out.println(iriOrUrl + " does not end with local part of curie " + curie + ". The curie will be printed without adding a prefix to the curie_map.");
System.out.println(iriOrUrl + " does not end with local part of curie " + curie + ". This mapping will be omitted from the results.");

// We can't print the iri/url in SSSOM and we can't put the CURIE in the prefix map
// Not sure whether to drop the mapping or just to print the CURIE. So we'll print the CURIE for now.
// TODO: Currently we just drop the mapping, maybe a better way to approach this.
//
CurieMapping mapping = new CurieMapping();
mapping.curiePrefix = curiePrefix;
mapping.curieLocalPart = curieLocalPart;
mapping.curie = curie;
return mapping;
return null;
// CurieMapping mapping = new CurieMapping();
// mapping.curiePrefix = curiePrefix;
// mapping.curieLocalPart = curieLocalPart;
// mapping.curie = curie;
// return mapping;
}

String curieNamespace = iriOrUrl.substring(0, iriOrUrl.length() - curieLocalPart.length());
Expand Down
93 changes: 50 additions & 43 deletions dataload/extras/json2sssom/src/main/java/JSON2SSSOM.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,42 @@ public static void writeMappingsForEntity(JsonObject entity, CSVPrinter writer,

JsonElement exactMatch = entity.get("http://www.w3.org/2004/02/skos/core#exactMatch");
if(exactMatch != null) {
writeMappingsForEntity(entity, "skos:exactMatch", exactMatch, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "skos:exactMatch", exactMatch, null, null, ontologyProperties, writer, curieMap);
}
JsonElement hasDbXref = entity.get("http://www.geneontology.org/formats/oboInOwl#hasDbXref");
if(hasDbXref != null) {
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", hasDbXref, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", hasDbXref, null, null, ontologyProperties, writer, curieMap);
}
JsonElement equivalentClass = entity.get("http://www.w3.org/2002/07/owl#equivalentClass");
if(equivalentClass != null) {
writeMappingsForEntity(entity, "owl:equivalentClass", equivalentClass, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "owl:equivalentClass", equivalentClass, null, null, ontologyProperties, writer, curieMap);
}

// chemical specific mapping predicates
//
// hacky special cases for chemical specific mapping predicates
//
JsonElement inchi = entity.get("http://purl.obolibrary.org/obo/chebi/inchi");
if(inchi != null) {
writeMappingsForEntity(entity, "chebi:inchi", inchi, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", inchi, "inchi:", null, ontologyProperties, writer, curieMap);
}
JsonElement inchiKey = entity.get("http://purl.obolibrary.org/obo/chebi/inchikey");
if(inchiKey != null) {
writeMappingsForEntity(entity, "chebi:inchikey", inchiKey, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", inchiKey, "inchikey:", null, ontologyProperties, writer, curieMap);
}
JsonElement smiles = entity.get("http://purl.obolibrary.org/obo/chebi/smiles");
if(smiles != null) {
writeMappingsForEntity(entity, "chebi:smiles", smiles, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, "oboInOwl:hasDbXref", smiles, "smiles:", null, ontologyProperties, writer, curieMap);
}
}

public static void writeMappingsForEntity(JsonObject entity, String predicate, JsonElement mappingValue, JsonObject reificationMetadata, Map<String,JsonElement> ontologyProperties, CSVPrinter writer, CurieMap curieMap) throws IOException {
public static void writeMappingsForEntity(
JsonObject entity,
String predicate,
JsonElement mappingValue,
String valuePrefix,
JsonObject reificationMetadata,
Map<String,JsonElement> ontologyProperties,
CSVPrinter writer,
CurieMap curieMap) throws IOException {

JsonElement isDefiningOntology = entity.get("isDefiningOntology");

Expand All @@ -200,7 +208,7 @@ public static void writeMappingsForEntity(JsonObject entity, String predicate, J
if(mappingValue.isJsonArray()) {
for(JsonElement entry : mappingValue.getAsJsonArray()) {
if(entry.isJsonObject()) {
writeMappingsForEntity(entity, predicate, entry, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, predicate, entry, valuePrefix, null, ontologyProperties, writer, curieMap);
}
}
} else if(mappingValue.isJsonObject()) {
Expand All @@ -210,10 +218,10 @@ public static void writeMappingsForEntity(JsonObject entity, String predicate, J
List<String> types = JsonHelper.jsonArrayToStrings(type.getAsJsonArray());
if(types.contains("reification")) {
JsonElement value = exactMatchObj.get("value");
writeMappingsForEntity(entity, predicate, value, exactMatchObj, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, predicate, value, valuePrefix, exactMatchObj, ontologyProperties, writer, curieMap);
} else if(types.contains("literal")) {
JsonElement value = exactMatchObj.get("value");
writeMappingsForEntity(entity, predicate, value, null, ontologyProperties, writer, curieMap);
writeMappingsForEntity(entity, predicate, value, valuePrefix, null, ontologyProperties, writer, curieMap);
}
} else {
// System.out.println("mapping value was object but had no type? " + gson.toJson(mappingValue));
Expand All @@ -228,50 +236,49 @@ public static void writeMappingsForEntity(JsonObject entity, String predicate, J
CurieMap.CurieMapping subjCurie = curieMap.mapEntity(entity);

if(subjCurie == null) {
return;
}
return;
}

String subject_id = subjCurie.curie != null ? subjCurie.curie : subjCurie.iriOrUrl;
String subject_id = subjCurie.curie != null ? subjCurie.curie : subjCurie.iriOrUrl;



String value = mappingValue.getAsString();

String object_id = null;
String object_label = "";
String object_id = null;
String object_label = "";


if(valuePrefix != null) {

// hack for chemical mappings (smiles, inchi, inchikey)
object_id = valuePrefix + value;
object_label = value;

} else {

// TODO: hacks for specific chemical mappings, should generalise
if(predicate.equals("chebi:inchikey")) {
object_id = "inchikey:" + value;
object_label = value;
} else if(predicate.equals("chebi:inchi")) {
object_id = "inchi:" + value;
object_label = value;
} else if(predicate.equals("chebi:smiles")) {
object_id = "smiles:" + value;
object_label = value;
} else {
// all the other mappings in OLS end up here

JsonElement linkedEntityElem = linkedEntities.get(value);
JsonElement linkedEntityElem = linkedEntities.get(value);

if(linkedEntityElem == null || !linkedEntityElem.isJsonObject()) {
return;
}
if(linkedEntityElem == null || !linkedEntityElem.isJsonObject()) {
return;
}

JsonObject linkedEntity = linkedEntityElem.getAsJsonObject();
JsonObject linkedEntity = linkedEntityElem.getAsJsonObject();

CurieMap.CurieMapping objCurie = curieMap.mapEntity(linkedEntity);
CurieMap.CurieMapping objCurie = curieMap.mapEntity(linkedEntity);

if(objCurie == null) {
return;
}
if(objCurie == null) {
return;
}

object_id = objCurie.curie != null ? objCurie.curie : objCurie.iriOrUrl;
object_id = objCurie.curie != null ? objCurie.curie : objCurie.iriOrUrl;

if(linkedEntity.has("label")) {
object_label = JsonHelper.getFirstStringValue( linkedEntity.get("label") );
}
}
if(linkedEntity.has("label")) {
object_label = JsonHelper.getFirstStringValue( linkedEntity.get("label") );
}
}


String[] record = new String[tsvHeader.size()];
Expand All @@ -294,7 +301,7 @@ public static void writeMappingsForEntity(JsonObject entity, String predicate, J
record[i] = JsonHelper.getFirstStringValue( entity.get("label") );
break;
case "object_label":
record[i] = object_label;
record[i] = object_label;
break;
// case "comment":
// record[i] = "extracted from " + getFirstStringValue(entity.get("ontologyId"));
Expand Down

0 comments on commit ca030ef

Please sign in to comment.