Coverage for src/hdmf/term_set.py: 90%
53 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-07-10 23:48 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-07-10 23:48 +0000
1from collections import namedtuple
2from .utils import docval
5class TermSet():
6 """
7 Class for implementing term sets from ontologies and other resources used to define the
8 meaning and/or identify of terms.
10 :ivar term_schema_path: The LinkML YAML enumeration schema
11 :ivar sources: The prefixes for the ontologies used in the TermSet
12 :ivar view: SchemaView of the term set schema
13 """
14 def __init__(self,
15 term_schema_path: str,
16 ):
17 """
18 :param term_schema_path: The path to LinkML YAML enumeration schema
20 """
21 try:
22 from linkml_runtime.utils.schemaview import SchemaView
23 except ImportError:
24 msg = "Install linkml_runtime"
25 raise ValueError(msg)
26 self.term_schema_path = term_schema_path
27 self.view = SchemaView(self.term_schema_path)
28 self.sources = self.view.schema.prefixes
30 def __repr__(self):
31 re = "class: %s\n" % str(self.__class__)
32 re += "term_schema_path: %s\n" % self.term_schema_path
33 return re
35 def __perm_value_key_info(self, perm_values_dict: dict, key: str):
36 """
37 Private method to retrieve the id, description, and the meaning.
38 """
39 prefix_dict = self.view.schema.prefixes
40 info_tuple = namedtuple("Term_Info", ["id", "description", "meaning"])
41 description = perm_values_dict[key]['description']
42 enum_meaning = perm_values_dict[key]['meaning']
44 # filter for prefixes
45 marker = ':'
46 prefix = enum_meaning.split(marker, 1)[0]
47 id = enum_meaning.split(marker, 1)[1]
48 prefix_obj = prefix_dict[prefix]
49 prefix_reference = prefix_obj['prefix_reference']
51 # combine prefix and prefix_reference to make full term uri
52 meaning = prefix_reference+id
54 return info_tuple(enum_meaning, description, meaning)
56 @docval({'name': 'term', 'type': str, 'doc': "term to be validated"})
57 def validate(self, **kwargs):
58 """
59 Validate term in dataset towards a termset.
60 """
61 term = kwargs['term']
62 try:
63 self[term]
64 return True
65 except ValueError:
66 return False
68 @property
69 def view_set(self):
70 """
71 Property method to return a view of all terms in the the LinkML YAML Schema.
72 """
73 enumeration = list(self.view.all_enums())[0]
75 perm_values_dict = self.view.all_enums()[enumeration].permissible_values
76 enum_dict = {}
77 for perm_value_key in perm_values_dict.keys():
78 enum_dict[perm_value_key] = self.__perm_value_key_info(perm_values_dict=perm_values_dict,
79 key=perm_value_key)
81 return enum_dict
83 def __getitem__(self, term):
84 """
85 Method to retrieve a term and term information (LinkML description and LinkML meaning) from the set of terms.
86 """
87 enumeration = list(self.view.all_enums())[0]
88 perm_values_dict = self.view.all_enums()[enumeration].permissible_values
90 try:
91 term_info = self.__perm_value_key_info(perm_values_dict=perm_values_dict, key=term)
92 return term_info
94 except KeyError:
95 msg = 'Term not in schema'
96 raise ValueError(msg)