1010
1111
1212@dataclass
13- class TermWithLabels :
13+ class OntologyTerm :
1414 iri : str
1515 label : list [str ]
1616 additional_label : list [str ]
@@ -58,7 +58,9 @@ def _collect_short_ids(short_id: str | list[str], short_ids: list):
5858 short_ids += short_id
5959
6060 @cache
61- def fetch_labels_for_term (self , ontology : str , term_iri : str ):
61+ def fetch_term_from_ontology (
62+ self , ontology : str , term_iri : str
63+ ) -> None | OntologyTerm :
6264 if ontology not in self .avaliable_ontology_ids :
6365 raise KeyError (f"{ ontology } is not in ols" )
6466
@@ -75,13 +77,45 @@ def fetch_labels_for_term(self, ontology: str, term_iri: str):
7577
7678 return self ._create_term_with_labels (term_info )
7779
80+ def fetch_term_by_iri (self , term_iri : str ) -> None | OntologyTerm :
81+ ontology = self ._ontology_for_term_iri (term_iri )
82+ if ontology is None :
83+ return
84+
85+ try :
86+ return self .fetch_term_from_ontology (ontology , term_iri )
87+ except KeyError :
88+ return
89+
90+ def fetch_label_by_iri (self , term_iri : str ) -> str | None :
91+ term_with_labels = self .fetch_term_by_iri (term_iri )
92+ if term_with_labels is None :
93+ return None
94+ return term_with_labels .label [0 ] if term_with_labels .label else None
95+
96+ def _ontology_for_term_iri (self , term_iri : str ) -> str | None :
97+ if term_iri .startswith ("obo:" ):
98+ term_iri = term_iri .removeprefix ("obo:" )
99+
100+ if term_iri .startswith ("http://purl.obolibrary.org/obo/" ):
101+ local_part = term_iri .rsplit ("/" , 1 )[- 1 ]
102+ prefix = local_part .split ("_" , 1 )[0 ].lower ()
103+ return prefix
104+
105+ if term_iri .startswith ("http://www.bioassayontology.org/bao#" ):
106+ return "bao"
107+ if term_iri .startswith ("bao:" ):
108+ return "bao"
109+
110+ return None
111+
78112 @cache
79113 def _get_iri_for_class_in_ontology (
80114 self , ontology : str , search_terms : str , required_superclass : str | None = None
81- ) -> list [TermWithLabels ]:
115+ ) -> list [OntologyTerm ]:
82116 api_response = self ._find_class_in_ontology (ontology , search_terms )
83117
84- iris_and_labels : list [TermWithLabels ] = []
118+ iris_and_labels : list [OntologyTerm ] = []
85119 for ontology_term in api_response ["elements" ]:
86120 if required_superclass :
87121 if not ontology_term ["hasDirectParents" ]:
@@ -97,7 +131,7 @@ def _get_iri_for_class_in_ontology(
97131
98132 return iris_and_labels
99133
100- def _create_term_with_labels (self , ontology_term ) -> TermWithLabels :
134+ def _create_term_with_labels (self , ontology_term ) -> OntologyTerm :
101135 short_ids = []
102136 self ._collect_short_ids (ontology_term .get ("obo_id" ), short_ids )
103137 self ._collect_short_ids (ontology_term .get ("curie" ), short_ids )
@@ -109,7 +143,7 @@ def _create_term_with_labels(self, ontology_term) -> TermWithLabels:
109143 else [ontology_term ["label" ]]
110144 )
111145
112- return TermWithLabels (
146+ return OntologyTerm (
113147 ** {
114148 "iri" : ontology_term ["iri" ],
115149 "label" : label ,
0 commit comments