2727
2828from . import db
2929from .data_repository import generate_ars_mapping , generate_mac_codes , generate_alleles_and_xx_codes , \
30- generate_serology_mapping
31- from .db import is_valid_mac_code , mac_code_to_alleles
30+ generate_serology_mapping , generate_v2_to_v3_mapping
31+ from .db import is_valid_mac_code , mac_code_to_alleles , v2_to_v3_allele
3232from .smart_sort import smart_sort_comparator
3333
3434HLA_regex = re .compile ("^HLA-" )
@@ -66,6 +66,8 @@ def __init__(self, imgt_version: str = 'Latest',
6666 self .dup_g , self ._G , self ._lg , self ._lgx = generate_ars_mapping (self .db_connection , imgt_version )
6767 # Load Serology mappings
6868 generate_serology_mapping (self .db_connection , imgt_version )
69+ # Load V2 to V3 mappings
70+ generate_v2_to_v3_mapping (self .db_connection , imgt_version )
6971
7072 # Close the current read-write db connection
7173 self .db_connection .close ()
@@ -172,6 +174,11 @@ def redux_gl(self, glstring: str, redux_type: str) -> str:
172174 return "/" .join (sorted (set ([self .redux_gl (a , redux_type ) for a in glstring .split ("/" )]),
173175 key = functools .cmp_to_key (smart_sort_comparator )))
174176
177+ # Handle V2 to V3 mapping
178+ if self .is_v2 (glstring ):
179+ glstring = self ._map_v2_to_v3 (glstring )
180+ return self .redux_gl (glstring , redux_type )
181+
175182 # Handle Serology
176183 if self .is_serology (glstring ):
177184 alleles = self ._get_alleles_from_serology (glstring )
@@ -232,6 +239,17 @@ def is_mac(gl: str) -> bool:
232239 """
233240 return re .search (r":\D+" , gl ) is not None
234241
242+ @staticmethod
243+ def is_v2 (allele : str ) -> bool :
244+ """
245+ Version 2 of the nomenclature is a single field.
246+ It does not have any ':' field separator.
247+ Eg: A*0104
248+ :param allele: Possible allele
249+ :return: Is the allele in V2 nomenclature
250+ """
251+ return '*' in allele and not ':' in allele
252+
235253 def _is_valid_allele (self , allele ):
236254 """
237255 Test if allele is valid in the current imgt database
@@ -255,7 +273,7 @@ def _get_alleles(self, code, locus_antigen) -> Iterable[str]:
255273 # else it's a group expansion
256274 is_allelic_expansion = any ([':' in allele for allele in alleles ])
257275 if is_allelic_expansion :
258- locus = locus_antigen .split ('*' )[0 ] # Just keep the locus name
276+ locus = locus_antigen .split ('*' )[0 ] # Just keep the locus name
259277 alleles = [f'{ locus } *{ a } ' for a in alleles ]
260278 else :
261279 alleles = [f'{ locus_antigen } :{ a } ' for a in alleles ]
@@ -272,6 +290,14 @@ def _get_alleles_from_serology(self, serology) -> Iterable[str]:
272290 else :
273291 return alleles
274292
293+ def _map_v2_to_v3 (self , v2_allele ):
294+ """
295+ Get V3 version of V2 versioned allele
296+ :param v2_allele: V2 versioned allele
297+ :return: V3 versioned allele
298+ """
299+ return v2_to_v3_allele (self .db_connection , v2_allele )
300+
275301 def isvalid (self , allele : str ) -> bool :
276302 """
277303 Determines validity of an allele
@@ -283,7 +309,9 @@ def isvalid(self, allele: str) -> bool:
283309 """
284310 if allele == '' :
285311 return False
286- if not self .is_mac (allele ) and not self .is_serology (allele ):
312+ if not self .is_mac (allele ) and \
313+ not self .is_serology (allele ) and \
314+ not self .is_v2 (allele ):
287315 # Alleles ending with P or G are valid_alleles
288316 if allele .endswith (('P' , 'G' )):
289317 # remove the last character
@@ -330,7 +358,7 @@ def mac_toG(self, allele: str) -> str:
330358 """
331359 locus_antigen , code = allele .split (":" )
332360 if HLA_regex .search (allele ):
333- locus_antigen = locus_antigen .split ("-" )[1 ] # Remove HLA- prefix
361+ locus_antigen = locus_antigen .split ("-" )[1 ] # Remove HLA- prefix
334362 if is_valid_mac_code (self .db_connection , code ):
335363 alleles = self ._get_alleles (code , locus_antigen )
336364 group = [self .toG (a ) for a in alleles ]
@@ -370,7 +398,7 @@ def expand_mac(self, mac_code: str):
370398 locus_antigen , code = mac_code .split (":" )
371399 if is_valid_mac_code (self .db_connection , code ):
372400 if HLA_regex .search (mac_code ):
373- locus_antigen = locus_antigen .split ("-" )[1 ] # Remove HLA- prefix
401+ locus_antigen = locus_antigen .split ("-" )[1 ] # Remove HLA- prefix
374402 return ['HLA-' + a for a in self ._get_alleles (code , locus_antigen )]
375403 else :
376404 return list (self ._get_alleles (code , locus_antigen ))
0 commit comments