Skip to content

Commit 17690fa

Browse files
authored
Merge branch 'master' into default_redux_is_lgx
2 parents 89f0d63 + 69edffd commit 17690fa

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,17 @@ def test_is_null(self):
206206
self.ard.is_null(allele), msg="MACs shouldn't be called as Nulls"
207207
)
208208

209+
209210
def test_default_redux_is_lgx(self):
210211
allele = "A*24:BKKPV+A*26:03^B*15:BKNTS+B*15:07"
211212
lgx_redux = self.ard.redux(allele, "lgx")
212213
default_redux = self.ard.redux(allele)
213214
self.assertEqual(lgx_redux, default_redux, msg="Default redux should be lgx")
215+
216+
217+
def test_mac_is_reversible(self):
218+
mac_code = "A*68:AJEBX"
219+
expanded_mac = self.ard.expand_mac(mac_code)
220+
lookup_mac = self.ard.lookup_mac(expanded_mac)
221+
self.assertEqual(mac_code, lookup_mac, msg="MACs should be reversible")
222+

0 commit comments

Comments
 (0)