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]

[Source ontology]

[Low level source term URIs] #food source

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

[Source term retrieval setting]

[Source annotation URIs] #iao:definition

[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)


If you want to launch SPARQL queries via command line directly, use the ROBOT ( tool. ROBOT’s query command ( 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

NOTE: if cutting and pasting this text to a command line generates a robot error, try typing it out instead, or at least retype the double dashes (possibly Unicode character set, so you may need to type each character. The example “owl_get_labels.sparql” query is located in the FoodOn /src/ontology/ folder, as well as another simple “owl_get_mushroom.sparql” query. Robot is handy for many other things too, including updating annotations and merging content in owl ontologies.

The “owl_get_subdomains.sparql” example query returns the count of main branches (facets) of FoodOn:

PREFIX owl: <>
PREFIX rdf: <>
PREFIX rdfs: <>
PREFIX obo: <>
PREFIX foodon: <>
	(STR(?lable) AS ?name) 
	(count(?class) as ?total) 
	values ?search {
		foodon:03411041 # Chemical food component
		obo:OBI_0100026 # Organism (NCBI taxonomy)
		foodon:03411564 # Food product organismal source
		foodon:03420116 # Part of organism (anatomy)
		foodon:00002381 # Food product by organism (~single component food)
		foodon:00002501 # Multi-component food product
		foodon:00002451 # Food transformation process
		foodon:00003368 # Food contact material
		foodon:03400361 # Agency food product type
	{?class rdfs:subClassOf+ ?search}
	OPTIONAL {?search rdfs:label ?lable.}
GROUP BY ?search ?lable

Result as of Sept 15, 2020:

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: <>
PREFIX obo: <>
SELECT DISTINCT ?id ?parent ?label ?definition
		{?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: <>
PREFIX obo: <>
		{?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.


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:

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