Search Mailing List Archives


Limit search to: Subject & Body Subject Author
Sort by: Reverse Sort
Limit to: All This Week Last Week This Month Last Month
Select Date Range     through    

[protege-owl] How to get SPARQL query results as an Excel or other format

Trish Whetzel whetzel at pcbi.upenn.edu
Sun Apr 29 22:22:29 PDT 2007


Forwarding mail, with permission of the code author, from another list.

Trish

---------- Forwarded message ----------

In case anyone else is faced with the same problem we did, Josef Spidlen
in my group wrote some simple code to parse owl files into a format
suitable for the branches submission. The java code below uses Jena
library (tested with Jena 2.5.1; .jar files from Jena/lib need to be in
the class path). It runs a SPARQL query - in this version the query is
built in the Java code to make it "easier" to distribute. However, you
can just replace QueryFactory.create(sQueryString) with
QueryFactory.read(filename) and create your SPARQL query externally.



The query has been tweaked to work with OWL files that we received our
collaborators. It expects the ontology classes being annotated in a
certain way, e.g., rumeta:def for definition, rumeta:editor for
definition editor, ... , rumeta:synonym or rumeta:acronym for
alternative terms. This may be easily modified in case you annotate
different way; it just needs to know "where to search for what".



The parent_term works fine for simple terms organized in a nice
hierarchy (i.e., 98% of what we had). A small problem is typically if a
class A is a subclass of B and also a subclass of C, while B and C are
not any hierarchical order. This can for example happen what you start
playing with owl constraints and defined classes. In this case, both B
and C are potential parents and two lines are outputted in the output
file. So far it was not worth to write something "smart" that could
handle this case somehow special (i.e., it was easier to fix two terms
manually).



Usage: ParseOwl OntologyFileName [OutputFileName]

- Processes the input OWL file and outputs a text based spreadsheet to
OutputFileName or to console (System.out) if OutputFileName isn't
specified.



The output is a text file with fixed field width; OpenOffice or MS Excel
text input can be used to create tab delimited files and to clean it a
bit - remove ^^xsd:string suffixes, OBI: prefixes, trim the text, remove
double-spaces, etc.



--Ryan



Code follows:



package org.flowcyt.parseowl;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;



import com.hp.hpl.jena.query.Query;

import com.hp.hpl.jena.query.QueryExecution;

import com.hp.hpl.jena.query.QueryExecutionFactory;

import com.hp.hpl.jena.query.QueryFactory;

import com.hp.hpl.jena.query.ResultSet;

import com.hp.hpl.jena.query.ResultSetFormatter;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.ModelFactory;



public class ParseOwl {



                 private static void wrongUsage()

                 {

                                 System.out.println("Wrong usage, not all
arguments found.\n");

                                 System.out.println("Usage: ParseOwl
OntologyFileName OutputFileName");

                                 System.out.println(" - outputs to
file");

                                 System.out.println("Usage: ParseOwl
OntologyFileName");

                                 System.out.println(" - outputs to
System.out");

                                 System.exit(1);

                 }



                 /**

                  * Parses an OWL ontology file and creates a simple
table out of it

                  *

                  * Usage: ParseOwl OntologyFileName OutputFileName

                  *  - outputs to file

                  * Usage: ParseOwl OntologyFileName

                  *  - outputs to System.out

                  *

                  * @author Josef Spidlen jspidlen_at_bccrc.ca

                  * @param args one or two file names; the first one is
the input ontology, the second one the output file name

                  *

                  */

                 public static void main(String[] args) throws
FileNotFoundException, IOException {



                                 OutputStream out = null; InputStream in;



                                 if(null == args) {

                                                 wrongUsage();

                                 } else if(args.length == 1) {

                                                 out = System.out;

                                 } else if(args.length == 2) {

                                                 out = new
FileOutputStream(new File(args[1]));

                                 } else {

                                                 wrongUsage();

                                 }



                                 Model model =
ModelFactory.createMemModelMaker().createModel("Model " + args[0]);

                                 in = new FileInputStream(new
File(args[0]));

                                 model.read(in, null);

                                 in.close();



                                 String sQuery =

                                                 "PREFIX dc:
<http://purl.org/dc/elements/1.1/> " +

                                                 "PREFIX owl:
<http://www.w3.org/2002/07/owl#> " +

                                                 "PREFIX protege:
<http://protege.stanford.edu/plugins/owl/protege#> " +

                                                 "PREFIX rameta:
<http://msi-ontology.sourceforge.net/ontology/RA_metadata.owl#> " +

                                                 "PREFIX rdf:
<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +

                                                 "PREFIX rdfs:
<http://www.w3.org/2000/01/rdf-schema#> " +

                                                 "PREFIX rumeta:
<http://msi-ontology.sourceforge.net/ontology/RU_metadata.owl#> " +

                                                 "PREFIX xsd:
<http://www.w3.org/2001/XMLSchema#> " +

                                                 "PREFIX OBI:
<http://www.isac.org/instrument#> " +

                                                 "" +

                                                 "SELECT DISTINCT
?preffered_term ?definition ?explanation ?definition_editor
?definition_source ?parent_term ?alternative_term " +

                                                 "WHERE  { " +


"?preffered_term a owl:Class . " +


"OPTIONAL { ?preffered_term rumeta:def ?definition . } " +


"OPTIONAL { ?preffered_term rumeta:editor ?definition_editor . } " +


"OPTIONAL { ?preffered_term rumeta:source ?definition_source . } " +


"OPTIONAL { ?preffered_term rdfs:subClassOf ?parent_term . } " +


"OPTIONAL { ?preffered_term rumeta:cls_expl ?explanation . } " +


"OPTIONAL { ?preffered_term ?p ?alternative_term . FILTER ( ?p =
rumeta:synonym || ?p = rumeta:acronym ) } " +

                                                 "}";



                                 Query query =
QueryFactory.create(sQuery);

                                 QueryExecution qe =
QueryExecutionFactory.create(query, model);

                                 ResultSet results = qe.execSelect();

                                 ResultSetFormatter.out(out, results,
query);



                                 out.close(); qe.close();




                 }

}
-------------- next part --------------
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
-------------- next part --------------
_______________________________________________
Obi-devel mailing list
Obi-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/obi-devel


More information about the protege-owl mailing list