Hello all, I was wondering if anyone could help me
with this query:
I'm
trying to retrieve certain elements from an rdf ontology
using rdflib, which uses SPARQL 1.1. The ontology is of the
human body, and all the parts have their own class. Here is
an example:
<owl:Class
rdf:about="http://human.owl#NCI_C12909"><rdfs:label
rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Hematopoietic_System</rdfs:label><rdfs:subClassOf
rdf:resource="http://human.owl#NCI_C41166"/>rdfs:subClassOfowl:Restriction<owl:onProperty
rdf:resource="http://human.owl#UNDEFINED_part_of"/><owl:someValuesFrom
rdf:resource="http://human.owl#NCI_C41165"/></owl:Restriction></rdfs:subClassOf><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7506"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7507"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7508"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7509"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7510"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7511"/><oboInOwl:hasRelatedSynonym
rdf:resource="http://human.owl#genid7513"/></owl:Class>
As you can
see, some of the classes have properties with multiple
values, such as hasRelatedSynonym, and SomeValuesFrom. I'm
trying to run a query that returns the class, the class
label, the super class, the superclass label, and any other
hasRelatedSynonym values and SomeValuesFrom values. I would
like to use group_concat for the properties with the
multiple values, and have tried the following query:
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 }
OPTIONAL { ?restriction a owl:Restriction }
OPTIONAL { ?restriction owl:someValuesFrom ?node2 }
?node rdfs:label ?nodeLabel .
?superclass rdfs:label ?superclassLabel .
OPTIONAL { ?node2 rdfs:label ?node2Label }
}
group by ?node ?nodeLabel ?superclass ?superclassLabel
?node2 ?node2Label
LIMIT 10""")
The
query seems to execute, but when I try to iterate over its
rows and print them, Jupyter Notebook goes on thinking
forever and doesn't return anything. Could it be that I've
made a mistake and have created some kind of recursive
infinite loop? Any help would be much appreciated.