Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 4 additions & 7 deletions src/hla_algorithm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,14 @@ def _identify_longest_prefix(allele_prefixes: list[GeneCoord]) -> GeneCoord:
"""
Identify the longest gene coordinate "prefix" in the given allele prefixes.

Precondition: that the input must all share at least the same first
coordinate. The algorithm may not return cogent values if not.

Precondition: the specified allele prefixes do not all perfectly match,
so we lose nothing by trimming one coordinate off the end of all of
them.
Precondition: all allele prefixes in the input must all share at least
the same first coordinate. The algorithm may not return cogent values
if not.
"""
longest_prefix: GeneCoord = ()
if len(allele_prefixes) > 0:
max_length: int = max([len(allele) for allele in allele_prefixes])
for i in range(max_length - 1, 0, -1):
for i in range(max_length, 0, -1):
curr_prefixes: set[GeneCoord] = {allele[0:i] for allele in allele_prefixes}
if len(curr_prefixes) == 1:
longest_prefix = curr_prefixes.pop()
Expand Down
32 changes: 30 additions & 2 deletions tests/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,31 @@ def test_identify_clean_prefix_in_pairs(
(),
id="trivial_case",
),
# Note: we have no single allele tests because that contradicts one
# of our preconditions.
pytest.param(
[("C*01", "02", "03", "04G")],
("C*01", "02", "03", "04G"),
id="single_input_length_4",
),
pytest.param(
[("C*01", "02", "03")],
("C*01", "02", "03"),
id="single_input_length_3",
),
pytest.param(
[("C*01", "02")],
("C*01", "02"),
id="single_input_length_2",
),
pytest.param(
[("C*01",)],
("C*01",),
id="single_input_length_1",
),
pytest.param(
[("C*01", "02", "03", "04G"), ("C*01", "02", "03", "04G")],
("C*01", "02", "03", "04G"),
id="best_match_length_4",
),
pytest.param(
[("C*01", "02", "03", "04G"), ("C*01", "02", "03", "110N")],
("C*01", "02", "03"),
Expand Down Expand Up @@ -1248,6 +1271,11 @@ def test_identify_clean_prefix_in_pairs(
("C*01",),
id="best_match_length_1_different_lengths_one_with_no_excess",
),
pytest.param(
[("C*01", "07", "88"), ("C*01", "07", "01"), ("C*01", "07", "01", "110N")],
("C*01", "07"),
id="typical_case",
),
],
)
def test_identify_longest_prefix(
Expand Down