Skip to content

Commit 19670a2

Browse files
committed
Remove Unbounded Cache
- The LRU cache was unbounded and is problematic when used in long running services. Set it to 1000. - Use raw strings for regex characters - General cleanup
1 parent 7cbd4a1 commit 19670a2

File tree

9 files changed

+96
-108
lines changed

9 files changed

+96
-108
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,11 @@ ENV/
107107

108108
# BDD Testing
109109
testing/
110-
!test-requirements.txt
110+
!test-requirements.txt
111+
pretty.output
112+
113+
# Misc
114+
tags
115+
.swp
116+
*.patch
117+
.idea/

pyard/pyard.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939

4040

4141
def 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

4545
class 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
"""

pyard/smart_sort.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
expr_regex = re.compile('[NQLSGg]')
2828

2929

30-
@functools.lru_cache(maxsize=None)
30+
@functools.lru_cache(maxsize=1000)
3131
def smart_sort_comparator(a1, a2):
3232
"""
3333
Natural sort 2 given alleles.

tests/__init__.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/features/allele.feature

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
Feature: Alleles
22

3-
Scenario Outline:
3+
Scenario Outline:
44

5-
Given the allele as <Allele>
6-
When reducing on the <Level> level
7-
Then the reduced allele is found to be <Redux Allele>
5+
Given the allele as <Allele>
6+
When reducing on the <Level> level
7+
Then the reduced allele is found to be <Redux Allele>
88

9-
Examples:
10-
| Allele | Level | Redux Allele |
11-
| A*01:01:01 | G | A*01:01:01G |
12-
| A*01:01:01 | lg | A*01:01g |
13-
| A*01:01:01 | lgx | A*01:01 |
9+
Examples:
10+
| Allele | Level | Redux Allele |
11+
| A*01:01:01 | G | A*01:01:01G |
12+
| A*01:01:01 | lg | A*01:01g |
13+
| A*01:01:01 | lgx | A*01:01 |
1414

15-
| HLA-A*01:01:01 | G | HLA-A*01:01:01G |
16-
| HLA-A*01:01:01 | lg | HLA-A*01:01g |
17-
| HLA-A*01:01:01 | lgx | HLA-A*01:01 |
15+
| HLA-A*01:01:01 | G | HLA-A*01:01:01G |
16+
| HLA-A*01:01:01 | lg | HLA-A*01:01g |
17+
| HLA-A*01:01:01 | lgx | HLA-A*01:01 |
1818

19-
| DRB1*14:05:01 | lgx | DRB1*14:05 |
20-
| DRB1*14:05:01 | lg | DRB1*14:05g |
21-
22-
| DRB1*14:06:01 | lgx | DRB1*14:06 |
23-
| DRB1*14:06:01 | lg | DRB1*14:06g |
19+
| DRB1*14:05:01 | lgx | DRB1*14:05 |
20+
| DRB1*14:05:01 | lg | DRB1*14:05g |
21+
22+
| DRB1*14:06:01 | lgx | DRB1*14:06 |
23+
| DRB1*14:06:01 | lg | DRB1*14:06g |

tests/features/glstring.feature

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
Feature: GL (Genotype List) Strings
22

3-
Scenario Outline:
3+
Scenario Outline:
44

5-
Given the allele as <Allele>
6-
When reducing on the <Level> level (ambiguous)
7-
Then the reduced allele is found to be <Redux Allele>
5+
Given the allele as <Allele>
6+
When reducing on the <Level> level (ambiguous)
7+
Then the reduced allele is found to be <Redux Allele>
88

