33
SPARQL SPARQL Protocol And RDF Query Language Amrapali Zaveri, Ph.D.

Introduction to Bio SPARQL

Embed Size (px)

Citation preview

Page 1: Introduction to Bio SPARQL

SPARQLSPARQLProtocolAndRDFQueryLanguage

AmrapaliZaveri,Ph.D.

Page 2: Introduction to Bio SPARQL

What is SPARQL?SPARQL(pronouncedsparkle)standsfor:SPARQLProtocolAndRDFQueryLanguage• SPARQL1.0W3C-RecommendaFonsinceJanuary15th2008• SPARQL1.1W3C-RecommendaFonsinceMarch21st2013• QuerylanguagetoqueryinstancesinRDFdocuments• GreatpracFcalimportance(almostallapplicaFonsneedit)

Page 3: Introduction to Bio SPARQL

SPARQL ExampleSELECT

*

WHERE {

?subject?predicate?object.

}

Similar to SQL !

Page 4: Introduction to Bio SPARQL

SPARQL ComponentsASPARQLquerycomprises,inorder:• PREFIXdeclaraFons,forabbreviaFngURIs• Datasetdefini;on,staFngwhatRDFgraph(s)arebeingqueried• Aresultclause,idenFfyingwhatinformaFontoreturnfromthequery

• Thequerypa@ern,specifyingwhattoqueryforintheunderlyingdataset

• Querymodifiers,slicing,ordering,andotherwiserearrangingqueryresults

Page 5: Introduction to Bio SPARQL

SPARQL Components#prefixdeclara;onsPREFIXfoo:<h`p://example.com/resources/>#datasetdefini;onFROM#resultclauseSELECT#querypa@ernWHERE{...}#querymodifiersORDERBY...

Page 6: Introduction to Bio SPARQL

SPARQL ExampleSELECT*WHERE{

?bookrdf:type<h`p://dbpedia.org/ontology/Book>.

}

Try it yourself: dbpedia.org/sparql/

Page 7: Introduction to Bio SPARQL

SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>

SELECT*WHERE{

?bookadbo:Book.

?bookdbo:author?author.

}

Try it yourself: dbpedia.org/sparql/

Page 8: Introduction to Bio SPARQL

SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>

SELECT*WHERE{

?bookadbo:Book.

?bookdbo:author?author.

}LIMIT10

Try it yourself: dbpedia.org/sparql/

Page 9: Introduction to Bio SPARQL

SPARQL ExamplePREFIXdbo:<h@p://dbpedia.org/ontology/>

SELECT?authorWHERE{

?bookadbo:Book.

?bookdbo:author?author.

}LIMIT10

Try it yourself: dbpedia.org/sparql/

Page 10: Introduction to Bio SPARQL

Filter in SPARQL• KeywordFILTER,followedbyfilterexpressioninparentheses• FiltercondiFonsoutputtruthvalues(andpossiblyerrors)• ManyfilterfuncFonsarenotspecifiedbyRDF funcFons• partlytakenfromXQuery/XPath-StandardforXML

Page 11: Introduction to Bio SPARQL

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)

Page 12: Introduction to Bio SPARQL

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/

Page 13: Introduction to Bio SPARQL

Special Filter FunctionsOtherRDF-specificfilterfuncFons:

sameTERM(A,B) true,ifAandBarethesameRDF-terms.

langMATCHES(A,B) true,ifthelanguagespecificaFonAfitsthepa`ernB

REGEX(A,B) true,ifthecharacterstringAcontainstheregularexpressionB

Page 14: Introduction to Bio SPARQL

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/

Page 15: Introduction to Bio SPARQL

Filter Functions: Boolean operators

FiltercondiFonscanbelinkedwithbooleanoperators:&&,||,!• ParFallyalsoexpressiblethroughgraphpa`ern:

• ConjuncFoncorrespondstospecificaFonsofseveralfilters• DisjuncFoncorrespondstoapplicaFonoffiltersinalternaFvepa`erns

Page 16: Introduction to Bio SPARQL

Filter Functions: Boolean operators

UnFlnow,onlybasicformaongseongforresults:• Howcanoneretrievedefinedpartsoftheoutputset?• Howaretheresultsordered?• Canduplicateresultrowsberemovedinstantaneously?

Page 17: Introduction to Bio SPARQL

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/

Page 18: Introduction to Bio SPARQL

Resulting Sorting• SorFngsameaswithfiltercomparisonoperators• SorFngofURIsalphabeFcallyassequenceofcharacters

OtherpossiblespecificaFons:•ORDERBYDESC(?price):descending•ORDERBYASC(?price):ascending,defaultseong•ORDERBYDESC(?price),?Ftle:hierarchicalclassificaFoncriteria

Page 19: Introduction to Bio SPARQL

LIMIT, OFFSET, DISTINCTRestricFonofoutputset:• LIMIT:maximalnumberofresults(tablerows)• OFFSET:posiFonofthefirstdeliveredresult• SELECTDISTINCT:removalofduplicatetablerows

LIMITandOFFSETonlymakesensewithORDERBY!

Page 20: Introduction to Bio SPARQL

Hands-on Bio2RDF• SPARQL endpoint

• http://sparql.openlifedata.org/

• Datasets

• http://download.bio2rdf.org/release/3/release.html

Page 21: Introduction to Bio SPARQL

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

Page 22: Introduction to Bio SPARQL

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!

Page 23: Introduction to Bio SPARQL

Hands-on Bio2RDF - 3

SELECT * WHERE { ?drug a <http://bio2rdf.org/drugbank_vocabulary:Drug>. }

• filter for drugs starting with A

Page 24: Introduction to Bio SPARQL

Hands-on Bio2RDF -3

SELECT * WHERE { ?drug a <http:bio2rdf.orgdrugbank_vocabulary:Drug>. }

• filter for drugs starting with A

• FILTER regex(?title, "A")

Page 25: Introduction to Bio SPARQL

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

Page 26: Introduction to Bio SPARQL

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

Page 27: Introduction to Bio SPARQL

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.

Page 28: Introduction to Bio SPARQL

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

Page 29: Introduction to Bio SPARQL

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

Page 30: Introduction to Bio SPARQL

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") }

Page 31: Introduction to Bio SPARQL

SPARQL Components#prefixdeclara;onsPREFIXfoo:<h`p://example.com/resources/>#datasetdefini;onFROM#resultclauseSELECT#querypa@ernWHERE{...}#querymodifiersORDERBY...

Page 32: Introduction to Bio SPARQL

SummaryBASIC Structure

PREFIX

WHERE

GRAPH Patternsimple graph pattern

{…}OPTIONAL

UNION

FILTERLANG

DATATYPEREGEX

OUTPUT FormatSELECT

CONSTRUCTASK

DESCRIBE

MODIFIERSORDER BY

LIMITOFFSET

DISTINCT

Page 33: Introduction to Bio SPARQL

THANK YOU! QUESTIONS?

@AmrapaliZ

[email protected]

Contact me if you are interested in doing a project at the Institute of Data Science, Uni Maastricht!