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
30 changes: 22 additions & 8 deletions pyard/ard.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,8 @@ def _redux_allele(
else:
return redux_allele

# In non-strict mode, if the allele is not valid,
# try it with expression characters suffixed
if not self._config["strict"] and not self._is_valid_allele(allele):
for expr_char in expression_chars:
if self._is_valid_allele(allele + expr_char):
if self._config["verbose_log"]:
print(f"{allele} is not valid. Using {allele}{expr_char}")
allele = allele + expr_char
if not self._config["strict"]:
allele = self._get_non_strict_allele(allele)

# g_group maps alleles to their g_group
# note: this includes mappings for shortened version of alleles
Expand Down Expand Up @@ -301,6 +295,23 @@ def _redux_allele(
else:
raise InvalidAlleleError(f"{allele} is an invalid allele.")

def _get_non_strict_allele(self, allele):
"""
In non-strict mode, if the allele is not valid,
try it with expression characters suffixed

@param allele: allele that might have non-strict version
@return: non-strict version of the allele if it exists
"""
if not self._is_valid_allele(allele):
for expr_char in expression_chars:
if self._is_valid_allele(allele + expr_char):
if self._config["verbose_log"]:
print(f"{allele} is not valid. Using {allele}{expr_char}")
allele = allele + expr_char
break
return allele

def _sorted_unique_gl(self, gls: List[str], delim: str) -> str:
"""
Make a list of sorted unique GL Strings separated by delim.
Expand Down Expand Up @@ -695,6 +706,9 @@ def _is_valid(self, allele: str) -> bool:
if not alphanum_allele.isalnum():
return False

if not self._config["strict"]:
allele = self._get_non_strict_allele(allele)

if (
not self.is_mac(allele)
and not self.is_XX(allele)
Expand Down
15 changes: 15 additions & 0 deletions tests/features/allele.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ Feature: Alleles
| A*24:329 | lgx | A*24:329Q |
| DQB1*03:276 | lgx | DQB1*03:01 |

Scenario Outline: Allele validation in non-strict mode

Similar to reduction, handle non-strict mode when validating an allele.
The test version of IPD/IMGT-HLA database (see environment.py),
A*11:403 is invalid and A*24:329 is valid for A*24:329Q

Given the allele as <Allele>
When checking for validity of the allele in non-strict mode
Then the validness of the allele is <Validity>

Examples:
| Allele | Validity |
| A*11:403 | Invalid |
| A*24:329 | Valid |


Scenario Outline: Single field MICA, MICB Alleles

Expand Down
2 changes: 1 addition & 1 deletion tests/features/mac.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Feature: MAC (Multiple Allele Code)

Given the MAC code is <MAC>
When checking for validity of the MAC
Then the validness is <Validity>
Then the validness of MAC is <Validity>

Examples:
| MAC | Validity |
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def step_impl(context):
context.is_valid = False


@then("the validness is {validity}")
@then("the validness of MAC is {validity}")
def step_impl(context, validity):
valid = validity == "Valid"
assert_that(context.is_valid, is_(valid))
14 changes: 14 additions & 0 deletions tests/steps/redux_allele.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ def step_impl(context, expanded_alleles):
def step_impl(context, level):
context.level = level
context.redux_allele = context.ard_non_strict.redux(context.allele, level)


@when("checking for validity of the allele in non-strict mode")
def step_impl(context):
try:
context.is_valid = context.ard_non_strict.validate(context.allele)
except InvalidAlleleError:
context.is_valid = False


@then("the validness of the allele is {validity}")
def step_impl(context, validity):
valid = validity == "Valid"
assert_that(context.is_valid, is_(valid))