9-
Examples:
10-
| Allele | Level | Redux Allele |
11-
| A*01:01:01:01+A*01:01:01:01 | G | A*01:01:01G+A*01:01:01G |
12-
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | G | HLA-A*01:01:01G+HLA-A*01:01:01G |
13-
| A*01:01:01:01+A*01:01:01:01 | lg | A*01:01g+A*01:01g |
14-
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | lg | HLA-A*01:01g+HLA-A*01:01g |
15-
| A*01:01:01:01+A*01:01:01:01 | lgx | A*01:01+A*01:01 |
16-
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | lgx | HLA-A*01:01+HLA-A*01:01 |
17-
| A*01:01+A*01:01^B*07:02+B*07:02 | G | A*01:01:01G+A*01:01:01G^B*07:02:01G+B*07:02:01G |
18-
| A*01:01+A*01:01^B*07:02+B*07:02 | lg | A*01:01g+A*01:01g^B*07:02g+B*07:02g |
19-
| A*01:01~B*07:02+A*01:01~B*07:02 | G | A*01:01:01G~B*07:02:01G+A*01:01:01G~B*07:02:01G |
20-
| A*01:01~B*07:02+A*01:01~B*07:02 | lg | A*01:01g~B*07:02g+A*01:01g~B*07:02g |
21-
| A*01:01~B*07:02+A*01:01~B*07:02\|A*02:01~B*07:02+A*02:01~B*07:02 | lg | A*01:01g~B*07:02g+A*01:01g~B*07:02g\|A*02:01g~B*07:02g+A*02:01g~B*07:02g |
9+
Examples:
10+
| Allele | Level | Redux Allele |
11+
| A*01:01:01:01+A*01:01:01:01 | G | A*01:01:01G+A*01:01:01G |
12+
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | G | HLA-A*01:01:01G+HLA-A*01:01:01G |
13+
| A*01:01:01:01+A*01:01:01:01 | lg | A*01:01g+A*01:01g |
14+
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | lg | HLA-A*01:01g+HLA-A*01:01g |
15+
| A*01:01:01:01+A*01:01:01:01 | lgx | A*01:01+A*01:01 |
16+
| HLA-A*01:01:01:01+HLA-A*01:01:01:01 | lgx | HLA-A*01:01+HLA-A*01:01 |
17+
| A*01:01+A*01:01^B*07:02+B*07:02 | G | A*01:01:01G+A*01:01:01G^B*07:02:01G+B*07:02:01G |
18+
| A*01:01+A*01:01^B*07:02+B*07:02 | lg | A*01:01g+A*01:01g^B*07:02g+B*07:02g |
19+
| A*01:01~B*07:02+A*01:01~B*07:02 | G | A*01:01:01G~B*07:02:01G+A*01:01:01G~B*07:02:01G |
20+
| A*01:01~B*07:02+A*01:01~B*07:02 | lg | A*01:01g~B*07:02g+A*01:01g~B*07:02g |
21+
| A*01:01~B*07:02+A*01:01~B*07:02\|A*02:01~B*07:02+A*02:01~B*07:02 | lg | A*01:01g~B*07:02g+A*01:01g~B*07:02g\|A*02:01g~B*07:02g+A*02:01g~B*07:02g |

tests/features/mac.feature

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Feature: MAC (Multiple Allele Code)
22

3-
Scenario Outline:
3+
Scenario Outline:
44

5-
Given the allele as <Allele>
6-
When reducing on the <Level> level (ambiguous)
7-
Then the reduced allele is found to be <Redux Allele>
5+
Given the allele as <Allele>
6+
When reducing on the <Level> level (ambiguous)
7+
Then the reduced allele is found to be <Redux Allele>
88

9-
Examples:
10-
| Allele | Level | Redux Allele |
11-
| A*01:AB | G | A*01:01:01G/A*01:02 |
12-
| A*01:AB | lgx | A*01:01/A*01:02 |
13-
| HLA-A*01:AB | G | HLA-A*01:01:01G/HLA-A*01:02 |
14-
| HLA-A*01:AB | lgx | HLA-A*01:01/HLA-A*01:02 |
9+
Examples:
10+
| Allele | Level | Redux Allele |
11+
| A*01:AB | G | A*01:01:01G/A*01:02 |
12+
| A*01:AB | lgx | A*01:01/A*01:02 |
13+
| HLA-A*01:AB | G | HLA-A*01:01:01G/HLA-A*01:02 |
14+
| HLA-A*01:AB | lgx | HLA-A*01:01/HLA-A*01:02 |

tests/features/p_g_group.feature

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Feature: P and G Groups
22

3-
Scenario Outline:
3+
Scenario Outline:
44

5-
Given the allele as <Allele>
6-
When reducing on the <Level> level (ambiguous)
7-
Then the reduced allele is found to be <Redux Allele>
5+
Given the allele as <Allele>
6+
When reducing on the <Level> level (ambiguous)
7+
Then the reduced allele is found to be <Redux Allele>
88

9-
Examples:
10-
| Allele | Level | Redux Allele |
11-
| A*02:01P | lgx | A*02:01 |
12-
| A*02:01:01G | lgx | A*02:01 |
9+
Examples:
10+
| Allele | Level | Redux Allele |
11+
| A*02:01P | lgx | A*02:01 |
12+
| A*02:01:01G | lgx | A*02:01 |

tests/steps/redux_allele.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
from behave import given, when, then
12
from hamcrest import assert_that, is_
23

4+
35
@given('the allele as {allele}')
46
def step_impl(context, allele):
57
context.allele = allele
68

9+
710
@when('reducing on the {level} level')
811
def step_impl(context, level):
912
context.level = level
1013
context.redux_allele = context.ard.redux(context.allele, level)
1114

15+
1216
@when('reducing on the {level} level (ambiguous)')
1317
def step_impl(context, level):
1418
context.level = level
1519
context.redux_allele = context.ard.redux_gl(context.allele, level)
1620

21+
1722
@then('the reduced allele is found to be {redux_allele}')
1823
def step_impl(context, redux_allele):
19-
assert_that(context.redux_allele, is_(redux_allele))
24+
assert_that(context.redux_allele, is_(redux_allele))

0 commit comments

Comments
 (0)