sparql-examples

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

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

069

rq turtle/ttl

Disease aspect: related diseases

Use at

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX target: <http://www.wikidata.org/entity/Q917357>

SELECT
  ?count
  ?gene_count
  ?symptom_count
  ?disease ?diseaseLabel
  ?genes
  ?symptoms
{
  {
    SELECT ?disease (COUNT(?gene) AS ?gene_count) (GROUP_CONCAT(?gene_label; separator=" // ") AS ?genes) WHERE {
      target: wdt:P2293 ?gene .
      ?gene wdt:P2293 ?disease . 
      FILTER (target: != ?disease)
      ?gene rdfs:label ?gene_label
      FILTER(lang(?gene_label) = "en")
    }
    GROUP BY ?disease
  }
  UNION
  {
    SELECT
      ?disease (COUNT(?symptom) AS ?symptom_count) (GROUP_CONCAT(?symptom_label; separator=" // ") AS ?symptoms)
    {
      target: wdt:P780 ?symptom .
      ?disease wdt:P780 ?symptom . 
      FILTER (target: != ?disease)
      ?symptom rdfs:label ?symptom_label . FILTER(lang(?symptom_label) = "en")
    }
    GROUP BY ?disease
  }

  # Aggregate count
  BIND((COALESCE(?symptom_count, 0) + COALESCE(?gene_count, 0)) AS ?count)

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en" . }
}
ORDER BY DESC(?count)

graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v11("?count"):::projected 
  v3("?disease"):::projected 
  v4("?gene")
  v5("?gene_count"):::projected 
  v2("?gene_label")
  v6("?genes"):::projected 
  v8("?symptom")
  v9("?symptom_count"):::projected 
  v7("?symptom_label")
  v10("?symptoms"):::projected 
  c2([http://www.wikidata.org/entity/Q917357]):::iri 
  c7([http://www.bigdata.com/rdf#serviceParam]):::iri 
  c9(["#91;AUTO_LANGUAGE#93;,mul,en"]):::literal 
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    f0[["?symptom_label = 'en'"]]
    f0 --> v7
    f1[["http://www.wikidata.org/entity/Q917357 != ?disease"]]
    f1 --> v3
    c2 --"p:direct/P780"-->  v8
    v3 --"p:direct/P780"-->  v8
    v8 --"rdfs:label"-->  v7
    bind4[/"count(?symptom)"/]
    v8 --o bind4
    bind4 --as--o v9
    bind5[/"?symptom_label"/]
    v7 --o bind5
    bind5 --as--o v10
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    f6[["?gene_label = 'en'"]]
    f6 --> v2
    f7[["http://www.wikidata.org/entity/Q917357 != ?disease"]]
    f7 --> v3
    c2 --"p:direct/P2293"-->  v4
    v4 --"p:direct/P2293"-->  v3
    v4 --"rdfs:label"-->  v2
    bind10[/"count(?gene)"/]
    v4 --o bind10
    bind10 --as--o v5
    bind11[/"?gene_label"/]
    v2 --o bind11
    bind11 --as--o v6
  end
  union0r <== or ==> union0l
  end
  bind12[/"?symptom_count'0^^xsd:integer' + ?gene_count'0^^xsd:integer'"/]
  v9 --o bind12
  v5 --o bind12
  bind12 --as--o v11
  subgraph s1["http://wikiba.se/ontology#label"]
    style s1 stroke-width:4px;
    c7 --"wikibase:language"-->  c9
  end