Skip to content

Commit 29792ad

Browse files
committed
Changed MAC validation/check to use is_mac
1 parent ab10d21 commit 29792ad

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

pyard/ard.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPES) -> str:
409409
)
410410

411411
# Handle MAC
412-
if self._config["reduce_MAC"] and self.is_mac(glstring):
413-
if db.is_valid_mac_code(self.db_connection, code):
412+
if self._config["reduce_MAC"] and code.isalpha():
413+
if self.is_mac(glstring): # Make sure it's a valid MAC
414414
if HLA_regex.search(glstring):
415415
# Remove HLA- prefix
416416
allele_name = glstring.split("-")[1]
@@ -487,15 +487,16 @@ def is_mac(self, allele: str) -> bool:
487487
if ":" in allele:
488488
allele_split = allele.split(":")
489489
if len(allele_split) == 2: # MACs have only single :
490-
locus_antigen, code = allele.split(":")
490+
locus_antigen, code = allele_split
491491
if code.isalpha():
492492
try:
493493
alleles = db.mac_code_to_alleles(self.db_connection, code)
494494
if alleles:
495495
if any(map(lambda a: ":" in a, alleles)):
496-
# allele specific antigen codes has ':' in the MAC mapping
496+
# allele specific antigen codes have ':' in the MAC mapping
497497
# e.g. CFWRN -> 15:01/15:98/15:157/15:202/
498498
# 15:239/15:280/15:340/35:43/35:67/35:79/35:102/35:118/35:185/51:220
499+
# Extract the antigens from the mapped alleles
499500
antigen_groups = map(lambda a: a.split(":")[0], alleles)
500501
# Rule 1: The 1st field with the most allele designations in the request is
501502
# the 1st field of the allele code designation
@@ -746,8 +747,8 @@ def expand_mac(self, mac_code: str):
746747
:return: GL String of expanded alleles
747748
:rtype: str
748749
"""
749-
locus_antigen, code = mac_code.split(":")
750-
if db.is_valid_mac_code(self.db_connection, code):
750+
if self.is_mac(mac_code): # Validate MAC first
751+
locus_antigen, code = mac_code.split(":")
751752
if HLA_regex.search(mac_code):
752753
locus_antigen = locus_antigen.split("-")[1] # Remove HLA- prefix
753754
return "/".join(

pyard/db.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,6 @@ def alleles_to_mac_code(
172172
return None
173173

174174

175-
def is_valid_mac_code(connection: sqlite3.Connection, code: str) -> bool:
176-
"""
177-
Check db if the MAC code exists.
178-
179-
:param connection: db connection of type sqlite.Connection
180-
:param code: MAC code
181-
:return: code is MAC code ?
182-
"""
183-
mac_query = "SELECT count(alleles) from mac_codes where code = ?"
184-
cursor = connection.execute(mac_query, (code,))
185-
result = cursor.fetchone()
186-
cursor.close()
187-
return result[0] > 0
188-
189-
190175
def serology_to_alleles(connection: sqlite3.Connection, serology: str) -> List[str]:
191176
"""
192177
Look up Serology in the database and return corresponding list of alleles.

scripts/pyard

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ if __name__ == "__main__":
155155
except InvalidTypingError as e:
156156
print("Typing Error:", e.message, file=sys.stderr)
157157
sys.exit(2)
158+
except InvalidMACError as e:
159+
print("MAC Error:", e.message, file=sys.stderr)
160+
sys.exit(3)
158161
else:
159162
# Remove ard and close db connection
160163
del ard

tests/features/mac.feature

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ Feature: MAC (Multiple Allele Code)
5050
| HLA-A*02:01/HLA-A*02:09/HLA-A*02:43N | HLA-A*02:GNF |
5151

5252

53-
Scenario Outline: Invalid MACs
53+
Scenario Outline: Validate allele specific antigen MACs
54+
55+
MAC validation rules for allele specific antigen codes:
56+
- The 1st field with the most allele designations in the request is
57+
the 1st field of the allele code designation
58+
- If there is a tie in the number of alleles designations sharing the 1st field,
59+
the 1st field with the lowest numeric value is selected.
60+
5461

5562
Given the MAC code is <MAC>
5663
When checking for validity of the MAC
@@ -59,5 +66,8 @@ Feature: MAC (Multiple Allele Code)
5966
Examples:
6067
| MAC | Validity |
6168
| DRB1*07:DFJR | Invalid |
69+
| DRB1*15:DFJR | Valid |
6270
| DPB1*08:BHHE | Invalid |
71+
| DPB1*19:BHHE | Valid |
6372
| A*31:CMZEY | Invalid |
73+
| A*02:CMZEY | Valid |

0 commit comments

Comments
 (0)