Skip to content

Commit

Permalink
Merge pull request #70 from biolink/mapping_trouble
Browse files Browse the repository at this point in the history
add method to remove munged slots from alias calculation
  • Loading branch information
sierra-moxon authored Jan 5, 2022
2 parents b9c36cf + 6215bcb commit 342ac35
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions bmt/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from bmt.toolkit import Toolkit
from bmt.toolkit import ToolkitGenerator
20 changes: 15 additions & 5 deletions bmt/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Path = str

REMOTE_PATH = (
"https://raw.githubusercontent.com/biolink/biolink-model/2.2.12/biolink-model.yaml"
"https://raw.githubusercontent.com/biolink/biolink-model/2.2.13/biolink-model.yaml"
)
RELATED_TO = "related to"
CACHE_SIZE = 1024
Expand All @@ -42,7 +42,7 @@ def __init__(
) -> None:
self.generator = ToolkitGenerator(schema)
self.generator.serialize()
self.view = SchemaView(REMOTE_PATH)
self.view = SchemaView(schema)

@lru_cache(CACHE_SIZE)
def get_all_elements(self, formatted: bool = False) -> List[str]:
Expand Down Expand Up @@ -432,10 +432,14 @@ def get_element(self, name: str) -> Optional[Element]:
"""
parsed_name = parse_name(name)
logger.debug(parsed_name)
element = self.generator.obj_for(parsed_name)
if element is None and name in self.generator.aliases:
logger.debug("in aliases")
logger.debug(self.generator.aliases)
element = self.get_element(self.generator.aliases[name])
if element is None and "_" in name:
logger.debug("has a _")
element = self.get_element(name.replace("_", " "))
return element

Expand Down Expand Up @@ -1012,7 +1016,6 @@ def get_element_by_prefix(
element = self.get_element(category)
if hasattr(element, 'id_prefixes') and prefix in element.id_prefixes:
categories.append(element.name)
print(categories)
if len(categories) == 0:
logger.warning("no biolink class found for the given curie: %s, try get_element_by_mapping?", identifier)

Expand Down Expand Up @@ -1058,10 +1061,16 @@ def get_element_by_mapping(
ancestors.append(
[x for x in self.get_ancestors(m, mixin)[::-1] if x in mappings]
)
logger.debug(ancestors)
without_empty_lists = list(filter(None, ancestors))
common_ancestors = reduce(
lambda s, l: s.intersection(set(l)), ancestors[1:], set(ancestors[0])
lambda s, l: s.intersection(set(l)), without_empty_lists[1:], set(without_empty_lists[0])
)
for a in ancestors[0]:
logger.debug("common_ancestors")
logger.debug(common_ancestors)
for a in without_empty_lists[0]:
logger.debug("ancestors[0]")
logger.debug(a)
if a in common_ancestors:
if formatted:
element = format_element(self.generator.obj_for(a))
Expand Down Expand Up @@ -1133,6 +1142,7 @@ def get_element_by_exact_mapping(
mappings = self.generator.exact_mappings.get(
self.generator.namespaces.uri_for(identifier), set()
)
logger.debug(mappings)
return self._format_all_elements(mappings, formatted)

@lru_cache(CACHE_SIZE)
Expand Down
14 changes: 12 additions & 2 deletions bmt/toolkit_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def visit_element(self, element: Element, element_uri: Optional[str]) -> None:
self.id_prefixes[element.name].add(id_prefix)
if element_uri:
self.mappings[self.namespaces.uri_for(element_uri)].add(element.name)
self.aliases.update({a: element.name for a in element.aliases})

new_aliases = self.fix_aliases(element)
self.aliases.update({a: element.name for a in new_aliases})

def visit_slot(self, aliased_slot_name: str, slot: SlotDefinition) -> None:
"""
Expand All @@ -85,7 +87,15 @@ def visit_slot(self, aliased_slot_name: str, slot: SlotDefinition) -> None:
"""
self.visit_element(slot, slot.slot_uri)
self.aliases.update({a: slot.name for a in slot.aliases})
new_aliases = self.fix_aliases(slot)
self.aliases.update({a: slot.name for a in new_aliases})

def fix_aliases(self, aelement: Element):
if '_' in aelement.name:
new_aliases = []
else:
new_aliases = aelement.aliases
return new_aliases

def visit_type(self, typ: TypeDefinition) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def toolkit():

def test_get_model_version(toolkit):
version = toolkit.get_model_version()
assert version == "2.2.12"
assert version == "2.2.13"


def test_get_inverse(toolkit):
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_toolkit_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from bmt import ToolkitGenerator

REMOTE_PATH = (
"https://raw.githubusercontent.com/biolink/biolink-model/2.2.13/biolink-model.yaml"
)


def test_get_generator():
gen = ToolkitGenerator(REMOTE_PATH)
tk = gen.serialize()
print(tk)

0 comments on commit 342ac35

Please sign in to comment.