11package uk .ac .ebi .spot .ols .loader ;
22
33import com .google .common .collect .Multimap ;
4+ import com .google .common .collect .SetMultimap ;
5+
46import org .semanticweb .owlapi .apibinding .OWLManager ;
57import org .semanticweb .owlapi .io .OWLObjectRenderer ;
68import org .semanticweb .owlapi .manchestersyntax .renderer .ManchesterOWLSyntaxOWLObjectRendererImpl ;
@@ -324,6 +326,27 @@ private void initializeVocabularyToIgnore() throws OntologyLoadingException {
324326 owlVocabulary .add (factory .getOWLBottomObjectProperty ().getIRI ());
325327 }
326328
329+
330+ private void populateOntologyLanguages (Collection <OWLEntity > allEntities ) {
331+
332+ for (OWLEntity owlEntity : allEntities ) {
333+ for (OWLOntology anOntology : getManager ().ontologies ().collect (Collectors .toSet ())){
334+ EntitySearcher .getAnnotationAssertionAxioms (owlEntity , anOntology ).forEach (annotationAssertionAxiom -> {
335+
336+ OWLAnnotationValue value = annotationAssertionAxiom .getValue ();
337+
338+ if (value .isLiteral ()) {
339+ if (((OWLLiteral ) value ).hasLang ()) {
340+ ontologyLanguages .add ( ((OWLLiteral ) value ).getLang () );
341+ }
342+ }
343+
344+ });
345+ }
346+ }
347+ }
348+
349+
327350 @ Override
328351 public String getShortForm (IRI ontologyTermIRI ) {
329352
@@ -436,6 +459,8 @@ protected OWLOntology loadOntology() throws OWLOntologyCreationException {
436459 ResourceUsage .logUsage (getLogger (), "#### Monitoring " , getOntologyName () +
437460 ":After copying of entities" , ":" );
438461
462+ populateOntologyLanguages (allEntities );
463+
439464 indexTerms (allEntities );
440465 ResourceUsage .logUsage (getLogger (), "#### Monitoring " , getOntologyName () +
441466 ":After index terms" , ":" );
@@ -1231,11 +1256,18 @@ protected Optional<String> evaluateLabelAnnotationValue(OWLEntity entity, OWLAnn
12311256
12321257 private boolean isEnglishLabel (OWLAnnotationValue value ) {
12331258 return value instanceof OWLLiteral && ((OWLLiteral ) value ).getLang ().equalsIgnoreCase ("en" );
1259+ }
1260+
1261+ protected void evaluateAnnotationValue (OWLEntity entity , OWLAnnotationProperty annotationProperty , IRI annotationPropertyIRI , OWLAnnotationValue value , String lang ) {
1262+
1263+
1264+
12341265 }
12351266
12361267 protected void evaluateAllAnnotationsValues (OWLEntity owlEntity ) {
12371268
12381269 IRI owlEntityIRI = owlEntity .getIRI ();
1270+
12391271 Set <String > slims = new HashSet <>();
12401272
12411273 LocalizedStrings definitions = new LocalizedStrings ();
@@ -1250,45 +1282,46 @@ protected void evaluateAllAnnotationsValues(OWLEntity owlEntity) {
12501282 Collection <OBOSynonym > oboSynonyms = new HashSet <>();
12511283 Collection <OBOXref > oboEntityXrefs = new HashSet <>();
12521284
1253- // loop through other annotations in the imports closure
1254- for (OWLOntology anOntology : getManager ().ontologies ().collect (Collectors .toSet ())) {
1255- EntitySearcher .getAnnotationAssertionAxioms (owlEntity , anOntology ).forEach (annotationAssertionAxiom -> {
1256- OWLAnnotationProperty annotationProperty = annotationAssertionAxiom .getProperty ();
1257- IRI annotationPropertyIRI = annotationProperty .getIRI ();
1258- OWLAnnotationValue value = annotationAssertionAxiom .getValue ();
1259-
1260- // blank string = default lang
1261- String lang = "" ;
1262-
1263- // but take lang from the value if it is present
1264- if (value .isLiteral ()) {
1265- if (((OWLLiteral ) value ).hasLang ()) {
1266- lang = ((OWLLiteral ) value ).getLang ();
1267- ontologyLanguages .add (lang );
1268- }
1269- }
1285+ Set <IRI > annotationProperties = new HashSet <>();
12701286
1287+ // pass 1: populate the set of all annotation properties used
1288+ for (OWLOntology anOntology : getManager ().ontologies ().collect (Collectors .toSet ())){
1289+ EntitySearcher .getAnnotationAssertionAxioms (owlEntity , anOntology ).forEach (annotationAssertionAxiom -> {
12711290
1272- if (getLabelIRI ().equals (annotationPropertyIRI )) {
1273- classLabels .addString (lang , evaluateLabelAnnotationValue (
1274- owlEntity , value ).get ());
1275- } else if (getSynonymIRIs ().contains (annotationPropertyIRI )) {
1276- synonyms .addString (lang , getOWLAnnotationValueAsString (value ).get ());
1277- } else if (getDefinitionIRIs ().contains (annotationPropertyIRI )) {
1278- definitions .addString (lang , getOWLAnnotationValueAsString (value ).get ());
1279- } else if (annotationPropertyIRI .equals (Namespaces .OBOINOWL .createIRI ("inSubset" )) && value instanceof IRI ) {
1280- if (extractShortForm ((IRI ) value ).isPresent ()) {
1281- slims .add (extractShortForm ((IRI ) value ).get ());
1282- }
1283- } else if (annotationPropertyIRI .equals (Namespaces .OWL .createIRI ("deprecated" ))) {
1284- addObsoleteTerms (owlEntityIRI );
1285- } else {
1286- if (getOWLAnnotationValueAsString (value ).isPresent ()) {
1287- // initialise maps if first time
1288- if (!termAnnotations .containsKey (owlEntityIRI )) {
1289- HashMap <IRI , LocalizedStrings > newMap = new HashMap <>();
1290- newMap .put (annotationPropertyIRI , new LocalizedStrings ());
1291- termAnnotations .put (owlEntityIRI , newMap );
1291+ OWLAnnotationProperty annotationProperty = annotationAssertionAxiom .getProperty ();
1292+ IRI annotationPropertyIRI = annotationProperty .getIRI ();
1293+
1294+ annotationProperties .add (annotationPropertyIRI );
1295+ });
1296+ }
1297+
1298+
1299+ // pass 2: read the annotations
1300+ for (OWLOntology anOntology : getManager ().ontologies ().collect (Collectors .toSet ())){
1301+ EntitySearcher .getAnnotationAssertionAxioms (owlEntity , anOntology ).forEach (annotationAssertionAxiom -> {
1302+ OWLAnnotationProperty annotationProperty = annotationAssertionAxiom .getProperty ();
1303+ IRI annotationPropertyIRI = annotationProperty .getIRI ();
1304+ OWLAnnotationValue value = annotationAssertionAxiom .getValue ();
1305+
1306+ // blank string = default lang
1307+ String lang = "" ;
1308+
1309+ // but take lang from the value if it is present
1310+ if (value .isLiteral ()) {
1311+ if (((OWLLiteral ) value ).hasLang ()) {
1312+ lang = ((OWLLiteral ) value ).getLang ();
1313+ }
1314+ }
1315+
1316+ if (getLabelIRI ().equals (annotationPropertyIRI )) {
1317+ classLabels .addString (lang , evaluateLabelAnnotationValue (
1318+ owlEntity , value ).get ());
1319+ }
1320+ else if (getSynonymIRIs ().contains (annotationPropertyIRI )) {
1321+ synonyms .addString (lang , getOWLAnnotationValueAsString (value ).get ());
1322+ }
1323+ else if (getDefinitionIRIs ().contains (annotationPropertyIRI )) {
1324+ definitions .addString (lang , getOWLAnnotationValueAsString (value ).get ());
12921325 }
12931326
12941327 if (!termAnnotations .get (owlEntityIRI ).containsKey (annotationPropertyIRI )) {
@@ -1302,7 +1335,7 @@ protected void evaluateAllAnnotationsValues(OWLEntity owlEntity) {
13021335 termAnnotations .get (owlEntityIRI ).get (annotationPropertyIRI ).addString (
13031336 lang , getOWLAnnotationValueAsString (value ).get ());
13041337 }
1305- }
1338+ });
13061339 }
13071340
13081341 // pull out term replaced by
@@ -1370,13 +1403,41 @@ protected void evaluateAllAnnotationsValues(OWLEntity owlEntity) {
13701403 String description = getOWLAnnotationValueAsString (axiomAnnotation .getValue ()).get ();
13711404 oboXrefs .setDescription (description );
13721405 }
1373- }
1374- oboEntityXrefs .add (oboXrefs );
1375- }
1376- });
1406+ });
13771407 }
13781408
1379- setClassLabels (owlEntityIRI , classLabels );
1409+
1410+ // cross reference the set of all ontologies properties with the set of
1411+ // languages in the ontology. Any missing languages are added with the English strings.
1412+ // Otherwise, if you switch language in the webapp to a language into which the ontology
1413+ // is not localised, the annotations will not appear!
1414+ //
1415+ if (termAnnotations .containsKey (owlEntityIRI )) {
1416+
1417+ Map <IRI , LocalizedStrings > annotations = termAnnotations .get (owlEntityIRI );
1418+
1419+ for (IRI annotationPropertyIri : annotations .keySet ()) {
1420+
1421+ LocalizedStrings annos = annotations .get (annotationPropertyIri );
1422+
1423+ for (String ontologyLang : ontologyLanguages ) {
1424+
1425+ if (!annos .getLanguages ().contains (ontologyLang )) {
1426+ annos .setStrings (ontologyLang , annos .getStrings ("" , "en" , "en-US" ));
1427+ }
1428+ }
1429+
1430+ }
1431+ }
1432+
1433+ for (String ontologyLang : ontologyLanguages ) {
1434+ if (!classLabels .getLanguages ().contains (ontologyLang )) {
1435+ classLabels .setStrings (ontologyLang , classLabels .getStrings ("" , "en" , "en-US" ));
1436+ }
1437+ }
1438+
1439+
1440+ setClassLabels (owlEntityIRI , classLabels );
13801441
13811442 if (definitionCitations .size () > 0 ) {
13821443 setOboDefinitionCitation (owlEntityIRI , definitionCitations );
@@ -1399,6 +1460,7 @@ protected void evaluateAllAnnotationsValues(OWLEntity owlEntity) {
13991460 if (slims .size () > 0 ) {
14001461 setSlims (owlEntityIRI , slims );
14011462 }
1463+
14021464 }
14031465
14041466 private OBOXref extractOBOXrefs (OWLAnnotation annotation ) {
0 commit comments