@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix ex: <https://sparql.rhea-db.org/.well-known/sparql-examples/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <https://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

ex:66_Select_all_compounds_and_count_their_occurrence_in_Rhea_reactions a sh:SPARQLExecutable,
    sh:SPARQLSelectExecutable;
  rdfs:comment "Select all compounds and count their occurrence in Rhea reactions"@en ;
  sh:select """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rh:<http://rdf.rhea-db.org/>

SELECT
  ?reaction
  ?reactionSide1
  (count(distinct ?participant1) as ?side1ParticipantCount)
  ?reactionSide2
  (count(distinct ?participant2) as ?side2ParticipantCount)
WHERE {
  ?reaction rdfs:subClassOf rh:Reaction .
  ?reaction rh:status rh:Approved .

  ?reaction rh:side ?reactionSide1 .
  ?reactionSide1 rh:contains ?participant1 .
  ?reactionSide1 rh:curatedOrder ?curatedOrder1 .

  ?reaction rh:side ?reactionSide2 .
  ?reactionSide2 rh:contains ?participant2 .
  ?reactionSide2 rh:curatedOrder ?curatedOrder2 .

  ?reactionSide1 rh:transformableTo ?reactionSide2 .
  FILTER (?curatedOrder1 < ?curatedOrder2) .
}
GROUP BY ?reaction ?reactionSide1 ?reactionSide2
ORDER BY DESC(?side1ParticipantCount) DESC(?side2ParticipantCount) ?reaction
""";
  schema:target <https://sparql.rhea-db.org/sparql> .
