From 070820441d2ff3478a19abf8bcc33908289ce4eb Mon Sep 17 00:00:00 2001 From: pbashyal-nmdp Date: Tue, 12 Mar 2024 12:27:40 -0500 Subject: [PATCH 1/3] MAC lookup error - Alleles need to be sorted when looking up in database - Allow the given order first --- pyard/ard.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pyard/ard.py b/pyard/ard.py index 05b2647..2111d01 100644 --- a/pyard/ard.py +++ b/pyard/ard.py @@ -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 @@ -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}" From b8c20bceb31473faa32124e290f0e5e71c6bb1a5 Mon Sep 17 00:00:00 2001 From: pbashyal-nmdp Date: Tue, 12 Mar 2024 12:31:40 -0500 Subject: [PATCH 2/3] Add reversible test --- tests/test_pyard.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_pyard.py b/tests/test_pyard.py index 39dbd60..5766e1e 100644 --- a/tests/test_pyard.py +++ b/tests/test_pyard.py @@ -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) From 66e9f20af5ac236dbe4a5472cf15d86492b3916e Mon Sep 17 00:00:00 2001 From: pbashyal-nmdp Date: Tue, 12 Mar 2024 12:41:07 -0500 Subject: [PATCH 3/3] Add a message to test. --- tests/test_pyard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pyard.py b/tests/test_pyard.py index 5766e1e..85f35dc 100644 --- a/tests/test_pyard.py +++ b/tests/test_pyard.py @@ -210,4 +210,4 @@ 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) + self.assertEqual(mac_code, lookup_mac, msg="MACs should be reversible")