*Deprecated* See https://wiki.duraspace.org/display/VIVODOC/All+Documentation for current documentation

SPARQL: Tools, Tips, Sample queries

This page is just starting out and we hope can become a resource for useful snippets of information shared among members of the community.

Note that we have a Tools page in Sourceforge that lists more substantial contributions and extensions to VIVO.

Useful SPARQL queries

A little old but useful: SPARQL Queries for Data Management

finding instances having only a single rdf:type assertion

  1. This example query looks for individuals that have
  2. ONLY a vivoc:AcademicDegreeGrantingAgent type assertion
  3. Tested by substituting vivo:Address and looking at 1 of the results

CONSTRUCT {
?s rdf:type vivoc:AcademicDegreeGrantingAgent .
}
WHERE
{
GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-2> {
?s rdf:type vivoc:AcademicDegreeGrantingAgent .
OPTIONAL {
?s rdf:type ?type .
FILTER (?type != vivoc:AcademicDegreeGrantingAgent )
}
FILTER (!bound(?type))
}
}

 

 

Publications

SELECT ?doi ?infoResource_label ?pmid $type ?journal ?issn ?pubVenue ?issue ?startPage ?endPage ?volume ?dateTime
WHERE{
?infoResource vivo:hasPublicationVenue ?pubVenue .
?pubVenue rdf:type bibo:Journal .
?infoResource rdf:type $anyType .
?infoResource bibo:doi ?doi .
?infoResource bibo:pmid ?pmid .
?infoResource bibo:pageStart ?startPage .
?infoResource bibo:pageEnd ?endPage .
?pubVenue bibo:issn ?issn .
OPTIONAL { ?pubVenue bibo:volume ?volume } .
OPTIONAL { ?pubVenue vivo:DateTimeValue ?dateTime } .
OPTIONAL { ?pubVenue bibo:issue ?issue } .
?pubVenue rdfs:label ?journal .
?anyType rdfs:label ?type .
?infoResource rdfs:label $infoResource_label .
}

Authors

SELECT ?infoResource_label ?type ?author ?firstName ?lastName ?position_label ?department ?pemail
WHERE {
?infoResource vivo:hasPublicationVenue ?pubVenue .
?pubVenue rdf:type bibo:Journal .
?infoResource rdf:type ?anyType .
?anyType rdfs:label ?type .
?infoResource vivo:informationResourceInAuthorship ?authorship .
?authorship rdf:type vivo:Authorship .
?authorship vivo:linkedAuthor ?authorURI .
?authorURI rdfs:label ?author .
?infoResource rdfs:label ?infoResource_label .
OPTIONAL { $authorURI foaf:firstName ?firstName } .
OPTIONAL { $authorURI foaf:lastName ?lastName } .
OPTIONAL { $authorURI vivo:primaryEmail ?pemail } .
?authorURI vivo:personInPosition ?position .
?position vivo:positionInOrganization $organization .
?organization rdfs:label ?department .
?position rdfs:label ?position_label .
}

Find Authors with less than 5 publications

Useful for finding good test cases, for example. This is a Vivo 1.5 example, but could be updated pretty easily.

SELECT ?person
WHERE
{
      ?person vivo:authorInAuthorship ?authorship. 
}
GROUP BY ?person
HAVING (COUNT(?authorship) < 5)
LIMIT 1

 

 

SPARQL Queries for Publications and Authors

 

