Manipulating Schemas — linkml documentation

# 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)

  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:

    - https://github.com/linkml/linkml/issues/59

    - https://github.com/linkml/linkml/discussions/144

    - https://github.com/linkml/linkml/issues/48

    - https://github.com/linkml/linkml/issues/270

  add_class(cls: linkml_runtime.linkml_model.meta.ClassDefinition) → None
    Parameters

      cls – class to be added

    Returns

  add_enum(enum: linkml_runtime.linkml_model.meta.EnumDefinition) → None
    Parameters

      enum – enum to be added

    Returns

  add_slot(slot: linkml_runtime.linkml_model.meta.SlotDefinition) → None
    Parameters

      slot – slot to be added

    Returns

  add_subset(subset: linkml_runtime.linkml_model.meta.SubsetDefinition) → None
    Parameters

      subset – subset to be added

    Returns

  add_type(type: linkml_runtime.linkml_model.meta.TypeDefinition) → None
    Parameters

      type – type to be added

    Returns

  all_class(imports=True) → Dict[linkml_runtime.linkml_model.meta.ClassDefinitionName, linkml_runtime.linkml_model.meta.ClassDefinition]
    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]
    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]
    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]
    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]
    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]
    Parameters

      imports – include imports closure

    Returns

      all enums in schema view

  all_schema(imports: True) → List[linkml_runtime.linkml_model.meta.SchemaDefinition]
    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]
    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]
    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]
    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]
    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]
    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]
    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]

    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]

    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]
    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]

    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]

    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]
    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]

    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]
    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
    Parameters

      class_name – class to be deleted

    Returns

  delete_enum(enum_name: linkml_runtime.linkml_model.meta.EnumDefinitionName) → None
    Parameters

      enum_name – enum to be deleted

    Returns

  delete_slot(slot_name: linkml_runtime.linkml_model.meta.SlotDefinitionName) → None
    Parameters

      slot_name – slot to be deleted

    Returns

  delete_subset(subset_name: linkml_runtime.linkml_model.meta.SubsetDefinitionName) → None
    Parameters

      subset_name – subset to be deleted

    Returns

  delete_type(type_name: linkml_runtime.linkml_model.meta.TypeDefinitionName) → None
    Parameters

      type_name – type to be deleted

    Returns

  expand_curie(uri: str) → str

    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
    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

    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
    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]
    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]]]

    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]]

    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
    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
    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
    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

    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]

    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
    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

    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

    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

    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

    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]

    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]
    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]

    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]
    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]

    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]]
    Returns

      dictionary keyed by used elements