sparql-examples

A set of SPARQL examples that are used in different TGX resources

View the Project on GitHub BiGCAT-UM/sparql-examples

047

rq turtle/ttl

Chemical aspect: publications per year for this chemical

Use at

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
#defaultView:BarChart
PREFIX target: <http://www.wikidata.org/entity/Q2270>

# title: publications per year for this chemical
# Inspired from LEGOLAS - http://abel.lis.illinois.edu/legolas/
# Shubhanshu Mishra, Vetle Torvik
select ?year (count(?work) as ?number_of_publications) where {
  {
    select (str(?year_) as ?year) (0 as ?pages) where {
      # default values = 0
      ?year_item wdt:P31 wd:Q577 . 
      ?year_item wdt:P585 ?date .
      bind(year(?date) as ?year_)
      {
        select (min(?year_) as ?earliest_year) where {
          { ?work wdt:P921/wdt:P31*/wdt:P279* target: . }
          union { ?work wdt:P921/(^wdt:P361)+ target: . }
          union { ?work wdt:P921/wdt:P1269+ target: . }
          union { target: ?propp ?statement .
                  ?statement a wikibase:BestRank ;
                             prov:wasDerivedFrom/pr:P248 ?work . }
          ?work wdt:P577 ?publication_date . 
          bind(year(?publication_date) as ?year_)
        }
      }
      bind(year(now()) as ?next_year)
      filter (?year_ >= ?earliest_year && ?year_ <= ?next_year)
    }
  }
  union {
    select ?work (min(?years) as ?year) where {
      { ?work wdt:P921/wdt:P31*/wdt:P279* target: . }
      union { ?work wdt:P921/wdt:P527+ target: . }
      union { ?work wdt:P921/wdt:P1269+ target: . }
      union { target: ?propp ?statement .
              ?statement a wikibase:BestRank ;
                         prov:wasDerivedFrom/pr:P248 ?work . }
      ?work wdt:P577 ?dates .
      bind(str(year(?dates)) as ?years) .
    }
    group by ?work 
  }
}
group by ?year 
order by ?year

graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v6("?date")
  v12("?dates")
  v11("?earliest_year")
  v11("?next_year")
  v14("?number_of_publications")
  v11("?pages")
  v8("?propp")
  v10("?publication_date")
  v9("?statement")
  v7("?work"):::projected 
  v14("?year"):::projected 
  v11("?year_")
  v5("?year_item")
  v13("?years")
  a1((" "))
  a2((" "))
  a3((" "))
  a4((" "))
  a5((" "))
  a6((" "))
  a7((" "))
  a8((" "))
  a9((" "))
  a10((" "))
  c2([http://wikidata.org/entity/Q577]):::iri 
  c10(["wikibase:BestRank"]):::iri 
  c6(["wd:Q2270"]):::iri 
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    subgraph union1[" Union "]
    subgraph union1l[" "]
      style union1l fill:#abf,stroke-dasharray: 3 3;
      subgraph union2[" Union "]
      subgraph union2l[" "]
        style union2l fill:#abf,stroke-dasharray: 3 3;
        subgraph union3[" Union "]
        subgraph union3l[" "]
          style union3l fill:#abf,stroke-dasharray: 3 3;
          c6 -->v8--> v9
          v9 --"a"-->  c10
          v9 --"prov:wasDerivedFrom"-->  a10
          a10 --"p:reference/P248"-->  v7
        end
        subgraph union3r[" "]
          style union3r fill:#abf,stroke-dasharray: 3 3;
          v7 --"p:direct/P921"-->  a9
          a9 --"p:direct/P1269"-->  c6
        end
        union3r <== or ==> union3l
        end
      end
      subgraph union2r[" "]
        style union2r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P921"-->  a8
        a8 --"p:direct/P527"-->  c6
      end
      union2r <== or ==> union2l
      end
    end
    subgraph union1r[" "]
      style union1r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P921"-->  a6
      a6 --"p:direct/P31"-->  a7
      a7 --"p:direct/P279"-->  c6
    end
    union1r <== or ==> union1l
    end
    v7 --"p:direct/P577"-->  v12
    bind0[/"str(year-from-dateTime(?dates))"/]
    v12 --o bind0
    bind0 --as--o v13
    bind2[/"min(?years)"/]
    v13 --o bind2
    bind2 --as--o v14
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    f3[["?year_ >= ?earliest_year?year_ <= ?next_year"]]
    f3 --> v11
    f3 --> v11
    f3 --> v11
    v5 --"p:direct/P31"-->  c2
    v5 --"p:direct/P585"-->  v6
    bind4[/"year-from-dateTime(?date)"/]
    v6 --o bind4
    bind4 --as--o v11
    subgraph union4[" Union "]
    subgraph union4l[" "]
      style union4l fill:#abf,stroke-dasharray: 3 3;
      subgraph union5[" Union "]
      subgraph union5l[" "]
        style union5l fill:#abf,stroke-dasharray: 3 3;
        subgraph union6[" Union "]
        subgraph union6l[" "]
          style union6l fill:#abf,stroke-dasharray: 3 3;
          c6 -->v8--> v9
          v9 --"a"-->  c10
          v9 --"prov:wasDerivedFrom"-->  a5
          a5 --"p:reference/P248"-->  v7
        end
        subgraph union6r[" "]
          style union6r fill:#abf,stroke-dasharray: 3 3;
          v7 --"p:direct/P921"-->  a4
          a4 --"p:direct/P1269"-->  c6
        end
        union6r <== or ==> union6l
        end
      end
      subgraph union5r[" "]
        style union5r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P921"-->  a3
        c6 --"p:direct/P361"-->  a3
      end
      union5r <== or ==> union5l
      end
    end
    subgraph union4r[" "]
      style union4r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P921"-->  a1
      a1 --"p:direct/P31"-->  a2
      a2 --"p:direct/P279"-->  c6
    end
    union4r <== or ==> union4l
    end
    v7 --"p:direct/P577"-->  v10
    bind5[/"year-from-dateTime(?publication_date)"/]
    v10 --o bind5
    bind5 --as--o v11
    bind7[/"min(?year_)"/]
    v11 --o bind7
    bind7 --as--o v11
    bind8[/"year-from-dateTime(NOW())"/]
    bind8 --as--o v11
    bind9[/"str(?year_)"/]
    v11 --o bind9
    bind9 --as--o v14
    bind10[/"'0^^xsd:integer'"/]
    bind10 --as--o v11
  end
  union0r <== or ==> union0l
  end
  bind12[/"count(?work)"/]
  v7 --o bind12
  bind12 --as--o v14