|
| 1 | +package org.obolibrary.oboformat; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.semanticweb.owlapi.api.test.baseclasses.TestBase; |
| 5 | +import org.semanticweb.owlapi.apibinding.OWLManager; |
| 6 | +import org.semanticweb.owlapi.formats.OBODocumentFormat; |
| 7 | +import org.semanticweb.owlapi.model.*; |
| 8 | +import org.semanticweb.owlapi.search.EntitySearcher; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +public class PrefixesTest extends TestBase { |
| 18 | + |
| 19 | + @Test |
| 20 | + void testPrefixesRoundtrip() throws OWLOntologyStorageException, IOException { |
| 21 | + OWLDataFactory factory = OWLManager.getOWLDataFactory(); |
| 22 | + OWLAnnotationProperty termReplacedBy = factory.getOWLAnnotationProperty(IRI.create("http://purl.obolibrary.org/obo/IAO_0100001")); |
| 23 | + OWLAnnotationProperty consider = factory.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#consider")); |
| 24 | + OWLOntology oboOnt = loadOntology("obo/test_prefixes.obo", OWLManager.createOWLOntologyManager()); |
| 25 | + assertTrue(oboOnt.containsClassInSignature(IRI.create("http://purl.obolibrary.org/obo/FOO_1234"))); |
| 26 | + assertTrue(oboOnt.containsClassInSignature(IRI.create("http://somewhere.org/MyClass"))); |
| 27 | + assertFalse(oboOnt.containsClassInSignature(IRI.create("https://example.org/myns/#ABC_123"))); |
| 28 | + Map<String, String> prefixMap = oboOnt.getOWLOntologyManager().getOntologyFormat(oboOnt).asPrefixOWLOntologyFormat().getPrefixName2PrefixMap(); |
| 29 | + assertEquals("http://somewhere.org/", prefixMap.get("sw:")); |
| 30 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 31 | + oboOnt.getOWLOntologyManager().saveOntology(oboOnt, stream); |
| 32 | + stream.close(); |
| 33 | + String roundtripOBO = new String(stream.toByteArray(), StandardCharsets.UTF_8); |
| 34 | + assertTrue(roundtripOBO.contains("idspace: sw http://somewhere.org/")); |
| 35 | + assertTrue(roundtripOBO.contains("[Term]\nid: FOO:1234\nis_a: sw:MyClass")); |
| 36 | + |
| 37 | + OWLOntology replacementsOnt = loadOntology("obo/iris_for_obsoletes_replacements.obo", OWLManager.createOWLOntologyManager()); |
| 38 | + assertTrue(EntitySearcher.getAnnotationAssertionAxioms(factory.getOWLClass(IRI.create("http://purl.obolibrary.org/obo/GO_0000108")), replacementsOnt).stream() |
| 39 | + .filter(ax -> ax.getProperty().equals(termReplacedBy)) |
| 40 | + .anyMatch(ax -> ax.getValue().asIRI().get().equals(IRI.create("http://purl.obolibrary.org/obo/GO_0000109"))), |
| 41 | + "Values for replaced_by should be IRIs rather than strings"); |
| 42 | + assertTrue(EntitySearcher.getAnnotationAssertionAxioms(factory.getOWLClass(IRI.create("http://purl.obolibrary.org/obo/GO_0000114")), replacementsOnt).stream() |
| 43 | + .filter(ax -> ax.getProperty().equals(consider)) |
| 44 | + .anyMatch(ax -> ax.getValue().asIRI().get().equals(IRI.create("http://purl.obolibrary.org/obo/GO_0000083"))), |
| 45 | + "Values for consider should be IRIs rather than strings"); |
| 46 | + assertTrue(EntitySearcher.getAnnotationAssertionAxioms(factory.getOWLClass(IRI.create("http://purl.obolibrary.org/obo/GO_0010553")), replacementsOnt).stream() |
| 47 | + .filter(ax -> ax.getProperty().equals(termReplacedBy)) |
| 48 | + .anyMatch(ax -> ax.getValue().asIRI().get().equals(IRI.create("http://purl.obolibrary.org/obo/GO_0000122"))), |
| 49 | + "Values for replaced_by on alt_ids should be IRIs"); |
| 50 | + ByteArrayOutputStream stream2 = new ByteArrayOutputStream(); |
| 51 | + replacementsOnt.getOWLOntologyManager().saveOntology(replacementsOnt, stream2); |
| 52 | + stream2.close(); |
| 53 | + String roundtripReplacementsOnt = new String(stream2.toByteArray(), StandardCharsets.UTF_8); |
| 54 | + assertFalse(roundtripReplacementsOnt.contains("idspace:")); |
| 55 | + assertTrue(roundtripReplacementsOnt.contains("replaced_by: GO:0000109")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testHandlingOfDeclaredOBOPrefix() throws OWLOntologyStorageException, IOException, OWLOntologyCreationException { |
| 60 | + OWLOntology oboOnt = loadOntology("obo/test_obo_prefix.obo", OWLManager.createOWLOntologyManager()); |
| 61 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 62 | + oboOnt.getOWLOntologyManager().saveOntology(oboOnt, stream); |
| 63 | + stream.close(); |
| 64 | + String roundtripOBO = new String(stream.toByteArray(), StandardCharsets.UTF_8); |
| 65 | + assertFalse(roundtripOBO.contains("obo:")); |
| 66 | + assertFalse(roundtripOBO.contains("obo ")); |
| 67 | + assertFalse(roundtripOBO.contains("ex:MMyClass"), "The longest available namespace match should be used"); |
| 68 | + assertFalse(roundtripOBO.contains("owl-axioms:")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testOBOFormatShouldNotInjectPrefixesInConstructedDocFormat() throws OWLOntologyStorageException, IOException { |
| 73 | + OWLOntology oboOnt = loadOntology("obo/test_obo_prefix.obo", OWLManager.createOWLOntologyManager()); |
| 74 | + OWLOntologyManager manager = oboOnt.getOWLOntologyManager(); |
| 75 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 76 | + OWLDocumentFormat format = new OBODocumentFormat(); |
| 77 | + String defaultNamespace = format.asPrefixOWLOntologyFormat().getDefaultPrefix(); |
| 78 | + format.asPrefixOWLOntologyFormat().copyPrefixesFrom(manager.getOntologyFormat(oboOnt).asPrefixOWLOntologyFormat()); |
| 79 | + format.asPrefixOWLOntologyFormat().setDefaultPrefix(defaultNamespace); |
| 80 | + manager.saveOntology(oboOnt, format, stream); |
| 81 | + stream.close(); |
| 82 | + String roundtripOBO = new String(stream.toByteArray(), StandardCharsets.UTF_8); |
| 83 | + assertFalse(roundtripOBO.contains("idspace: rdf")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testOBOFormatShouldNotInjectPrefixesInLoadedDocFormat() throws OWLOntologyStorageException, IOException { |
| 88 | + OWLOntology oboOnt = loadOntology("obo/test_obo_prefix.obo", OWLManager.createOWLOntologyManager()); |
| 89 | + OWLOntologyManager manager = oboOnt.getOWLOntologyManager(); |
| 90 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 91 | + OWLDocumentFormat format = manager.getOntologyFormat(oboOnt); |
| 92 | + format.asPrefixOWLOntologyFormat().copyPrefixesFrom(manager.getOntologyFormat(oboOnt).asPrefixOWLOntologyFormat()); |
| 93 | + manager.saveOntology(oboOnt, format, stream); |
| 94 | + stream.close(); |
| 95 | + String roundtripOBO = new String(stream.toByteArray(), StandardCharsets.UTF_8); |
| 96 | + assertFalse(roundtripOBO.contains("idspace: rdf")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testOBOFormatShouldPreventOBOPrefixes() throws OWLOntologyStorageException, IOException { |
| 101 | + OWLOntology oboOnt = loadOntology("obo/test_obo_prefix.obo", OWLManager.createOWLOntologyManager()); |
| 102 | + OWLOntologyManager manager = oboOnt.getOWLOntologyManager(); |
| 103 | + ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 104 | + OWLDocumentFormat format = manager.getOntologyFormat(oboOnt); |
| 105 | + format.asPrefixOWLOntologyFormat().setPrefix("GO", "http://purl.obolibrary.org/obo/GX_"); |
| 106 | + manager.saveOntology(oboOnt, format, stream); |
| 107 | + stream.close(); |
| 108 | + String roundtripOBO = new String(stream.toByteArray(), StandardCharsets.UTF_8); |
| 109 | + assertFalse(roundtripOBO.contains("idspace: GO")); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments