Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/hla_algorithm/interpret_from_json_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 10 additions & 6 deletions src/hla_algorithm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ 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
allele_2: str

@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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion src/hla_algorithm/reformat_old_alleles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions src/hla_algorithm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]]:
Expand Down
2 changes: 1 addition & 1 deletion tests/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down