From 16d038eb31f04bd60906e5b300dc29d80141f607 Mon Sep 17 00:00:00 2001 From: rhliang Date: Fri, 15 May 2026 17:58:40 -0700 Subject: [PATCH 1/3] Fixed a typo ("7501" should have been "5701"). --- src/hla_algorithm/interpret_from_json_lib.py | 2 +- src/hla_algorithm/models.py | 16 ++++++++++------ tests/models_test.py | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/hla_algorithm/interpret_from_json_lib.py b/src/hla_algorithm/interpret_from_json_lib.py index 1be5341..79fb807 100644 --- a/src/hla_algorithm/interpret_from_json_lib.py +++ b/src/hla_algorithm/interpret_from_json_lib.py @@ -156,7 +156,7 @@ def build_from_interpretation( alleles_version=alleles_version, alleles_last_updated=alleles_last_updated, b5701=interp.is_b5701(), - dist_b5701=interp.distance_from_b7501(), + dist_b5701=interp.distance_from_b5701(), all_mismatches={ cs.get_allele_pair_str(): HLAMatchAdaptor.from_match_details(match) for cs, match in interp.matches.items() diff --git a/src/hla_algorithm/models.py b/src/hla_algorithm/models.py index 19ae05c..04eff0d 100644 --- a/src/hla_algorithm/models.py +++ b/src/hla_algorithm/models.py @@ -84,6 +84,7 @@ class MatchingAllelePair(BaseModel): - the number of mismatches identified; and - the allele pair (i.e. names of the two alleles in the combination). """ + standard_bin: tuple[int, ...] mismatch_count: int allele_1: str @@ -91,7 +92,9 @@ class MatchingAllelePair(BaseModel): @model_validator(mode="after") def check_alleles_ordered(self) -> Self: - if allele_coordinates_sort_key(self.allele_1) > allele_coordinates_sort_key(self.allele_2): + if allele_coordinates_sort_key(self.allele_1) > allele_coordinates_sort_key( + self.allele_2 + ): raise ValueError("allele_1 should be less than or equal to allele_2") return self @@ -474,7 +477,9 @@ def _identify_longest_prefix(allele_prefixes: list[GeneCoord]) -> GeneCoord: if len(allele_prefixes) > 0: max_length: int = max([len(allele) for allele in allele_prefixes]) for i in range(max_length, 0, -1): - curr_prefixes: set[GeneCoord] = {allele[0:i] for allele in allele_prefixes} + curr_prefixes: set[GeneCoord] = { + allele[0:i] for allele in allele_prefixes + } if len(curr_prefixes) == 1: longest_prefix = curr_prefixes.pop() if i > 1: @@ -522,9 +527,8 @@ def best_common_allele_pair_str( ) second_prefix: GeneCoord = ( - intermediate_data.second_prefix or self._identify_longest_prefix( - intermediate_data.remaining_prefixes - ) + intermediate_data.second_prefix + or self._identify_longest_prefix(intermediate_data.remaining_prefixes) ) # Turn the two prefixes we found into strings and strip any trailing @@ -640,7 +644,7 @@ def best_common_allele_pair( ap_to_cs[best_representative], ) - def distance_from_b7501(self) -> Optional[int]: + def distance_from_b5701(self) -> Optional[int]: """ Return the Hamming distance from this sequence to B*57:01. diff --git a/tests/models_test.py b/tests/models_test.py index 43dcbf4..6a0aa77 100644 --- a/tests/models_test.py +++ b/tests/models_test.py @@ -2529,7 +2529,7 @@ def test_distance_from_b5701( allele_frequencies={}, b5701_standards=b5701_standards, ) - assert interp.distance_from_b7501() == expected_result + assert interp.distance_from_b5701() == expected_result @pytest.mark.parametrize( "raw_matches, expected_result", From 60206dd8db5fc40e0ffdac159d1a161ea75eb6de Mon Sep 17 00:00:00 2001 From: Richard Liang Date: Tue, 19 May 2026 12:49:09 -0700 Subject: [PATCH 2/3] Fixed a typo ("7501" rather than "5701"), and forced the return type of `count_forgiving_mismatches` to be a Python integer. --- src/hla_algorithm/interpret_from_json_lib.py | 2 +- src/hla_algorithm/models.py | 16 ++++++++++------ src/hla_algorithm/utils.py | 13 +++++-------- tests/models_test.py | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/hla_algorithm/interpret_from_json_lib.py b/src/hla_algorithm/interpret_from_json_lib.py index 1be5341..79fb807 100644 --- a/src/hla_algorithm/interpret_from_json_lib.py +++ b/src/hla_algorithm/interpret_from_json_lib.py @@ -156,7 +156,7 @@ def build_from_interpretation( alleles_version=alleles_version, alleles_last_updated=alleles_last_updated, b5701=interp.is_b5701(), - dist_b5701=interp.distance_from_b7501(), + dist_b5701=interp.distance_from_b5701(), all_mismatches={ cs.get_allele_pair_str(): HLAMatchAdaptor.from_match_details(match) for cs, match in interp.matches.items() diff --git a/src/hla_algorithm/models.py b/src/hla_algorithm/models.py index 19ae05c..9ff318a 100644 --- a/src/hla_algorithm/models.py +++ b/src/hla_algorithm/models.py @@ -84,6 +84,7 @@ class MatchingAllelePair(BaseModel): - the number of mismatches identified; and - the allele pair (i.e. names of the two alleles in the combination). """ + standard_bin: tuple[int, ...] mismatch_count: int allele_1: str @@ -91,7 +92,9 @@ class MatchingAllelePair(BaseModel): @model_validator(mode="after") def check_alleles_ordered(self) -> Self: - if allele_coordinates_sort_key(self.allele_1) > allele_coordinates_sort_key(self.allele_2): + if allele_coordinates_sort_key(self.allele_1) > allele_coordinates_sort_key( + self.allele_2 + ): raise ValueError("allele_1 should be less than or equal to allele_2") return self @@ -474,7 +477,9 @@ def _identify_longest_prefix(allele_prefixes: list[GeneCoord]) -> GeneCoord: if len(allele_prefixes) > 0: max_length: int = max([len(allele) for allele in allele_prefixes]) for i in range(max_length, 0, -1): - curr_prefixes: set[GeneCoord] = {allele[0:i] for allele in allele_prefixes} + curr_prefixes: set[GeneCoord] = { + allele[0:i] for allele in allele_prefixes + } if len(curr_prefixes) == 1: longest_prefix = curr_prefixes.pop() if i > 1: @@ -522,9 +527,8 @@ def best_common_allele_pair_str( ) second_prefix: GeneCoord = ( - intermediate_data.second_prefix or self._identify_longest_prefix( - intermediate_data.remaining_prefixes - ) + intermediate_data.second_prefix + or self._identify_longest_prefix(intermediate_data.remaining_prefixes) ) # Turn the two prefixes we found into strings and strip any trailing @@ -640,7 +644,7 @@ def best_common_allele_pair( ap_to_cs[best_representative], ) - def distance_from_b7501(self) -> Optional[int]: + def distance_from_b5701(self) -> int | None: """ Return the Hamming distance from this sequence to B*57:01. diff --git a/src/hla_algorithm/utils.py b/src/hla_algorithm/utils.py index fb263ca..5996e45 100644 --- a/src/hla_algorithm/utils.py +++ b/src/hla_algorithm/utils.py @@ -133,7 +133,7 @@ def count_forgiving_mismatches( overlaps: np.ndarray = seq1_np & seq2_np matches: np.ndarray = (overlaps == seq1_np) | (overlaps == seq2_np) - return np.count_nonzero(matches == False) + return int(np.count_nonzero(matches == False)) class InvalidBaseException(Exception): @@ -368,19 +368,16 @@ def allele_coordinates_sort_key(allele: str) -> tuple[tuple[int, ...], str]: return (integer_part, letters_at_end) -def allele_pair_sort_key(pair: tuple[str, str]) -> tuple[ - tuple[int, ...], str, tuple[int, ...], str -]: +def allele_pair_sort_key( + pair: tuple[str, str], +) -> tuple[tuple[int, ...], str, tuple[int, ...], str]: """ Produce a sortable key for an allele pair. Pairs should be sorted according to "coordinate order". If there's a tie, a last letter is used to attempt to break the tie. """ - return ( - allele_coordinates_sort_key(pair[0]) - + allele_coordinates_sort_key(pair[1]) - ) + return allele_coordinates_sort_key(pair[0]) + allele_coordinates_sort_key(pair[1]) def sort_allele_pairs(allele_pairs: Iterable[tuple[str, str]]) -> list[tuple[str, str]]: diff --git a/tests/models_test.py b/tests/models_test.py index 43dcbf4..6a0aa77 100644 --- a/tests/models_test.py +++ b/tests/models_test.py @@ -2529,7 +2529,7 @@ def test_distance_from_b5701( allele_frequencies={}, b5701_standards=b5701_standards, ) - assert interp.distance_from_b7501() == expected_result + assert interp.distance_from_b5701() == expected_result @pytest.mark.parametrize( "raw_matches, expected_result", From 3013a794eb5421d9cc677e21c3bef59bba39112a Mon Sep 17 00:00:00 2001 From: Richard Liang Date: Tue, 19 May 2026 13:19:36 -0700 Subject: [PATCH 3/3] Fixed a linting error (un-updated type hint). --- src/hla_algorithm/reformat_old_alleles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hla_algorithm/reformat_old_alleles.py b/src/hla_algorithm/reformat_old_alleles.py index 5557f13..6c96831 100644 --- a/src/hla_algorithm/reformat_old_alleles.py +++ b/src/hla_algorithm/reformat_old_alleles.py @@ -77,7 +77,7 @@ def main(): elif args.verbose > 1: logger.setLevel(logging.DEBUG) - input_filenames_by_locus: dict[HLA_LOCUS, str] = { + input_filenames_by_locus: dict[HLA_LOCUS, Path] = { "A": args.a_standards, "B": args.b_standards, "C": args.c_standards,