Unclear ParsingError trying to run SPARQL INSERT query

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unclear ParsingError trying to run SPARQL INSERT query

maxrosen
Running this code:

import owlready2 as or2
or2.onto_path.append("../../data/ontologies/")
mondo_ontology = or2.get_ontology("../../data/ontologies/mondo.owl").load()
icd10cm_ontology = or2.get_ontology("../../data/ontologies/icd10cm_rdfxml.owl").load()

query = """
    PREFIX MONDO: <http://purl.obolibrary.org/obo/MONDO_>
    PREFIX ICD10CM: <http://purl.bioontology.org/ontology/ICD10CM/>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

    INSERT {
        MONDO:0000088 skos:exactMatch ICD10CM:E30.1 .
    }
    """
or2.default_world.sparql(query)

I get:

{
        "name": "ParsingError",
        "message": "('Error at $end', None)",
        "stack": "---------------------------------------------------------------------------
ParsingError                              Traceback (most recent call last)
Cell In[32], line 15
      5 for index, row in df.iterrows():
      6     query = \"\"\"
      7     PREFIX MONDO: <http://purl.obolibrary.org/obo/MONDO_>
      8     PREFIX ICD10CM: <http://purl.bioontology.org/ontology/ICD10CM/>
   (...)
     13     }
     14     \"\"\"
---> 15     or2.default_world.sparql(query)

File /usr/local/lib/python3.11/site-packages/owlready2/namespace.py:466, in World.sparql(self, sparql, params, error_on_undefined_entities, spawn)
    465 def sparql(self, sparql, params = (), error_on_undefined_entities = True, spawn = False):
--> 466   return self._prepare_sparql(sparql, error_on_undefined_entities).execute(params, None, spawn)

File /usr/local/lib/python3.11/site-packages/owlready2/namespace.py:471, in World._prepare_sparql(self, sparql, error_on_undefined_entities)
    468 @lru_cache(maxsize = 1024)
    469 def _prepare_sparql(self, sparql, error_on_undefined_entities):
    470   import owlready2.sparql.main
--> 471   return owlready2.sparql.main.Translator(self, error_on_undefined_entities).parse(sparql)

File /usr/local/lib/python3.11/site-packages/owlready2/sparql/main.py:61, in Translator.parse(self, sparql)
     59   self.escape_mark += \"รง\"
     60 CURRENT_TRANSLATOR.set(self)
---> 61 self.main_query = PARSER.parse(LEXER.lex(sparql))
     62 return self.finalize()

File /usr/local/lib/python3.11/site-packages/owlready2/rply.py:451, in LRParser.parse(self, tokenizer, state)
    449   raise AssertionError(\"For now, error_handler must raise.\")
    450 else:
--> 451   raise ParsingError(\"Error at %s\" % lookahead, lookahead.source_pos)

ParsingError: ('Error at $end', None)"
}

What am I doing wrong? Any help would be appreciated. Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Unclear ParsingError trying to run SPARQL INSERT query

maxrosen
OK, I got it. I was missing "WHERE {}" and a with statement. Thanks.

import owlready2 as or2
or2.onto_path.append("../../data/ontologies/")
mondo_ontology = or2.get_ontology("../../data/ontologies/mondo.owl").load()
icd10cm_ontology = or2.get_ontology("../../data/ontologies/icd10cm_rdfxml.owl").load()

query = """
    PREFIX MONDO: <http://purl.obolibrary.org/obo/MONDO_>
    PREFIX ICD10CM: <http://purl.bioontology.org/ontology/ICD10CM/>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

    INSERT { MONDO:0000088 skos:exactMatch ICD10CM:E30.1 . }
    WHERE {}
    """

with mondo_ontology:
    or2.default_world.sparql(query)