@prefix ex: <https://sparql.uniprot.org/.well-known/sparql-examples/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <https://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix spex:<https://purl.expasy.org/sparql-examples/ontology#> .

ex:45 a sh:SPARQLExecutable,
        sh:SPARQLSelectExecutable ;
    rdfs:comment "Retrieve drugs that target human enzymes involved in sterol metabolism (federated query with WikiData and Rhea)."@en ;
    sh:prefixes _:sparql_examples_prefixes ;
    sh:select """PREFIX CHEBI: <http://purl.obolibrary.org/obo/CHEBI_>
PREFIX chebihash: <http://purl.obolibrary.org/obo/chebi#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rh: <http://rdf.rhea-db.org/>
PREFIX taxon: <http://purl.uniprot.org/taxonomy/>
PREFIX uniprotkb: <http://purl.uniprot.org/uniprot/>
PREFIX up: <http://purl.uniprot.org/core/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?protein ?proteinFullName ?wikiChemical ?wikiChemicalLabel ?medicalConditionTreatedLabel
WHERE {
  # ChEBI: retrieve members of the ChEBI class ChEBI:15889 (sterol)
  # Rhea: retrieve the reactions involving these ChEBI as participants
  SERVICE <https://sparql.rhea-db.org/sparql> {
    ?reaction rdfs:subClassOf rh:Reaction ;
      rh:status rh:Approved ;
      rh:side ?reactionSide .
    ?reactionSide
      rh:contains ?participant .
    ?participant rh:compound ?compound
    {
      ?compound rh:chebi ?chebi .
      ?chebi (rdfs:subClassOf)+ CHEBI:15889
    } UNION {
      ?compound rh:chebi ?chebi .
      ?chebi2 rdfs:subClassOf ?chebiRestriction .
      ?chebiRestriction
		a owl:Restriction ;
		owl:onProperty chebihash:has_major_microspecies_at_pH_7_3 ;
		owl:someValuesFrom ?chebi .
      ?chebi2 (rdfs:subClassOf)+ CHEBI:15889
    }
  }
  # UniProt: retrieve the human (taxid:9606) enzymes catalyzing these Rhea reactions
  ?ca up:catalyzedReaction  ?reaction .
  ?a up:catalyticActivity  ?ca .
  ?proteinIRI  up:annotation ?a ;
    up:organism taxon:9606 ;
    up:recommendedName ?proteinRecName .
  ?proteinRecName up:fullName ?proteinFullName .
  # Find drugs in wikidata that interact with the UniProt Proteins
  BIND (SUBSTR(STR(?proteinIRI), STRLEN(STR(uniprotkb:))+1) AS ?protein)
  SERVICE <https://query.wikidata.org/sparql> {
    ?wp wdt:P352  ?protein .
    ?wikiChemical wdt:P129 ?wp . # Physically interacts with
    ?wikiChemical rdfs:label ?wikiChemicalLabel .
    ?wikiChemical wdt:P2175 ?wmc . # Medical conndition treated
    ?wmc rdfs:label ?medicalConditionTreatedLabel .
    FILTER(lang(?medicalConditionTreatedLabel) = 'en')
    FILTER(lang(?wikiChemicalLabel) = 'en')
  }
}""" ;
    schema:keywords "drug",
        "enzyme",
        "metabolite" ;
    schema:keywords "enzyme" , "pathway" , "drug" , "Rhea" , "Wikidata" , "federated query" ;
    schema:target <https://sparql.uniprot.org/sparql/> ;
    spex:federatesWith <https://query.wikidata.org/sparql> , <https://sparql.rhea-db.org/sparql> .

