Fetching triples using SPARQL query from turtle file

Question:

I am new to SPARQL and currently struglling to fetch triples from a turtle file.

###  https://ontology/1001
<https://ontology/1001> rdf:type owl:Class ;
                                             rdfs:subClassOf <https://ontology/748>;
                                             <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> "Injury, neuronal" ,
                                                                                                           "Neurotrauma" ;
                                             rdfs:label "Nervous system injury" .
                                             


###  https://ontology/10021
<https://ontology/10021> rdf:type owl:Class ;
                                              rdfs:subClassOf <https://ontology/2034> ;
                                              rdfs:label "C3 glomerulopathy" .
                                              

I am trying to extract all classes with their superclasses, labels and Synonym. The query which I am running is below.

query_id = """

        prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
        prefix obo: <http://purl.obolibrary.org/obo/>
        prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

        SELECT distinct ?cid ?label ?class ?synonyms
        WHERE {
            ?cid rdfs:label ?label .
            ?cid rdfs:subClassOf ?class .
            ?cid oboInOwl:hasExactSynonym ?synonyms .
        }

"""

However, this query is filtering the triple where ‘hasExactSynonym’ doesn’t exists.

Following is the output:

cid    label                   class  synonyms
1001   Nervous system injury   748    Injury, neuronal , Neurotrauma

The expected output is:

cid    label                   class  synonyms
1001   Nervous system injury   748    Injury, neuronal , Neurotrauma
10021  C3 glomerulopathy       2034  
Asked By: rshar

||

Answers:

You can use OPTIONAL to make the synonyms optional:

WHERE {
  ?cid rdfs:label ?label .
  ?cid rdfs:subClassOf ?class .
  OPTIONAL { ?cid oboInOwl:hasExactSynonym ?synonyms . }
}
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.