Skip to content

Commit 289f467

Browse files
cleaning up the usage of biolink for attribute type mapping
1 parent dea00cc commit 289f467

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

reasoner_transpiler/attributes.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from pathlib import Path
44

5-
from .biolink import bmt
5+
from .biolink import bmt, is_biolink_slot, is_biolink_class, get_slot_uri, get_class_uri
66

77
DIR_PATH = Path(__file__).parent
88

@@ -111,19 +111,30 @@ def transform_attributes(result_entity, node=False):
111111
for qualifier in qualifiers]
112112

113113
# 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
127138

128139
# format the rest of the attributes, look up their attribute type and value type
129140
trapi_attributes.extend([

reasoner_transpiler/biolink.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import os
22
from bmt import Toolkit
33

4+
from bmt.toolkit import ClassDefinition
5+
from bmt.toolkit import SlotDefinition
46

57
BIOLINK_MODEL_VERSION = os.environ.get('BL_VERSION', '4.2.6-rc5')
68
BIOLINK_MODEL_SCHEMA_URL = f"https://raw.githubusercontent.com/biolink/biolink-model/v{BIOLINK_MODEL_VERSION}/biolink-model.yaml"
79
PREDICATE_MAP_URL = f"https://raw.githubusercontent.com/biolink/biolink-model/v{BIOLINK_MODEL_VERSION}/predicate_mapping.yaml"
810

911
bmt = Toolkit(schema=BIOLINK_MODEL_SCHEMA_URL, predicate_map=PREDICATE_MAP_URL)
1012
ALL_BIOLINK_ENUMS = bmt.view.all_enums().keys()
13+
14+
15+
def is_biolink_slot(obj):
16+
return isinstance(obj, SlotDefinition)
17+
18+
19+
def get_slot_uri(obj):
20+
return obj["slot_uri"]
21+
22+
23+
def is_biolink_class(obj):
24+
return isinstance(obj, ClassDefinition)
25+
26+
27+
def get_class_uri(obj):
28+
return obj["class_uri"]

0 commit comments

Comments
 (0)