Skip to content

Commit 1dd6d93

Browse files
author
Richard Liang
committed
Cleaned up some unused imports and cleaned up the clinical HLA script's reporting of mismatches when there aren't any.
1 parent 7e68912 commit 1dd6d93

File tree

3 files changed

+128
-11
lines changed

3 files changed

+128
-11
lines changed

src/easyhla/clinical_hla_lib.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class HLASequenceCommonFields(TypedDict):
3737
ambiguous: str
3838
homozygous: str
3939
mismatch_count: int
40-
mismatches: str
40+
mismatches: Optional[str]
4141
enterdate: datetime
4242

4343

@@ -62,10 +62,13 @@ def get_common_serialization_fields(
6262
rep_cs: HLACombinedStandard
6363
rep_ap, alleles_clean, rep_cs = interpretation.best_common_allele_pair()
6464

65-
match_details: HLAMatchDetails = interpretation.matches[rep_cs]
66-
mismatches_str: str = f"({rep_ap[0]} - {rep_ap[1]}) " + ";".join(
67-
str(x) for x in match_details.mismatches
68-
)
65+
mismatches_str: Optional[str] = None
66+
mismatch_count: int = interpretation.lowest_mismatch_count()
67+
if mismatch_count > 0:
68+
match_details: HLAMatchDetails = interpretation.matches[rep_cs]
69+
mismatches_str = f"({rep_ap[0]} - {rep_ap[1]}) " + ";".join(
70+
str(x) for x in match_details.mismatches
71+
)
6972

7073
return {
7174
"enum": interpretation.hla_sequence.name,

src/easyhla/interpret_from_json.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
import argparse
44
import json
55
import logging
6-
from typing import Optional
76

87
from .easyhla import EasyHLA
98
from .interpret_from_json_lib import HLAInput, HLAResult
10-
from .models import (
11-
HLAInterpretation,
12-
HLAProteinPair,
13-
HLAStandard,
14-
)
9+
from .models import HLAInterpretation
1510

1611
logger: logging.Logger = logging.getLogger(__name__)
1712

tests/clinical_hla_lib_test.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ def dummy_matches(locus: HLA_LOCUS) -> dict[HLACombinedStandard, HLAMatchDetails
7373
}
7474

7575

76+
def dummy_matches_no_mismatches(
77+
locus: HLA_LOCUS,
78+
) -> dict[HLACombinedStandard, HLAMatchDetails]:
79+
return {
80+
HLACombinedStandard(
81+
standard_bin=(2, 2, 1, 2, 1, 4, 4, 2, 8),
82+
possible_allele_pairs=((f"{locus}*01:01:01", f"{locus}*02:02:02"),),
83+
): HLAMatchDetails(mismatch_count=0, mismatches=[]),
84+
}
85+
86+
7687
DUMMY_FREQUENCIES: Final[dict[HLAProteinPair, int]] = {
7788
HLAProteinPair(
7889
first_field_1="01",
@@ -119,6 +130,33 @@ def test_hla_sequence_a_build_from_interpretation():
119130
assert seq_a == expected_result
120131

121132

133+
def test_hla_sequence_a_build_from_interpretation_no_mismatches():
134+
interp: HLAInterpretation = HLAInterpretation(
135+
hla_sequence=dummy_hla_sequence("A"),
136+
matches=dummy_matches_no_mismatches("A"),
137+
allele_frequencies=DUMMY_FREQUENCIES,
138+
)
139+
processing_datetime: datetime = datetime(2025, 5, 9, 11, 0, 0)
140+
141+
seq_a: HLASequenceA = HLASequenceA.build_from_interpretation(
142+
interp, processing_datetime
143+
)
144+
145+
expected_result: HLASequenceA = HLASequenceA(
146+
enum="dummy_seq",
147+
alleles_clean="A*01:01:01 - A*02:02:02",
148+
alleles_all="A*01:01:01 - A*02:02:02",
149+
ambiguous="False",
150+
homozygous="False",
151+
mismatch_count=0,
152+
mismatches=None,
153+
seq="CCACAGGCT",
154+
enterdate=processing_datetime,
155+
)
156+
157+
assert seq_a == expected_result
158+
159+
122160
def test_hla_sequence_b_build_from_interpretation_non_b5701():
123161
b5701_standards: list[HLAStandard] = [
124162
# "Forgiving distance" from sequence: 8
@@ -175,6 +213,58 @@ def test_hla_sequence_b_build_from_interpretation_non_b5701():
175213
assert seq_b == expected_result
176214

177215

216+
def test_hla_sequence_b_build_from_interpretation_no_mismatches():
217+
b5701_standards: list[HLAStandard] = [
218+
# "Forgiving distance" from sequence: 8
219+
HLAStandard(
220+
allele="B*57:01:01",
221+
two=(8, 8, 8, 8),
222+
three=(8, 8, 8, 8, 8),
223+
),
224+
# "Forgiving distance" from sequence: 7
225+
HLAStandard(
226+
allele="B*57:01:02:01N",
227+
two=(4, 8, 4, 8),
228+
three=(8, 4, 8, 4, 8),
229+
),
230+
# "Forgiving distance" from sequence: 6, because 12 "contains" 8
231+
HLAStandard(
232+
allele="B*57:01:03",
233+
two=(4, 4, 8, 8),
234+
three=(8, 4, 4, 8, 12),
235+
),
236+
]
237+
interp: HLAInterpretation = HLAInterpretation(
238+
hla_sequence=dummy_hla_sequence("B"),
239+
matches=dummy_matches_no_mismatches("B"),
240+
allele_frequencies=DUMMY_FREQUENCIES,
241+
b5701_standards=b5701_standards,
242+
)
243+
processing_datetime: datetime = datetime(2025, 5, 9, 11, 0, 0)
244+
245+
seq_b: HLASequenceB = HLASequenceB.build_from_interpretation(
246+
interp, processing_datetime
247+
)
248+
249+
expected_result: HLASequenceB = HLASequenceB(
250+
enum="dummy_seq",
251+
alleles_clean="B*01:01:01 - B*02:02:02",
252+
alleles_all="B*01:01:01 - B*02:02:02",
253+
ambiguous="False",
254+
homozygous="False",
255+
mismatch_count=0,
256+
mismatches=None,
257+
b5701="False",
258+
b5701_dist=6,
259+
seqa="CCAC",
260+
seqb="AGGCT",
261+
reso_status=None,
262+
enterdate=processing_datetime,
263+
)
264+
265+
assert seq_b == expected_result
266+
267+
178268
MATCHES_FOR_B5701_CASES: dict[HLACombinedStandard, HLAMatchDetails] = {
179269
HLACombinedStandard(
180270
standard_bin=(1, 4, 9, 4),
@@ -241,6 +331,7 @@ def test_hla_sequence_b_build_from_interpretation_non_b5701():
241331
),
242332
]
243333

334+
244335
def test_hla_sequence_b_build_from_interpretation_is_b5701():
245336
interp: HLAInterpretation = HLAInterpretation(
246337
hla_sequence=dummy_hla_sequence("B"),
@@ -307,6 +398,34 @@ def test_hla_sequence_c_build_from_interpretation():
307398
assert seq_c == expected_result
308399

309400

401+
def test_hla_sequence_c_build_from_interpretation_no_mismatches():
402+
interp: HLAInterpretation = HLAInterpretation(
403+
hla_sequence=dummy_hla_sequence("C"),
404+
matches=dummy_matches_no_mismatches("C"),
405+
allele_frequencies=DUMMY_FREQUENCIES,
406+
)
407+
processing_datetime: datetime = datetime(2025, 5, 9, 11, 0, 0)
408+
409+
seq_c: HLASequenceC = HLASequenceC.build_from_interpretation(
410+
interp, processing_datetime
411+
)
412+
413+
expected_result: HLASequenceC = HLASequenceC(
414+
enum="dummy_seq",
415+
alleles_clean="C*01:01:01 - C*02:02:02",
416+
alleles_all="C*01:01:01 - C*02:02:02",
417+
ambiguous="False",
418+
homozygous="False",
419+
mismatch_count=0,
420+
mismatches=None,
421+
seqa="CCAC",
422+
seqb="AGGCT",
423+
enterdate=processing_datetime,
424+
)
425+
426+
assert seq_c == expected_result
427+
428+
310429
@pytest.mark.parametrize(
311430
"raw_contents, locus, sample_name, expected_sanitized_contents",
312431
[

0 commit comments

Comments
 (0)