3939
4040
4141def is_mac (x ):
42- return True if re .search (":\D+" , x ) else False
42+ return True if re .search (r ":\D+" , x ) else False
4343
4444
4545class ARD (object ):
4646 """ ARD reduction for HLA """
47+
4748 def __init__ (self , dbversion : str = 'Latest' ,
4849 load_mac_file : bool = True ,
4950 verbose : bool = False ,
@@ -85,7 +86,7 @@ def __init__(self, dbversion: str = 'Latest',
8586 # TODO: add check for valid db version
8687
8788 # List of expression characters
88- expre_chars = ['N' , 'Q' , 'L' , 'S' ]
89+ expression_chars = ['N' , 'Q' , 'L' , 'S' ]
8990
9091 # Set data directory where all the downloaded files will go
9192 if data_dir is None :
@@ -157,7 +158,7 @@ def __init__(self, dbversion: str = 'Latest',
157158 allele_df ['2d' ] = allele_df ['Allele' ].apply (lambda a :
158159 ":" .join (a .split (":" )[0 :2 ]) +
159160 list (a )[- 1 ] if list (a )[- 1 ]
160- in expre_chars and
161+ in expression_chars and
161162 len (a .split (":" )) > 2
162163 else ":" .join (a .split (":" )[0 :2 ]))
163164
@@ -188,7 +189,7 @@ def __init__(self, dbversion: str = 'Latest',
188189 allele_df ['3d' ] = allele_df ['Allele' ].apply (lambda a :
189190 ":" .join (a .split (":" )[0 :3 ]) +
190191 list (a )[- 1 ] if list (a )[- 1 ]
191- in expre_chars and
192+ in expression_chars and
192193 len (a .split (":" )) > 3
193194 else ":" .join (a .split (":" )[0 :3 ]))
194195
@@ -221,14 +222,14 @@ def __init__(self, dbversion: str = 'Latest',
221222 df ['2d' ] = df ['A' ].apply (lambda a :
222223 ":" .join (a .split (":" )[0 :2 ]) +
223224 list (a )[- 1 ] if list (a )[- 1 ]
224- in expre_chars and
225+ in expression_chars and
225226 len (a .split (":" )) > 2
226227 else ":" .join (a .split (":" )[0 :2 ]))
227228
228229 df ['3d' ] = df ['A' ].apply (lambda a :
229230 ":" .join (a .split (":" )[0 :3 ]) +
230231 list (a )[- 1 ] if list (a )[- 1 ]
231- in expre_chars and
232+ in expression_chars and
232233 len (a .split (":" )) > 3
233234 else ":" .join (a .split (":" )[0 :3 ]))
234235
@@ -345,7 +346,7 @@ def lgx(self):
345346 """
346347 return self ._lgx
347348
348- @functools .lru_cache (maxsize = None )
349+ @functools .lru_cache (maxsize = 1000 )
349350 def redux (self , allele : str , ars_type : str ) -> str :
350351 """
351352 Does ARS reduction with allele and ARS type
@@ -399,7 +400,7 @@ def redux(self, allele: str, ars_type: str) -> str:
399400 else :
400401 return allele
401402
402- @functools .lru_cache (maxsize = None )
403+ @functools .lru_cache (maxsize = 1000 )
403404 def redux_gl (self , glstring : str , redux_type : str ) -> str :
404405 """
405406 Does ARS reduction with gl string and ARS type
@@ -415,19 +416,19 @@ def redux_gl(self, glstring: str, redux_type: str) -> str:
415416 if not self .isvalid_gl (glstring ):
416417 return ""
417418
418- if re .search ("\^" , glstring ):
419+ if re .search (r "\^" , glstring ):
419420 return "^" .join (sorted (set ([self .redux_gl (a , redux_type ) for a in glstring .split ("^" )]),
420421 key = functools .cmp_to_key (smart_sort_comparator )))
421422
422- if re .search ("\|" , glstring ):
423+ if re .search (r "\|" , glstring ):
423424 return "|" .join (sorted (set ([self .redux_gl (a , redux_type ) for a in glstring .split ("|" )]),
424425 key = functools .cmp_to_key (smart_sort_comparator )))
425426
426- if re .search ("\+" , glstring ):
427+ if re .search (r "\+" , glstring ):
427428 return "+" .join (sorted ([self .redux_gl (a , redux_type ) for a in glstring .split ("+" )],
428429 key = functools .cmp_to_key (smart_sort_comparator )))
429430
430- if re .search ("\ ~" , glstring ):
431+ if re .search ("~" , glstring ):
431432 return "~" .join ([self .redux_gl (a , redux_type ) for a in glstring .split ("~" )])
432433
433434 if re .search ("/" , glstring ):
@@ -448,24 +449,24 @@ def redux_gl(self, glstring: str, redux_type: str) -> str:
448449 if self .HLA_regex .search (glstring ):
449450 hla , allele_name = glstring .split ("-" )
450451 loc_name , code = allele_name .split (":" )
451- loc , n = loc_name .split ("*" )
452- alleles = list (filter (lambda a : a in self .valid ,
453- [loc_name + ":" + a if len (a ) <= 3
454- else loc + "*" + a
455- for a in self .mac [code ]['Alleles' ]]))
452+ alleles = self .get_alleles (code , loc_name )
456453 return self .redux_gl (
457454 "/" .join (sorted (["HLA-" + a for a in alleles ], key = functools .cmp_to_key (smart_sort_comparator ))),
458455 redux_type )
459456 else :
460- loc , n = loc_name .split ("*" )
461- alleles = list (filter (lambda a : a in self .valid ,
462- [loc_name + ":" + a if len (a ) <= 3
463- else loc + "*" + a
464- for a in self .mac [code ]['Alleles' ]]))
457+ alleles = self .get_alleles (code , loc_name )
465458 return self .redux_gl ("/" .join (sorted (alleles , key = functools .cmp_to_key (smart_sort_comparator ))),
466459 redux_type )
467460 return self .redux (glstring , redux_type )
468461
462+ def get_alleles (self , code , loc_name ):
463+ loc , n = loc_name .split ("*" )
464+ alleles = list (filter (lambda a : a in self .valid ,
465+ [loc_name + ":" + a if len (a ) <= 3
466+ else loc + "*" + a
467+ for a in self .mac [code ]['Alleles' ]]))
468+ return alleles
469+
469470 def isvalid (self , allele : str ) -> bool :
470471 """
471472 Determines validity of an allele
@@ -499,13 +500,13 @@ def isvalid_gl(self, glstring: str) -> bool:
499500 :rtype: bool
500501 """
501502
502- if re .search ("\^" , glstring ):
503+ if re .search (r "\^" , glstring ):
503504 return all (map (self .isvalid_gl , glstring .split ("^" )))
504- if re .search ("\|" , glstring ):
505+ if re .search (r "\|" , glstring ):
505506 return all (map (self .isvalid_gl , glstring .split ("|" )))
506- if re .search ("\+" , glstring ):
507+ if re .search (r "\+" , glstring ):
507508 return all (map (self .isvalid_gl , glstring .split ("+" )))
508- if re .search ("\ ~" , glstring ):
509+ if re .search ("~" , glstring ):
509510 return all (map (self .isvalid_gl , glstring .split ("~" )))
510511 if re .search ("/" , glstring ):
511512 return all (map (self .isvalid_gl , glstring .split ("/" )))
@@ -533,12 +534,12 @@ def mac_toG(self, allele: str) -> str:
533534 set ([self .toG (allele = a )
534535 for a in alleles ])))
535536 if "X" in group :
536- return None
537+ return ''
537538 else :
538539 return "/" .join (group )
539540
540541 else :
541- return None
542+ return ''
542543
543544 def toG (self , allele : str ) -> str :
544545 """
0 commit comments