@@ -40,16 +40,17 @@ def get_term_label_to_id_map(self, ontology_name: str) -> Dict[str, str]:
40
40
41
41
:param ontology_name: str name of ontology to get map of term labels to term IDs
42
42
"""
43
- if ontology_name not in self .cxg_schema .supported_ontologies :
44
- raise ValueError (f"{ ontology_name } is not a supported ontology, its metadata cannot be fetched." )
43
+ supported_ontology_name : Optional [str ] = self ._get_supported_ontology_name (ontology_name )
44
+ if not supported_ontology_name :
45
+ raise ValueError (f"{ supported_ontology_name } is not a supported ontology, its metadata cannot be fetched." )
45
46
46
- if self .term_label_to_id_map [ontology_name ]:
47
- return self .term_label_to_id_map [ontology_name ]
47
+ if self .term_label_to_id_map [supported_ontology_name ]:
48
+ return self .term_label_to_id_map [supported_ontology_name ]
48
49
49
- for term_id , term_metadata in self .cxg_schema .ontology (ontology_name ).items ():
50
- self .term_label_to_id_map [ontology_name ][term_metadata ["label" ]] = term_id
50
+ for term_id , term_metadata in self .cxg_schema .ontology (supported_ontology_name ).items ():
51
+ self .term_label_to_id_map [supported_ontology_name ][term_metadata ["label" ]] = term_id
51
52
52
- return self .term_label_to_id_map [ontology_name ]
53
+ return self .term_label_to_id_map [supported_ontology_name ]
53
54
54
55
def _parse_ontology_name (self , term_id : str ) -> str :
55
56
"""
@@ -63,12 +64,31 @@ def _parse_ontology_name(self, term_id: str) -> str:
63
64
if not re .match (pattern , term_id ):
64
65
raise ValueError (f"{ term_id } does not conform to expected regex pattern { pattern } and cannot be queried." )
65
66
66
- ontology_name = term_id .split (":" )[0 ]
67
- if ontology_name not in self .cxg_schema .supported_ontologies :
67
+ ontology_term_prefix = term_id .split (":" )[0 ]
68
+ ontology_name : Optional [str ] = self ._get_supported_ontology_name (ontology_term_prefix )
69
+ if not ontology_name :
68
70
raise ValueError (f"{ term_id } is not part of a supported ontology, its metadata cannot be fetched." )
69
71
70
72
return ontology_name
71
73
74
+ def _get_supported_ontology_name (self , ontology_term_prefix : str ) -> Optional [str ]:
75
+ """
76
+ Get the source ontology name for a given ontology term prefix, if it is supported by the CxG schema.
77
+
78
+ If ontology_term_prefix is directly supported by the CxG schema, returns ontology_term_prefix.
79
+ If ontology_term_prefix is supported as an import from another ontology, returns the name of the source ontology
80
+ it is imported in.
81
+ Otherwise, returns None.
82
+
83
+ :param ontology_term_prefix: str ontology term prefix to check
84
+ :return: str name of ontology that term belongs to, or None if it is not directly supported nor imported in
85
+ a supported ontology in the CxG schema.
86
+ """
87
+ if ontology_term_prefix in self .cxg_schema .supported_ontologies :
88
+ return ontology_term_prefix
89
+ supported_ontology_name : Optional [str ] = self .cxg_schema .imported_ontologies .get (ontology_term_prefix )
90
+ return supported_ontology_name
91
+
72
92
def is_valid_term_id (self , term_id : str , ontology : Optional [str ] = None ) -> bool :
73
93
"""
74
94
Check if an ontology term ID is valid and defined in a supported ontology. If deprecated but defined
0 commit comments