From c58e8736d4b221081d7bafa16d56c9856dda14af Mon Sep 17 00:00:00 2001 From: James McLaughlin Date: Fri, 8 Nov 2024 01:05:19 +0000 Subject: [PATCH] move shortform to the linker --- .../linker/src/main/java/LinkerPass2.java | 62 ++++++------------- .../uk/ac/ebi/rdf2json/OntologyGraph.java | 4 +- ...FormAnnotator.java => CurieAnnotator.java} | 8 +-- .../rdf2json/annotators/LabelAnnotator.java | 2 +- 4 files changed, 25 insertions(+), 51 deletions(-) rename dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/{ShortFormAnnotator.java => CurieAnnotator.java} (86%) diff --git a/dataload/linker/src/main/java/LinkerPass2.java b/dataload/linker/src/main/java/LinkerPass2.java index 68dc92576..6c2878ed4 100644 --- a/dataload/linker/src/main/java/LinkerPass2.java +++ b/dataload/linker/src/main/java/LinkerPass2.java @@ -146,6 +146,14 @@ private static void writeEntityArray(JsonReader jsonReader, JsonWriter jsonWrite EntityDefinitionSet defOfThisEntity = pass1Result.iriToDefinitions.get(entityIri); + String curie = null; + if(defOfThisEntity.definingDefinitions.size() > 0) { + // always use the defining ontology's curie, as the defining + // ontology knows the base URI and we might not + // + curie = defOfThisEntity.definingDefinitions.iterator().next().curie.getAsString(); + } + while(jsonReader.peek() != JsonToken.END_OBJECT) { String name = jsonReader.nextName(); @@ -159,22 +167,23 @@ private static void writeEntityArray(JsonReader jsonReader, JsonWriter jsonWrite } if(name == "curie") { - if(defOfThisEntity.definingDefinitions.size() > 0) { - // always use the defining ontology's curie, as the defining - // ontology knows the base URI and we might not - // - com.google.gson.internal.Streams.write( - defOfThisEntity.definingDefinitions.iterator().next().curie, - jsonWriter); - continue; + if(curie != null) { + // use the defining ontology curie + jsonWriter.value(curie); } else { - // fallback to using the curie we already have + // fallthrough to using the curie from rdf2json + curie = jsonReader.nextString(); + jsonWriter.value(curie); } + continue; } CopyJsonGatheringStrings.copyJsonGatheringStrings(jsonReader, jsonWriter, stringsInEntity); } + jsonWriter.name("shortForm"); + jsonWriter.value(curie.replaceFirst(":", "_")); + if(defOfThisEntity != null) { jsonWriter.name(IS_DEFINING_ONTOLOGY.getText()); @@ -452,39 +461,4 @@ private static class CurieMapResult { public String url; public String source; } - - private static void processShortFormObject(JsonReader jsonReader, JsonWriter jsonWriter, LinkerPass1.LinkerPass1Result pass1Result, String entityIri) throws IOException { - jsonReader.beginObject(); - JsonObject shortFormObject = new JsonObject(); - - while (jsonReader.peek() != JsonToken.END_OBJECT) { - String shortFormFieldName = jsonReader.nextName(); - if (shortFormFieldName.equals("type")) { - JsonArray typeArray = new JsonArray(); - jsonReader.beginArray(); - while (jsonReader.peek() != JsonToken.END_ARRAY) { - typeArray.add(jsonReader.nextString()); - } - jsonReader.endArray(); - shortFormObject.add("type", typeArray); - } else if (shortFormFieldName.equals("value")) { - String shortFormValue = jsonReader.nextString(); - // Modify the value attribute - shortFormValue = getProcessedCurieValue(pass1Result, entityIri).replace(":", "_"); - shortFormObject.addProperty("value", shortFormValue); - } - } - jsonReader.endObject(); - - // Write the modified short form object - jsonWriter.beginObject(); - jsonWriter.name("type"); - jsonWriter.beginArray(); - for (JsonElement typeElement : shortFormObject.getAsJsonArray("type")) { - jsonWriter.value(typeElement.getAsString()); - } - jsonWriter.endArray(); - jsonWriter.name("value").value(shortFormObject.get("value").getAsString()); - jsonWriter.endObject(); - } } diff --git a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/OntologyGraph.java b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/OntologyGraph.java index 659583938..3595acf98 100644 --- a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/OntologyGraph.java +++ b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/OntologyGraph.java @@ -262,14 +262,14 @@ private String urlToFilename(String url) { HierarchicalParentsAnnotator.annotateHierarchicalParents(this); // must run after RelatedAnnotator AncestorsAnnotator.annotateAncestors(this); HierarchyMetricsAnnotator.annotateHierarchyMetrics(this); // must run after HierarchicalParentsAnnotator - ShortFormAnnotator.annotateShortForms(this); + CurieAnnotator.annotateCuries(this); DefinitionAnnotator.annotateDefinitions(this); SynonymAnnotator.annotateSynonyms(this); ReifiedPropertyAnnotator.annotateReifiedProperties(this); OntologyMetadataAnnotator.annotateOntologyMetadata(this); HierarchyFlagsAnnotator.annotateHierarchyFlags(this); // must run after DirectParentsAnnotator and HierarchicalParentsAnnotator IsObsoleteAnnotator.annotateIsObsolete(this); - LabelAnnotator.annotateLabels(this); // must run after ShortFormAnnotator + LabelAnnotator.annotateLabels(this); // must run after CurieAnnotator ConfigurablePropertyAnnotator.annotateConfigurableProperties(this); PreferredRootsAnnotator.annotatePreferredRoots(this); DisjointWithAnnotator.annotateDisjointWith(this); diff --git a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/ShortFormAnnotator.java b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/CurieAnnotator.java similarity index 86% rename from dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/ShortFormAnnotator.java rename to dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/CurieAnnotator.java index 60c2400fd..926adf93a 100644 --- a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/ShortFormAnnotator.java +++ b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/CurieAnnotator.java @@ -10,10 +10,10 @@ import uk.ac.ebi.rdf2json.annotators.helpers.OntologyBaseUris; import uk.ac.ebi.rdf2json.properties.PropertyValueLiteral; -public class ShortFormAnnotator { - private static final Logger logger = LoggerFactory.getLogger(ShortFormAnnotator.class); +public class CurieAnnotator { + private static final Logger logger = LoggerFactory.getLogger(CurieAnnotator.class); - public static void annotateShortForms(OntologyGraph graph) { + public static void annotateCuries(OntologyGraph graph) { long startTime3 = System.nanoTime(); @@ -41,7 +41,7 @@ public static void annotateShortForms(OntologyGraph graph) { } } long endTime3 = System.nanoTime(); - logger.info("annotate short forms: {}", ((endTime3 - startTime3) / 1000 / 1000 / 1000)); + logger.info("annotate curies: {}", ((endTime3 - startTime3) / 1000 / 1000 / 1000)); } diff --git a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/LabelAnnotator.java b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/LabelAnnotator.java index 9373f76ba..61eacb6ef 100644 --- a/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/LabelAnnotator.java +++ b/dataload/rdf2json/src/main/java/uk/ac/ebi/rdf2json/annotators/LabelAnnotator.java @@ -37,7 +37,7 @@ public static Set getLabelProperties(OntologyGraph graph) { } public static void annotateLabels(OntologyGraph graph) { - collateProperties(graph, "label", getLabelProperties(graph), List.of("shortForm")); + collateProperties(graph, "label", getLabelProperties(graph), List.of("curie")); } private static void collateProperties(OntologyGraph graph, String destProp, Collection sourceProps, Collection fallbackProps) {