Skip to content

Constants

Constants, enums, and schema helpers used across sssom-py.

sssom.constants

Constants.

SEMAPV

Bases: Enum

SEMAPV Enum containing different mapping_justification.

See also: https://mapping-commons.github.io/semantic-mapping-vocabulary/#matchingprocess

Source code in src/sssom/constants.py
class SEMAPV(Enum):
    """SEMAPV Enum containing different mapping_justification.

    See also: https://mapping-commons.github.io/semantic-mapping-vocabulary/#matchingprocess
    """

    LexicalMatching = "semapv:LexicalMatching"
    LogicalReasoning = "semapv:LogicalReasoning"
    CompositeMatching = "semapv:CompositeMatching"
    UnspecifiedMatching = "semapv:UnspecifiedMatching"
    SemanticSimilarityThresholdMatching = "semapv:SemanticSimilarityThresholdMatching"
    LexicalSimilarityThresholdMatching = "semapv:LexicalSimilarityThresholdMatching"
    MappingChaining = "semapv:MappingChaining"
    MappingReview = "semapv:MappingReview"
    ManualMappingCuration = "semapv:ManualMappingCuration"
    MappingInversion = "semapv:MappingInversion"
    CrossSpeciesExactMatch = CROSS_SPECIES_EXACT_MATCH
    CrossSpeciesNarrowMatch = CROSS_SPECIES_NARROW_MATCH
    CrossSpeciesBroadMatch = CROSS_SPECIES_BROAD_MATCH

SchemaValidationType

Bases: str, Enum

Schema validation types.

Source code in src/sssom/constants.py
class SchemaValidationType(str, Enum):
    """Schema validation types."""

    # TODO move this class into validators.py
    JsonSchema = "JsonSchema"
    Shacl = "Shacl"
    Sparql = "Sparql"
    PrefixMapCompleteness = "PrefixMapCompleteness"
    StrictCurieFormat = "StrictCurieFormat"

NewEnumValue dataclass

Bases: object

Represents a enum value that had been added posteriorly to 1.0.

Ideally that information should be encoded in the LinkML schema and made available through the SSSOMSchemaView class below, but it does not seem possible to annotate enum values in LinkML the way it can be done for slots. So the information comes from the spec instead, at https://mapping-commons.github.io/sssom/spec-model/#model-changes-across-versions.

Source code in src/sssom/constants.py
@dataclass
class NewEnumValue(object):
    """Represents a enum value that had been added posteriorly to 1.0.

    Ideally that information should be encoded in the LinkML schema and
    made available through the SSSOMSchemaView class below, but it does
    not seem possible to annotate enum values in LinkML the way it can
    be done for slots. So the information comes from the spec instead,
    at <https://mapping-commons.github.io/sssom/spec-model/#model-changes-across-versions>.
    """

    slots: list[str]  # Impacted slots
    value: str  # The new value
    added_in: tuple[int, int]  # Version that introduced the new value

SSSOMSchemaView

Bases: object

SchemaView class from linkml which is instantiated when necessary.

Reason for this: https://github.com/mapping-commons/sssom-py/issues/322 Implemented via PR: https://github.com/mapping-commons/sssom-py/pull/323

