SPARQL Aggregate query not working

16 views
Skip to first unread message

Boris Metodiev

unread,
Dec 13, 2017, 10:06:04 AM12/13/17
to rdflib-dev
I'm trying to write sparql query to extract certain properties and labels from an ontology in rdf. Im'm using rdflib 4.2.2 in python 2.7 (OS: Ubuntu 16.04, 64 bit)., which allows me to run queries in sparql 1.1. The ontology is of the anatomy of the human body, and all the organs have their own class. It is organized such that these organs have a superclass, and additional properties such as hasRelatedSynonym, and someValuesFrom, which have labels. Here is a sample of the ontology, a single class with the class restriction and prperties: 

<owl:Class rdf:about="http://human.owl#NCI_C12832"><rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Conus_Medullaris</rdfs:label><rdfs:subClassOf rdf:resource="http://human.owl#NCI_C33969"/><rdfs:subClassOf><owl:Restriction><owl:onProperty rdf:resource="http://human.owl#UNDEFINED_part_of"/><owl:someValuesFrom rdf:resource="http://human.owl#NCI_C12464"/></owl:Restriction></rdfs:subClassOf><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid5111"/>


Some of the classes have a restriction with multiple values for a single property, which is why I've tried to use group_concat to get all of these values in a single row, so as to have all the relevant information about a class in one row.

This is the query I'm trying to run: 

querytrial4=graph.query("""SELECT ?node ?nodeLabel ?superclass ?superclassLabel (group_concat(DISTINCT ?node2) as ?node2s) (group_concat(DISTINCT ?node2Label) as ?node2Labels) where {
    ?node rdf:type owl:Class .
    ?node rdfs:subClassOf ?superclass .
    OPTIONAL { 
        ?node rdfs:subClassOf ?restriction .
        ?restriction a owl:Restriction .
        ?restriction owl:someValuesFrom ?node2 .
        ?node2 rdfs:label ?node2Label } 
    ?node rdfs:label ?nodeLabel .
    ?superclass rdfs:label ?superclassLabel .
}
group by ?node ?nodeLabel ?superclass ?superclassLabel ?node2 ?node2Label
LIMIT 10""")

As suggested by several contributors on the rdf4j forum, I've placed the restriction and its property in an OPTIONAL block, because not all of the classes have these properties. However, when i try to run the query, I get an error saying the variable ?node2Label is unbound. Why is this error being thrown even though the variable is inside the optional block, and how could I resolve this query? Any advice would be much appreciated. I've attached the ontology in case anyone would like to try to run the query themselves. 
human.owl
Reply all
Reply to author
Forward
0 new messages