query-cb678acd6e5269ba5f2893213e5a22fa

rq turtle/ttl

.on TwitterOriginally posted

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX psn: <http://www.wikidata.org/prop/statement/value-normalized/>
PREFIX p: <http://www.wikidata.org/prop/>
# Average gestation period of genera, color-coded by order
#defaultView:BubbleChart
SELECT ?genus (AVG(?period) AS ?averagePeriod) (SAMPLE(?label) AS ?genusLabel) (SAMPLE(?rgb_) AS ?rgb) WHERE {
  {
    BIND("en" AS ?language)
    # find species with gestation period, normalized to seconds (SI base unit)
    ?species p:P3063/psn:P3063/wikibase:quantityAmount ?periodSeconds.
    # convert back to days
    BIND(?periodSeconds/(60*60*24) AS ?period)
    # find genus
    ?species wdt:P171* ?genus.
    ?genus wdt:P105 wd:Q34740.
    # find a good label – trivial name, else label, else “<no name>”
    OPTIONAL {
      ?genus wdt:P1843 ?trivialName.
      FILTER(LANG(?trivialName) = ?language)
    }
    OPTIONAL {
      ?genus rdfs:label ?genusLabel.
      FILTER(LANG(?genusLabel) = ?language)
    }
    BIND(COALESCE(?trivialName, ?genusLabel, "<no name>"@en) AS ?label)
    # find order
    ?genus wdt:P171* ?order.
    ?order wdt:P105 wd:Q36602.
    # choose “random but deterministic” color per order; you can play around with "-" (can be any string) to find a pleasing resulting color distribution
    BIND(UCASE(SUBSTR(SHA256(CONCAT("-", STR(?order))), 1, 6)) AS ?rgb_)
  } UNION {
    # add scale
    VALUES (?genus ?period ?label) {
      (wd:Q23387 7 "week"@en)
      (wd:Q5151 30.436875 "month"@en)
      (wd:Q1643308 91.310625 "3 months"@en)
      (wd:Q2269240 182.62125 "6 months"@en)
      (wd:Q577 365.2425 "year"@en)
    }
    BIND("CCCCCC" AS ?rgb_)
  }
}
GROUP BY ?genus

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v11("?averagePeriod") v11("?genus"):::projected v12("?genusLabel") v11("?label"):::projected v4("?language") v9("?order") v11("?period"):::projected v5("?periodSeconds") v12("?rgb") v11("?rgb_"):::projected v4("?species") v3("?trivialName") a1((" ")) a2((" ")) c6(["wd:Q34740"]):::iri c9(["wd:Q36602"]):::iri subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; bind0[/VALUES ?genus ?label ?period/] bind0-->v11 bind0-->v11 bind0-->v11 bind00(["wd:Q23387"]) bind00 --> bind0 bind01(["7^^xsd:integer"]) bind01 --> bind0 bind02([sweek^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind02 --> bind0 bind03(["wd:Q5151"]) bind03 --> bind0 bind04(["30.436875^^xsd:decimal"]) bind04 --> bind0 bind05([smonth^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind05 --> bind0 bind06(["wd:Q1643308"]) bind06 --> bind0 bind07(["91.310625^^xsd:decimal"]) bind07 --> bind0 bind08([s3 months^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind08 --> bind0 bind09(["wd:Q2269240"]) bind09 --> bind0 bind010(["182.62125^^xsd:decimal"]) bind010 --> bind0 bind011([s6 months^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind011 --> bind0 bind012(["wd:Q577"]) bind012 --> bind0 bind013(["365.2425^^xsd:decimal"]) bind013 --> bind0 bind014([syear^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind014 --> bind0 bind1[/"'CCCCCC'"/] bind1 --as--o v11 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; bind2[/"'en'"/] bind2 --as--o v4 v4 --"p:P3063"--> a1 a1 --"p:statement/value-normalized/P3063"--> a2 a2 --"wikibase:quantityAmount"--> v5 bind3[/"?periodSeconds / '60^^xsd:integer' * '60^^xsd:integer' * '24^^xsd:integer'"/] v5 --o bind3 bind3 --as--o v11 v4 --"p:direct/P171"--> v11 v11 --"p:direct/P105"--> c6 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v11 -."p:direct/P1843".-> v3 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v11 -."rdfs:label".-> v12 end bind4[/"?trivialName?genusLabels<no name>^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>'"/] v3 --o bind4 v12 --o bind4 bind4 --as--o v11 v11 --"p:direct/P171"--> v9 v9 --"p:direct/P105"--> c9 bind5[/"upper-case(substring(SHA256(concat('-',str(?order))),'1^^xsd:integer','6^^xsd:integer'))"/] v9 --o bind5 bind5 --as--o v11 end union0r <== or ==> union0l end bind9[/"average(?period)"/] v11 --o bind9 bind9 --as--o v11 bind10[/"sample(?label)"/] v11 --o bind10 bind10 --as--o v12 bind11[/"sample(?rgb_)"/] v11 --o bind11 bind11 --as--o v12