How to handle invalid property name in python in Owlready2

Viewed 86

A resource name begins with '2_', which seems invalid in python, because we got a message as follows:

invalid decimal literal

Could you tell me how to access the value of this resource? More specifically, its URI is as follows:

http://purl.obolibrary.org/obo/chebi.owl#2_STAR

and, accessing chebi.2_STAR.id results in the following.

File "<ipython-input-156-66b94380a437>", line 1
    chebi.2_STAR.id
           ^
SyntaxError: invalid decimal literal

Thanks.

1 Answers

So I'm not sure that is the correct URI you have. I believe that is what automatically happens when you read in the file. To access it, I think you need a namespace without the ".owl". Another option is to use the global "obo" namespace.

I was able to access it by:

obo = get_namespace("http://purl.obolibrary.org/obo/")
obo['chebi#2_STAR']

# 2_STAR only has comments so,
obo['chebi#2_STAR'].comment 
#returns
['Annotated by 3rd party', 'Annotated by 3rd party']

When encountering incompatible characters, wrap them in brackets and put them in as a string.

UPDATE (2021-09-23) Looking more at the CHEBI ontology, it looks like there are several namespaces available and the better way of calling the namespace within the CHEBI namespace is:

from owlready2 import *

#loaded from local file
chebi = get_ontology("chebi.owl").load()

#get the right namespace. note the 'chebi.' correction to what I initially did.
chebi2 = chebi.get_namespace("http://purl.obolibrary.org/obo/chebi#")
#then retrieve
chebi2['2_STAR']

I don't yet know how to query what namespaces are there but if you already know the prefix, that can help you get the namespace you want.

Related