Skip to content
Merged
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
13 changes: 11 additions & 2 deletions pyard/ard.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ def expand_mac(self, mac_code: str):

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

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

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

# Try the sorted list of first_field:second_field combinations
mac_expansion = "/".join(
sorted(allele_fields, key=functools.cmp_to_key(self.smart_sort_comparator))
)
mac_code = db.alleles_to_mac_code(self.db_connection, mac_expansion)
if mac_code:
locus = allelelist_gl.split("*")[0]
return f"{locus}*{antigen_groups[0]}:{mac_code}"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pyard.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,9 @@ def test_is_null(self):
self.assertFalse(
self.ard.is_null(allele), msg="MACs shouldn't be called as Nulls"
)

def test_mac_is_reversible(self):
mac_code = "A*68:AJEBX"
expanded_mac = self.ard.expand_mac(mac_code)
lookup_mac = self.ard.lookup_mac(expanded_mac)
self.assertEqual(mac_code, lookup_mac, msg="MACs should be reversible")