Manipulating Schemas

The SchemaView class in the linkml-runtime provides a method for dynamically introspecting and manipulating schemas, for example:

  • find all ancestors or descendants of a class

  • find all inferred (induced) slots for a class

See runtime notebooks for an example

class linkml_runtime.utils.schemaview.SchemaView(schema: Union[str, linkml_runtime.linkml_model.meta.SchemaDefinition], importmap: Optional[Mapping[str, str]] = None)[source]

A SchemaView provides a virtual schema layered on top of a schema plus its import closure

Most operations are parameterized by imports. If this is set to True (default), then the full import closure is considered when answering

This class utilizes caching for efficient lookup operations.

TODO: decide how to use this in conjunction with the existing schemaloader, which injects into the schema rather than providing dynamic methods.

See:
add_class(cls: linkml_runtime.linkml_model.meta.ClassDefinition) None[source]
Parameters

cls – class to be added

Returns

add_enum(enum: linkml_runtime.linkml_model.meta.EnumDefinition) None[source]
Parameters

enum – enum to be added

Returns

add_slot(slot: linkml_runtime.linkml_model.meta.SlotDefinition) None[source]
Parameters

slot – slot to be added

Returns

add_subset(subset: linkml_runtime.linkml_model.meta.SubsetDefinition) None[source]
Parameters

subset – subset to be added

Returns

add_type(type: linkml_runtime.linkml_model.meta.TypeDefinition) None[source]
Parameters

type – type to be added

Returns

all_class(imports=True) Dict[linkml_runtime.linkml_model.meta.ClassDefinitionName, linkml_runtime.linkml_model.meta.ClassDefinition][source]
Parameters

imports – include imports closure

Returns

all classes in schema view

all_classes(imports=True) Dict[linkml_runtime.linkml_model.meta.ClassDefinitionName, linkml_runtime.linkml_model.meta.ClassDefinition][source]
Parameters

imports – include imports closure

Returns

all classes in schema view

all_element(imports=True) Dict[linkml_runtime.linkml_model.meta.ElementName, linkml_runtime.linkml_model.meta.Element][source]
Parameters

imports – include imports closure

Returns

all elements in schema view

all_elements(imports=True) Dict[linkml_runtime.linkml_model.meta.ElementName, linkml_runtime.linkml_model.meta.Element][source]
Parameters

imports – include imports closure

Returns

all elements in schema view

all_enum(imports=True) Dict[linkml_runtime.linkml_model.meta.EnumDefinitionName, linkml_runtime.linkml_model.meta.EnumDefinition][source]
Parameters

imports – include imports closure

Returns

all enums in schema view

all_enums(imports=True) Dict[linkml_runtime.linkml_model.meta.EnumDefinitionName, linkml_runtime.linkml_model.meta.EnumDefinition][source]
Parameters

imports – include imports closure

Returns

all enums in schema view

all_schema(imports: True) List[linkml_runtime.linkml_model.meta.SchemaDefinition][source]
Parameters

imports – include imports closure

Returns

all schemas

all_slot(imports=True) Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, linkml_runtime.linkml_model.meta.SlotDefinition][source]
Parameters

imports – include imports closure

Returns

all slots in schema view

all_slots(imports=True) Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, linkml_runtime.linkml_model.meta.SlotDefinition][source]
Parameters

imports – include imports closure

Returns

all slots in schema view

all_subset(imports=True) Dict[linkml_runtime.linkml_model.meta.SubsetDefinitionName, linkml_runtime.linkml_model.meta.SubsetDefinition][source]
Parameters

imports – include imports closure

Returns

all subsets in schema view

all_subsets(imports=True) Dict[linkml_runtime.linkml_model.meta.SubsetDefinitionName, linkml_runtime.linkml_model.meta.SubsetDefinition][source]
Parameters

imports – include imports closure

Returns

all subsets in schema view

all_type(imports=True) Dict[linkml_runtime.linkml_model.meta.TypeDefinitionName, linkml_runtime.linkml_model.meta.TypeDefinition][source]
Parameters

imports – include imports closure

Returns

all types in schema view

all_types(imports=True) Dict[linkml_runtime.linkml_model.meta.TypeDefinitionName, linkml_runtime.linkml_model.meta.TypeDefinition][source]
Parameters

imports – include imports closure

Returns

all types in schema view

