99from collections import defaultdict
1010from collections .abc import Iterable , Mapping , Sequence
1111from pathlib import Path
12- from typing import TYPE_CHECKING , Callable , NamedTuple , overload
12+ from typing import TYPE_CHECKING , Any , Callable , NamedTuple , overload
1313
1414from bioregistry import NormalizedNamableReference , NormalizedNamedReference
1515from 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
199203def _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-
254248def 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