Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve usage of SSSOM-py #666

Merged
merged 19 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ linkml-runtime = ">=1.5.3"
linkml-renderer = ">=0.3.0"
networkx = ">=2.7.1"
sssom-schema = ">=0.11.0"
sssom = ">=0.3.38"
sssom = ">=0.4.0"
cthoyt marked this conversation as resolved.
Show resolved Hide resolved
ratelimit = ">=2.2.1"
appdirs = ">=1.4.4"
semsql = ">=0.3.1"
Expand Down
4 changes: 1 addition & 3 deletions src/oaklib/interfaces/basic_ontology_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ def set_metamodel_mappings(self, mappings: Union[str, Path, List[Mapping]]) -> N
:param mappings: Mappings for predicates such as rdfs:subClassOf
:return:
"""
if isinstance(mappings, Path):
mappings = str
if isinstance(mappings, str):
if isinstance(mappings, (str, Path)):
msdf = parse_sssom_table(mappings)
msd = to_mapping_set_document(msdf)
mappings = msd.mapping_set.mappings
Expand Down
46 changes: 20 additions & 26 deletions src/oaklib/utilities/lexical/lexical_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
"""
import logging
import re
import typing as t
from collections import defaultdict
from pathlib import Path
from typing import Collection, Dict, List, Optional, Tuple, Union

import curies
from curies import Converter
from linkml_runtime.dumpers import json_dumper, yaml_dumper
from linkml_runtime.loaders import json_loader, yaml_loader
from linkml_runtime.utils.metamodelcore import URIorCURIE
from sssom.constants import LICENSE, MAPPING_SET_ID
from sssom.context import get_default_metadata
from sssom.sssom_document import MappingSetDocument
from sssom.typehints import Metadata
from sssom.util import MappingSetDataFrame, to_mapping_set_dataframe
from sssom_schema import Mapping, MappingSet
from sssom.constants import CURIE_MAP
from sssom.context import ensure_converter
from sssom.util import MappingSetDataFrame
from sssom_schema import Mapping

from oaklib.datamodels.lexical_index import (
LexicalGrouping,
Expand Down Expand Up @@ -244,8 +245,8 @@ def lexical_index_to_sssom(
oi: BasicOntologyInterface,
lexical_index: LexicalIndex,
ruleset: MappingRuleCollection = None,
meta: Metadata = None,
prefix_map: dict = None,
meta: Optional[Dict[str, t.Any]] = None,
prefix_map: Union[None, Converter, t.Mapping[str, str]] = None,
subjects: Collection[CURIE] = None,
objects: Collection[CURIE] = None,
symmetric: bool = False,
Expand Down Expand Up @@ -310,24 +311,17 @@ def lexical_index_to_sssom(
# mappings.append(create_mapping(oi, term, r1, r2))
logging.info("Done creating SSSOM mappings")

if meta is None:
meta = get_default_metadata()
if prefix_map:
meta.prefix_map.update(
{k: v for k, v in prefix_map.items() if k not in meta.prefix_map.keys()}
)
mapping_set_id = meta.metadata[MAPPING_SET_ID]
license = meta.metadata[LICENSE]

mset = MappingSet(mapping_set_id=mapping_set_id, mappings=mappings, license=license)
# doc = MappingSetDocument(prefix_map=oi.prefix_map(), mapping_set=mset)
doc = MappingSetDocument(prefix_map=meta.prefix_map, mapping_set=mset)
msdf = to_mapping_set_dataframe(doc)
num_mappings = len(msdf.df.index)
if ensure_strict_prefixes:
msdf.clean_prefix_map()
if len(msdf.df.index) < num_mappings:
raise ValueError("Mappings included prefixes that were not in the prefix map")
# TODO what's the business logic for combining these three
# different streams of prefix maps?
converter = curies.chain(
cthoyt marked this conversation as resolved.
Show resolved Hide resolved
[
Converter.from_prefix_map((meta or {}).pop(CURIE_MAP, {})),
oi.converter,
ensure_converter(prefix_map, use_defaults=False),
]
)
msdf = MappingSetDataFrame.from_mappings(mappings=mappings, metadata=meta, converter=converter)
msdf.clean_prefix_map(strict=ensure_strict_prefixes)
return msdf


Expand Down
20 changes: 6 additions & 14 deletions src/oaklib/utilities/mapping/sssom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
from typing import Iterable, List, Optional, Tuple

from linkml_runtime.utils.metamodelcore import URIorCURIE
from sssom.sssom_document import MappingSetDocument
from sssom.util import to_mapping_set_dataframe
from sssom.util import MappingSetDataFrame
from sssom.writers import write_table
from sssom_schema import Mapping, MappingSet
from sssom_schema import Mapping

from oaklib.io.streaming_writer import StreamingWriter
from oaklib.types import CURIE
from oaklib.utilities.basic_utils import get_curie_prefix

UNSPECIFIED_MAPPING_SET_ID = "https://w3id.org/sssom/license/unspecified"


def create_sssom_mapping(
subject_id: CURIE, object_id: CURIE, strict=False, **kwargs
Expand Down Expand Up @@ -74,14 +71,9 @@ def emit(self, obj: Mapping):
self.mappings.append(obj)

def finish(self):
mset = MappingSet(
mapping_set_id="temp", mappings=self.mappings, license=UNSPECIFIED_MAPPING_SET_ID
)
if self.ontology_interface:
prefix_map = self.ontology_interface.prefix_map()
else:
prefix_map = {}
doc = MappingSetDocument(prefix_map=prefix_map, mapping_set=mset)
msdf = to_mapping_set_dataframe(doc)
converter = self.ontology_interface.converter if self.ontology_interface else None
# default metadata, including an auto-generated mapping set URI and
# a license, are automatically added with this function
msdf = MappingSetDataFrame.from_mappings(self.mappings, converter=converter)
cthoyt marked this conversation as resolved.
Show resolved Hide resolved
msdf.clean_prefix_map(strict=False)
write_table(msdf, self.file)
7 changes: 4 additions & 3 deletions tests/test_utilities/test_sssom_matcher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from pathlib import Path

from sssom.io import get_metadata_and_prefix_map
import yaml
from sssom.parsers import to_mapping_set_document
from sssom.writers import write_table
from sssom_schema import Mapping
Expand Down Expand Up @@ -169,8 +169,9 @@ def test_sssom_with_rules_file(self):
lexical_index = create_lexical_index(oi)
add_labels_from_uris(oi)
ruleset = load_mapping_rules(str(RULES))
metadata = get_metadata_and_prefix_map(MATCHER_META_YML)
msdf = lexical_index_to_sssom(self.oi, lexical_index, ruleset=ruleset, meta=metadata)

meta = yaml.safe_load(MATCHER_META_YML.read_text())
msdf = lexical_index_to_sssom(self.oi, lexical_index, ruleset=ruleset, meta=meta)
with open(MATCHER_TEST_SSSOM_OUT, "w", encoding="utf-8") as file:
write_table(msdf, file)
msdoc = to_mapping_set_document(msdf)
Expand Down
Loading