annotation_dict(element_name: linkml_runtime.linkml_model.meta.ElementName, imports=True) Dict[linkml_runtime.utils.metamodelcore.URIorCURIE, Any][source]

Return a dictionary where keys are annotation tags and values are annotation values for any given element.

Note this will not include higher-order annotations

See also: https://github.com/linkml/linkml/issues/296

Parameters
  • element_name

  • imports

Returns

annotation dictionary

class_ancestors(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True, mixins=True, reflexive=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]

Closure of class_parents method

Parameters
  • class_name – query class

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of ancestors

Returns

ancestor class names

class_children(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]
Parameters
  • class_name – parent class name

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

Returns

all direct child class names (is_a and mixins)

class_descendants(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True, mixins=True, reflexive=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]

Closure of class_children method

Parameters
  • class_name – query class

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of descendants

Returns

descendants class names

class_leaves(imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]

All classes that have no children :param imports: :param mixins: :param is_a: include is_a parents (default is True) :return:

class_parents(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]
Parameters
  • class_name – child class name

  • imports – include import closure

  • mixins – include mixins (default is True)

Returns

all direct parent class names (is_a and mixins)

class_roots(imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.ClassDefinitionName][source]

All classes that have no parents :param imports: :param mixins: :param is_a: include is_a parents (default is True) :return:

class_slots(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True, direct=False, attributes=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]
Parameters
  • class_name

  • imports – include imports closure

  • direct – only returns slots directly associated with a class (default is False)

  • attributes – include attribute declarations as well as slots (default is True)

Returns

all slot names applicable for a class

delete_class(class_name: linkml_runtime.linkml_model.meta.ClassDefinitionName, delete_references=True) None[source]
Parameters

class_name – class to be deleted

Returns

delete_enum(enum_name: linkml_runtime.linkml_model.meta.EnumDefinitionName) None[source]
Parameters

enum_name – enum to be deleted

Returns

delete_slot(slot_name: linkml_runtime.linkml_model.meta.SlotDefinitionName) None[source]
Parameters

slot_name – slot to be deleted

Returns

delete_subset(subset_name: linkml_runtime.linkml_model.meta.SubsetDefinitionName) None[source]
Parameters

subset_name – subset to be deleted

Returns

delete_type(type_name: linkml_runtime.linkml_model.meta.TypeDefinitionName) None[source]
Parameters

type_name – type to be deleted

Returns

expand_curie(uri: str) str[source]

Expands a URI or CURIE to a full URI :param uri: :return: URI as a string

get_class(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True) linkml_runtime.linkml_model.meta.ClassDefinition[source]
Parameters
  • class_name – name of the class to be retrieved

  • imports – include import closure

Returns

class definition

get_element(element: Union[linkml_runtime.linkml_model.meta.ElementName, linkml_runtime.linkml_model.meta.Element], imports=True) linkml_runtime.linkml_model.meta.Element[source]

Fetch an element by name

Parameters
  • element – query element

  • imports – include imports closure

Returns

get_enum(enum_name: Union[linkml_runtime.linkml_model.meta.EnumDefinitionName, str], imports=True) linkml_runtime.linkml_model.meta.EnumDefinition[source]
Parameters
  • enum_name – name of the enum to be retrieved

  • imports – include import closure

Returns

enum definition

get_identifier_slot(cn: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str], imports=True) Optional[linkml_runtime.linkml_model.meta.SlotDefinition][source]
Parameters
  • cn – class name

  • imports

Returns

the slot that acts as identifier

get_mapping_index(imports=True, expand=False) Dict[linkml_runtime.utils.metamodelcore.URIorCURIE, List[Tuple[str, linkml_runtime.linkml_model.meta.Element]]][source]

Returns an index of all elements keyed by the mapping value. The index values are tuples of mapping type and element

Parameters
  • imports

  • expand – if true the index will be keyed by expanded URIs, not CURIEs

Returns

index

get_mappings(element_name: linkml_runtime.linkml_model.meta.ElementName = None, imports=True, expand=False) Dict[str, List[linkml_runtime.utils.metamodelcore.URIorCURIE]][source]

Get all mappings for a given element

Parameters
  • element_name – the query element

  • imports – include imports closure

  • expand – expand CURIEs to URIs

Returns

index keyed by mapping type

get_slot(slot_name: Union[linkml_runtime.linkml_model.meta.SlotDefinitionName, str], imports=True, attributes=False) linkml_runtime.linkml_model.meta.SlotDefinition[source]
Parameters
  • slot_name – name of the slot to be retrieved

  • imports – include import closure

