Skip to content
Merged
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
27 changes: 18 additions & 9 deletions pyard/pyard.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ def is_serology(allele: str) -> bool:
@staticmethod
def is_mac(gl: str) -> bool:
"""
MAC has there are non-digit characters after the : character,
then it's a MAC.
MAC has non-digit characters after the : character.

:param gl: glstring to test if it has a MAC code
:return: True if MAC
"""
Expand All @@ -283,11 +283,14 @@ def is_v2(allele: str) -> bool:
"""
Version 2 of the nomenclature is a single field.
It does not have any ':' field separator.
Eg: A*0104
Eg: A*0104
Exceptions:
DRB3*NNNN is not v2 allele
:param allele: Possible allele
:return: Is the allele in V2 nomenclature
"""
return '*' in allele and ':' not in allele
return '*' in allele and ':' not in allele \
and not allele.endswith('*NNNN')

def _is_who_allele(self, allele):
"""
Expand Down Expand Up @@ -395,20 +398,26 @@ def isvalid(self, allele: str) -> bool:
:return: allele or empty
:rtype: bool
"""
if allele == '':
if allele == '' or allele.endswith('*'):
return False

# validate allele without the 'HLA-' prefix
if HLA_regex.search(allele):
# remove 'HLA-' prefix
allele = allele[4:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


if '*' in allele:
alphanum_allele = allele.replace('*', '').replace(':', '')
if not alphanum_allele.isalnum():
return False

if not self.is_mac(allele) and \
not self.is_serology(allele) and \
not self.is_v2(allele):
# Alleles ending with P or G are valid_alleles
if allele.endswith(('P', 'G')):
# remove the last character
allele = allele[:-1]
# validate allele without the 'HLA-' prefix
if HLA_regex.search(allele):
# remove 'HLA-' prefix
allele = allele[4:]
return self._is_valid_allele(allele)
return True

Expand Down