Skip to content

Commit

Permalink
Update reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Apr 22, 2024
1 parent a407d95 commit fc0aeb7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/curies/reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections import Counter, defaultdict
from typing import Collection, List, Mapping, Optional, Tuple

from typing_extensions import Literal

from .api import Converter, Record

__all__ = [
Expand Down Expand Up @@ -34,7 +36,12 @@ def __str__(self) -> str:
)


def remap_curie_prefixes(converter: Converter, remapping: Mapping[str, str]) -> Converter:
def remap_curie_prefixes(
converter: Converter,
remapping: Mapping[str, str],
*,
intersection_resolution: Literal["overwrite", "drop"] = "drop",
) -> Converter:
"""Apply CURIE prefix remappings.
:param converter: A converter
Expand Down Expand Up @@ -68,9 +75,16 @@ def remap_curie_prefixes(converter: Converter, remapping: Mapping[str, str]) ->
new_record,
)
elif old in intersection:
record.prefix_synonyms = sorted(
set(record.prefix_synonyms).difference({old, new_prefix})
)
if intersection_resolution == "drop":
# throw away all synonyms from intersections,
# since there can be non-trivial overlaps
record.prefix_synonyms = []
elif intersection_resolution == "overwrite":
record.prefix_synonyms = sorted(
set(record.prefix_synonyms).difference({old, new_prefix})
)
else:
raise TypeError(f"invalid intersection resolution mode: {intersection_resolution}")
record.prefix = new_prefix
else:
record.prefix_synonyms = sorted(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def test_remapping_with_synonym(self):
)
self.assertEqual([r4, r3], c2.records)

def test_remapping_invalid_mode(self):
"""Test that remapping with synonym prefixes works as expected."""
r1 = Record(
prefix="geo", # also should not survive
prefix_synonyms=["GEO", "should_not_survive"],
uri_prefix="https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=",
)
r2 = Record(
prefix="geogeo",
prefix_synonyms=["GEOGEO"],
uri_prefix="http://purl.obolibrary.org/obo/GEO_",
)
c1 = Converter([r1, r2])
remapping = {
"GEO": "ncbi.geo",
"geogeo": "GEO",
}
with self.assertRaises(TypeError):
remap_curie_prefixes(c1, remapping, intersection_resolution="nope")

def test_cycles(self):
"""Test detecting bad mapping with cycles."""
converter = Converter(
Expand Down

0 comments on commit fc0aeb7

Please sign in to comment.