Source code in src/sssom/constants.py
class SSSOMSchemaView(object):
    """SchemaView class from linkml which is instantiated when necessary.

    Reason for this: https://github.com/mapping-commons/sssom-py/issues/322 Implemented via PR:
    https://github.com/mapping-commons/sssom-py/pull/323
    """

    instance: ClassVar[SSSOMSchemaView]

    def __new__(cls) -> SSSOMSchemaView:
        """Create a instance of the SSSOM schema view if non-existent."""
        if not hasattr(cls, "instance"):
            cls.instance = super(SSSOMSchemaView, cls).__new__(cls)
        return cls.instance

    @cached_property
    def view(self) -> SchemaView:
        """Return SchemaView object."""
        return SchemaView(SCHEMA_YAML)

    @cached_property
    def dict(self) -> Dict[str, Any]:
        """Return SchemaView as a dictionary."""
        return schema_as_dict(self.view.schema)  # type: ignore

    @cached_property
    def mapping_slots(self) -> List[str]:
        """Return list of mapping slots."""
        return self.view.get_class("mapping").slots  # type: ignore

    @cached_property
    def mapping_set_slots(self) -> List[str]:
        """Return list of mapping set slots."""
        return cast(List[str], self.view.get_class("mapping set").slots)

    @cached_property
    def multivalued_slots(self) -> Set[str]:
        """Return set of multivalued slots."""
        return {c for c in self.view.all_slots() if self.view.get_slot(c).multivalued}

    @cached_property
    def entity_reference_slots(self) -> Set[str]:
        """Return set of entity reference slots."""
        return {c for c in self.view.all_slots() if self.view.get_slot(c).range == ENTITY_REFERENCE}

    @cached_property
    def mapping_enum_keys(self) -> Set[str]:
        """Return a set of mapping enum keys."""
        return set(_get_sssom_schema_object().dict["enums"].keys())

    @cached_property
    def slots(self) -> Dict[str, str]:
        """Return the slots for SSSOMSchemaView object."""
        return self.dict["slots"]  # type: ignore

    @cached_property
    def double_slots(self) -> Set[str]:
        """Return the slot names for SSSOMSchemaView object."""
        return {k for k, v in self.dict["slots"].items() if v["range"] == "double"}

    @cached_property
    def propagatable_slots(self) -> List[str]:
        """Return the names of all propagatable slots."""
        slots = []
        for slot_name in self.mapping_set_slots:
            annotations = self.view.annotation_dict(slot_name)
            if annotations is not None and "propagated" in annotations:
                slots.append(slot_name)
        return slots

    def get_new_enum_values(self, after: Tuple[int, int] = (1, 0)) -> List[NewEnumValue]:
        """Get enum values introduced after a given version of the specification.

        :param after: The target version of the SSSOM specification, as
                      a (major, minor) tuple. The default is (1,0),
                      meaning all enum values introduced in any version
                      after 1.0 will be returned.
        :return: The list of newly introduced enum values.
        """
        return [v for v in NEW_ENUM_VALUES if v.added_in > after]

    def get_minimum_version(
        self, slot_name: str, class_name: str = "mapping"
    ) -> Optional[Tuple[int, int]]:
        """Get the minimum version of SSSOM required for a given slot.

        :param slot_name: The queried slot.
        :param class_name: The class the slot belongs to. This is needed
                           because a slot may have been added to a class
                           in a later version than the version in which
                           it was first introduced in the schema.
        :return: A tuple containing the major and minor numbers of the
                 earliest version of SSSOM that defines the given slot
                 in the given class. May be None if the requested slot
                 name is not a valid slot name.
        """
        try:
            slot = self.view.induced_slot(slot_name, class_name)
            return parse_sssom_version(slot.annotations.added_in.value)
        except AttributeError:  # No added_in annotation, defaults to 1.0
            return (1, 0)
        except ValueError:  # No such slot
            return None
view cached property

Return SchemaView object.

dict cached property

Return SchemaView as a dictionary.

mapping_slots cached property

Return list of mapping slots.

mapping_set_slots cached property

Return list of mapping set slots.

multivalued_slots cached property

Return set of multivalued slots.

entity_reference_slots cached property

Return set of entity reference slots.

mapping_enum_keys cached property

Return a set of mapping enum keys.

slots cached property

Return the slots for SSSOMSchemaView object.

double_slots cached property

Return the slot names for SSSOMSchemaView object.

propagatable_slots cached property

Return the names of all propagatable slots.

__new__()

Create a instance of the SSSOM schema view if non-existent.

Source code in src/sssom/constants.py
def __new__(cls) -> SSSOMSchemaView:
    """Create a instance of the SSSOM schema view if non-existent."""
    if not hasattr(cls, "instance"):
        cls.instance = super(SSSOMSchemaView, cls).__new__(cls)
    return cls.instance
get_new_enum_values(after=(1, 0))

Get enum values introduced after a given version of the specification.

