diff --git a/bmt/__init__.py b/bmt/__init__.py index b03f7a1..5ecabc1 100644 --- a/bmt/__init__.py +++ b/bmt/__init__.py @@ -1,2 +1 @@ from bmt.toolkit import Toolkit - diff --git a/bmt/toolkit.py b/bmt/toolkit.py index 2be5def..5715fc8 100644 --- a/bmt/toolkit.py +++ b/bmt/toolkit.py @@ -1,8 +1,14 @@ from functools import lru_cache, reduce from typing import List, Union, TextIO, Optional import deprecation -from linkml_runtime.linkml_model.meta import SchemaDefinition, Element, ElementName, \ - Definition, ClassDefinition, SlotDefinition +from linkml_runtime.linkml_model.meta import ( + SchemaDefinition, + Element, + ElementName, + Definition, + ClassDefinition, + SlotDefinition, +) from bmt.toolkit_generator import ToolkitGenerator from bmt.utils import format_element, parse_name @@ -10,8 +16,10 @@ Url = str Path = str -REMOTE_PATH = 'https://raw.githubusercontent.com/biolink/biolink-model/2.2.3/biolink-model.yaml' -RELATED_TO = 'related to' +REMOTE_PATH = ( + "https://raw.githubusercontent.com/biolink/biolink-model/2.2.3/biolink-model.yaml" +) +RELATED_TO = "related to" CACHE_SIZE = 1024 @@ -26,7 +34,9 @@ class Toolkit(object): """ - def __init__(self, schema: Union[Url, Path, TextIO, SchemaDefinition] = REMOTE_PATH) -> None: + def __init__( + self, schema: Union[Url, Path, TextIO, SchemaDefinition] = REMOTE_PATH + ) -> None: self.generator = ToolkitGenerator(schema) self.generator.serialize() @@ -148,7 +158,7 @@ def get_all_entities(self, formatted: bool = False) -> List[str]: A list of elements """ - elements = self.get_descendants('named thing') + elements = self.get_descendants("named thing") return self._format_all_elements(elements, formatted) @lru_cache(CACHE_SIZE) @@ -170,7 +180,7 @@ def get_all_associations(self, formatted: bool = False) -> List[str]: A list of elements """ - elements = self.get_descendants('association') + elements = self.get_descendants("association") return self._format_all_elements(elements, formatted) @lru_cache(CACHE_SIZE) @@ -192,8 +202,8 @@ def get_all_node_properties(self, formatted: bool = False) -> List[str]: A list of elements """ - elements = self.get_all_slots_with_class_domain('entity') - elements += self.get_descendants('node property') + elements = self.get_all_slots_with_class_domain("entity") + elements += self.get_descendants("node property") filtered_elements = self._filter_secondary(elements) return self._format_all_elements(filtered_elements, formatted) @@ -216,8 +226,8 @@ def get_all_edge_properties(self, formatted: bool = False) -> List[str]: A list of elements """ - elements = self.get_all_slots_with_class_domain('entity') - elements += self.get_descendants('association slot') + elements = self.get_all_slots_with_class_domain("entity") + elements += self.get_descendants("association slot") filtered_elements = self._filter_secondary(elements) return self._format_all_elements(filtered_elements, formatted) @@ -250,10 +260,13 @@ def _filter_secondary(self, elements: List[str]) -> List[str]: return filtered_elements @lru_cache(CACHE_SIZE) - def get_ancestors(self, name: str, - reflexive: bool = True, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_ancestors( + self, + name: str, + reflexive: bool = True, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Gets a list of names of ancestors. @@ -300,10 +313,13 @@ def _get_mixin_descendants(self, ancestors: List[ElementName]) -> List[ElementNa return mixins_parents @lru_cache(CACHE_SIZE) - def get_descendants(self, name: str, - reflexive: bool = True, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_descendants( + self, + name: str, + reflexive: bool = True, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Gets a list of names of descendants. @@ -340,7 +356,9 @@ def get_descendants(self, name: str, return self._format_all_elements(filtered_desc, formatted) @lru_cache(CACHE_SIZE) - def get_children(self, name: str, formatted: bool = False, mixin: bool = True) -> List[str]: + def get_children( + self, name: str, formatted: bool = False, mixin: bool = True + ) -> List[str]: """ Gets a list of names of children. @@ -416,15 +434,17 @@ def get_element(self, name: str) -> Optional[Element]: if name in self.generator.aliases: element = self.get_element(self.generator.aliases[name]) if element is None: - if '_' in name: - element = self.get_element(name.replace('_', ' ')) + if "_" in name: + element = self.get_element(name.replace("_", " ")) return element - def get_slot_domain(self, - slot_name, - include_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_slot_domain( + self, + slot_name, + include_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Get the domain for a given slot. @@ -451,21 +471,28 @@ def get_slot_domain(self, if element and element.domain: domain_classes.add(element.domain) if include_ancestors: - slot_domain.extend(self.get_ancestors(element.domain, reflexive=True, mixin=mixin)) + slot_domain.extend( + self.get_ancestors(element.domain, reflexive=True, mixin=mixin) + ) else: slot_domain.append(element.domain) for d in element.domain_of: if d not in domain_classes: if include_ancestors: - slot_domain.extend(self.get_ancestors(d, reflexive=True, mixin=mixin)) + slot_domain.extend( + self.get_ancestors(d, reflexive=True, mixin=mixin) + ) else: slot_domain.append(d) return self._format_all_elements(slot_domain, formatted) - def get_slot_range(self, slot_name, - include_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_slot_range( + self, + slot_name, + include_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Get the range for a given slot. @@ -496,10 +523,13 @@ def get_slot_range(self, slot_name, slot_range.extend(ancs) return self._format_all_elements(slot_range, formatted) - def get_all_slots_with_class_domain(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_all_slots_with_class_domain( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Given a class, get all the slots where the class is the domain. @@ -525,10 +555,13 @@ def get_all_slots_with_class_domain(self, class_name, slot_names = [x.name for x in slots] return self._format_all_elements(slot_names, formatted) - def get_all_slots_with_class_range(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_all_slots_with_class_range( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Given a class, get all the slots where the class is the range. @@ -554,10 +587,13 @@ def get_all_slots_with_class_range(self, class_name, slot_names = [x.name for x in slots] return self._format_all_elements(slot_names, formatted) - def get_all_predicates_with_class_domain(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_all_predicates_with_class_domain( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Given a class, get all Biolink predicates where the class is the domain. @@ -581,16 +617,21 @@ def get_all_predicates_with_class_domain(self, class_name, filtered_slots = [] element = self.get_element(class_name) if element: - slots = self._get_all_slots_with_class_domain(element, check_ancestors, mixin) + slots = self._get_all_slots_with_class_domain( + element, check_ancestors, mixin + ) for s in slots: if not s.alias and RELATED_TO in self.get_ancestors(s.name, mixin): filtered_slots.append(s.name) return self._format_all_elements(filtered_slots, formatted) - def get_all_predicates_with_class_range(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True): + def get_all_predicates_with_class_range( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ): """ Given a class, get all Biolink predicates where the class is the range. @@ -614,16 +655,21 @@ def get_all_predicates_with_class_range(self, class_name, filtered_slots = [] element = self.get_element(class_name) if element: - slots = self._get_all_slots_with_class_range(element, check_ancestors, mixin) + slots = self._get_all_slots_with_class_range( + element, check_ancestors, mixin + ) for s in slots: if not s.alias and RELATED_TO in self.get_ancestors(s.name, mixin): filtered_slots.append(s.name) return self._format_all_elements(filtered_slots, formatted) - def get_all_properties_with_class_domain(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_all_properties_with_class_domain( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Given a class, get all Biolink properties where the class is the domain. @@ -647,16 +693,21 @@ def get_all_properties_with_class_domain(self, class_name, filtered_slots = [] element = self.get_element(class_name) if element: - slots = self._get_all_slots_with_class_domain(element, check_ancestors, mixin) + slots = self._get_all_slots_with_class_domain( + element, check_ancestors, mixin + ) for s in slots: if not s.alias and RELATED_TO not in self.get_ancestors(s.name, mixin): filtered_slots.append(s.name) return self._format_all_elements(filtered_slots, formatted) - def get_all_properties_with_class_range(self, class_name, - check_ancestors: bool = False, - formatted: bool = False, - mixin: bool = True) -> List[str]: + def get_all_properties_with_class_range( + self, + class_name, + check_ancestors: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> List[str]: """ Given a class, get all Biolink properties where the class is the range. @@ -680,14 +731,15 @@ def get_all_properties_with_class_range(self, class_name, filtered_slots = [] element = self.get_element(class_name) if element: - slots = self._get_all_slots_with_class_range(element, check_ancestors, mixin) + slots = self._get_all_slots_with_class_range( + element, check_ancestors, mixin + ) for s in slots: if not s.alias and RELATED_TO not in self.get_ancestors(s.name, mixin): filtered_slots.append(s.name) return self._format_all_elements(filtered_slots, formatted) - def get_value_type_for_slot(self, slot_name, - formatted: bool = False) -> str: + def get_value_type_for_slot(self, slot_name, formatted: bool = False) -> str: """ Get the value type for a given slot. @@ -711,16 +763,16 @@ def get_value_type_for_slot(self, slot_name, if element.range in types: et = element.range else: - et = 'uriorcurie' + et = "uriorcurie" if formatted: element_type = format_element(self.generator.obj_for(et)) else: element_type = et return element_type - def _get_all_slots_with_class_domain(self, element: Element, - check_ancestors: bool, - mixin: bool = True) -> List[Element]: + def _get_all_slots_with_class_domain( + self, element: Element, check_ancestors: bool, mixin: bool = True + ) -> List[Element]: """ Given a class, get all the slots where the class is the domain. @@ -742,18 +794,18 @@ def _get_all_slots_with_class_domain(self, element: Element, slots = [] for k, v in self.generator.schema.slots.items(): if check_ancestors: - if v.domain == element.name or v.domain in self.get_ancestors(element.name, mixin) \ - or element.name in v.domain_of \ - or any(v.domain_of) in self.get_ancestors(element.name, mixin): + if (v.domain == element.name or v.domain in self.get_ancestors(element.name, mixin) + or element.name in v.domain_of + or any(v.domain_of) in self.get_ancestors(element.name, mixin)): slots.append(v) else: if element.name == v.domain or element.name in v.domain_of: slots.append(v) return slots - def _get_all_slots_with_class_range(self, element: Element, - check_ancestors: bool, - mixin: bool = True) -> List[Element]: + def _get_all_slots_with_class_range( + self, element: Element, check_ancestors: bool, mixin: bool = True + ) -> List[Element]: """ Given a class, get all the slots where the class is the range. @@ -775,7 +827,9 @@ def _get_all_slots_with_class_range(self, element: Element, slots = [] for k, v in self.generator.schema.slots.items(): if check_ancestors: - if v.range == element.name or v.range in self.get_ancestors(element.name, mixin): + if v.range == element.name or v.range in self.get_ancestors( + element.name, mixin + ): slots.append(v) else: if v.range and element.name == v.range: @@ -827,8 +881,16 @@ def is_translator_canonical_predicate(self, name: str, mixin: bool = True) -> bo if element: for annotation in element.annotations: annotation_tags.append(annotation) - is_canonical = True if element is not None and 'biolink:canonical_predicate' in annotation_tags else False - return True if RELATED_TO in self.get_ancestors(name, mixin) and is_canonical else False + is_canonical = ( + True + if element is not None and "biolink:canonical_predicate" in annotation_tags + else False + ) + return ( + True + if RELATED_TO in self.get_ancestors(name, mixin) and is_canonical + else False + ) @lru_cache(CACHE_SIZE) def is_mixin(self, name: str) -> bool: @@ -913,13 +975,16 @@ def is_category(self, name: str, mixin: bool = True) -> bool: bool That the named element is a valid category in Biolink Model """ - return 'named thing' in self.get_ancestors(name, mixin) + return "named thing" in self.get_ancestors(name, mixin) @lru_cache(CACHE_SIZE) - def get_element_by_mapping(self, identifier: str, - most_specific: bool = False, - formatted: bool = False, - mixin: bool = True) -> Optional[str]: + def get_element_by_mapping( + self, + identifier: str, + most_specific: bool = False, + formatted: bool = False, + mixin: bool = True, + ) -> Optional[str]: """ Get a Biolink Model element by mapping. This method return the common ancestor of the set of elements referenced by uriorcurie. @@ -949,8 +1014,12 @@ def get_element_by_mapping(self, identifier: str, if mappings: ancestors: List[List[str]] = [] for m in mappings: - ancestors.append([x for x in self.get_ancestors(m, mixin)[::-1] if x in mappings]) - common_ancestors = reduce(lambda s, l: s.intersection(set(l)), ancestors[1:], set(ancestors[0])) + ancestors.append( + [x for x in self.get_ancestors(m, mixin)[::-1] if x in mappings] + ) + common_ancestors = reduce( + lambda s, l: s.intersection(set(l)), ancestors[1:], set(ancestors[0]) + ) for a in ancestors[0]: if a in common_ancestors: if formatted: @@ -979,7 +1048,9 @@ def _get_element_by_mapping(self, identifier: str) -> List[str]: A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) if not mappings: exact = set(self.get_element_by_exact_mapping(identifier)) mappings.update(exact) @@ -998,7 +1069,9 @@ def _get_element_by_mapping(self, identifier: str) -> List[str]: return mappings @lru_cache(CACHE_SIZE) - def get_element_by_exact_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_element_by_exact_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find a Biolink element that corresponds to the given identifier as part of its exact_mappings. @@ -1016,11 +1089,15 @@ def get_element_by_exact_mapping(self, identifier: str, formatted: bool = False) A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.exact_mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.exact_mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) return self._format_all_elements(mappings, formatted) @lru_cache(CACHE_SIZE) - def get_element_by_close_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_element_by_close_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find a Biolink element that corresponds to the given identifier as part of its close_mappings. @@ -1038,11 +1115,15 @@ def get_element_by_close_mapping(self, identifier: str, formatted: bool = False) A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.close_mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.close_mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) return self._format_all_elements(mappings, formatted) @lru_cache(CACHE_SIZE) - def get_element_by_related_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_element_by_related_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find a Biolink element that corresponds to the given identifier as part of its related_mappings. @@ -1060,11 +1141,15 @@ def get_element_by_related_mapping(self, identifier: str, formatted: bool = Fals A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.related_mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.related_mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) return self._format_all_elements(mappings, formatted) @lru_cache(CACHE_SIZE) - def get_element_by_narrow_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_element_by_narrow_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find a Biolink element that corresponds to the given identifier as part of its narrow_mappings. @@ -1082,11 +1167,15 @@ def get_element_by_narrow_mapping(self, identifier: str, formatted: bool = False A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.narrow_mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.narrow_mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) return self._format_all_elements(mappings, formatted) @lru_cache(CACHE_SIZE) - def get_element_by_broad_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_element_by_broad_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find a Biolink element that corresponds to the given identifier as part of its broad_mappings. @@ -1104,11 +1193,15 @@ def get_element_by_broad_mapping(self, identifier: str, formatted: bool = False) A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.broad_mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.broad_mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) return self._format_all_elements(mappings, formatted) @lru_cache(CACHE_SIZE) - def get_all_elements_by_mapping(self, identifier: str, formatted: bool = False) -> List[str]: + def get_all_elements_by_mapping( + self, identifier: str, formatted: bool = False + ) -> List[str]: """ Given an identifier as IRI/CURIE, find all Biolink element that corresponds to the given identifier as part of its mappings. @@ -1126,7 +1219,9 @@ def get_all_elements_by_mapping(self, identifier: str, formatted: bool = False) A list of Biolink elements that correspond to the given identifier IRI/CURIE """ - mappings = self.generator.mappings.get(self.generator.namespaces.uri_for(identifier), set()) + mappings = self.generator.mappings.get( + self.generator.namespaces.uri_for(identifier), set() + ) exact = set(self.get_element_by_exact_mapping(identifier)) mappings.update(exact) close = set(self.get_element_by_close_mapping(identifier)) @@ -1139,7 +1234,9 @@ def get_all_elements_by_mapping(self, identifier: str, formatted: bool = False) mappings.update(broad) return self._format_all_elements(mappings, formatted) - def _format_all_elements(self, elements: List[str], formatted: bool = False) -> List[str]: + def _format_all_elements( + self, elements: List[str], formatted: bool = False + ) -> List[str]: """ Format all the elements in a given list. @@ -1157,7 +1254,9 @@ def _format_all_elements(self, elements: List[str], formatted: bool = False) -> """ if formatted: - formatted_elements = [format_element(self.generator.obj_for(x)) for x in elements] + formatted_elements = [ + format_element(self.generator.obj_for(x)) for x in elements + ] else: formatted_elements = elements return formatted_elements @@ -1175,36 +1274,64 @@ def get_model_version(self) -> str: """ return self.generator.schema.version - - - @deprecation.deprecated(deprecated_in='0.3.0', removed_in='1.0', details='Use get_all_elements method instead') + @deprecation.deprecated( + deprecated_in="0.3.0", + removed_in="1.0", + details="Use get_all_elements method instead", + ) def names(self, formatted: bool = False) -> List[str]: return self.get_all_elements(formatted) - @deprecation.deprecated(deprecated_in='0.2.0', removed_in='1.0', details='Use get_descendants method instead') + @deprecation.deprecated( + deprecated_in="0.2.0", + removed_in="1.0", + details="Use get_descendants method instead", + ) def descendents(self, name: str, mixin: bool = True) -> List[str]: return self.get_descendants(name, mixin) - @deprecation.deprecated(deprecated_in='0.2.0', removed_in='1.0', details='Use get_ancestors method instead') + @deprecation.deprecated( + deprecated_in="0.2.0", + removed_in="1.0", + details="Use get_ancestors method instead", + ) def ancestors(self, name: str, mixin: bool = True) -> List[str]: return self.get_ancestors(name, mixin) - @deprecation.deprecated(deprecated_in='0.2.0', removed_in='1.0', details='Use get_children method instead') + @deprecation.deprecated( + deprecated_in="0.2.0", + removed_in="1.0", + details="Use get_children method instead", + ) def children(self, name: str, mixin: bool = True) -> List[str]: return self.get_children(name, mixin) - @deprecation.deprecated(deprecated_in='0.2.0', removed_in='1.0', details='Use get_parent method instead') + @deprecation.deprecated( + deprecated_in="0.2.0", removed_in="1.0", details="Use get_parent method instead" + ) def parent(self, name: str, mixin: bool = True) -> Optional[str]: return self.get_parent(name, mixin) - @deprecation.deprecated(deprecated_in='0.1.1', removed_in='1.0', details='Use is_predicate method instead') + @deprecation.deprecated( + deprecated_in="0.1.1", + removed_in="1.0", + details="Use is_predicate method instead", + ) def is_edgelabel(self, name: str, mixin: bool = True) -> bool: return self.is_predicate(name, mixin) - @deprecation.deprecated(deprecated_in='0.1.1', removed_in='1.0', details='Use get_all_elements_by_mapping method instead') + @deprecation.deprecated( + deprecated_in="0.1.1", + removed_in="1.0", + details="Use get_all_elements_by_mapping method instead", + ) def get_all_by_mapping(self, uriorcurie: str) -> List[str]: return self.get_all_elements_by_mapping(uriorcurie) - @deprecation.deprecated(deprecated_in='0.1.1', removed_in='1.0', details='Use get_element_by_mapping method instead') + @deprecation.deprecated( + deprecated_in="0.1.1", + removed_in="1.0", + details="Use get_element_by_mapping method instead", + ) def get_by_mapping(self, uriorcurie: str) -> Optional[str]: - return self.get_element_by_mapping(uriorcurie) \ No newline at end of file + return self.get_element_by_mapping(uriorcurie) diff --git a/bmt/toolkit_generator.py b/bmt/toolkit_generator.py index 0dd92b0..6416557 100644 --- a/bmt/toolkit_generator.py +++ b/bmt/toolkit_generator.py @@ -1,8 +1,14 @@ from collections import defaultdict from typing import Union, Dict, Set, Optional, List -from linkml_runtime.linkml_model.meta import ClassDefinition, SlotDefinition, TypeDefinition, Element, \ - SubsetDefinition, ElementName +from linkml_runtime.linkml_model.meta import ( + ClassDefinition, + SlotDefinition, + TypeDefinition, + Element, + SubsetDefinition, + ElementName, +) from linkml.utils.generator import Generator from linkml.utils.typereferences import References @@ -19,6 +25,7 @@ class ToolkitGenerator(Generator): Additional arguments """ + valid_formats = [None] def __init__(self, *args, **kwargs): @@ -116,7 +123,9 @@ def visit_subset(self, subset: SubsetDefinition) -> None: """ self.visit_element(subset, None) - def obj_for(self, el_or_elname: str, is_range_name: bool = False) -> Optional[Element]: + def obj_for( + self, el_or_elname: str, is_range_name: bool = False + ) -> Optional[Element]: """ Get object for a given element name. @@ -138,7 +147,10 @@ def obj_for(self, el_or_elname: str, is_range_name: bool = False) -> Optional[El element_obj = self.class_or_type_for(el_or_elname) elif el_or_elname in self.schema.slots: element_obj = self.slot_for(el_or_elname) - elif el_or_elname in self.schema.types or el_or_elname == self.schema.default_range: + elif ( + el_or_elname in self.schema.types + or el_or_elname == self.schema.default_range + ): element_obj = self.class_or_type_for(el_or_elname) elif el_or_elname in self.schema.subsets: element_obj = self.subset_for(el_or_elname) @@ -156,7 +168,10 @@ def obj_for(self, el_or_elname: str, is_range_name: bool = False) -> Optional[El element_obj = self.class_or_type_for(classes[el_or_elname_lower].name) elif el_or_elname_lower in slots: element_obj = self.slot_for(slots[el_or_elname_lower].name) - elif el_or_elname_lower in types or el_or_elname_lower == self.schema.default_range: + elif ( + el_or_elname_lower in types + or el_or_elname_lower == self.schema.default_range + ): element_obj = self.class_or_type_for(types[el_or_elname_lower].name) elif el_or_elname_lower in self.schema.subsets: element_obj = self.subset_for(subsets[el_or_elname_lower].name) @@ -165,7 +180,9 @@ def obj_for(self, el_or_elname: str, is_range_name: bool = False) -> Optional[El return element_obj - def ancestors(self, element: Union[ClassDefinition, SlotDefinition]) -> List[ElementName]: + def ancestors( + self, element: Union[ClassDefinition, SlotDefinition] + ) -> List[ElementName]: """ Return an ordered list of ancestor names for the supplied slot or class. @@ -177,7 +194,9 @@ def ancestors(self, element: Union[ClassDefinition, SlotDefinition]) -> List[Ele If True, then that means we want to find mixin ancestors as well as is_a ancestors """ - return [element.name] + ([] if element.is_a is None else self.ancestors(self.parent(element))) + return [element.name] + ( + [] if element.is_a is None else self.ancestors(self.parent(element)) + ) def descendants(self, element_name: str, mixin: bool = True): """ @@ -216,7 +235,9 @@ def children(self, name: str, mixin: bool = True) -> List[str]: """ kids_or_mixin_kids = [] if mixin: - for mixin in self._union_of(self.synopsis.mixinrefs.get(name, References())): + for mixin in self._union_of( + self.synopsis.mixinrefs.get(name, References()) + ): kids_or_mixin_kids.append(mixin) for kid in self._union_of(self.synopsis.isarefs.get(name, References())): kids_or_mixin_kids.append(kid) diff --git a/bmt/utils.py b/bmt/utils.py index f018ac7..fe4ef17 100644 --- a/bmt/utils.py +++ b/bmt/utils.py @@ -1,8 +1,15 @@ import re import stringcase -from linkml_runtime.linkml_model.meta import ClassDefinition, SlotDefinition, Element, ClassDefinitionName, \ - SlotDefinitionName, ElementName, TypeDefinition +from linkml_runtime.linkml_model.meta import ( + ClassDefinition, + SlotDefinition, + Element, + ClassDefinitionName, + SlotDefinitionName, + ElementName, + TypeDefinition, +) lowercase_pattern = re.compile(r"[a-zA-Z]*[a-z][a-zA-Z]*") underscore_pattern = re.compile(r"(? str: str string in CamelCase form """ - return re.sub( - r"(?:^| )([a-zA-Z])", - lambda match: match.group(1).upper(), - s - ) + return re.sub(r"(?:^| )([a-zA-Z])", lambda match: match.group(1).upper(), s) def format_element(element: Element) -> str: @@ -108,7 +111,7 @@ def format_element(element: Element) -> str: elif isinstance(element, SlotDefinition): formatted = f"biolink:{sentencecase_to_snakecase(element.name)}" elif isinstance(element, TypeDefinition): - if element.from_schema == 'https://w3id.org/linkml/types': + if element.from_schema == "https://w3id.org/linkml/types": formatted = f"metatype:{sentencecase_to_camelcase(element.name)}" else: formatted = f"biolink:{sentencecase_to_camelcase(element.name)}" @@ -140,13 +143,13 @@ def parse_name(name) -> str: m = re.match("biolink:(.+)", name) if m: r = m.groups()[0] - if '_' in r: + if "_" in r: actual_name = snakecase_to_sentencecase(r) else: actual_name = camelcase_to_sentencecase(r) - elif '_' in name: + elif "_" in name: actual_name = snakecase_to_sentencecase(name) - elif ' ' in name: + elif " " in name: actual_name = name else: actual_name = camelcase_to_sentencecase(name) diff --git a/tests/unit/test_toolkit.py b/tests/unit/test_toolkit.py index 786e295..3bbf472 100644 --- a/tests/unit/test_toolkit.py +++ b/tests/unit/test_toolkit.py @@ -7,11 +7,11 @@ def toolkit(): return Toolkit() -ASSOCIATION = 'association' +ASSOCIATION = "association" BIOLOGICAL_ENTITY = "biological entity" BIOLINK_BIOLOGICAL_ENTITY = "biolink:BiologicalEntity" -BIOLINK_SUBJECT = 'biolink:subject' -BIOLINK_RELATED_TO = 'biolink:related_to' +BIOLINK_SUBJECT = "biolink:subject" +BIOLINK_RELATED_TO = "biolink:related_to" CAUSES = "causes" ENABLED_BY = "enabled by" GENE = "gene" @@ -21,22 +21,22 @@ def toolkit(): MOLECULAR_ACTIVITY = "molecular activity" NUCLEIC_ACID_ENTITY = "nucleic acid entity" NAMED_THING = "named thing" -ORGANISM_TAXON = 'organism taxon' -PHENOTYPIC_FEATURE = 'phenotypic feature' +ORGANISM_TAXON = "organism taxon" +PHENOTYPIC_FEATURE = "phenotypic feature" RELATED_TO = "related to" SUBJECT = "subject" -THING_WITH_TAXON = 'thing with taxon' -TREATMENT = 'treatment' +THING_WITH_TAXON = "thing with taxon" +TREATMENT = "treatment" def test_get_model_version(toolkit): version = toolkit.get_model_version() - assert version == '2.2.3' + assert version == "2.2.3" def test_get_element_by_mapping(toolkit): - element_name = toolkit.get_element_by_mapping('RO:0002410') - assert element_name == 'causes' + element_name = toolkit.get_element_by_mapping("RO:0002410") + assert element_name == "causes" def test_get_all_elements(toolkit): @@ -44,57 +44,57 @@ def test_get_all_elements(toolkit): assert NAMED_THING in elements assert ASSOCIATION in elements assert RELATED_TO in elements - assert 'uriorcurie' in elements - assert 'thing does not exist' not in elements + assert "uriorcurie" in elements + assert "thing does not exist" not in elements elements = toolkit.get_all_elements(formatted=True) - assert 'biolink:ThingDoesNotExist' not in elements - assert 'biolink:NamedThing' in elements - assert 'biolink:GeneToGeneAssociation' in elements + assert "biolink:ThingDoesNotExist" not in elements + assert "biolink:NamedThing" in elements + assert "biolink:GeneToGeneAssociation" in elements assert BIOLINK_RELATED_TO in elements - assert 'metatype:Uriorcurie' in elements - assert 'biolink:FrequencyValue' in elements + assert "metatype:Uriorcurie" in elements + assert "biolink:FrequencyValue" in elements def test_get_all_entities(toolkit): entities = toolkit.get_all_entities() assert NAMED_THING in entities assert GENE in entities - assert 'disease' in entities + assert "disease" in entities assert ASSOCIATION not in entities assert RELATED_TO not in entities entities = toolkit.get_all_entities(formatted=True) - assert 'biolink:NamedThing' in entities - assert 'biolink:Gene' in entities - assert 'biolink:Disease' in entities - assert 'biolink:Association' not in entities + assert "biolink:NamedThing" in entities + assert "biolink:Gene" in entities + assert "biolink:Disease" in entities + assert "biolink:Association" not in entities def test_get_all_associations(toolkit): associations = toolkit.get_all_associations() assert ASSOCIATION in associations - assert 'gene to gene association' in associations + assert "gene to gene association" in associations assert NAMED_THING not in associations associations = toolkit.get_all_associations(formatted=True) - assert 'biolink:Association' in associations - assert 'biolink:GeneToGeneAssociation' in associations - assert 'biolink:NamedThing' not in associations + assert "biolink:Association" in associations + assert "biolink:GeneToGeneAssociation" in associations + assert "biolink:NamedThing" not in associations def test_get_all_node_properties(toolkit): properties = toolkit.get_all_node_properties() - assert 'name' in properties - assert 'category' in properties - assert 'has gene' in properties + assert "name" in properties + assert "category" in properties + assert "has gene" in properties assert RELATED_TO not in properties assert SUBJECT not in properties properties = toolkit.get_all_node_properties(formatted=True) - assert 'biolink:name' in properties - assert 'biolink:category' in properties - assert 'biolink:has_gene' in properties + assert "biolink:name" in properties + assert "biolink:category" in properties + assert "biolink:has_gene" in properties assert BIOLINK_SUBJECT not in properties assert BIOLINK_RELATED_TO not in properties @@ -102,38 +102,38 @@ def test_get_all_node_properties(toolkit): def test_get_all_edge_properties(toolkit): properties = toolkit.get_all_edge_properties() assert SUBJECT in properties - assert 'object' in properties - assert 'frequency qualifier' in properties - assert 'not in the model' not in properties + assert "object" in properties + assert "frequency qualifier" in properties + assert "not in the model" not in properties properties = toolkit.get_all_edge_properties(formatted=True) assert BIOLINK_SUBJECT in properties - assert 'biolink:object' in properties - assert 'biolink:frequency_qualifier' in properties + assert "biolink:object" in properties + assert "biolink:frequency_qualifier" in properties def test_get_element(toolkit): gene = toolkit.get_element(GENE) - locus = toolkit.get_element('locus') + locus = toolkit.get_element("locus") assert gene == locus - o = toolkit.get_element('drug intake') - assert o and o.name == 'drug exposure' + o = toolkit.get_element("drug intake") + assert o and o.name == "drug exposure" - o = toolkit.get_element('molecular function') + o = toolkit.get_element("molecular function") assert o and o.name == MOLECULAR_ACTIVITY - o = toolkit.get_element('molecular_function') + o = toolkit.get_element("molecular_function") assert o and o.name == MOLECULAR_ACTIVITY - o = toolkit.get_element('cellular_component') - assert o and o.name == 'cellular component' + o = toolkit.get_element("cellular_component") + assert o and o.name == "cellular component" - o = toolkit.get_element('RNA Product') - assert o and o.name == 'RNA product' + o = toolkit.get_element("RNA Product") + assert o and o.name == "RNA product" - o = toolkit.get_element('rna product') - assert o and o.name == 'RNA product' + o = toolkit.get_element("rna product") + assert o and o.name == "RNA product" def test_predicate(toolkit): @@ -144,55 +144,63 @@ def test_predicate(toolkit): def test_mixin(toolkit): assert not toolkit.is_mixin(NAMED_THING) - assert toolkit.is_mixin('ontology class') - assert not toolkit.is_mixin('this_does_not_exist') + assert toolkit.is_mixin("ontology class") + assert not toolkit.is_mixin("this_does_not_exist") def test_is_translator_canonical_predicate(toolkit): - assert toolkit.is_translator_canonical_predicate('treats') - assert not toolkit.is_translator_canonical_predicate('treated by') - assert not toolkit.is_translator_canonical_predicate('this_does_not_exist') - assert not toolkit.is_translator_canonical_predicate('completed by') - assert toolkit.is_translator_canonical_predicate('decreases molecular interaction') + assert toolkit.is_translator_canonical_predicate("treats") + assert not toolkit.is_translator_canonical_predicate("treated by") + assert not toolkit.is_translator_canonical_predicate("this_does_not_exist") + assert not toolkit.is_translator_canonical_predicate("completed by") + assert toolkit.is_translator_canonical_predicate("decreases molecular interaction") def test_has_inverse(toolkit): - assert not toolkit.has_inverse('contributor') - assert toolkit.has_inverse('superclass of') - assert toolkit.has_inverse('completed by') - assert not toolkit.has_inverse('this_does_not_exist') + assert not toolkit.has_inverse("contributor") + assert toolkit.has_inverse("superclass of") + assert toolkit.has_inverse("completed by") + assert not toolkit.has_inverse("this_does_not_exist") def test_category(toolkit): assert toolkit.is_category(NAMED_THING) assert toolkit.is_category(GENE) assert not toolkit.is_category(CAUSES) - assert not toolkit.is_category('affects') - assert not toolkit.is_category('gene or gene product') + assert not toolkit.is_category("affects") + assert not toolkit.is_category("gene or gene product") def test_ancestors(toolkit): assert RELATED_TO in toolkit.get_ancestors(CAUSES) - assert 'biolink:ChemicalEntityOrGeneOrGeneProduct' in toolkit.get_ancestors(GENE, formatted=True) - assert 'biolink:GenomicEntity' in toolkit.get_ancestors(GENE, formatted=True) + assert "biolink:ChemicalEntityOrGeneOrGeneProduct" in toolkit.get_ancestors( + GENE, formatted=True + ) + assert "biolink:GenomicEntity" in toolkit.get_ancestors(GENE, formatted=True) assert BIOLINK_RELATED_TO in toolkit.get_ancestors(CAUSES, formatted=True) - assert 'biolink:GeneOrGeneProduct' in toolkit.get_ancestors(GENE, formatted=True) - assert 'biolink:GeneOrGeneProduct' not in toolkit.get_ancestors(GENE, formatted=True, mixin=False) + assert "biolink:GeneOrGeneProduct" in toolkit.get_ancestors(GENE, formatted=True) + assert "biolink:GeneOrGeneProduct" not in toolkit.get_ancestors( + GENE, formatted=True, mixin=False + ) assert NAMED_THING in toolkit.get_ancestors(GENE) assert GENE_OR_GENE_PRODUCT in toolkit.get_ancestors(GENE) assert GENE_OR_GENE_PRODUCT not in toolkit.get_ancestors(GENE, mixin=False) - assert 'biolink:NamedThing' in toolkit.get_ancestors(GENE, formatted=True) - assert 'biological entity' in toolkit.get_ancestors(GENE) - assert 'transcript' not in toolkit.get_ancestors(GENE) + assert "biolink:NamedThing" in toolkit.get_ancestors(GENE, formatted=True) + assert "biological entity" in toolkit.get_ancestors(GENE) + assert "transcript" not in toolkit.get_ancestors(GENE) assert CAUSES in toolkit.get_ancestors(CAUSES) assert CAUSES in toolkit.get_ancestors(CAUSES, reflexive=True) assert CAUSES not in toolkit.get_ancestors(CAUSES, reflexive=False) - assert 'biolink:causes' in toolkit.get_ancestors(CAUSES, reflexive=True, formatted=True) + assert "biolink:causes" in toolkit.get_ancestors( + CAUSES, reflexive=True, formatted=True + ) assert GENOMIC_ENTITY in toolkit.get_ancestors(GENE) assert GENOMIC_ENTITY not in toolkit.get_ancestors(BIOLOGICAL_ENTITY) assert GENOMIC_ENTITY in toolkit.get_ancestors(GENOMIC_ENTITY, reflexive=True) assert GENOMIC_ENTITY not in toolkit.get_ancestors(GENOMIC_ENTITY, reflexive=False) - assert THING_WITH_TAXON not in toolkit.get_ancestors(PHENOTYPIC_FEATURE, mixin=False) + assert THING_WITH_TAXON not in toolkit.get_ancestors( + PHENOTYPIC_FEATURE, mixin=False + ) assert THING_WITH_TAXON in toolkit.get_ancestors(PHENOTYPIC_FEATURE) @@ -208,138 +216,192 @@ def test_ancestors_for_kgx(toolkit): def test_descendants(toolkit): assert GENE in toolkit.get_descendants(GENE_OR_GENE_PRODUCT) assert GENE not in toolkit.get_descendants(GENE_OR_GENE_PRODUCT, mixin=False) - assert MOLECULAR_ACTIVITY in toolkit.get_descendants('occurrent') - assert GENE not in toolkit.get_descendants('outcome') + assert MOLECULAR_ACTIVITY in toolkit.get_descendants("occurrent") + assert GENE not in toolkit.get_descendants("outcome") assert GENE in toolkit.get_descendants(NAMED_THING) assert CAUSES in toolkit.get_descendants(RELATED_TO) assert INTERACTS_WITH in toolkit.get_descendants(RELATED_TO) assert PHENOTYPIC_FEATURE in toolkit.get_descendants(NAMED_THING) assert RELATED_TO not in toolkit.get_descendants(NAMED_THING) - assert 'biolink:PhenotypicFeature' in toolkit.get_descendants(NAMED_THING, formatted=True) - assert 'molecular activity_has output' not in toolkit.get_descendants(MOLECULAR_ACTIVITY, reflexive=True) - assert 'molecular activity_has output' not in toolkit.get_descendants('has output', reflexive=True) + assert "biolink:PhenotypicFeature" in toolkit.get_descendants( + NAMED_THING, formatted=True + ) + assert "molecular activity_has output" not in toolkit.get_descendants( + MOLECULAR_ACTIVITY, reflexive=True + ) + assert "molecular activity_has output" not in toolkit.get_descendants( + "has output", reflexive=True + ) assert GENE in toolkit.get_descendants(GENE, reflexive=True) def test_children(toolkit): - assert CAUSES in toolkit.get_children('contributes to') - assert 'physically interacts with' in toolkit.get_children(INTERACTS_WITH) - assert 'transcript' in toolkit.get_children(NUCLEIC_ACID_ENTITY) + assert CAUSES in toolkit.get_children("contributes to") + assert "physically interacts with" in toolkit.get_children(INTERACTS_WITH) + assert "transcript" in toolkit.get_children(NUCLEIC_ACID_ENTITY) assert GENE in toolkit.get_children(GENE_OR_GENE_PRODUCT) assert GENE not in toolkit.get_children(GENE_OR_GENE_PRODUCT, mixin=False) - assert 'biolink:Transcript' in toolkit.get_children(NUCLEIC_ACID_ENTITY, formatted=True) + assert "biolink:Transcript" in toolkit.get_children( + NUCLEIC_ACID_ENTITY, formatted=True + ) def test_parent(toolkit): - assert 'contributes to' in toolkit.get_parent(CAUSES) - assert INTERACTS_WITH in toolkit.get_parent('physically interacts with') + assert "contributes to" in toolkit.get_parent(CAUSES) + assert INTERACTS_WITH in toolkit.get_parent("physically interacts with") assert BIOLOGICAL_ENTITY in toolkit.get_parent(GENE) assert BIOLINK_BIOLOGICAL_ENTITY in toolkit.get_parent(GENE, formatted=True) def test_mapping(toolkit): - assert len(toolkit.get_all_elements_by_mapping('SO:0000704')) == 1 - assert GENE in toolkit.get_all_elements_by_mapping('SO:0000704') + assert len(toolkit.get_all_elements_by_mapping("SO:0000704")) == 1 + assert GENE in toolkit.get_all_elements_by_mapping("SO:0000704") - assert len(toolkit.get_all_elements_by_mapping('MONDO:0000001')) == 1 - assert 'disease' in toolkit.get_all_elements_by_mapping('MONDO:0000001') + assert len(toolkit.get_all_elements_by_mapping("MONDO:0000001")) == 1 + assert "disease" in toolkit.get_all_elements_by_mapping("MONDO:0000001") - assert len(toolkit.get_all_elements_by_mapping('UPHENO:0000001')) == 1 - assert 'affects' in toolkit.get_all_elements_by_mapping('UPHENO:0000001') + assert len(toolkit.get_all_elements_by_mapping("UPHENO:0000001")) == 1 + assert "affects" in toolkit.get_all_elements_by_mapping("UPHENO:0000001") - assert len(toolkit.get_all_elements_by_mapping('RO:0004033')) == 2 - assert 'negatively regulates' in toolkit.get_all_elements_by_mapping('RO:0004033') - assert 'biolink:negatively_regulates' in toolkit.get_all_elements_by_mapping('RO:0004033', formatted=True) + assert len(toolkit.get_all_elements_by_mapping("RO:0004033")) == 2 + assert "negatively regulates" in toolkit.get_all_elements_by_mapping("RO:0004033") + assert "biolink:negatively_regulates" in toolkit.get_all_elements_by_mapping( + "RO:0004033", formatted=True + ) def test_get_slot_domain(toolkit): - assert BIOLOGICAL_ENTITY in toolkit.get_slot_domain('ameliorates') - assert 'biological process or activity' in toolkit.get_slot_domain(ENABLED_BY) - assert BIOLOGICAL_ENTITY in toolkit.get_slot_domain(ENABLED_BY, include_ancestors=True) - assert BIOLINK_BIOLOGICAL_ENTITY in toolkit.get_slot_domain(ENABLED_BY, include_ancestors=True, formatted=True) - assert 'entity' in toolkit.get_slot_domain('name') - assert 'entity' in toolkit.get_slot_domain('category') - assert ASSOCIATION in toolkit.get_slot_domain('predicate') + assert BIOLOGICAL_ENTITY in toolkit.get_slot_domain("ameliorates") + assert "biological process or activity" in toolkit.get_slot_domain(ENABLED_BY) + assert BIOLOGICAL_ENTITY in toolkit.get_slot_domain( + ENABLED_BY, include_ancestors=True + ) + assert BIOLINK_BIOLOGICAL_ENTITY in toolkit.get_slot_domain( + ENABLED_BY, include_ancestors=True, formatted=True + ) + assert "entity" in toolkit.get_slot_domain("name") + assert "entity" in toolkit.get_slot_domain("category") + assert ASSOCIATION in toolkit.get_slot_domain("predicate") def test_get_slot_range(toolkit): - assert 'disease or phenotypic feature' in toolkit.get_slot_range('treats') - assert BIOLOGICAL_ENTITY in toolkit.get_slot_range('treats', include_ancestors=True) - assert BIOLINK_BIOLOGICAL_ENTITY in toolkit.get_slot_range('treats', include_ancestors=True, formatted=True) - assert 'label type' in toolkit.get_slot_range('name') + assert "disease or phenotypic feature" in toolkit.get_slot_range("treats") + assert BIOLOGICAL_ENTITY in toolkit.get_slot_range("treats", include_ancestors=True) + assert BIOLINK_BIOLOGICAL_ENTITY in toolkit.get_slot_range( + "treats", include_ancestors=True, formatted=True + ) + assert "label type" in toolkit.get_slot_range("name") def test_get_all_slots_with_class_domain(toolkit): - assert 'has drug' in toolkit.get_all_slots_with_class_domain(TREATMENT) - assert 'name' in toolkit.get_all_slots_with_class_domain('entity', check_ancestors=True, mixin=True) - assert 'name' not in toolkit.get_all_slots_with_class_domain(TREATMENT, check_ancestors=False, mixin=False) - assert 'name' not in toolkit.get_all_slots_with_class_domain(TREATMENT, check_ancestors=True, mixin=True) + assert "has drug" in toolkit.get_all_slots_with_class_domain(TREATMENT) + assert "name" in toolkit.get_all_slots_with_class_domain( + "entity", check_ancestors=True, mixin=True + ) + assert "name" not in toolkit.get_all_slots_with_class_domain( + TREATMENT, check_ancestors=False, mixin=False + ) + assert "name" not in toolkit.get_all_slots_with_class_domain( + TREATMENT, check_ancestors=True, mixin=True + ) # we don't really have this use case in the model right now - where a domain's mixin has an attribute - assert 'has unit' in toolkit.get_all_slots_with_class_domain('quantity value', - check_ancestors=False, - mixin=True) - assert 'name' in toolkit.get_all_slots_with_class_domain('entity', check_ancestors=True, mixin=False) - assert 'biolink:has_drug' in toolkit.get_all_slots_with_class_domain(TREATMENT, formatted=True) + assert "has unit" in toolkit.get_all_slots_with_class_domain( + "quantity value", check_ancestors=False, mixin=True + ) + assert "name" in toolkit.get_all_slots_with_class_domain( + "entity", check_ancestors=True, mixin=False + ) + assert "biolink:has_drug" in toolkit.get_all_slots_with_class_domain( + TREATMENT, formatted=True + ) def test_get_all_slots_with_class_range(toolkit): - assert 'in taxon' in toolkit.get_all_slots_with_class_range(ORGANISM_TAXON) - assert 'biolink:in_taxon' in toolkit.get_all_slots_with_class_range(ORGANISM_TAXON, formatted=True) - assert SUBJECT in toolkit.get_all_slots_with_class_range(ORGANISM_TAXON, check_ancestors=True, mixin=False) - assert 'primary knowledge source' in toolkit.get_all_slots_with_class_range('information resource', - check_ancestors=True, - mixin=False) - assert 'knowledge source' in toolkit.get_all_slots_with_class_range('information resource', check_ancestors=True, - mixin=True) + assert "in taxon" in toolkit.get_all_slots_with_class_range(ORGANISM_TAXON) + assert "biolink:in_taxon" in toolkit.get_all_slots_with_class_range( + ORGANISM_TAXON, formatted=True + ) + assert SUBJECT in toolkit.get_all_slots_with_class_range( + ORGANISM_TAXON, check_ancestors=True, mixin=False + ) + assert "primary knowledge source" in toolkit.get_all_slots_with_class_range( + "information resource", check_ancestors=True, mixin=False + ) + assert "knowledge source" in toolkit.get_all_slots_with_class_range( + "information resource", check_ancestors=True, mixin=True + ) def test_get_all_predicates_with_class_domain(toolkit): - assert 'genetically interacts with' in toolkit.get_all_slots_with_class_domain(GENE) - assert INTERACTS_WITH in toolkit.get_all_slots_with_class_domain(GENE, check_ancestors=True) - assert 'biolink:interacts_with' in toolkit.get_all_slots_with_class_domain('gene', check_ancestors=True, - formatted=True) - assert 'in complex with' in toolkit.get_all_slots_with_class_domain(GENE_OR_GENE_PRODUCT) - assert 'expressed in' in toolkit.get_all_slots_with_class_domain(GENE_OR_GENE_PRODUCT) - assert RELATED_TO not in toolkit.get_all_slots_with_class_domain(GENE_OR_GENE_PRODUCT, check_ancestors=False, - mixin=False) - assert RELATED_TO not in toolkit.get_all_slots_with_class_domain(GENE_OR_GENE_PRODUCT, check_ancestors=True, - mixin=True) + assert "genetically interacts with" in toolkit.get_all_slots_with_class_domain(GENE) + assert INTERACTS_WITH in toolkit.get_all_slots_with_class_domain( + GENE, check_ancestors=True + ) + assert "biolink:interacts_with" in toolkit.get_all_slots_with_class_domain( + "gene", check_ancestors=True, formatted=True + ) + assert "in complex with" in toolkit.get_all_slots_with_class_domain( + GENE_OR_GENE_PRODUCT + ) + assert "expressed in" in toolkit.get_all_slots_with_class_domain( + GENE_OR_GENE_PRODUCT + ) + assert RELATED_TO not in toolkit.get_all_slots_with_class_domain( + GENE_OR_GENE_PRODUCT, check_ancestors=False, mixin=False + ) + assert RELATED_TO not in toolkit.get_all_slots_with_class_domain( + GENE_OR_GENE_PRODUCT, check_ancestors=True, mixin=True + ) def test_get_all_predicates_with_class_range(toolkit): - assert 'manifestation of' in toolkit.get_all_predicates_with_class_range('disease') - assert 'disease has basis in' in toolkit.get_all_predicates_with_class_range('disease', - check_ancestors=True) - assert 'biolink:disease_has_basis_in' in toolkit.get_all_predicates_with_class_range('disease', - check_ancestors=True, - formatted=True) - assert RELATED_TO not in toolkit.get_all_predicates_with_class_range('disease', - check_ancestors=True, - formatted=True) + assert "manifestation of" in toolkit.get_all_predicates_with_class_range("disease") + assert "disease has basis in" in toolkit.get_all_predicates_with_class_range( + "disease", check_ancestors=True + ) + assert ( + "biolink:disease_has_basis_in" + in toolkit.get_all_predicates_with_class_range( + "disease", check_ancestors=True, formatted=True + ) + ) + assert RELATED_TO not in toolkit.get_all_predicates_with_class_range( + "disease", check_ancestors=True, formatted=True + ) def test_get_all_properties_with_class_domain(toolkit): - assert 'category' in toolkit.get_all_properties_with_class_domain('entity') - assert 'category' in toolkit.get_all_properties_with_class_domain(GENE, check_ancestors=True) - assert 'biolink:category' in toolkit.get_all_properties_with_class_domain(GENE, check_ancestors=True, formatted=True) + assert "category" in toolkit.get_all_properties_with_class_domain("entity") + assert "category" in toolkit.get_all_properties_with_class_domain( + GENE, check_ancestors=True + ) + assert "biolink:category" in toolkit.get_all_properties_with_class_domain( + GENE, check_ancestors=True, formatted=True + ) assert SUBJECT in toolkit.get_all_properties_with_class_domain(ASSOCIATION) - assert SUBJECT in toolkit.get_all_properties_with_class_domain(ASSOCIATION, check_ancestors=True) - assert BIOLINK_SUBJECT in toolkit.get_all_properties_with_class_domain(ASSOCIATION, check_ancestors=True, - formatted=True) + assert SUBJECT in toolkit.get_all_properties_with_class_domain( + ASSOCIATION, check_ancestors=True + ) + assert BIOLINK_SUBJECT in toolkit.get_all_properties_with_class_domain( + ASSOCIATION, check_ancestors=True, formatted=True + ) def test_get_all_properties_with_class_range(toolkit): - assert 'has gene' in toolkit.get_all_properties_with_class_range(GENE) - assert SUBJECT in toolkit.get_all_properties_with_class_range(GENE, check_ancestors=True) - assert 'biolink:subject' in toolkit.get_all_properties_with_class_range(GENE, check_ancestors=True, formatted=True) + assert "has gene" in toolkit.get_all_properties_with_class_range(GENE) + assert SUBJECT in toolkit.get_all_properties_with_class_range( + GENE, check_ancestors=True + ) + assert "biolink:subject" in toolkit.get_all_properties_with_class_range( + GENE, check_ancestors=True, formatted=True + ) def test_get_value_type_for_slot(toolkit): - assert 'uriorcurie' in toolkit.get_value_type_for_slot(SUBJECT) - assert 'uriorcurie' in toolkit.get_value_type_for_slot('object') - assert 'string' in toolkit.get_value_type_for_slot('symbol') - assert 'biolink:CategoryType' in toolkit.get_value_type_for_slot('category', formatted=True) - - - + assert "uriorcurie" in toolkit.get_value_type_for_slot(SUBJECT) + assert "uriorcurie" in toolkit.get_value_type_for_slot("object") + assert "string" in toolkit.get_value_type_for_slot("symbol") + assert "biolink:CategoryType" in toolkit.get_value_type_for_slot( + "category", formatted=True + ) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 5a15f71..fb1164c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -3,48 +3,58 @@ from bmt.utils import parse_name, format_element, sentencecase_to_camelcase + @pytest.fixture(scope="module") def toolkit(): return Toolkit() -@pytest.mark.parametrize('query', [ - ('biolink:Gene', 'gene'), - ('biolink:NamedThing', 'named thing'), - ('biolink:related_to', 'related to'), - ('PhenotypicFeature', 'phenotypic feature'), - ('related_to', 'related to'), - ('related to', 'related to'), - ('causes', 'causes'), - ('treats', 'treats'), - ('gene', 'gene'), - ('has_gene', 'has gene'), - ('biolink:GeneToGeneAssociation', 'gene to gene association'), - ('RNA product', 'RNA product'), - ('RNA Product', 'RNA Product'), - ('Rna Product', 'Rna Product'), - ('biolink:RNAProduct', 'RNA product'), -]) +@pytest.mark.parametrize( + "query", + [ + ("biolink:Gene", "gene"), + ("biolink:NamedThing", "named thing"), + ("biolink:related_to", "related to"), + ("PhenotypicFeature", "phenotypic feature"), + ("related_to", "related to"), + ("related to", "related to"), + ("causes", "causes"), + ("treats", "treats"), + ("gene", "gene"), + ("has_gene", "has gene"), + ("biolink:GeneToGeneAssociation", "gene to gene association"), + ("RNA product", "RNA product"), + ("RNA Product", "RNA Product"), + ("Rna Product", "Rna Product"), + ("biolink:RNAProduct", "RNA product"), + ], +) def test_parse_name(query): n = parse_name(query[0]) assert n == query[1] -@pytest.mark.parametrize('query', [ - ('phenotypic feature', 'PhenotypicFeature'), - ('noncoding RNA product', 'NoncodingRNAProduct'), -]) +@pytest.mark.parametrize( + "query", + [ + ("phenotypic feature", "PhenotypicFeature"), + ("noncoding RNA product", "NoncodingRNAProduct"), + ], +) def test_sentencecase_to_camelcase(query): n = sentencecase_to_camelcase(query[0]) assert n == query[1] -@pytest.mark.parametrize('query', [ - ('related to', 'biolink:related_to'), - ('caused_by', 'biolink:caused_by'), - ('PhenotypicFeature', 'biolink:PhenotypicFeature'), - ('noncoding RNA product', 'biolink:NoncodingRNAProduct'), -]) +@pytest.mark.parametrize( + "query", + [ + ("related to", "biolink:related_to"), + ("caused_by", "biolink:caused_by"), + ("PhenotypicFeature", "biolink:PhenotypicFeature"), + ("noncoding RNA product", "biolink:NoncodingRNAProduct"), + ], +) def test_format_element(query, toolkit): n = format_element(toolkit.get_element(query[0])) assert n == query[1] diff --git a/tox.ini b/tox.ini index 4d59a16..7caa4b6 100644 --- a/tox.ini +++ b/tox.ini @@ -36,7 +36,7 @@ commands = flake8 bmt tests [flake8] -ignore = E501 +ignore = E501, W503 max_line_length = 70 max-complexity = 10 per-file-ignores = __init__.py:F401