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 2 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
44 changes: 20 additions & 24 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.context import ensure_converter
from sssom.typehints import Metadata
from sssom.util import MappingSetDataFrame, to_mapping_set_dataframe
from sssom_schema import Mapping, MappingSet
from sssom.util import MappingSetDataFrame
from sssom_schema import Mapping

from oaklib.datamodels.lexical_index import (
LexicalGrouping,
Expand Down Expand Up @@ -245,7 +246,7 @@ def lexical_index_to_sssom(
lexical_index: LexicalIndex,
ruleset: MappingRuleCollection = None,
meta: Metadata = None,
prefix_map: dict = 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,19 @@ 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.prefix_map) if meta is not None else Converter([]),
oi.converter,
ensure_converter(prefix_map, use_defaults=False),
]
)
msdf = MappingSetDataFrame.from_mappings(
mappings=mappings, metadata=meta and meta.metadata, converter=converter
)
msdf.clean_prefix_map(strict=ensure_strict_prefixes)
return msdf


Expand Down
16 changes: 4 additions & 12 deletions src/oaklib/utilities/mapping/sssom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
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
Expand Down Expand Up @@ -74,14 +73,7 @@ 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
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)
Loading