Repository: ontodev/robot (java)

Issue:
ROBOT convert is stripping prefixes
robot convert -i cl-edit.owl -f ofn -o tmp.owl

Before

```
Prefix(:=<http://purl.obolibrary.org/obo/cl.owl#>)
Prefix(obo:=<http://purl.obolibrary.org/obo/>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
Prefix(oboInOwl:=<http://www.geneontology.org/formats/oboInOwl#>)
```

After
```
Prefix(:=<http://purl.obolibrary.org/obo/cl.owl#>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
```

This can => massive changes in diffs when used to reserialize.

Pull request description:
Preserve prefixes from loaded ontology.
Resolves #1101.

- [ ] `docs/` have been added/updated
- [ ] tests have been added/updated
- [ ] `mvn verify` says all tests pass
- [ ] `mvn site` says all JavaDocs correct
- [ ] `CHANGELOG.md` has been updated

This change gets the document format used to load the ontology, and sets it as the prefix manager for the document format being used to save the ontology.


Patch:
diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
index eb2c1497..24fc21dd 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
@@ -859,6 +859,16 @@ public class IOHelper {
       saveCompressedOntology(data, ontologyIRI);
       return ontology;
     }
+    OWLDocumentFormat previousFormat = ontology.getOWLOntologyManager().getOntologyFormat(ontology);
+    if (format.isPrefixOWLOntologyFormat()
+        && previousFormat != null
+        && previousFormat.isPrefixOWLOntologyFormat()) {
+      String defaultNamespace = format.asPrefixOWLOntologyFormat().getDefaultPrefix();
+      format
+          .asPrefixOWLOntologyFormat()
+          .copyPrefixesFrom(previousFormat.asPrefixOWLOntologyFormat());
+      format.asPrefixOWLOntologyFormat().setDefaultPrefix(defaultNamespace);
+    }
     // If not compressed, just save the file as-is
     if (addPrefixes != null && !addPrefixes.isEmpty()) {
       addPrefixes(format, addPrefixes);


Test patch:
diff --git a/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java b/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
index 4324753f..a7011099 100644
--- a/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
+++ b/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
@@ -189,6 +189,37 @@ public class IOHelperTest extends CoreTest {
         pm.getIRI("GO:12345").toString());
   }
 
+  /**
+   * Test that original prefixes are preserved when saving.
+   *
+   * @throws IOException on file problem
+   */
+  @Test
+  public void testPrefixConservation() throws IOException {
+    OWLOntology ontology = loadOntology("/simple.owl");
+    String origNamespace =
+        ontology
+            .getOWLOntologyManager()
+            .getOntologyFormat(ontology)
+            .asPrefixOWLOntologyFormat()
+            .getPrefix("obo:");
+
+    File tempFile = File.createTempFile("simple-roundtrip", ".owl");
+    tempFile.deleteOnExit();
+    IOHelper ioHelper = new IOHelper();
+    ioHelper.saveOntology(ontology, new RDFXMLDocumentFormat(), tempFile);
+
+    OWLOntology ontology2 = ioHelper.loadOntology(tempFile.getPath());
+    String savedNamespace =
+        ontology2
+            .getOWLOntologyManager()
+            .getOntologyFormat(ontology2)
+            .asPrefixOWLOntologyFormat()
+            .getPrefix("obo:");
+
+    assertEquals(origNamespace, savedNamespace);
+  }
+
   /**
    * Test getting terms from strings.
    *