Skip to content

Commit 85066e0

Browse files
committed
extract _redux_non_glstring method from redux
1 parent a063e4b commit 85066e0

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

pyard/ard_refactored.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
DEFAULT_CACHE_SIZE,
1313
G_GROUP_LOCI,
1414
VALID_REDUCTION_TYPE,
15+
expression_chars,
1516
)
1617
from .exceptions import InvalidMACError, InvalidTypingError
1718
from .handlers import (
@@ -199,24 +200,16 @@ def _redux_allele(
199200

200201
return self.allele_reducer.reduce_allele(allele, redux_type, re_ping)
201202

202-
@functools.lru_cache(maxsize=DEFAULT_CACHE_SIZE)
203-
def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
204-
"""Main redux method using specialized handlers"""
205-
# Handle GL string delimiters first
206-
processed_gl = self.gl_processor.process_gl_string(glstring, redux_type)
207-
if processed_gl != glstring or self.is_glstring(processed_gl):
208-
return processed_gl
209-
210-
# Remove HLA- prefix for processing the allele
211-
is_hla_prefix = HLA_regex.search(glstring)
212-
if is_hla_prefix:
213-
allele = glstring.split("-")[1]
214-
else:
215-
allele = glstring
216-
# Handle ignored allele suffixes
217-
if self._config["ignore_allele_with_suffixes"]:
218-
_, fields = allele.split("*")
219-
if fields in self._config["ignore_allele_with_suffixes"]:
203+
def _redux_non_glstring(
204+
self, allele: str, glstring: str, redux_type: VALID_REDUCTION_TYPE
205+
):
206+
if "*" in allele:
207+
locus, fields = allele.split("*")
208+
# Handle ignored allele suffixes
209+
if self._config["ignore_allele_with_suffixes"]:
210+
if fields in self._config["ignore_allele_with_suffixes"]:
211+
return allele
212+
if locus not in G_GROUP_LOCI:
220213
return allele
221214

222215
# Handle V2 to V3 mapping
@@ -233,7 +226,7 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
233226
return self.redux("/".join(alleles), redux_type)
234227
return ""
235228

236-
# Validate format
229+
# Validate allele format is correct
237230
if ":" in allele:
238231
loc_allele = allele.split(":")
239232
if len(loc_allele) < 2:
@@ -247,10 +240,6 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
247240
f"{glstring} is not a valid V2 or Serology typing."
248241
)
249242
else:
250-
if "*" in allele:
251-
locus, _ = allele.split("*")
252-
if locus not in G_GROUP_LOCI:
253-
return allele
254243
raise InvalidTypingError(
255244
f"{glstring} is not a valid V2 or Serology typing."
256245
)
@@ -264,16 +253,12 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
264253
reduced_alleles = self.redux(
265254
"/".join(self.code_mappings.xx_codes[loc_antigen]), redux_type
266255
)
267-
if is_hla_prefix:
268-
return "/".join([f"HLA-{a}" for a in reduced_alleles.split("/")])
269256
return reduced_alleles
270257

271258
# Handle MAC
272259
if self._config["reduce_MAC"] and code.isalpha():
273260
if self.mac_handler.is_mac(allele):
274261
alleles = self.mac_handler.get_alleles(code, loc_antigen)
275-
if is_hla_prefix:
276-
alleles = [f"HLA-{a}" for a in alleles]
277262
return self.redux("/".join(alleles), redux_type)
278263
else:
279264
raise InvalidMACError(f"{glstring} is an invalid MAC.")
@@ -285,6 +270,26 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
285270
return self.redux("/".join(self.shortnulls[allele]), redux_type)
286271

287272
redux_allele = self._redux_allele(allele, redux_type)
273+
return redux_allele
274+
275+
@functools.lru_cache(maxsize=DEFAULT_CACHE_SIZE)
276+
def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPE = "lgx") -> str:
277+
"""Main redux method using specialized handlers"""
278+
279+
# Handle GL string delimiters first
280+
processed_gl = self.gl_processor.process_gl_string(glstring, redux_type)
281+
if processed_gl != glstring or self.is_glstring(processed_gl):
282+
return processed_gl
283+
284+
# Remove HLA- prefix for processing the allele
285+
is_hla_prefix = HLA_regex.search(glstring)
286+
if is_hla_prefix:
287+
allele = glstring.split("-")[1]
288+
else:
289+
allele = glstring
290+
291+
# Handle non GL string
292+
redux_allele = self._redux_non_glstring(allele, glstring, redux_type)
288293
# Add back 'HLA-' prefix when redux is done if needed
289294
if is_hla_prefix:
290295
if "/" in redux_allele:
@@ -341,8 +346,6 @@ def validate(self, glstring: str) -> bool:
341346

342347
def _get_non_strict_allele(self, allele: str) -> str:
343348
"""Handle non-strict allele validation"""
344-
from .constants import expression_chars
345-
346349
if not self._is_allele_in_db(allele):
347350
for expr_char in expression_chars:
348351
if self._is_allele_in_db(allele + expr_char):

0 commit comments

Comments
 (0)