Table query
medication-classes-through-precise-ingredients
Each class of each active medication entry, by the drug class rows of the tables an app holds, told apart by the precise ingredient a code holds. It reads the entries of the views, as the pod questions do, and the rows inside GRAPH ?product and GRAPH ?origin, as a rule's query does. The entry is active by rule L1, as pod/My active medications has it, and its codes are the RxNorm codes it states, the one the source selected and each it carries, a literal read as the IRI of that code. ?entry, ?medication and ?class are as they sound, ?relation is the class row's predicate (cascade:establishedPharmacologicClass, cascade:mechanismOfActionClass, cascade:physiologicEffectClass or cascade:chemicalStructureClass), ?origin the version of the drug classes table that holds the row, and ?through the code the row is on, which ?stated says the entry states itself.
MED-RT classes the ingredient and the precise ingredient, never a product, and FDA keys a class on whichever of them its label names. A join is made only on exact evidence, relation by relation, for each code an entry states, in a version of a product ingredients table where the code maps to an ingredient (obo:RO_0002248), and from nothing else:
- a class row on the code itself is the code's class;
- else, the version gives the code precise ingredients (cascade:hasPreciseIngredient) that map to that ingredient, and one or more of them bear rows of the relation: their classes are the code's, ?through each. That keeps a product of fluoroestradiol F-18 (2372221) out of Estrogen [EPC], and levoketoconazole 150 MG Oral Tablet (2588848) and Recorlev (2588849) out of Azole Antifungal [EPC];
- else, the version states precise ingredients (it holds a cascade:hasPreciseIngredient row, so by its kind it holds one for every code RxNorm ties to one), and the ingredient itself bears rows of the relation: they are the code's, ?through the ingredient. That gives estradiol 1 MG Oral Tablet (197658, no precise ingredient) Estrogen [EPC] through estradiol (4083).
A class on another precise ingredient of the same ingredient is never the code's, whatever the codes under the ingredient agree on: Lutathera (1999687) is not given gallium Ga-68 dotatate's Radioactive Diagnostic Agent [EPC], nor choline salicylate (315666) choline C-11's. So lisinopril 10 MG Oral Tablet (314076), whose RxNorm relations name only lisinopril (29046), has no ACE inhibitor [EPC]: FDA classes lisinopril anhydrous (1546022), a different form. A version that states no precise ingredient gives step 1 only, as step 3 would give levoketoconazole products ketoconazole's class. A combination product maps to its multiple ingredient concept, which MED-RT does not class and none of its precise ingredients maps to, so it reaches no class here. An entry reaches a class once for each relation and version, whatever the codes it reached it through; ?through is the one of the lowest step, the least of them within a step.
The query
PREFIX cascade: <https://ns.cascadeprotocol.org/cascade#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX prov: <http://www.w3.org/ns/prov#>
SELECT ?entry ?medication ?class ?relation ?origin (SUBSTR(MIN(CONCAT(?step, STR(?on))), 2) AS ?through) (STRSTARTS(MIN(CONCAT(?step, STR(?on))), "0") AS ?stated)
WHERE {
{
{ SELECT ?entry ?medication ?code
WHERE {
?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ; cascade:drugName ?medication .
?entry (cascade:userSelectedCode | cascade:rxNormCode) ?written .
BIND (IF(isIRI(?written), ?written, IRI(CONCAT("https://ns.cascadeprotocol.org/codes/rxnorm/", ENCODE_FOR_URI(STR(?written))))) AS ?code)
FILTER (STRSTARTS(STR(?code), "https://ns.cascadeprotocol.org/codes/rxnorm/"))
} }
?origin prov:specializationOf/cascade:tableKind cascade:DrugClasses .
GRAPH ?origin { ?row owl:annotatedSource ?code ; owl:annotatedProperty ?relation ; owl:annotatedTarget ?class }
BIND (?code AS ?on)
BIND ("0" AS ?step)
} UNION {
{ SELECT ?product ?ingredient ?origin ?relation ?code
(SUM(IF(?held = ?code, 1, 0)) AS ?owned) (SUM(IF(BOUND(?precise), 1, 0)) AS ?precisely)
WHERE {
?product prov:specializationOf/cascade:tableKind cascade:ProductIngredients .
?origin prov:specializationOf/cascade:tableKind cascade:DrugClasses .
GRAPH ?product { ?member owl:annotatedSource ?code ; owl:annotatedProperty obo:RO_0002248 ; owl:annotatedTarget ?ingredient .
?left owl:annotatedTarget ?ingredient ; owl:annotatedProperty obo:RO_0002248 ; owl:annotatedSource ?held
OPTIONAL { ?precise owl:annotatedSource ?code ; owl:annotatedProperty ?preciseProperty ; owl:annotatedTarget ?held
FILTER (?preciseProperty = cascade:hasPreciseIngredient) } }
GRAPH ?origin { ?by owl:annotatedSource ?held ; owl:annotatedProperty ?relation ; owl:annotatedTarget ?given }
}
GROUP BY ?product ?ingredient ?origin ?relation ?code }
{ SELECT ?entry ?medication ?code
WHERE {
?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ; cascade:drugName ?medication .
?entry (cascade:userSelectedCode | cascade:rxNormCode) ?written .
BIND (IF(isIRI(?written), ?written, IRI(CONCAT("https://ns.cascadeprotocol.org/codes/rxnorm/", ENCODE_FOR_URI(STR(?written))))) AS ?code)
FILTER (STRSTARTS(STR(?code), "https://ns.cascadeprotocol.org/codes/rxnorm/"))
} }
GRAPH ?product { ?mine owl:annotatedSource ?code ; owl:annotatedProperty cascade:hasPreciseIngredient ; owl:annotatedTarget ?via .
?form owl:annotatedSource ?via ; owl:annotatedProperty obo:RO_0002248 ; owl:annotatedTarget ?ingredient }
GRAPH ?origin { ?row owl:annotatedSource ?via ; owl:annotatedProperty ?relation ; owl:annotatedTarget ?class }
FILTER (?owned = 0 && ?precisely > 0)
BIND (?via AS ?on)
BIND ("1" AS ?step)
} UNION {
{ SELECT ?product ?ingredient ?origin ?relation ?code
(SUM(IF(?held = ?code, 1, 0)) AS ?owned) (SUM(IF(BOUND(?precise), 1, 0)) AS ?precisely)
WHERE {
?product prov:specializationOf/cascade:tableKind cascade:ProductIngredients .
?origin prov:specializationOf/cascade:tableKind cascade:DrugClasses .
GRAPH ?product { ?member owl:annotatedSource ?code ; owl:annotatedProperty obo:RO_0002248 ; owl:annotatedTarget ?ingredient .
?left owl:annotatedTarget ?ingredient ; owl:annotatedProperty obo:RO_0002248 ; owl:annotatedSource ?held
OPTIONAL { ?precise owl:annotatedSource ?code ; owl:annotatedProperty ?preciseProperty ; owl:annotatedTarget ?held
FILTER (?preciseProperty = cascade:hasPreciseIngredient) } }
GRAPH ?origin { ?by owl:annotatedSource ?held ; owl:annotatedProperty ?relation ; owl:annotatedTarget ?given }
}
GROUP BY ?product ?ingredient ?origin ?relation ?code }
{ SELECT ?product (COUNT(?some) AS ?stating)
WHERE {
?product prov:specializationOf/cascade:tableKind cascade:ProductIngredients .
GRAPH ?product { OPTIONAL { ?some owl:annotatedProperty cascade:hasPreciseIngredient } }
}
GROUP BY ?product }
{ SELECT ?entry ?medication ?code
WHERE {
?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ; cascade:drugName ?medication .
?entry (cascade:userSelectedCode | cascade:rxNormCode) ?written .
BIND (IF(isIRI(?written), ?written, IRI(CONCAT("https://ns.cascadeprotocol.org/codes/rxnorm/", ENCODE_FOR_URI(STR(?written))))) AS ?code)
FILTER (STRSTARTS(STR(?code), "https://ns.cascadeprotocol.org/codes/rxnorm/"))
} }
GRAPH ?origin { ?row owl:annotatedSource ?ingredient ; owl:annotatedProperty ?relation ; owl:annotatedTarget ?class }
FILTER (?owned = 0 && ?precisely = 0 && ?stating > 0)
BIND (?ingredient AS ?on)
BIND ("2" AS ?step)
}
}
GROUP BY ?entry ?medication ?class ?relation ?origin
ORDER BY ?medication ?entry ?relation ?class
Terms it reads
cascade:DrugClasses · cascade:MedicationActive · cascade:PrescribedOrReportedMedication · cascade:ProductIngredients · cascade:drugName · cascade:hasPreciseIngredient · cascade:lifecycle · cascade:rxNormCode · cascade:userSelectedCode · obo:RO_0002248 · owl:annotatedProperty · owl:annotatedSource · owl:annotatedTarget · prov:specializationOf
Rules and decision notes that name it
No rule or decision note writes its path.
queries/tables/medication-classes-through-precise-ingredients.rq on GitHub ↗