Skip to content

Commit b1a995d

Browse files
authored
Merge pull request #287 from pbashyal-nmdp/validate-non-strict
Fix `validate()` in non-strict mode
2 parents a63042b + e84a2d3 commit b1a995d

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

pyard/ard.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,8 @@ def _redux_allele(
184184
else:
185185
return redux_allele
186186

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

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

298+
def _get_non_strict_allele(self, allele):
299+
"""
300+
In non-strict mode, if the allele is not valid,
301+
try it with expression characters suffixed
302+
303+
@param allele: allele that might have non-strict version
304+
@return: non-strict version of the allele if it exists
305+
"""
306+
if not self._is_valid_allele(allele):
307+
for expr_char in expression_chars:
308+
if self._is_valid_allele(allele + expr_char):
309+
if self._config["verbose_log"]:
310+
print(f"{allele} is not valid. Using {allele}{expr_char}")
311+
allele = allele + expr_char
312+
break
313+
return allele
314+
304315
def _sorted_unique_gl(self, gls: List[str], delim: str) -> str:
305316
"""
306317
Make a list of sorted unique GL Strings separated by delim.
@@ -695,6 +706,9 @@ def _is_valid(self, allele: str) -> bool:
695706
if not alphanum_allele.isalnum():
696707
return False
697708

709+
if not self._config["strict"]:
710+
allele = self._get_non_strict_allele(allele)
711+
698712
if (
699713
not self.is_mac(allele)
700714
and not self.is_XX(allele)

tests/features/allele.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ Feature: Alleles
7272
| A*24:329 | lgx | A*24:329Q |
7373
| DQB1*03:276 | lgx | DQB1*03:01 |
7474

75+
Scenario Outline: Allele validation in non-strict mode
76+
77+
Similar to reduction, handle non-strict mode when validating an allele.
78+
The test version of IPD/IMGT-HLA database (see environment.py),
79+
A*11:403 is invalid and A*24:329 is valid for A*24:329Q
80+
81+
Given the allele as <Allele>
82+
When checking for validity of the allele in non-strict mode
83+
Then the validness of the allele is <Validity>
84+
85+
Examples:
86+
| Allele | Validity |
87+
| A*11:403 | Invalid |
88+
| A*24:329 | Valid |
89+
7590

7691
Scenario Outline: Single field MICA, MICB Alleles
7792

tests/features/mac.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Feature: MAC (Multiple Allele Code)
6161

6262
Given the MAC code is <MAC>
6363
When checking for validity of the MAC
64-
Then the validness is <Validity>
64+
Then the validness of MAC is <Validity>
6565

6666
Examples:
6767
| MAC | Validity |

tests/steps/mac.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def step_impl(context):
4242
context.is_valid = False
4343

4444

45-
@then("the validness is {validity}")
45+
@then("the validness of MAC is {validity}")
4646
def step_impl(context, validity):
4747
valid = validity == "Valid"
4848
assert_that(context.is_valid, is_(valid))

tests/steps/redux_allele.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,17 @@ def step_impl(context, expanded_alleles):
112112
def step_impl(context, level):
113113
context.level = level
114114
context.redux_allele = context.ard_non_strict.redux(context.allele, level)
115+
116+
117+
@when("checking for validity of the allele in non-strict mode")
118+
def step_impl(context):
119+
try:
120+
context.is_valid = context.ard_non_strict.validate(context.allele)
121+
except InvalidAlleleError:
122+
context.is_valid = False
123+
124+
125+
@then("the validness of the allele is {validity}")
126+
def step_impl(context, validity):
127+
valid = validity == "Valid"
128+
assert_that(context.is_valid, is_(valid))

0 commit comments

Comments
 (0)