Skip to content

Instantly share code, notes, and snippets.

@balhoff
Last active September 3, 2021 16:08
Show Gist options
  • Save balhoff/9792489 to your computer and use it in GitHub Desktop.
Save balhoff/9792489 to your computer and use it in GitHub Desktop.
Retrieve the nearest common ancestors for two terms using SPARQL
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX Term1: <http://purl.obolibrary.org/obo/PATO_0000587>
PREFIX Term2: <http://purl.obolibrary.org/obo/PATO_0000586>
SELECT ?nearest
FROM <http://purl.obolibrary.org/obo/pato.owl>
WHERE {
Term1: rdfs:subClassOf* ?nearest .
Term2: rdfs:subClassOf* ?nearest .
Term1: rdfs:subClassOf* ?indirect .
Term2: rdfs:subClassOf* ?indirect .
?indirect rdfs:subClassOf* ?nearest .
}
GROUP BY ?nearest
HAVING(COUNT(DISTINCT ?indirect) = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment