sparql-examples

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

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

153

rq turtle/ttl

Protein aspect: publications per year

Use at

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
#defaultView:BarChart

PREFIX target: <http://www.wikidata.org/entity/Q21109365>

# 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: . }
          ?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:P361+ target: . }
      union { ?work wdt:P921/wdt:P1269+ target: . }
      ?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")
  v10("?dates")
  v9("?earliest_year")
  v9("?next_year")
  v12("?number_of_publications")
  v9("?pages")
  v8("?publication_date")
  v7("?work"):::projected 
  v12("?year"):::projected 
  v9("?year_")
  v5("?year_item")
  v11("?years")
  a1((" "))
  a2((" "))
  a3((" "))
  a4((" "))
  a5((" "))
  a6((" "))
  a7((" "))
  a8((" "))
  c6(["wd:Q21109365"]):::iri 
  c2([http://wikidata.org/entity/Q577]):::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;
        v7 --"p:direct/P921"-->  a8
        a8 --"p:direct/P1269"-->  c6
      end
      subgraph union2r[" "]
        style union2r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P921"-->  a7
        a7 --"p:direct/P361"-->  c6
      end
      union2r <== or ==> union2l
      end
    end
    subgraph union1r[" "]
      style union1r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P921"-->  a5
      a5 --"p:direct/P31"-->  a6
      a6 --"p:direct/P279"-->  c6
    end
    union1r <== or ==> union1l
    end
    v7 --"p:direct/P577"-->  v10
    bind0[/"str(year-from-dateTime(?dates))"/]
    v10 --o bind0
    bind0 --as--o v11
    bind2[/"min(?years)"/]
    v11 --o bind2
    bind2 --as--o v12
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    f3[["?year_ >= ?earliest_year?year_ <= ?next_year"]]
    f3 --> v9
    f3 --> v9
    f3 --> v9
    v5 --"p:direct/P31"-->  c2
    v5 --"p:direct/P585"-->  v6
    bind4[/"year-from-dateTime(?date)"/]
    v6 --o bind4
    bind4 --as--o v9
    subgraph union3[" Union "]
    subgraph union3l[" "]
      style union3l fill:#abf,stroke-dasharray: 3 3;
      subgraph union4[" Union "]
      subgraph union4l[" "]
        style union4l fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P921"-->  a4
        a4 --"p:direct/P1269"-->  c6
      end
      subgraph union4r[" "]
        style union4r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P921"-->  a3
        a3 --"p:direct/P361"-->  c6
      end
      union4r <== or ==> union4l
      end
    end
    subgraph union3r[" "]
      style union3r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P921"-->  a1
      a1 --"p:direct/P31"-->  a2
      a2 --"p:direct/P279"-->  c6
    end
    union3r <== or ==> union3l
    end
    v7 --"p:direct/P577"-->  v8
    bind5[/"year-from-dateTime(?publication_date)"/]
    v8 --o bind5
    bind5 --as--o v9
    bind7[/"min(?year_)"/]
    v9 --o bind7
    bind7 --as--o v9
    bind8[/"year-from-dateTime(NOW())"/]
    bind8 --as--o v9
    bind9[/"str(?year_)"/]
    v9 --o bind9
    bind9 --as--o v12
    bind10[/"'0^^xsd:integer'"/]
    bind10 --as--o v9
  end
  union0r <== or ==> union0l
  end
  bind12[/"count(?work)"/]
  v7 --o bind12
  bind12 --as--o v12