:param after: The target version of the SSSOM specification, as a (major, minor) tuple. The default is (1,0), meaning all enum values introduced in any version after 1.0 will be returned. :return: The list of newly introduced enum values.

Source code in src/sssom/constants.py
def get_new_enum_values(self, after: Tuple[int, int] = (1, 0)) -> List[NewEnumValue]:
    """Get enum values introduced after a given version of the specification.

    :param after: The target version of the SSSOM specification, as
                  a (major, minor) tuple. The default is (1,0),
                  meaning all enum values introduced in any version
                  after 1.0 will be returned.
    :return: The list of newly introduced enum values.
    """
    return [v for v in NEW_ENUM_VALUES if v.added_in > after]
get_minimum_version(slot_name, class_name='mapping')

Get the minimum version of SSSOM required for a given slot.

:param slot_name: The queried slot. :param class_name: The class the slot belongs to. This is needed because a slot may have been added to a class in a later version than the version in which it was first introduced in the schema. :return: A tuple containing the major and minor numbers of the earliest version of SSSOM that defines the given slot in the given class. May be None if the requested slot name is not a valid slot name.

Source code in src/sssom/constants.py
def get_minimum_version(
    self, slot_name: str, class_name: str = "mapping"
) -> Optional[Tuple[int, int]]:
    """Get the minimum version of SSSOM required for a given slot.

    :param slot_name: The queried slot.
    :param class_name: The class the slot belongs to. This is needed
                       because a slot may have been added to a class
                       in a later version than the version in which
                       it was first introduced in the schema.
    :return: A tuple containing the major and minor numbers of the
             earliest version of SSSOM that defines the given slot
             in the given class. May be None if the requested slot
             name is not a valid slot name.
    """
    try:
        slot = self.view.induced_slot(slot_name, class_name)
        return parse_sssom_version(slot.annotations.added_in.value)
    except AttributeError:  # No added_in annotation, defaults to 1.0
        return (1, 0)
    except ValueError:  # No such slot
        return None

parse_sssom_version(version)

Parse a string into a valid SSSOM version number.

:param version: The string to parse into a version number. :return: A (major, minor) tuple.

Source code in src/sssom/constants.py
def parse_sssom_version(version: str) -> Tuple[int, int]:
    """Parse a string into a valid SSSOM version number.

    :param version: The string to parse into a version number.
    :return: A (major, minor) tuple.
    """
    v = [int(n) for n in SssomVersionEnum(version).code.text.split(".")]
    if len(v) != 2:
        # Should never happen, should be caught by the SssomVersionEnum
        # constructor before we arrive here
        raise ValueError("Invalid version")
    return (v[0], v[1])

generate_mapping_set_id()

Generate a mapping set ID.

Source code in src/sssom/constants.py
def generate_mapping_set_id() -> str:
    """Generate a mapping set ID."""
    return f"{SSSOM_URI_PREFIX}mappings/{uuid.uuid4()}"

get_default_metadata()

Get default metadata.

:returns: A metadata dictionary containing a default license with value :data:DEFAULT_LICENSE and an auto-generated mapping set ID

If you want to combine some metadata you loaded but ensure that there is also default metadata, the best tool is :class:collections.ChainMap. You can do:

.. code-block:: python

my_metadata: dict | None = ...

from collections import ChainMap
from sssom import get_default_metadata

metadata = dict(ChainMap(my_metadata or {}, get_default_metadata()))
Source code in src/sssom/constants.py
def get_default_metadata() -> MetadataType:
    """Get default metadata.

    :returns: A metadata dictionary containing a default license with value :data:`DEFAULT_LICENSE`
        and an auto-generated mapping set ID

    If you want to combine some metadata you loaded but ensure that there is also default metadata,
    the best tool is :class:`collections.ChainMap`. You can do:

    .. code-block:: python

        my_metadata: dict | None = ...

        from collections import ChainMap
        from sssom import get_default_metadata

        metadata = dict(ChainMap(my_metadata or {}, get_default_metadata()))
    """
    return {
        "mapping_set_id": generate_mapping_set_id(),
        "license": DEFAULT_LICENSE,
    }