Unexpected ValueError during SPARQL INSERT operation

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

Unexpected ValueError during SPARQL INSERT operation

maxrosen
Any help would be appreciated.

This works:

import owlready2 as or2
mondo_ontology = or2.get_ontology("http://purl.obolibrary.org/obo/mondo.owl").load()

s_prefix = "http://purl.obolibrary.org/obo/MONDO_"
p = "oboInOwl:hasDbXref"
o_prefix = "UMLS:"
p2 = "skos:mappingRelation"
o2_prefix = "http://linkedlifedata.com/resource/umls/id/"
query ="""
  PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>

  SELECT ?s ?o2
  WHERE {
    ?s """ + p + """ ?o.
    FILTER (
      STRSTARTS(str(?s), str(\"""" + s_prefix + """\")) &&
      STRSTARTS(str(?o), str(\"""" + o_prefix + """\"))
    )
    BIND(IRI(SIMPLEREPLACE(STR(?o), "UMLS:", \"""" + o2_prefix + """\")) AS ?o2).
  }
"""
with mondo_ontology:
  results = list(or2.default_world.sparql(query, error_on_undefined_entities=False))
print(results)

This does not:

s_prefix = "http://purl.obolibrary.org/obo/MONDO_"
p = "oboInOwl:hasDbXref"
o_prefix = "UMLS:"
p2 = "skos:mappingRelation"
o2_prefix = "http://linkedlifedata.com/resource/umls/id/"

query ="""
  PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>

  INSERT { ?s """ + p2 + """ ?o2 . }
  WHERE {
    ?s """ + p + """ ?o.
    FILTER (
      STRSTARTS(str(?s), str(\"""" + s_prefix + """\")) &&
      STRSTARTS(str(?o), str(\"""" + o_prefix + """\"))
    )
    BIND(IRI(SIMPLEREPLACE(STR(?o), "UMLS:", \"""" + o2_prefix + """\")) AS ?o2).
  }
"""
with mondo_ontology:
  results = list(or2.default_world.sparql(query, error_on_undefined_entities=False))
print(results)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[100], line 23
      7 query ="""
      8   PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
      9   PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
   (...)
     19   }
     20 """
     22 with mondo_ontology:
---> 23   results = list(or2.default_world.sparql(query, error_on_undefined_entities=False))
     24 print(results)

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/sparql/main.py:536, in PreparedModifyQuery.execute(self, params, execute_raw_result, spawn)
    533     #print("ADD", insert, triple)
    534     added_triples.append(triple)
--> 536 if added_triples: self.world._add_quads_with_update(self.ontology, added_triples)
    537 return nb_match

File /usr/local/lib/python3.11/site-packages/owlready2/namespace.py:598, in World._add_quads_with_update(self, ontology0, quads)
    595           with ontology: sub.range.append(obj)
    596           continue
--> 598   if d is None: ontology._add_obj_triple_raw_spo  (s, p, o)
    599   else:         ontology._add_data_triple_raw_spod(s, p, o, d)
    601 # Factorize is_a quads for better performance

File /usr/local/lib/python3.11/site-packages/owlready2/triplelite.py:1064, in SubGraph._add_obj_triple_raw_spo(self, s, p, o)
   1063 def _add_obj_triple_raw_spo(self, s, p, o):
-> 1064   if (s is None) or (p is None) or (o is None): raise ValueError
   1065   self.execute("INSERT OR IGNORE INTO objs VALUES (?, ?, ?, ?)", (self.c, s, p, o))
   1066   self.parent.nb_added_triples += 1

ValueError:
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected ValueError during SPARQL INSERT operation

maxrosen
OK, so it looks like  BIND(IRI(SIMPLEREPLACE(STR(?o), "UMLS:", \"""" + o2_prefix + """\")) AS ?o2) does not guarantee ?o2 to be bound to anything, it can be "None". I don't understand why though.
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected ValueError during SPARQL INSERT operation

Jiba
Administrator
Hi,

?o2 is not bound when the ?o string does not include "UMLS:", because in that case the resulting string after the SIMPLEREPLACE is not an IRI (e.g. "NCIT:xxxx"). Consequently, IRI() return None/null.

You can fix the problem by verifying that ?o2 is an IRI with FILTER(ISIRI(?o2)):

query ="""
  PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>

  INSERT { ?s """ + p2 + """ ?o2 . }
  WHERE {
    ?s """ + p + """ ?o.
    FILTER (
      STRSTARTS(str(?s), str(\"""" + s_prefix + """\")) &&
      STRSTARTS(str(?o), str(\"""" + o_prefix + """\"))
    )
    BIND(IRI(SIMPLEREPLACE(STR(?o), "UMLS:", \"""" + o2_prefix + """\")) AS ?o2).
    FILTER(ISIRI(?o2)) .
  }
"""

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected ValueError during SPARQL INSERT operation

maxrosen
Hey Jiba, thanks for looking into this.

I would expect the filter (STRSTARTS(str(?o), str(\"""" + o_prefix + """\"))) to make sure that ?o has the right values.

I just checked again and for ?s = obo.MONDO_0100255 and ?o = "UMLS:C3280381", I still get ?o2 = "None" in the first query.

ISIRI is False.

C3280381 does seem to be a class in UMLS though:

https://ncim-stage.nci.nih.gov/ncimbrowser/ConceptReport.jsp?dictionary=NCI%20MetaThesaurus&code=C3280381

"http://linkedlifedata.com/resource/umls/id/<cui>" times out and "https://linkedlifedata.com/resource/umls/id/<cui>" gives 401 for all UMLS CUIs I have tried.

Any ideas? Thank you.

EDIT: OK, I think I figured this out. The difference between C3280381 and the other CUIs is that I asserted triples using the other CUIs before the query, but not C3280381. Thanks again.