Reuse – technical

Importing FoodOn (or other ontology) terms into your ontology or application

We recognize that FoodOn itself is a very large ontology, especially if one includes all food product type facets. For reuse it becomes important to just select the terms and axioms one needs for a smaller import file. A variety of methods are supplied below. (A list of other projects reusing FoodOn is here).

Ontofox to OWL file

Lets say you want to import selected FoodOn terms into an ontology. The Ontofox ontology import file service is a convenient tool to enter term ids into, and retrieve the OWL file equivalent that includes annotation (label, description) and other axioms you are interested in. The Ontofox specification file it generates can then be reused in the future to import updated versions of those terms. Here is an example specification file (these parameters can also be entered into the Ontofox web form itself):

[URI of the OWL(RDF/XML) output file]
http://purl.obolibrary.org/obo/generic/foodon_import.owl

[Source ontology]
FOODON

[Low level source term URIs]
http://purl.obolibrary.org/obo/FOODON_03411564 #food source
includeAllChildren

[Top level source term URIs and target direct superclass URIs]


[Source term retrieval setting]
includeNoIntermediates

[Source annotation URIs]
http://www.w3.org/2000/01/rdf-schema#label
http://purl.obolibrary.org/obo/IAO_0000115 #iao:definition
http://www.geneontology.org/formats/oboInOwl#hasSynonym

[Source annotation URIs to be excluded]

This will create an import file containing the entire tree of “food source” (FOODON_03411564) terms from FoodOn.

Using Protégé

When viewing or editing FoodOn with the Stanford Protégé OWL ontology editor, a few methods for extracting branches of vocabulary are available. The “Refactor > Copy/move/delete axioms > Axioms by reference” menu option enables manual selection of items to copy into a new or existing ontology. However, each item has to be selected manually in their interface, which is painful when there are thousands of items descended from a root item that one wants to import.

Alternately, one can add the “Window > Views > Query views > Snap SPARQL Query” editor to an existing or new tab, and launch speedy SPARQL queries. (Note that other SPARQL editors seem to perform poorly or freeze up altogether because of the size of FoodOn)

Using ROBOT + SPARQL

If you want to launch SPARQL queries via command line directly, use the ROBOT (http://robot.obolibrary.org/) tool. ROBOT’s query command (http://robot.obolibrary.org/query.html) launches a SPARQL query against a given ontology, and outputs in a few formats. Under the hood the java OWLAPI is used.

> robot query –input foodon-merged.owl –query owl_get_labels.sparql test.tsv –format TSV

The example “owl_get_labels.sparql” query is located in the FoodOn /src/ontology/ folder. Robot is handy for many other things too, including updating annotations and merging content in owl ontologies.

Using SPARQL + Ontofox

One can use a combination of Ontofox and the following SPARQL query in Protégé 5 to achieve term selections in a way Ontofox doesn’t support out of the box. This query retrieves all underlying terms for the given top classes. Add more top-level FoodOn ids and/or make them more specific if desired.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
SELECT DISTINCT ?id ?parent ?label ?definition
	WHERE {
		{?parent rdfs:subClassOf* obo:FOODON_03411297} #animal food source
		UNION {?parent rdfs:subClassOf* obo:FOODON_03411347} # plant food source
		UNION {?parent rdfs:subClassOf* obo:FOODON_03470107} #preservation type    

		?id rdfs:subClassOf ?parent.
		OPTIONAL {?id rdfs:label ?label}.
		OPTIONAL {?id obo:IAO_0000115 ?definition.}
	}
ORDER BY ?parent ?label ?uiLabel

Below is a more precise query that gets only the “leaf” term ids, and so skips label and definition etc. We insert these results in the Ontofox form/specification file “[Low level source term URIs]” section, and we tell Ontofox to retrieve all parent terms with the “includeAllIntermediates” parameter.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
SELECT DISTINCT ?id
	WHERE {
		{?parent rdfs:subClassOf* obo:FOODON_03411297} #animal food source
		UNION {?parent rdfs:subClassOf* obo:FOODON_03411347} # plant food source
		UNION {?parent rdfs:subClassOf* obo:FOODON_03470107} #preservation type
		?id rdfs:subClassOf ?parent.
	}
ORDER BY ?label

One can copy the list of ontology term ids with their full URL and then paste them directly into the Ontofox specification file.

Using PYTHON and RDFLIB

RDFLIB is a Python library for reading XML/RDF formatted OWL ontologies directly, and provides SPARQL querying on them. It provides comprehensive querying and RDF or tabular results.  A stand-alone Python 2.7 example is here:
https://github.com/GenEpiO/genepio/blob/master/src/ontology/ontology_query.py

A more elaborate Python 3 example is here, containing queries that fetch branches of an ontology starting from a given URI:

https://github.com/Public-Health-Bioinformatics/ontofetch/blob/master/ontofetch.py