Extracting properties with different namespaces

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

Extracting properties with different namespaces

June27
Hi,
I have an ontology that contains different namespaces, and I'm trying to iterate through all classes and extract specific properties from each class. An example class of my ontology looks like:


    <owl:Class rdf:about="&obo;CHEBI_28260">
        <oboInOwl:hasRelatedSynonym>galactose, d-</oboInOwl:hasRelatedSynonym>
        <rdfs:label rdf:datatype="&xsd;string">galactose</rdfs:label>
    </owl:Class>


So in the case above, I'm trying to extract rdfs:label and oboInOwl:hasRelatedSynonym with the following code:

my_world = World()
onto = my_world.get_ontology(infile).load()

for i in onto.classes():
    label1 = i.label
    synonyms1 = i.hasRelatedSynonym
    ...

This code executes without error but no synonyms are extracted. I assume this is due to the different namespace because if I replace all 'oboInOwl' in the ontology with 'rdf', then I get an AttributeError ("'hasRelatedSynonym' property is not defined."), I assume because "hasRelatedSynonym" is not defined in the rdfs namespace.

So my question is, is there a way to indicate a specific namespace within my for loop?

Thanks for any help.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

Jiba
Administrator
Hi,

The fact that the property is not defined in the same namespace is not a problem -- Owlready considers all properties, independently from their namespace.

Are you sure the oboInOwl ontology is imported/loaded? If not, the property will not be available.

If this is not the cause of the problem, could you send me a (non-)working example, so I can reproduce the problem?

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

June27
Hi,
Thanks very much for your reply. My ontology doesn't import any other ontologies, and I've actually been using a really subset subset of my overall ontology, to try and isolate the problem. Here's the test ontology I've been working with:

<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
    <!ENTITY terms "http://purl.org/dc/terms/" >
    <!ENTITY foaf "http://xmlns.com/foaf/0.1/" >
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY obo "http://purl.obolibrary.org/obo/" >
    <!ENTITY patterns "http://www.co-ode.org/patterns#" >
    <!ENTITY dc "http://purl.org/dc/elements/1.1/" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY xml "http://www.w3.org/XML/1998/namespace" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY NCBITaxon "http://purl.obolibrary.org/obo/NCBITaxon#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY oboInOwl "http://www.geneontology.org/formats/oboInOwl#" >
]>

<rdf:RDF
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:terms="http://purl.org/dc/terms/"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:oboInOwl="http://www.geneontology.org/formats/oboInOwl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:patterns="http://www.co-ode.org/patterns#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:NCBITaxon="&obo;NCBITaxon#"
     xmlns:obo="http://purl.obolibrary.org/obo/"
     xmlns:foaf="http://xmlns.com/foaf/0.1/"
     xmlns:dc="http://purl.org/dc/elements/1.1/">

    <owl:Class rdf:about="&obo;CHEBI_28260">
        <rdfs:subClassOf rdf:resource="&obo;CHEBI_33917"/>
        <rdfs:subClassOf rdf:resource="&obo;FOODON_03420108"/>
        <obo:IAO_0000115 rdf:datatype="&xsd;string">An aldohexose that is the C-4 epimer of glucose.</obo:IAO_0000115>
        <obo:IAO_0000118>galactose</obo:IAO_0000118>
        <obo:IAO_0000412 rdf:resource="&obo;chebi.owl"/>
        <obo:IAO_0000412>http://langual.org</obo:IAO_0000412>
        <oboInOwl:hasDbXref>http://www.langual.org/langual_thesaurus.asp?termid=C0254</oboInOwl:hasDbXref>
        <oboInOwl:hasRelatedSynonym>galactose, d-</oboInOwl:hasRelatedSynonym>
        <rdfs:label rdf:datatype="&xsd;string">galactose</rdfs:label>
    </owl:Class>

    <owl:Class rdf:about="&obo;CHEBI_28757">
        <rdfs:subClassOf rdf:resource="&obo;CHEBI_24973"/>
        <rdfs:subClassOf rdf:resource="&obo;FOODON_03420108"/>
        <obo:IAO_0000115 rdf:datatype="&xsd;string">A ketohexose that is an isomer of glucose.</obo:IAO_0000115>
        <obo:IAO_0000118>fructose</obo:IAO_0000118>
        <obo:IAO_0000412 rdf:resource="&obo;chebi.owl"/>
        <obo:IAO_0000412>http://langual.org</obo:IAO_0000412>
        <oboInOwl:hasDbXref>http://www.langual.org/langual_thesaurus.asp?termid=C0223</oboInOwl:hasDbXref>
        <oboInOwl:hasRelatedSynonym>levulose</oboInOwl:hasRelatedSynonym>
        <rdfs:comment>LanguaL curation note: Used when pure fructose is the major ingredient. If *HIGH FRUCTOSE SYRUP* is the major ingredient, use that term for indexing.</rdfs:comment>
        <rdfs:label rdf:datatype="&xsd;string">fructose</rdfs:label>
    </owl:Class>
   
  </rdf:RDF>


So from the above, I'm just trying to iterate through all classes and extract "label" and "hasRelatedSynonym" using the code:

allLabels=[]
for i in onto.classes():
    label1 = i.label[0]
    synonyms1 ='; '.join(i.hasRelatedSynonym)
    attributes = '; '.join(filter(None, [label1, synonyms1]))
    allLabels.append(attributes)

 
Thanks again for any help!
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

Jiba
Administrator
Hi,

The problem is that the "hasRelatedSynonym" property is not defined. You need to define it (as a DataProperty or AnnotationProperty), either in your ontology, in a separate ontology (imported by your ontology or loaded in Python) or directly in Python.

If you don't want to modify the existant ontology, you can create the property directly in Python as follows:

with get_ontology("http://temporary.org/onto.owl"):
    class hasRelatedSynonym(AnnotationProperty): pass

for i in onto.classes():
    label1 = i.label[0]
    synonyms1 ='; '.join(i.hasRelatedSynonym)
    attributes = '; '.join(filter(None, [label1, synonyms1]))
    print(attributes)

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

June27
Hi,
Thanks again for your help!

If you don't mind clearing up one last thing for me, I notice that 'hasRelatedSynonym' is defined as an annotationProperty here: http://www.geneontology.org/formats/oboInOwl#, which is cited in the header of my ontology. So why is it that 'hasRelatedSynonym' needs to be defined within my ontology but 'label' does not?

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

Jiba
Administrator
Hi,

"label" is defined in RDF; Owlready automatically define/import entities defined in RDF and OWL.

oboInOwl is defined in your ontology as an XML prefix, but the ontology is not imported. For importing, you need an import XML tag inside the ontology tag (or use the imported ontologies field in protégé).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Extracting properties with different namespaces

June27
Thanks very much, that's clear now.

What's strange is that, using that same test ontology from above and the following code:

my_world = World()
onto = my_world.get_ontology("testontology.owl").load()
with onto:
    class hasRelatedSynonym(AnnotationProperty): pass

The following returns an empty list:
list(onto.classes())[0].hasRelatedSynonym

Whereas this does not:
list(onto.classes())[0].label

I apologize if I'm missing something obvious!