SPARQLSPARQLProtocolAndRDFQueryLanguage
AmrapaliZaveri,Ph.D.
What is SPARQL?SPARQL(pronouncedsparkle)standsfor:SPARQLProtocolAndRDFQueryLanguage• SPARQL1.0W3C-RecommendaFonsinceJanuary15th2008• SPARQL1.1W3C-RecommendaFonsinceMarch21st2013• QuerylanguagetoqueryinstancesinRDFdocuments• GreatpracFcalimportance(almostallapplicaFonsneedit)
SPARQL ExampleSELECT
*
WHERE {
?subject?predicate?object.
}
Similar to SQL !
SPARQL ComponentsASPARQLquerycomprises,inorder:• PREFIXdeclaraFons,forabbreviaFngURIs• Datasetdefini;on,staFngwhatRDFgraph(s)arebeingqueried• Aresultclause,idenFfyingwhatinformaFontoreturnfromthequery
• Thequerypa@ern,specifyingwhattoqueryforintheunderlyingdataset
• Querymodifiers,slicing,ordering,andotherwiserearrangingqueryresults
SPARQL Components#prefixdeclara;onsPREFIXfoo:<h`p://example.com/resources/>#datasetdefini;onFROM#resultclauseSELECT#querypa@ernWHERE{...}#querymodifiersORDERBY...
SPARQL ExampleSELECT*WHERE{
?bookrdf:type<h`p://dbpedia.org/ontology/Book>.
}
Try it yourself: dbpedia.org/sparql/
SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>
SELECT*WHERE{
?bookadbo:Book.
?bookdbo:author?author.
}
Try it yourself: dbpedia.org/sparql/
SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>
SELECT*WHERE{
?bookadbo:Book.
?bookdbo:author?author.
}LIMIT10
Try it yourself: dbpedia.org/sparql/
SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>
SELECT?authorWHERE{
?bookadbo:Book.
?bookdbo:author?author.
}LIMIT10
Try it yourself: dbpedia.org/sparql/
Filter in SPARQL• KeywordFILTER,followedbyfilterexpressioninparentheses• FiltercondiFonsoutputtruthvalues(andpossiblyerrors)• ManyfilterfuncFonsarenotspecifiedbyRDF funcFons• partlytakenfromXQuery/XPath-StandardforXML
Filter FunctionsComparisonoperators:<,=,>,<=,>=,!=• Comparisonofdataliteralsaccordingtonaturalorder• Supportfornumericaldatatypes,xsd:dateTime,xsd:string
(alphabeFcordering),xsd:Boolean(1>0)• ForothertypesandotherRDF-elements,only=and!=are
available• ComparisonofliteralsofincompaFbletypes(e.g.xsd:stringand
xsd:integer)isnotallowedArithmaFcoperators:+,-,*,/• Supportfornumericaldatatypes• UsedtocombinevaluesinfiltercondiFonsEx.:FILTER(?weight/
(?size*?size)>=25)
SPARQL ExamplePREFIXdbo:<h`p://dbpedia.org/ontology/>
SELECT?authorWHERE{
?bookadbo:Book.
?bookdbo:author?author.
?bookdbo:numberOfPages?pages.
FILTER(?pages>500)
}LIMIT10
Try it yourself: dbpedia.org/sparql/
Special Filter FunctionsOtherRDF-specificfilterfuncFons:
sameTERM(A,B) true,ifAandBarethesameRDF-terms.
langMATCHES(A,B) true,ifthelanguagespecificaFonAfitsthepa`ernB
REGEX(A,B) true,ifthecharacterstringAcontainstheregularexpressionB
SPARQL ExamplePREFIXdbo:<h`p://dbpedia.org/ontology/>PREFIXdbp:<h@p://dbpedia.org/property/>
SELECT*WHERE{
?bookadbo:Book.
?bookdbo:author?author.?bookdbo:numberOfPages?pages.
?bookdbp:name?name.
FILTER(?pages>500)
FILTER(langMATCHES(LANG(?name),"en))}LIMIT10
Try it yourself: dbpedia.org/sparql/
Filter Functions: Boolean operators
FiltercondiFonscanbelinkedwithbooleanoperators:&&,||,!• ParFallyalsoexpressiblethroughgraphpa`ern:
• ConjuncFoncorrespondstospecificaFonsofseveralfilters• DisjuncFoncorrespondstoapplicaFonoffiltersinalternaFvepa`erns
Filter Functions: Boolean operators
UnFlnow,onlybasicformaongseongforresults:• Howcanoneretrievedefinedpartsoftheoutputset?• Howaretheresultsordered?• Canduplicateresultrowsberemovedinstantaneously?
Result SortingPREFIXdbo:<h`p://dbpedia.org/ontology/>PREFIXdbp:<h`p://dbpedia.org/property/>
SELECT*WHERE{
?bookadbo:Book.
?bookdbo:author?author.?bookdbo:numberOfPages?pages.
?bookdbp:name?name.
FILTER(?pages>500)
FILTER(langMATCHES(LANG(?name),"en))}ORDERBY?pagesLIMIT10
Try it yourself: dbpedia.org/sparql/
Resulting Sorting• SorFngsameaswithfiltercomparisonoperators• SorFngofURIsalphabeFcallyassequenceofcharacters
OtherpossiblespecificaFons:•ORDERBYDESC(?price):descending•ORDERBYASC(?price):ascending,defaultseong•ORDERBYDESC(?price),?Ftle:hierarchicalclassificaFoncriteria
LIMIT, OFFSET, DISTINCTRestricFonofoutputset:• LIMIT:maximalnumberofresults(tablerows)• OFFSET:posiFonofthefirstdeliveredresult• SELECTDISTINCT:removalofduplicatetablerows
LIMITandOFFSETonlymakesensewithORDERBY!
Hands-on Bio2RDF• SPARQL endpoint
• http://sparql.openlifedata.org/
• Datasets
• http://download.bio2rdf.org/release/3/release.html
Hands-on DIY -1
DESCRIBE <http://bio2rdf.org/drugbank:DB00088>
The DESCRIBE query result clause allows the server to return whatever RDF it wants that describes the given resource(s).
Hands-on Bio2RDF -2<http://bio2rdf.org/drugbank:DB00088>
• Retrieve title, affected organism, target and group of this Drug
TIP: View it as NTriples to construct your query!
Hands-on Bio2RDF - 3
SELECT * WHERE { ?drug a <http://bio2rdf.org/drugbank_vocabulary:Drug>. }
• filter for drugs starting with A
Hands-on Bio2RDF -3
SELECT * WHERE { ?drug a <http:bio2rdf.orgdrugbank_vocabulary:Drug>. }
• filter for drugs starting with A
• FILTER regex(?title, "A")
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
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
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.
Answer 1
PREFIX dv: <http://bio2rdf.org/drugbank_vocabulary:> PREFIX dct: <http://purl.org/dc/terms/>
SELECT distinct ?enzyme_name ?drug_name { ?s a dv:Enzyme-Relation . ?s dv:enzyme/dct:title ?enzyme_name . ?s dv:drug/dct:title ?drug_name .} LIMIT 100
Answer 2PREFIX om: <http://bio2rdf.org/omim_vocabulary:>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX b: <http://bio2rdf.org/bio2rdf_vocabulary:>
SELECT * {
?s a om:Phenotype .
?s dct:title ?name .
?s om:clinical-synopsis ?cs .
?cs om:feature ?f .
?f om:x-umls/b:identifier ?umls .
?f om:x-hp/dct:identifier ?hp .
?s om:phenotype-map ?pm .
?pm om:gene-symbol ?gene.
} LIMIT 10
Answer 3PREFIX om: <http://bio2rdf.org/omim_vocabulary:>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX hgnc:<http://bio2rdf.org/hgnc.symbol>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?geneName ?name ?pubmedArticle{
?s a om:Phenotype .
?s dct:title ?name .
?s om:phenotype-map ?pm .
?pm om:gene-symbol ?gene.
?gene dct:title ?geneName.
?s om:article ?pubmed.
?pubmed dct:title ?pubmedArticle.
FILTER regex(?gene, “BRCA1") }
SPARQL Components#prefixdeclara;onsPREFIXfoo:<h`p://example.com/resources/>#datasetdefini;onFROM#resultclauseSELECT#querypa@ernWHERE{...}#querymodifiersORDERBY...
SummaryBASIC Structure
PREFIX
WHERE
GRAPH Patternsimple graph pattern
{…}OPTIONAL
UNION
FILTERLANG
DATATYPEREGEX
OUTPUT FormatSELECT
CONSTRUCTASK
DESCRIBE
MODIFIERSORDER BY
LIMITOFFSET
DISTINCT
THANK YOU! QUESTIONS?
@AmrapaliZ
Contact me if you are interested in doing a project at the Institute of Data Science, Uni Maastricht!