Skip to content

Commit 8b02a2f

Browse files
authored
Simplify data structure (#211)
1 parent 86ddca8 commit 8b02a2f

6 files changed

Lines changed: 171 additions & 166 deletions

File tree

src/biomappings/contribute/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@ def get_curated_mappings(prefix: str) -> list[SemanticMapping]:
1616
if mapping.subject.prefix == prefix:
1717
mappings.append(mapping)
1818
elif mapping.object.prefix == prefix:
19-
mappings.append(mapping.flip())
19+
mappings.append(_flip(mapping))
2020
return mappings
21+
22+
23+
def _flip(mapping: SemanticMapping) -> SemanticMapping:
24+
"""Flip the mapping, if it's an exact match."""
25+
if mapping.predicate.curie != "skos:exactMatch":
26+
raise NotImplementedError
27+
return mapping.model_copy(
28+
update={
29+
"subject": mapping.object,
30+
"object": mapping.subject,
31+
}
32+
)

src/biomappings/lexical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from biomappings import SemanticMapping
2323
from biomappings.mapping_graph import get_mutual_mapping_filter
24-
from biomappings.resources import append_prediction_tuples, mapping_sort_key
24+
from biomappings.resources import append_prediction_tuples
2525
from biomappings.utils import EXACT_MATCH, LEXICAL_MATCHING_PROCESS, CMapping, get_script_url
2626

2727
__all__ = [
@@ -126,7 +126,7 @@ def append_lexical_predictions(
126126
if custom_filter_function:
127127
predictions = list(filter(custom_filter_function, predictions))
128128

129-
predictions = sorted(predictions, key=mapping_sort_key)
129+
predictions = sorted(predictions)
130130
tqdm.write(f"[{prefix}] generated {len(predictions):,} predictions")
131131

132132
# since the function that constructs the predictions already

src/biomappings/resources/__init__.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import defaultdict
1010
from collections.abc import Iterable, Mapping, Sequence
1111
from pathlib import Path
12-
from typing import TYPE_CHECKING, Callable, NamedTuple, overload
12+
from typing import TYPE_CHECKING, Any, Callable, NamedTuple, overload
1313

1414
from bioregistry import NormalizedNamableReference, NormalizedNamedReference
1515
from curies import NamableReference
@@ -140,17 +140,6 @@ class SemanticMapping(BaseModel):
140140
""",
141141
)
142142

143-
def flip(self) -> Self:
144-
"""Flip the mapping, if it's an exact match."""
145-
if self.predicate.curie != "skos:exactMatch":
146-
raise NotImplementedError
147-
return self.model_copy(
148-
update={
149-
"subject": self.object,
150-
"object": self.subject,
151-
}
152-
)
153-
154143
def as_curated_row(self) -> _CuratedTuple:
155144
"""Get a row for a curated SSSOM TSV."""
156145
return _CuratedTuple(
@@ -195,6 +184,21 @@ def as_predicted_row(self) -> _PredictedTuple:
195184
self.mapping_tool or "",
196185
)
197186

187+
def __lt__(self, other: Any) -> bool:
188+
if not isinstance(other, SemanticMapping):
189+
raise TypeError
190+
return self._sort_key() < other._sort_key()
191+
192+
def _sort_key(self: SemanticMapping) -> tuple[str, ...]:
193+
"""Return a tuple for sorting mapping dictionaries."""
194+
return (
195+
self.subject.curie,
196+
self.predicate.curie,
197+
self.object.curie,
198+
self.mapping_justification.curie,
199+
self.mapping_tool or "",
200+
)
201+
198202

199203
def _load_table(path: str | Path, *, standardize: bool) -> list[SemanticMapping]:
200204
reference_cls: type[NamableReference]
@@ -223,7 +227,8 @@ def _write_helper(
223227
mode: Literal["w", "a"],
224228
t: Literal["curated", "predicted"],
225229
) -> None:
226-
mappings = sorted(set(mappings), key=mapping_sort_key)
230+
mappings = _remove_redundant(mappings)
231+
mappings = sorted(mappings)
227232
header: Sequence[str]
228233
to_row: Callable[[SemanticMapping], _PredictedTuple | _CuratedTuple]
229234
if t == "curated":
@@ -240,17 +245,6 @@ def _write_helper(
240245
print(*to_row(mapping), sep="\t", file=file)
241246

242247

243-
def mapping_sort_key(mapping: SemanticMapping) -> tuple[str, ...]:
244-
"""Return a tuple for sorting mapping dictionaries."""
245-
return (
246-
mapping.subject.curie,
247-
mapping.predicate.curie,
248-
mapping.object.curie,
249-
mapping.mapping_justification.curie,
250-
mapping.mapping_tool or "",
251-
)
252-
253-
254248
def load_mappings(
255249
*, path: str | Path | None = None, standardize: bool = False
256250
) -> list[SemanticMapping]:
@@ -448,7 +442,7 @@ def lint_predictions(
448442
),
449443
)
450444
mappings = _remove_redundant(mappings)
451-
mappings = sorted(mappings, key=mapping_sort_key)
445+
mappings = sorted(mappings)
452446
write_predictions(mappings, path=path)
453447

454448

0 commit comments

Comments
 (0)