Getting Ancestors and Descendants in Order

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

Getting Ancestors and Descendants in Order

Clair Kronk
Hi! I'm having an amazing time using Owlready2 and it has been really helpful, but one issue I've run into recently is that I want to display a hierarchical view of the ontology (akin to the classes view in the NCBO BioPortal, see here for example: https://bioportal.bioontology.org/ontologies/MESH/?p=classes&conceptid=root).

In order to do that, I think I need to be able to load an ordered version of the ancestors and descendants of a given class, but the ".ancestors()" and ".descendants()" functions appear to be unordered. Is there a way to order them or is there a work-around for something like this?

Thanks so much!
Reply | Threaded
Open this post in threaded view
|

Re: Getting Ancestors and Descendants in Order

Jiba
Administrator
Hi,

There is no ordered ancestors in Owlready (although PyMedTermino has some, for UMLS terminologies).

You can order the ancestors / descendants using Python's sort() method:

ancestors = MyClass.ancestors()
ancestors.sort(key = lambda e: e.name)

You may want to sort by label, replacing e.name with e.label.first() .

Jiba