@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:52 a sh:SPARQLExecutable,
        sh:SPARQLSelectExecutable ;
    rdfs:comment "Retrieve drugs that target human enzymes involved in sterol metabolism (federated query with Rhea and ChEMBL via IDSM/Elixir czech republic)."@en ;
    sh:prefixes _:sparql_examples_prefixes ;
    sh:select """PREFIX CHEBI: <http://purl.obolibrary.org/obo/CHEBI_>
PREFIX cco: <http://rdf.ebi.ac.uk/terms/chembl#>
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 skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX taxon: <http://purl.uniprot.org/taxonomy/>
PREFIX up: <http://purl.uniprot.org/core/>

SELECT
        DISTINCT
            ?protein
            ?proteinFullName
            ?activityType
            ?standardActivityValue
            ?standardActivityUnit
            ?chemblMolecule
            ?chemlbMoleculePrefLabel
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 .
  ?protein  up:annotation         ?a ;
	    up:organism           taxon:9606 ;
	    up:recommendedName    ?proteinRecName .
  ?proteinRecName
	    up:fullName           ?proteinFullName .
  # Find drugs in wikidata that interact with the UniProt Proteins
  # ChEMBL: retrieve the corresponding targets and with drugs in clinical phase 4
  # Via https://idsm.elixir-czech.cz/sparql/
  SERVICE <https://idsm.elixir-czech.cz/sparql/endpoint/idsm> {
    ?activity a cco:Activity ;
      cco:hasMolecule ?chemblMolecule ;
      cco:hasAssay ?assay ;
      cco:standardType ?activityType ;
      cco:standardValue ?standardActivityValue ;
      cco:standardUnits ?standardActivityUnit .
    ?chemblMolecule cco:highestDevelopmentPhase ?highestDevelopmentPhase ;
      rdfs:label ?chemblMoleculeLabel ;
      skos:prefLabel ?chemlbMoleculePrefLabel .
    FILTER (?highestDevelopmentPhase > 3)
    ?assay cco:hasTarget ?target .
    ?target cco:hasTargetComponent/cco:targetCmptXref ?protein .
    ?protein a cco:UniprotRef .
  }
}""" ;
    schema:keywords "drug",
        "enzyme",
        "metabolite" ;
    schema:keywords "enzyme" , "drug" , "pathway" , "Rhea" , "federated query" , "ChEMBL" ;
    schema:target <https://sparql.uniprot.org/sparql/> ;
    spex:federatesWith <https://sparql.rhea-db.org/sparql>, <https://idsm.elixir-czech.cz/sparql/endpoint/idsm> .

