Wikidata:ScienceSource project/Queries

This page is for sample ScienceSource SPARQL queries. Appearances to the contrary, these are not queries for the Wikidata Query Service. They are to be run on

http://sciencesource-query.wmflabs.org/

Links are provided on a ScienceSource wiki page.

The data schema at

http://sciencesource.wmflabs.org/wiki/Data_schema

is in use, so the P- and Q-numbers do not refer to those in use on Wikidata. The queries are displayed here using {{Sparql}} for readability.

For resources on SPARQL and homegrown queries, there are numerous examples on User:Charles Matthews/Queries and in Wikidata:WikiFactMine/Core SPARQL.

Articles by ascending length

edit
  • #Articles by ascending notional length
    SELECT ?annotation ?title ?length ?article
    WHERE 
       {?annotation wdt:P19 ?anchor.
        ?anchor wdt:P7 wd:Q6.
        ?anchor wdt:P12 ?article.
        ?anchor wdt:P10 ?length.
        ?article wdt:P20 ?title
       }
    ORDER BY ASC (?length)
    
    Try it!

Annotations for a particular term

edit
  • #Find all annotations with term "toxocariasis", showing the article in which the term is found.
    SELECT ?item ?article ?term 
      WHERE {?item wdt:P3 wd:Q5.
             ?item wdt:P15 ?term.
             ?item wdt:P20 ?article.
            FILTER (?term = "toxocariasis")
             }
    
    Try it!

Distinct terms found

edit
  • #Show all distinct terms found (currently disease and drug terms)
    SELECT DISTINCT ?term
      WHERE {?item wdt:P3 wd:Q5.
             ?item wdt:P15 ?term}
    
    Try it!

Counting annotations

