|
2 | 2 | import os |
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | | -from .biolink import bmt |
| 5 | +from .biolink import bmt, is_biolink_slot, is_biolink_class, get_slot_uri, get_class_uri |
6 | 6 |
|
7 | 7 | DIR_PATH = Path(__file__).parent |
8 | 8 |
|
@@ -111,19 +111,30 @@ def transform_attributes(result_entity, node=False): |
111 | 111 | for qualifier in qualifiers] |
112 | 112 |
|
113 | 113 | # for attributes that aren't in ATTRIBUTE_TYPES, see if they are valid biolink attributes |
114 | | - # add them to ATTRIBUTE_TYPES, so we don't need to look again |
115 | | - # TODO this is still inefficient - we should really map all the attributes on start up and/or not attempt this |
116 | | - for attribute in [key for key in result_entity.keys() if key not in ATTRIBUTE_TYPES]: |
117 | | - bmt_element = bmt.get_element(attribute) |
118 | | - if bmt_element: |
119 | | - attribute_mapping = { |
120 | | - 'attribute_type_id': bmt_element['slot_uri'] if 'slot_uri' in bmt_element else f'biolink:{attribute}' |
121 | | - } |
122 | | - if 'class_uri' in bmt_element: |
123 | | - attribute_mapping['value_type_id'] = bmt_element['class_uri'] |
124 | | - ATTRIBUTE_TYPES[attribute] = attribute_mapping |
125 | | - else: |
126 | | - ATTRIBUTE_TYPES[attribute] = DEFAULT_ATTRIBUTE_TYPE |
| 114 | + # TODO this is still inefficient - we should really map all the possible attributes on start up |
| 115 | + for attribute in result_entity: |
| 116 | + if attribute not in ATTRIBUTE_TYPES: |
| 117 | + bmt_element = bmt.get_element(attribute) |
| 118 | + if not bmt_element: |
| 119 | + ATTRIBUTE_TYPES[attribute] = DEFAULT_ATTRIBUTE_TYPE |
| 120 | + else: |
| 121 | + # This looks in the biolink model for the slot_uri or class_uri depending on if the element |
| 122 | + # is a slot or a class and attempts to populate the attribute_type_id and value_type_id with something |
| 123 | + # useful. Technically classes probably shouldn't be included as attribute_type_id but examples exist |
| 124 | + # and it seems better than having a default value that also isn't compliant. |
| 125 | + attribute_type_id = f'biolink:{attribute}' |
| 126 | + value_type_id = None |
| 127 | + if is_biolink_slot(bmt_element): |
| 128 | + bl_slot_uri = get_slot_uri(bmt_element) |
| 129 | + value_type_id = bl_slot_uri if bl_slot_uri != attribute_type_id else None |
| 130 | + elif is_biolink_class(bmt_element): |
| 131 | + attribute_type_id = get_class_uri(bmt_element) |
| 132 | + attribute_mapping = { |
| 133 | + 'attribute_type_id': attribute_type_id, |
| 134 | + **({'value_type_id': value_type_id} if value_type_id else {}) |
| 135 | + } |
| 136 | + # add it to ATTRIBUTE_TYPES, so we don't need to check biolink again |
| 137 | + ATTRIBUTE_TYPES[attribute] = attribute_mapping |
127 | 138 |
|
128 | 139 | # format the rest of the attributes, look up their attribute type and value type |
129 | 140 | trapi_attributes.extend([ |
|
0 commit comments