Koetai SPARQL examples

Community-curated SPARQL example queries for multiple life-science endpoints.

View the Project on GitHub Koetai/sparql-examples

find_applicable_taxonomic_literature_in_the_library_of_identification_resources

rq turtle/ttl

Get a checklist of species in a certain taxonomic group (Pentatomidae; gbifsp:9650) occurring in a certain country (Netherlands; NL), and then rank publications in the Library of Identification Resources based on what proportion of the species from the checklist are included.

Use at

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dwc: <http://rs.tdwg.org/dwc/terms/>
PREFIX dwciri: <http://rs.tdwg.org/dwc/iri/>
PREFIX gbifrank: <https://rs.gbif.org/terms/rank/>
PREFIX gbifsp: <https://www.gbif.org/species/>
PREFIX gbifterms: <https://rs.gbif.org/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT ?resource ?title (YEAR(?date) AS ?year) ?pTax ?pObs WHERE {
  {
    SELECT
      ?resource
      (COUNT(?taxon)/?totalCount AS ?pTax)
      (SUM(?amount)/?totalAmount AS ?pObs)
    WHERE {
      SERVICE <https://qlever.dev/api/gbif> {
        # Get species of Pentatomidae (gbifsp:9650) occuring in the Netherlands ("NL") and their count
        {
          SELECT ?taxon ?scientificName (COUNT(?occurrence) AS ?amount) WHERE {
            ?taxon skos:broader* gbifsp:9650 ;
                  gbifterms:rank gbifrank:species ;
                  dwc:scientificName ?scientificName .
            ?occurrence dwc:countryCode "NL" ;
                        dwciri:toTaxon ?taxon .
          } GROUP BY ?taxon ?scientificName
        }
        # Repeat to get the count of species and total amount of occurrences
        {
          SELECT (COUNT(?taxon) AS ?totalCount) (SUM(?amount) AS ?totalAmount) WHERE {
            {
              SELECT ?taxon (COUNT(?occurrence) AS ?amount) WHERE {
                ?taxon skos:broader* gbifsp:9650 ;
                       gbifterms:rank gbifrank:species ;
                       dwc:scientificName ?scientificName .
                ?occurrence dwc:countryCode "NL" ;
                            dwciri:toTaxon ?taxon .
              } GROUP BY ?taxon
            }
          }
        }
      }

      # Match with resource checklists
      ?resource dcterms:subject/dwc:taxonID ?taxon .
    } GROUP BY ?resource ?totalCount ?totalAmount
  }

  # Get additional metadata
  ?work dcterms:hasPart ?resource ;
        dcterms:title ?title ;
        dcterms:issued ?date .
} ORDER BY DESC(?pTax)
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v9("?_anon_4e19811bfd6d4ca7be2608910d12522b122")
  v10("?_anon_4e19811bfd6d4ca7be2608910d12522b123")
  v6("?amount")
  v14("?date"):::projected 
  v4("?occurrence")
  v11("?pObs"):::projected 
  v11("?pTax"):::projected 
  v8("?resource"):::projected 
  v3("?scientificName")
  v2("?taxon")
  v13("?title"):::projected 
  v7("?totalAmount")
  v6("?totalCount")
  v12("?work")
  v15("?year")
  a1((" "))
  c3([https://www.gbif.org/species/9650]):::iri 
  c5([https://rs.gbif.org/terms/rank/species]):::iri 
  c8(["NL"]):::literal 
  subgraph s1["https://qlever.dev/api/gbif"]
    style s1 stroke-width:4px;
    v2 --"skos:broader"-->  c3
    v2 --https://rs.gbif.org/terms/rank-->  c5
    v2 --http://rs.tdwg.org/dwc/terms/scientificName-->  v3
    v4 --http://rs.tdwg.org/dwc/terms/countryCode-->  c8
    v4 --http://rs.tdwg.org/dwc/iri/toTaxon-->  v2
    bind1[/"count(?occurrence)"/]
    v4 --o bind1
    bind1 --as--o v6
    v2 --"skos:broader"-->  c3
    v2 --https://rs.gbif.org/terms/rank-->  c5
    v2 --http://rs.tdwg.org/dwc/terms/scientificName-->  v3
    v4 --http://rs.tdwg.org/dwc/terms/countryCode-->  c8
    v4 --http://rs.tdwg.org/dwc/iri/toTaxon-->  v2
    bind3[/"count(?occurrence)"/]
    v4 --o bind3
    bind3 --as--o v6
    bind6[/"count(?taxon)"/]
    v2 --o bind6
    bind6 --as--o v6
    bind7[/"sum(?amount)"/]
    v6 --o bind7
    bind7 --as--o v7
  end
  v8 --"dcterms:subject"-->  a1
  a1 --http://rs.tdwg.org/dwc/terms/taxonID-->  v2
  bind10[/"count(?taxon)"/]
  v2 --o bind10
  bind10 --as--o v9
  bind11[/"sum(?amount)"/]
  v6 --o bind11
  bind11 --as--o v10
  bind12[/" / ?totalCount"/]
  null --o bind12
  v6 --o bind12
  bind12 --as--o v11
  bind13[/" / ?totalAmount"/]
  null --o bind13
  v7 --o bind13
  bind13 --as--o v11
  v12 --"dcterms:hasPart"-->  v8
  v12 --"dcterms:title"-->  v13
  v12 --"dcterms:issued"-->  v14
  bind14[/"year-from-dateTime(?date)"/]
  v14 --o bind14
  bind14 --as--o v15