Repository: ontodev/robot (java)

Issue:
Add an obograph-json export option
OWLAPI-level access to an ontology is generally not useful or appropriate for bioinformatics developers. To wean people off obo format we have created a json format that is graph-oriented

See:
https://github.com/geneontology/obographs

On mvncentral so easy to include. 

Note the owlapi isn't aware of this, so we'd either need a special purpose `convert` command, or an annoying switch in the code that checks the suffix and routes to this to the FromOwl converter


Pull request:
Adding a obo graph json export capability
Fixes #94 

This will fail until obographs 0.0.8 percolates to mvncentral in ~24hrs...

Patch:
diff --git a/robot-command/src/main/java/org/obolibrary/robot/ConvertCommand.java b/robot-command/src/main/java/org/obolibrary/robot/ConvertCommand.java
index 8660da04..be48ccfa 100644
--- a/robot-command/src/main/java/org/obolibrary/robot/ConvertCommand.java
+++ b/robot-command/src/main/java/org/obolibrary/robot/ConvertCommand.java
@@ -104,6 +104,7 @@ public class ConvertCommand implements Command {
      * <li>OWLXML .owx
      * <li>Manchester .omn
      * <li>OWL Functional .ofn
+     * <li>OboGraphs JSON .json
      * </ul>
      *
      * @param state the state from the previous command, or null
diff --git a/robot-core/pom.xml b/robot-core/pom.xml
index d2456816..4eda4ef1 100644
--- a/robot-core/pom.xml
+++ b/robot-core/pom.xml
@@ -37,5 +37,10 @@
           <artifactId>expression-materializing-reasoner</artifactId>
           <version>0.1.3</version>
         </dependency>
+	<dependency>
+	  <groupId>org.geneontology</groupId>
+ 	  <artifactId>obographs</artifactId>
+ 	  <version>0.0.8</version>
+	</dependency>
     </dependencies>
 </project>
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 a31f0530..c29852a4 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
@@ -9,6 +9,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -33,6 +34,10 @@ import com.opencsv.CSVReader;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
+import org.geneontology.obographs.io.OboGraphJsonDocumentFormat;
+import org.geneontology.obographs.io.OgJsonGenerator;
+import org.geneontology.obographs.model.GraphDocument;
+import org.geneontology.obographs.owlapi.FromOwl;
 import org.semanticweb.owlapi.apibinding.OWLManager;
 import org.semanticweb.owlapi.model.IRI;
 import org.semanticweb.owlapi.model.OWLDataFactory;
@@ -48,7 +53,9 @@ import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
 import org.semanticweb.owlapi.formats.ManchesterSyntaxDocumentFormat;
 import org.semanticweb.owlapi.formats.FunctionalSyntaxDocumentFormat;
 import org.semanticweb.owlapi.formats.OWLXMLDocumentFormat;
+import org.semanticweb.owlapi.formats.PrefixDocumentFormat;
 import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
+import org.semanticweb.owlapi.io.OWLOntologyLoaderMetaData;
 import org.semanticweb.owlapi.rdf.rdfxml.renderer.XMLWriterPreferences;
 import org.semanticweb.owlapi.util.DefaultPrefixManager;
 import org.yaml.snakeyaml.Yaml;
@@ -59,6 +66,7 @@ import org.yaml.snakeyaml.Yaml;
  * @author <a href="mailto:james@overton.ca">James A. Overton</a>
  */
 public class IOHelper {
+        
     /**
      * Logger.
      */
@@ -357,6 +365,8 @@ public class IOHelper {
             return new ManchesterSyntaxDocumentFormat();
         } else if (formatName.equals("ofn")) {
             return new FunctionalSyntaxDocumentFormat();
+        } else if (formatName.equals("json")) {
+            return new OboGraphJsonDocumentFormat();
         } else {
             throw new IllegalArgumentException(
                     "Unknown ontology format: " + formatName);
@@ -447,8 +457,20 @@ public class IOHelper {
         try {
             XMLWriterPreferences.getInstance()
                 .setUseNamespaceEntities(getXMLEntityFlag());
-            ontology.getOWLOntologyManager().saveOntology(
-                    ontology, format, ontologyIRI);
+            
+            // first handle any non-official output formats.
+            // currently this is just OboGraphs JSON format
+            if (format instanceof OboGraphJsonDocumentFormat) {
+                FromOwl fromOwl = new FromOwl();
+                GraphDocument gd = fromOwl.generateGraphDocument(ontology);
+                String doc = OgJsonGenerator.render(gd);
+                File outfile = new File(ontologyIRI.toURI());
+                FileUtils.writeStringToFile(outfile, doc);
+            } else {
+                // use native save functionality
+                ontology.getOWLOntologyManager().saveOntology(
+                        ontology, format, ontologyIRI);
+            }
         } catch (OWLOntologyStorageException e) {
             throw new IOException(e);
         }


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 f6007e0b..ab3ca6ee 100644
--- a/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
+++ b/robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
@@ -1,5 +1,7 @@
 package org.obolibrary.robot;
 
+import static org.junit.Assert.*;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
@@ -7,17 +9,15 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import static org.junit.Assert.assertEquals;
+import org.geneontology.obographs.io.OboGraphJsonDocumentFormat;
 import org.junit.Test;
-
-import com.github.jsonldjava.core.Context;
+import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
 import org.semanticweb.owlapi.model.IRI;
 import org.semanticweb.owlapi.model.OWLLiteral;
+import org.semanticweb.owlapi.model.OWLOntology;
 import org.semanticweb.owlapi.util.DefaultPrefixManager;
 
-import org.apache.commons.io.FileUtils;
-import org.semanticweb.owlapi.model.OWLOntology;
-import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
+import com.github.jsonldjava.core.Context;
 
 /**
  * Tests for IOHelper.
@@ -226,4 +226,21 @@ public class IOHelperTest extends CoreTest {
     }
 
 
+    /**
+     * Tests json saving
+     * 
+     * @throws IOException on error
+     */
+    @Test
+    public void testSaveOntologyAsJson()
+            throws IOException {
+        OWLOntology ontology = loadOntology("/simple.owl");
+        File tempFile = File.createTempFile("simple-saved", ".json");
+        tempFile.deleteOnExit();
+
+        IOHelper ioHelper = new IOHelper();
+        ioHelper.saveOntology(ontology, new OboGraphJsonDocumentFormat(), tempFile);
+        
+    }
+
 }