Skip to content

Commit c4431b8

Browse files
author
Richard Liang
committed
Closes #9.
1 parent f4f49f0 commit c4431b8

File tree

7 files changed

+550
-281
lines changed

7 files changed

+550
-281
lines changed

src/hla_algorithm/hla_algorithm.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
BIN2NUC,
2424
HLA_LOCUS,
2525
StoredHLAStandards,
26+
allele_coordinates_sort_key,
2627
count_strict_mismatches,
2728
nuc2bin,
29+
sort_allele_pairs,
2830
)
2931

3032
DATE_FORMAT = "%a %b %d %H:%M:%S %Z %Y"
@@ -277,7 +279,13 @@ def combine_standards_stepper(
277279
# that looks like what you get when you sequence HLA.
278280
std_bin = np.array(std_b.sequence) | np.array(std_a.sequence)
279281
allele_pair: tuple[str, str] = cast(
280-
tuple[str, str], tuple(sorted((std_a.allele, std_b.allele)))
282+
tuple[str, str],
283+
tuple(
284+
sorted(
285+
(std_a.allele, std_b.allele),
286+
key=allele_coordinates_sort_key,
287+
)
288+
),
281289
)
282290

283291
# There could be more than one combined standard with the
@@ -363,7 +371,7 @@ def combine_standards(
363371
if mismatch_count <= cutoff:
364372
combined_std: HLACombinedStandard = HLACombinedStandard(
365373
standard_bin=combined_std_bin,
366-
possible_allele_pairs=tuple(sorted(pair_list)),
374+
possible_allele_pairs=tuple(sort_allele_pairs(pair_list)),
367375
)
368376
result[combined_std] = mismatch_count
369377

src/hla_algorithm/interpret_from_json_lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
check_bases,
1919
check_length,
2020
nuc2bin,
21+
sort_allele_pairs,
2122
)
2223

2324

@@ -143,7 +144,7 @@ def build_from_interpretation(
143144

144145
return HLAResult(
145146
seqs=seqs,
146-
alleles_all=[f"{x[0]} - {x[1]}" for x in aps.sort_pairs()],
147+
alleles_all=[f"{x[0]} - {x[1]}" for x in sort_allele_pairs(aps.allele_pairs)],
147148
alleles_clean=alleles_clean,
148149
alleles_for_mismatches=f"{rep_ap[0]} - {rep_ap[1]}",
149150
mismatches=[str(x) for x in match_details.mismatches],

src/hla_algorithm/models.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
HLA_LOCUS,
1111
HLARawStandard,
1212
allele_coordinates,
13-
allele_coordinates_sort_key,
1413
bin2nuc,
1514
count_forgiving_mismatches,
1615
nuc2bin,
16+
sort_allele_pairs,
1717
)
1818

1919

@@ -389,20 +389,6 @@ def best_common_allele_pair_str(
389389
clean_allele_pair_str: str = " - ".join(clean_allele)
390390
return (clean_allele_pair_str, set(unambiguous_aps.allele_pairs))
391391

392-
def sort_pairs(self) -> list[tuple[str, str]]:
393-
"""
394-
Sort the pairs according to "coordinate order".
395-
396-
If there's a tie, a last letter is used to attempt to break the tie.
397-
"""
398-
return sorted(
399-
self.allele_pairs,
400-
key=lambda pair: (
401-
allele_coordinates_sort_key(pair[0]),
402-
allele_coordinates_sort_key(pair[1]),
403-
),
404-
)
405-
406392
def stringify(self, sorted=True, max_length: int = 3900) -> str:
407393
"""
408394
Produce a final outputtable string.
@@ -415,7 +401,7 @@ def stringify(self, sorted=True, max_length: int = 3900) -> str:
415401
"""
416402
allele_pairs: list[tuple[str, str]] = self.allele_pairs
417403
if sorted:
418-
allele_pairs = self.sort_pairs()
404+
allele_pairs = sort_allele_pairs(self.allele_pairs)
419405
summary_str: str = ";".join([f"{_a[0]} - {_a[1]}" for _a in allele_pairs])
420406
if len(summary_str) > max_length:
421407
summary_str = re.sub(
@@ -441,7 +427,7 @@ def get_allele_pairs(
441427
all_allele_pairs: list[tuple[str, str]] = []
442428
for combined_std in combined_standards:
443429
all_allele_pairs.extend(combined_std.possible_allele_pairs)
444-
all_allele_pairs.sort()
430+
all_allele_pairs = sort_allele_pairs(all_allele_pairs)
445431
return cls(allele_pairs=all_allele_pairs)
446432

447433
def contains_allele(self, allele_name: str) -> bool:

src/hla_algorithm/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ def allele_coordinates_sort_key(allele: str) -> tuple[tuple[int, ...], str]:
368368
return (integer_part, letters_at_end)
369369

370370

371+
def allele_pair_sort_key(pair: tuple[str, str]) -> tuple[
372+
tuple[int, ...], str, tuple[int, ...], str
373+
]:
374+
"""
375+
Produce a sortable key for an allele pair.
376+
377+
Pairs should be sorted according to "coordinate order".
378+
If there's a tie, a last letter is used to attempt to break the tie.
379+
"""
380+
return (
381+
allele_coordinates_sort_key(pair[0])
382+
+ allele_coordinates_sort_key(pair[1])
383+
)
384+
385+
386+
def sort_allele_pairs(allele_pairs: Iterable[tuple[str, str]]) -> list[tuple[str, str]]:
387+
"""
388+
Sort the pairs according to "coordinate order".
389+
"""
390+
return sorted(allele_pairs, key=allele_pair_sort_key)
391+
392+
371393
class HLARawStandard(BaseModel):
372394
allele: str
373395
exon2: str

0 commit comments

Comments
 (0)