on 8/27/13, Eliza writes:
Hello,
We'd like to display the citation counts for articles listed on the profile pages.
So I modified the configuration file and added a citation count variable to the query.
However, the freemarker template didn't seem to pick up that variable.
See below for what was changed in the listViewConfig-authorInAuthorship.xml file:
    <query-select>    
        PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;    
        PREFIX core: &lt;http://vivoweb.org/ontology/core#&gt;
        PREFIX afn:  &lt;http://jena.hpl.hp.com/ARQ/function#&gt;
        PREFIX bibo: &lt;http://purl.org/ontology/bibo/&gt
        PREFIX c4o: &lt;http://purl.org/spar/c4o/&gt;
              
        SELECT DISTINCT ?subclass 
                        ?authorship
                        ?infoResource 
                        ?infoResourceName 
                        ?globalCitationCount
                        ?dateTime
                        ?journal
                        ?volume
                        ?startPage
                        ?endPage
                        ?publisher
                        ?locale
                        ?appearsIn
                        ?partOf
                        ?editor
                        ?hideThis
        WHERE {
            ?subject ?property ?authorship  
            OPTIONAL { ?authorship core:linkedInformationResource ?infoResource .                      
                       ?infoResource rdfs:label ?infoResourceName .
            
                       OPTIONAL { ?infoResource bibo:volume ?volume }
                       OPTIONAL { ?infoResource bibo:pageStart ?startPage }
                       OPTIONAL { ?infoResource bibo:pageEnd ?endPage }
                       OPTIONAL { ?infoResource core:placeOfPublication ?locale }
                       OPTIONAL { ?infoResource bibo:reproducedIn ?appearsInObj .
                                  ?appearsInObj rdfs:label ?appearsIn
                       }
                       OPTIONAL { ?infoResource core:publisher ?publisherObj .
                                  ?publisherObj rdfs:label ?publisher
                       }
                       OPTIONAL { ?infoResource core:editor ?editorObj .
                                  ?editorObj rdfs:label ?editor
                       }
                       OPTIONAL { ?infoResource core:partOf ?partOfObj .
                                  ?partOfObj rdfs:label ?partOf
                       }
                       OPTIONAL {  ?infoResource vitro:mostSpecificType ?subclass .
                                   ?subclass rdfs:subClassOf core:InformationResource .
                       }     
                       
                       OPTIONAL { ?infoResource core:hasPublicationVenue ?publishedIn .
                                    ?publishedIn  rdfs:label ?journal 
                       }
                       OPTIONAL { ?infoResource core:dateTimeValue ?dateTimeValue .
                                  ?dateTimeValue core:dateTime ?dateTime  
                       }
                       
                       OPTIONAL { ?infoResource c4o:hasGlobalCitationFrequency ?hasGlobalCitationFrequencyObj .
                                    ?hasGlobalCitationFrequencyObj  rdfs:label ?globalCitationCount 
                       }
                       
                       OPTIONAL { ?authorship core:hideFromDisplay ?hideThis }                                                      
            }
    #        NOT EXISTS { ?authorship core:hideFromDisplay ?hideThis } 
            <critical-data-required>
            FILTER ( bound(?infoResource) )
            </critical-data-required>
        } ORDER BY DESC(?dateTime) ?infoResourceName   
    </query-select>
And the template propStatement-authorInAuthorship.ftl:
    <#local timesCited>
     <#if statement.subclass??>
     <#if statement.globalCitationCount??>
     <br/>${statement.globalCitationCount!}
 </#if>
     </#if>
    </#local>
    
    ${resourceTitle} ${citationDetails} <@dt.yearSpan "${statement.dateTime!}" /> ${resourceType} ${timesCited}
    
Any reason why it's not working?

Resolution:

on 8/28/13, Eliza writes:
It's finally resolved, but not until moving the variable to another construct:
    <query-construct>
        PREFIX core: &lt;http://vivoweb.org/ontology/core#&gt;
        PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;    
        PREFIX bibo: &lt;http://purl.org/ontology/bibo/&gt;
        PREFIX c4o: &lt;http://purl.org/spar/c4o/&gt
        CONSTRUCT { 
            ?subject ?property ?authorship .  
            ?authorship ?authorshipProperty ?authorshipValue .
            ?authorship core:linkedInformationResource ?infoResource .
            ?infoResource rdfs:label ?infoResourceName .
            ?infoResource core:hasPublicationVenue ?publishedIn .
            ?publishedIn  rdfs:label ?journal .
            ?infoResource c4o:hasGlobalCitationFrequency ?hasGlobalCitationFrequencyObj .
            ?hasGlobalCitationFrequencyObj  rdfs:label ?globalCitationCount
        } WHERE {
            {
               ?subject ?property ?authorship 
            }
            UNION {
               ?subject ?property ?authorship .
               ?authorship ?authorshipProperty ?authorshipValue 
            } UNION {
               ?subject ?property ?authorship .
               ?authorship core:linkedInformationResource ?infoResource 
            } UNION {
               ?subject ?property ?authorship .
               ?authorship core:linkedInformationResource ?infoResource .
               ?infoResource rdfs:label ?infoResourceName 
            } UNION {
               ?subject ?property ?authorship .
               ?authorship core:linkedInformationResource ?infoResource .
               ?infoResource core:hasPublicationVenue ?publishedIn 
            } UNION {
               ?subject ?property ?authorship .
               ?authorship core:linkedInformationResource ?infoResource .
               ?infoResource core:hasPublicationVenue ?publishedIn .
               ?publishedIn  rdfs:label ?journal
            } UNION {
               ?subject ?property ?authorship .
               ?authorship core:linkedInformationResource ?infoResource .
               ?infoResource c4o:hasGlobalCitationFrequency ?hasGlobalCitationFrequencyObj .
               ?hasGlobalCitationFrequencyObj  rdfs:label ?globalCitationCount
            } 
        } 
    </query-construct>
