Skip to content

Commit 69edffd

Browse files
authored
Merge pull request #314 from pbashyal-nmdp/fix_mac_lookup_not_sorting
MAC lookup error
2 parents 35e8f3e + 66e9f20 commit 69edffd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pyard/ard.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ def expand_mac(self, mac_code: str):
834834

835835
raise InvalidMACError(f"{mac_code} is an invalid MAC.")
836836

837+
@functools.lru_cache()
837838
def lookup_mac(self, allelelist_gl: str):
838839
"""
839840
Finds a MAC code corresponding to
@@ -856,10 +857,18 @@ def lookup_mac(self, allelelist_gl: str):
856857
locus = allelelist_gl.split("*")[0]
857858
return f"{locus}*{antigen_groups[0]}:{mac_code}"
858859

859-
# Try the list of first_field:second_field combinations
860-
mac_expansion = "/".join(sorted(allele_fields))
860+
# Try the given list order first with first_field:second_field combinations
861+
mac_expansion = "/".join(allele_fields)
861862
mac_code = db.alleles_to_mac_code(self.db_connection, mac_expansion)
863+
if mac_code:
864+
locus = allelelist_gl.split("*")[0]
865+
return f"{locus}*{antigen_groups[0]}:{mac_code}"
862866

867+
# Try the sorted list of first_field:second_field combinations
868+
mac_expansion = "/".join(
869+
sorted(allele_fields, key=functools.cmp_to_key(self.smart_sort_comparator))
870+
)
871+
mac_code = db.alleles_to_mac_code(self.db_connection, mac_expansion)
863872
if mac_code:
864873
locus = allelelist_gl.split("*")[0]
865874
return f"{locus}*{antigen_groups[0]}:{mac_code}"

tests/test_pyard.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,9 @@ def test_is_null(self):
205205
self.assertFalse(
206206
self.ard.is_null(allele), msg="MACs shouldn't be called as Nulls"
207207
)
208+
209+
def test_mac_is_reversible(self):
210+
mac_code = "A*68:AJEBX"
211+
expanded_mac = self.ard.expand_mac(mac_code)
212+
lookup_mac = self.ard.lookup_mac(expanded_mac)
213+
self.assertEqual(mac_code, lookup_mac, msg="MACs should be reversible")

0 commit comments

Comments
 (0)