10. Filter in SPARQL
• Keyword FILTER, followed by filter expression in parentheses
• Filter condiFons output truth values (and possibly errors)
• Many filter funcFons are not specified by RDF
funcFons
• partly taken from XQuery/XPath-Standard for XML
11. Filter Functions
Comparison operators: <, =, >, <=, >=, !=
• Comparison of data literals according to natural order
• Support for numerical data types, xsd:dateTime, xsd:string
(alphabeFc ordering), xsd:Boolean (1>0)
• For other types and other RDF-elements, only = and != are
available
• Comparison of literals of incompaFble types (e.g. xsd:string and
xsd:integer) is not allowed
ArithmaFc operators: +, -, *, /
• Support for numerical data types
• Used to combine values in filter condiFons Ex.: FILTER(?weight/
(?size*?size)>=25)
18. Resulting Sorting
• SorFng same as with filter comparison operators
• SorFng of URIs alphabeFcally as sequence of characters
Other possible specificaFons:
• ORDER BY DESC(?price): descending
• ORDER BY ASC(?price): ascending, default seong
• ORDER BY DESC(?price), ?Ftle: hierarchical classificaFon criteria
19. LIMIT, OFFSET, DISTINCT
RestricFon of output set:
• LIMIT: maximal number of results (table rows)
• OFFSET: posiFon of the first delivered result
• SELECT DISTINCT: removal of duplicate table rows
LIMIT and OFFSET only make sense with ORDER BY!
23. Hands-on Bio2RDF - 3
SELECT * WHERE {
?drug a
<http://bio2rdf.org/drugbank_vocabulary:Drug>. }
• filter for drugs starting with A
24. Hands-on Bio2RDF -3
SELECT * WHERE {
?drug a
<http:bio2rdf.orgdrugbank_vocabulary:Drug>. }
• filter for drugs starting with A
• FILTER regex(?title, "A")
25. Hands-on Bio2RDF -3
SELECT * WHERE {
?drug a
<http://bio2rdf.org/drugbank_vocabulary:Drug>. }
• filter for drugs starting with A
• FILTER regex(?title, “A")
• sort them alphabetically
26. Hands-on DIY
• Get 100 drug-metabolizing enzymes
• Get phenotypes and genes associated with
OMIM diseases
• Retrieve unique diseases and publications
associated with the BRCA1 gene
27. Hands-on DIY
• Get 100 drug-metabolizing enzymes
• Get phenotypes and genes associated with
OMIM diseases
• Retrieve unique diseases and publications
associated with the BRCA1 gene
TIPS:
- View the resource as NTriples to construct your query
- Use prefix.cc to look up prefixes
- Always use LIMIT
- Start by getting results one triple at a time and build up.