The document provides examples of SPARQL queries to retrieve information from an RDF triple store. It describes the basic structure of SPARQL queries, including SELECT clauses to define return variables, WHERE clauses to specify patterns to match, and OPTIONAL, UNION and FILTER clauses. It then gives several examples of SPARQL queries to retrieve classes, subclasses, annotations and logical relations within ontologies stored in the triple store.
4. A typical query includes the following structure, some parts are optional:
Declear prefix shortcuts (optional)
PREFIX foo: <...>
Query result clause
SELECT ...
Define the dataset (optional)
FROM <...>
Query pattern
WHERE {
...
}
Query modifiers (optional):
Group BY ...
HAVING
ORDER BY
LIMIT
OFFSET
VALUES
QUERY STRUCTURE IN SPARQL
6. Similar to its use in SQL, SELECT in SPARQL allows you to
define which variables you want values returned and output.
Like SQL you can list these individually or use an asterisk (*)
to specify values for each variable. E.g.
SELECT ?a ?text
SELECT *
If you don't want duplicates you can append DISTINCT after
SELECT. e.g.,
SELECT DISTINCT ?country
HOW TO SELECT?
7. The WHERE clause defines where you want to find values for
the variables defined in the SELECT clause.
HOW TO PROGRAM INSIDE WHERE?
8. The basic unit is a triple, which is made up of three components,
a subject, a predicate and an object. Each of the three
components can take one of two forms:
put a ? prior to the variable name, e.g., ?a.
a Universal Resource Identifier (URI).
Note: A prefix for the namespace of the URI can be utilized.
These triples can then be concatenated together using a full-stop
(.). Eventually, an interconnected graph of nodes joined by
relationships is built up.
A semi-colon (;) can be used after each triple to replace the
subject for the next triple if it is the same. In this way, you only
need to define the predicate and object. e.g.,
?x rdf:type mebase:User ;
foaf:homepage ?homepage ;
foaf:mbox ?mbox
TRIPLE REPRESENTATIONS.
9. The UNION clause:
return results to match at least one of multiple patterns
The OPTIONAL clause is also often used.
The FILTER clause filters the results based on certain
conditions.
CLAUSES
10. A solution sequence modifier is one of:
Order modifier: put the solutions in order
Projection modifier: choose certain variables
Distinct modifier: ensure solutions in the sequence are unique
Reduced modifier: permit elimination of some non-distinct
solutions
Offset modifier: control where the solutions start from in the
overall sequence of solutions
Limit modifier: restrict the number of solutions
HOW TO USE MODIFERS?
11. This section provides many examples on how to query the RDF
triple store:
Example #1: Find all class-containing ontology graphs:
This example is aimed to find all ontologies in our RDF triple
store. Typically every single ontology includes at least one
class. This SPARQL script searches those ontology graphs in
the RDF triple store that contains at least one class.
SPARQL EXAMPLES TO QUERY RDF
TRIPLE STORE
12. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT
distinct ?graph_uri WHERE { GRAPH ?graph_uri { ?s rdf:type
owl:Class } . }
BELOW IS THE SPARQL SCRIPT:
13. Example #2: Find subclasses of an ontology term:
This example is aimed to find all subclasses of an ontology
term.
14. PREFIX obo-term: <http://purl.obolibrary.org/obo/> SELECT
DISTINCT ?x ?label FROM
<http://purl.obolibrary.org/obo/merged/OAE> WHERE { ?x
rdfs:subClassOf obo-term:OAE_0000001. ?x rdfs:label ?label. }
BELOW IS THE SPARQL SCRIPT:
15. Example #3: Find the number of all class (or object,
annotation, or datatype property) terms of an ontology:
This example is aimed to find the number of all class terms of
an ontology.
16. SELECT count(?s) as ?VO_class_count FROM
<http://purl.obolibrary.org/obo/merged/VO> WHERE { ?s a
owl:Class . ?s rdfs:label ?label . FILTER regex( ?s, "VO_" ) }
BELOW IS THE SPARQL SCRIPT:
17. Example #4: Retrieve definition and authors of all classes in
an ontology:
This example is aimed to retrieve the definitions of all classes
that have definitions in an ontology.
18. PREFIX obo-term: <http://purl.obolibrary.org/obo/> SELECT
?s ?label ?definition ?author FROM
<http://purl.obolibrary.org/obo/merged/VO> WHERE { ?s a
owl:Class . ?s rdfs:label ?label . ?s obo-term:IAO_0000115
?definition . ?s obo-term:IAO_0000117 ?author . }
BELOW IS THE SPARQL SCRIPT:
19. Example #5: Retrieve general annotations of an ontology:
This example is aimed to retrieve general annotation
descriptions of the ontology. The result will return different
types of annotations such as "creator", "description"s, etc.
20. PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT
DISTINCT ?p, ?o FROM
<http://purl.obolibrary.org/obo/merged/OAE> WHERE{ ?s a
owl:Ontology . ?s ?p ?o .}
BELOW IS THE SPARQL SCRIPT
21. Example #5: Query on axiom: Find vaccines containing egg
protein allergen:
The above examples are mostly based on ontology annotation
properties. How to perform SPARQL queries based on logical
axioms with object properties (or relations)? Here we provide
some example.
22. PREFIX has_vaccine_allergen:
<http://purl.obolibrary.org/obo/VO_0000531> PREFIX
chicken_egg_protein_allergen:
<http://purl.obolibrary.org/obo/VO_0000912> SELECT
distinct ?vaccine_label ?vaccine FROM
<http://purl.obolibrary.org/obo/merged/VO> WHERE {
?vaccine rdfs:label ?vaccine_label . ?vaccine rdfs:subClassOf
?vaccine_restriction . ?vaccine_restriction owl:onProperty
has_vaccine_allergen:; owl:someValuesFrom
chicken_egg_protein_allergen: . }
BELOW IS ONE SUCH SPARQL SCRIPT:
23. Prefix obo: <http://purl.obolibrary.org/obo/> SELECT distinct
?label ?s From <http://purl.obolibrary.org/obo/merged/VO>
Where { ?s rdfs:label ?label . ?s rdfs:subClassOf ?s1 . ?s1
owl:onProperty obo:VO_0000531; owl:someValuesFrom
obo:VO_0000912 . }
NOTE THAT THE FOLLOWING CODE HAS
THE SAME EFFECT AS THE TOP CODE:
24. The difference between the above two sets of SPARQL codes
is that the first one looks easier to read.