diff --git a/pyard/ard.py b/pyard/ard.py index 95ccac0..b3661fc 100644 --- a/pyard/ard.py +++ b/pyard/ard.py @@ -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 @@ -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. @@ -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) diff --git a/tests/features/allele.feature b/tests/features/allele.feature index 0be6be1..d6f5df5 100644 --- a/tests/features/allele.feature +++ b/tests/features/allele.feature @@ -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 + When checking for validity of the allele in non-strict mode + Then the validness of the allele is + + Examples: + | Allele | Validity | + | A*11:403 | Invalid | + | A*24:329 | Valid | + Scenario Outline: Single field MICA, MICB Alleles diff --git a/tests/features/mac.feature b/tests/features/mac.feature index f517a73..9e7c8e5 100644 --- a/tests/features/mac.feature +++ b/tests/features/mac.feature @@ -61,7 +61,7 @@ Feature: MAC (Multiple Allele Code) Given the MAC code is When checking for validity of the MAC - Then the validness is + Then the validness of MAC is Examples: | MAC | Validity | diff --git a/tests/steps/mac.py b/tests/steps/mac.py index 64069d5..8c2f408 100644 --- a/tests/steps/mac.py +++ b/tests/steps/mac.py @@ -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)) diff --git a/tests/steps/redux_allele.py b/tests/steps/redux_allele.py index 5568c98..9236e44 100644 --- a/tests/steps/redux_allele.py +++ b/tests/steps/redux_allele.py @@ -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))