This time, it was able to pick up the variable.
Not exactly sure why but it's working.

SPARQL Example: Positions (RUN the exmaple here: http://senrabc.github.io/vivoSparqlExamples/)

Person and Positions Diagram (VIVO 1.4 Position diagram)

 

SPARQL Query to Retrieve the Position for a single Person

Live Results: (Note: You can click on the link below to run this query in realtime at http://sparql.vivo.ufl.edu. Be patient, it make take a few minutes to return.)

(http://sparql.vivo.ufl.edu/VIVO/query?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+rdf%3A++%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0APREFIX+bibo%3A+%3Chttp%3A%2F%2Fpurl.org%2Fontology%2Fbibo%2F%3E%0D%0APREFIX+core%3A+%3Chttp%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23%3E%0D%0APREFIX+ands%3A+%3Chttp%3A%2F%2Fpurl.org%2Fands%2Fontologies%2Fvivo%2F%3E%0D%0ASELECT++*+WHERE%0D%0A%7B%0D%0A%3FURI+core%3AprimaryEmail++%27cpb%40ufl.edu%27.%0D%0A%3FURI+core%3ApersonInPosition+%3FpositionURI.%0D%0A%3FpositionURI+rdfs%3Alabel+%3FPostionlabel%0D%0A%7D&output=text&stylesheet=%2Fxml-to-html.xsl)

 

SPARQL QUERY

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX core: <http://vivoweb.org/ontology/core#>
PREFIX ands: <http://purl.org/ands/ontologies/vivo/>
SELECT  * WHERE
{

?URI core:primaryEmail  'cpb@ufl.edu'.

?URI core:personInPosition ?positionURI.

?positionURI rdfs:label ?Postionlabel

}

Live Results in JSON: 

http://sparql.vivo.ufl.edu/VIVO/query?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+rdf%3A++%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0APREFIX+bibo%3A+%3Chttp%3A%2F%2Fpurl.org%2Fontology%2Fbibo%2F%3E%0D%0APREFIX+core%3A+%3Chttp%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23%3E%0D%0APREFIX+ands%3A+%3Chttp%3A%2F%2Fpurl.org%2Fands%2Fontologies%2Fvivo%2F%3E%0D%0ASELECT++*+WHERE%0D%0A%7B%0D%0A%3FURI+core%3AprimaryEmail++%27cpb%40ufl.edu%27.%0D%0A%3FURI+core%3ApersonInPosition+%3FpositionURI.%0D%0A%3FpositionURI+rdfs%3Alabel+%3FPostionlabel%0D%0A%7D&output=json&stylesheet=%2Fxml-to-html.xsl


JSON Result

{

 "head": {

   "vars": [ "URI" , "positionURI" , "Postionlabel" ]

 } ,

 "results": {

   "bindings": [

     {

       "URI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n64866" } ,

       "positionURI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n136666" } ,

       "Postionlabel": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "Assistant Director and Senior Architect" }

     } ,

     {

       "URI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n64866" } ,

       "positionURI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n31884" } ,

       "Postionlabel": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "Associate Director" }

     } ,

     {

       "URI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n64866" } ,

       "positionURI": { "type": "uri" , "value": "http://vivo.ufl.edu/individual/n908759050" } ,

       "Postionlabel": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "Associate Director, Software Engineering" }

     }

   ]

 }

}


 
 
 

Javascript Example to run query and get back JSON


Get the code here

https://github.com/senrabc/vivoSparqlExamples/blob/master/SPARQLExamplePositions.html

 

Set it run here:

http://senrabc.github.io/vivoSparqlExamples/

 


 

 

 


 

References

Live Google Doc: https://docs.google.com/document/d/1FlGWCG0nqQ4TAVZmnKXiVMZnGxpygdcoxRcZsxw4UG0/edit#heading=h.jx71i6lwn9um

Static Document: SPARQLExamplePositions.pdf

  • No labels