edit
  • #Counting drug annotations
    #defaultView:BubbleChart
    SELECT ?drug ?drugLabel ?count
    WHERE 
    {
      {
        SELECT ?drug (COUNT(?annotation) AS ?count)    
    WHERE {
            ?annotation wdt:P15 ?drug.
            ?annotation wdt:P16 "infectiousdiseasesdrugs" .
           }
      GROUP BY ?drug 
      HAVING (?count > 1)        
      }
       SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    ORDER BY DESC(?count)
    LIMIT 500
    
    Try it!
  • #Counting disease annotations
    #defaultView:BubbleChart
    SELECT ?disease ?diseaseLabel ?count
    WHERE 
    {
      {
        SELECT ?disease (COUNT(?annotation) AS ?count)    
    WHERE {
            ?annotation wdt:P15 ?disease.
            ?annotation wdt:P16 "infectiousdiseases" .
           }
      GROUP BY ?disease
      HAVING (?count > 1)        
      }
       SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    ORDER BY DESC(?count)
    LIMIT 500
    
    Try it!

Co-occurrence 1

edit
  • #Co-occurrence of drug and disease terms (duplications removed)
    SELECT DISTINCT ?articletitle ?disease ?drug
      WHERE {?term1 wdt:P19 ?anchor1.
             ?term2 wdt:P19 ?anchor2.
             ?term1 wdt:P15 ?disease.
             ?term2 wdt:P15 ?drug.
             ?term1 wdt:P16 "infectiousdiseases".
             ?term2 wdt:P16 "infectiousdiseasesdrugs".
             ?anchor1 wdt:P12 ?article.
             ?anchor2 wdt:P12 ?article.
             ?article wdt:P20 ?articletitle.
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
             }
    
    Try it!
  1. #Co-occurrence filtered by the condition "disease term before drug term"
    SELECT ?articletitle ?disease ?drug ?charnumber1 ?charnumber2
      WHERE {?term1 wdt:P19 ?anchor1.
             ?term2 wdt:P19 ?anchor2.
             ?term1 wdt:P15 ?disease.
             ?term2 wdt:P15 ?drug.
             ?term1 wdt:P16 "infectiousdiseases".
             ?term2 wdt:P16 "infectiousdiseasesdrugs".
             ?anchor1 wdt:P10 ?charnumber1.
             ?anchor2 wdt:P10 ?charnumber2.
             ?anchor1 wdt:P12 ?article.
             ?anchor2 wdt:P12 ?article.
             ?article wdt:P20 ?articletitle.
             FILTER (?charnumber2 > ?charnumber1)
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
             }
    
    Try it!

Co-occurrence 2

edit
  • #Co-occurrence filtered by the condition "disease term before drug term" and character distance apart < 100
    SELECT ?articletitle ?disease ?drug ?charnumber1 ?charnumber2
      WHERE {?term1 wdt:P19 ?anchor1.
             ?term2 wdt:P19 ?anchor2.
             ?term1 wdt:P15 ?disease.
             ?term2 wdt:P15 ?drug.
             ?term1 wdt:P16 "infectiousdiseases".
             ?term2 wdt:P16 "infectiousdiseasesdrugs".
             ?anchor1 wdt:P10 ?charnumber1.
             ?anchor2 wdt:P10 ?charnumber2.
             ?anchor1 wdt:P12 ?article.
             ?anchor2 wdt:P12 ?article.
             ?article wdt:P20 ?articletitle.
             FILTER (?charnumber2 > ?charnumber1)
             FILTER (?charnumber2 - ?charnumber1) < 100)
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
             }
    
    Try it!

Co-occurrence 3

edit
  • #Co-occurrence (filtered) displayed for single article Q6679
    #defaultView:Dimensions
    SELECT  ?drugLabel ?charnumber2 ?charnumber1 ?diseaseLabel
    WHERE {
             ?anchor1 wdt:P12 wd:Q6679;
                      wdt:P10 ?charnumber1.
             ?anchor2 wdt:P12 wd:Q6679;
                      wdt:P10 ?charnumber2.
             ?term1 wdt:P19 ?anchor1.
             ?term2 wdt:P19 ?anchor2.
             ?term1 wdt:P15 ?disease.
             ?term2 wdt:P15 ?drug.
             ?term1 wdt:P16 "infectiousdiseases".
             ?term2 wdt:P16 "infectiousdiseasesdrugs".     
             FILTER (?charnumber2 > ?charnumber1)
             FILTER (?charnumber2 - ?charnumber1 < 200)
     
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

Counting annotations in a time period

edit
  1. #Counting annotations in a time period: publication date 2013 earliest
    SELECT ?annotation ?term ?title ?date1
    WHERE 
       {?annotation wdt:P19 ?anchor.
        ?annotation wdt:P15 ?term.
        ?anchor wdt:P12 ?article.
        ?article wdt:P17 ?date.
        ?article wdt:P20 ?title.
       BIND (YEAR(?date) AS ?date1)
       FILTER (?date1 > 2012)
       }
    
    Try it!

Annotations and quantiles

edit
  • #Annotations in notional first 10% of paper, ordered by absolute distance
    #Good correlation with terms in title
    SELECT ?annotation ?title ?term ?length1 ?length
    WHERE 
       {?annotationZ wdt:P19 ?anchorZ.
        ?anchorZ wdt:P7 wd:Q6.
        ?anchorZ wdt:P12 ?article.
        ?anchorZ wdt:P10 ?length.
        ?article wdt:P20 ?title.
        ?annotation wdt:P19 ?anchor.
        ?anchor wdt:P12 ?article.
        ?annotation wdt:P15 ?term.
        ?anchor wdt:P10 ?length1.
       FILTER(10*?length1 < ?length)
       }
    ORDER BY ASC (?length1)
    
    Try it!

LineChart by decile for given dictionary and paper

edit
  • #defaultView:LineChart
    SELECT ?decile (COUNT(?annotation) AS ?count)
    WHERE
    {
        ?annotation wdt:P3 wd:Q5 ;
                    wdt:P19 ?anchor;
                    wdt:P16 "infectiousdiseasesdrugs".
        ?anchor wdt:P12 wd:Q6679.
        ?anchor wdt:P10 ?charnumber.
        ?annotationZ wdt:P19 ?anchorZ.
        ?anchorZ wdt:P7 wd:Q6;
                 wdt:P12 wd:Q6679;
                 wdt:P10 ?length.
       BIND (floor(10*?charnumber/?length) AS ?decile)
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      }
     GROUP BY ?decile
    
    Try it!