Skip to content

Commit a6c98ae

Browse files
committed
Changed MAC validation/check to use is_mac
1 parent 3d2b5d0 commit a6c98ae

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
@@ -412,8 +412,8 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPES) -> str:
412412
)
413413

414414
# Handle MAC
415-
if self._config["reduce_MAC"] and self.is_mac(glstring):
416-
if db.is_valid_mac_code(self.db_connection, code):
415+
if self._config["reduce_MAC"] and code.isalpha():
416+
if self.is_mac(glstring): # Make sure it's a valid MAC
417417
if HLA_regex.search(glstring):
418418
# Remove HLA- prefix
419419
allele_name = glstring.split("-")[1]
@@ -490,15 +490,16 @@ def is_mac(self, allele: str) -> bool:
490490
if ":" in allele:
491491
allele_split = allele.split(":")
492492
if len(allele_split) == 2: # MACs have only single :
493-
locus_antigen, code = allele.split(":")
493+
locus_antigen, code = allele_split
494494
if code.isalpha():
495495
try:
496496
alleles = db.mac_code_to_alleles(self.db_connection, code)
497497
if alleles:
498498
if any(map(lambda a: ":" in a, alleles)):
499-
# allele specific antigen codes has ':' in the MAC mapping
499+
# allele specific antigen codes have ':' in the MAC mapping
500500
# e.g. CFWRN -> 15:01/15:98/15:157/15:202/
501501
# 15:239/15:280/15:340/35:43/35:67/35:79/35:102/35:118/35:185/51:220
502+
# Extract the antigens from the mapped alleles
502503
antigen_groups = map(lambda a: a.split(":")[0], alleles)
503504
# Rule 1: The 1st field with the most allele designations in the request is
504505
# the 1st field of the allele code designation
@@ -749,8 +750,8 @@ def expand_mac(self, mac_code: str):
749750
:return: GL String of expanded alleles
750751
:rtype: str
751752
"""
752-
locus_antigen, code = mac_code.split(":")
753-
if db.is_valid_mac_code(self.db_connection, code):
753+
if self.is_mac(mac_code): # Validate MAC first
754+
locus_antigen, code = mac_code.split(":")
754755
if HLA_regex.search(mac_code):
755756
locus_antigen = locus_antigen.split("-")[1] # Remove HLA- prefix
756757
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)