importloggingasloggfromfunctoolsimportcached_propertyfrompathlibimportPathfromtypingimportIterable,Unionimportpandasaspdfrom._settingsimportformat_into_dataframeclassOntology:"""Ontology manager built on Owlready2. Args: base_iri: (Internationalized Resource Identifier) RDF/XML, OWL/XML or NTriples format # noqa load: Whether to load ontology """def__init__(self,base_iri:Union[str,Path],load:bool=True)->None:fromowlready2importget_ontologyifload:self._onto=get_ontology(base_iri).load()else:self._onto=get_ontology(base_iri)@propertydefonto(self):"""owlready2 ontology object."""returnself._onto@cached_propertydefonto_dict(self)->dict:"""Keyed by name, valued by label."""return{i.name:i.label[0]foriinself.onto.classes()}@cached_propertydefclasses(self)->dict:"""Indexed classes, owlready2 ThingClass object."""return{i.name:iforiinself.onto.classes()}defsearch(self,data:Iterable[str],id=False):"""Search in ontology labels. Args: data: search patterns id: whether to search by the id Returns: A list of ontology names """res={}fordindata:res[d]=self.search_one(text=d)returnresdefsearch_one(self,text:str,id=False)->dict:"""Search in ontology labels one by one. Args: text: search pattern id: whether to search by the id Returns: A list of ontology names """ifid:text=text.replace(":","_")res=self.onto.search(iri=f"*{text}")else:res=self.onto.search(label=text)return{i.name:i.label[0]foriinres}@format_into_dataframedefstandardize(self,terms:pd.DataFrame,_reformat=False):"""Checks if the ontology names are valid and in use. Args: terms: ontology ids Returns: a dataframe """terms.index=terms.index.str.replace(":","_")terms.index.name="ontology_id"terms["name"]=""nonstd={}forterminterms.index:# Ensuring the format of the IDsifterminself.onto_dict.keys():label=self.onto_dict[term]terms.loc[term]["name"]=labeliflabel.startswith("obsolete"):nonstd[term]=labelelse:nonstd[term]=labeliflen(nonstd)>0:logg.warn("The following terms were found to be obsolete or non-exist! Please"f" search the correct term via `.search`! \n{nonstd}")if_reformat:returnterms