Returns

slot definition

get_subset(subset_name: Union[linkml_runtime.linkml_model.meta.SubsetDefinitionName, str], imports=True) linkml_runtime.linkml_model.meta.SubsetDefinition[source]
Parameters
  • subset_name – name of the subsey to be retrieved

  • imports – include import closure

Returns

subset definition

get_type(type_name: Union[linkml_runtime.linkml_model.meta.TypeDefinitionName, str], imports=True) linkml_runtime.linkml_model.meta.TypeDefinition[source]
Parameters
  • type_name – name of the type to be retrieved

  • imports – include import closure

Returns

type definition

get_uri(element: Union[linkml_runtime.linkml_model.meta.ElementName, linkml_runtime.linkml_model.meta.Element], imports=True, expand=False, native=False) str[source]

Return the CURIE or URI for a schema element. If the schema defines a specific URI, this is used, otherwise this is constructed from the default prefix combined with the element name

Parameters
  • element_name – name of schema element

  • imports – include imports closure

  • native – return the native CURIE or URI rather than what is declared in the uri slot

  • expand – expand the CURIE to a URI; defaults to False

Returns

URI or CURIE as a string

imports_closure(traverse=True) List[linkml_runtime.linkml_model.meta.SchemaDefinitionName][source]

Return all imports

Parameters

traverse – if true, traverse recursively

Returns

all schema names in the transitive reflexive imports closure

in_schema(element_name: linkml_runtime.linkml_model.meta.ElementName) linkml_runtime.linkml_model.meta.SchemaDefinitionName[source]
Parameters

element_name

Returns

name of schema in which element is defined

induced_slot(slot_name: Union[linkml_runtime.linkml_model.meta.SlotDefinitionName, str], class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str] = None, imports=True) linkml_runtime.linkml_model.meta.SlotDefinition[source]

Given a slot, in the context of a particular class, yield a dynamic SlotDefinition that has all properties materialized.

This makes use of schema slots, such as attributes, slot_usage. It also uses ancestor relationships to infer missing values

Parameters
  • slot_name – slot to be queries

  • class_name – class used as context

  • imports – include imports closure

Returns

dynamic slot constructed by inference

is_inlined(slot: linkml_runtime.linkml_model.meta.SlotDefinition, imports=True) bool[source]

True if slot is inferred or asserted inline

Parameters
  • slot

  • imports

Returns

is_relationship(class_name: Union[linkml_runtime.linkml_model.meta.ClassDefinitionName, str] = None, imports=True) bool[source]

Tests if a class represents a relationship or reified statement

Parameters
  • class_name

  • imports

Returns

true if the class represents a relationship

merge_schema(schema: linkml_runtime.linkml_model.meta.SchemaDefinition) None[source]

merges another schema into this one :param schema: schema to be merged

slot_ancestors(slot_name: Union[linkml_runtime.linkml_model.meta.SlotDefinitionName, str], imports=True, mixins=True, reflexive=True, is_a=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]

Closure of slot_parents method

Parameters
  • slot_name – query slot

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

  • reflexive – include self in set of ancestors

Returns

ancestor slot names

slot_children(slot_name: Union[linkml_runtime.linkml_model.meta.SlotDefinitionName, str], imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]
Parameters
  • slot_name – parent slot name

  • imports – include import closure

  • mixins – include mixins (default is True)

  • is_a – include is_a parents (default is True)

Returns

all direct child slot names (is_a and mixins)

slot_leaves(imports=True, mixins=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]

All slotes that have no children :param imports: :param mixins: :return:

slot_parents(slot_name: Union[linkml_runtime.linkml_model.meta.SlotDefinitionName, str], imports=True, mixins=True, is_a=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]
Parameters
  • slot_name – child slot name

  • imports – include import closure

  • mixins – include mixins (default is True)

Returns

all direct parent slot names (is_a and mixins)

slot_roots(imports=True, mixins=True) List[linkml_runtime.linkml_model.meta.SlotDefinitionName][source]

All slotes that have no parents :param imports: :param mixins: :return:

usage_index() Dict[linkml_runtime.linkml_model.meta.ElementName, List[linkml_runtime.utils.schemaview.SchemaUsage]][source]
Returns

dictionary keyed by used elements