Skip to content

Commit 741c8fe

Browse files
committed
Refactor similar_alleles code to ARD.
1 parent a63042b commit 741c8fe

File tree

2 files changed

+59
-36
lines changed

2 files changed

+59
-36
lines changed

pyard/ard.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@
2626
import sqlite3
2727
import sys
2828
from collections import Counter
29-
from typing import Iterable, List
29+
from typing import Iterable, List, Union
3030

3131
from . import broad_splits, smart_sort
3232
from . import data_repository as dr
3333
from . import db
34-
from .exceptions import InvalidAlleleError, InvalidMACError, InvalidTypingError
35-
from .misc import (
36-
get_n_field_allele,
37-
get_2field_allele,
38-
is_2_field_allele,
39-
validate_reduction_type,
40-
)
4134
from .constants import (
4235
HLA_regex,
4336
VALID_REDUCTION_TYPES,
4437
expression_chars,
4538
DEFAULT_CACHE_SIZE,
4639
G_GROUP_LOCI,
4740
)
41+
from .exceptions import InvalidAlleleError, InvalidMACError, InvalidTypingError
42+
from .misc import (
43+
get_n_field_allele,
44+
get_2field_allele,
45+
is_2_field_allele,
46+
validate_reduction_type,
47+
)
4848

4949
default_config = {
5050
"reduce_serology": True,
@@ -837,3 +837,47 @@ def get_db_version(self) -> str:
837837
@return:
838838
"""
839839
return dr.get_db_version(self.db_connection)
840+
841+
def similar_alleles(self, prefix: str) -> Union[List, None]:
842+
"""
843+
Given a prefix, find similar alleles or MACs starting with the prefix.
844+
The minimum prefix needs to specify is the locus with a `*`,
845+
and a first field of the allele/MAC.
846+
847+
@param prefix: The prefix for allele or MAC
848+
@return: List of alleles/MACs that start with the prefix
849+
"""
850+
851+
if "*" not in prefix: # Only for those that have locus
852+
return None
853+
854+
locus, fields = prefix.split("*")
855+
# if at least a field is specified after *
856+
if fields:
857+
# Will check only for and after 2 fields
858+
if len(fields.split(":")) == 2:
859+
first_field, mac_prefix = fields.split(":")
860+
if mac_prefix.isalpha(): # Check for MACs
861+
similar_mac_names = db.similar_mac(self.db_connection, mac_prefix)
862+
if similar_mac_names:
863+
locus_prefix = f"{locus}*{first_field}"
864+
# Build all the mac codes with the prefix
865+
mac_codes = [
866+
f"{locus_prefix}:{code}" for code in similar_mac_names
867+
]
868+
# show only the valid macs
869+
real_mac_codes = sorted(
870+
filter(lambda mac: self.is_mac(mac), mac_codes)
871+
)
872+
return real_mac_codes
873+
874+
# find similar alleles
875+
similar_allele_names = db.similar_alleles(self.db_connection, prefix)
876+
if similar_allele_names:
877+
alleles = sorted(
878+
similar_allele_names,
879+
key=functools.cmp_to_key(smart_sort.smart_sort_comparator),
880+
)
881+
return alleles
882+
883+
return None

scripts/pyard

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,16 @@ from pyard.exceptions import InvalidAlleleError, InvalidTypingError, InvalidMACE
3333
from pyard.misc import get_data_dir, get_imgt_version
3434

3535

36-
def find_similar_alleles(ard, prefix):
37-
if "*" in prefix: # Only for those that have locus
38-
locus, fields = prefix.split("*")
39-
if fields: # Only if at least a field is specified after *
40-
if len(fields.split(":")) == 2: # Check for MACs
41-
first_field, mac_prefix = fields.split(":")
42-
if mac_prefix.isalpha():
43-
similar_mac_names = similar_mac(ard.db_connection, mac_prefix)
44-
if similar_mac_names:
45-
locus_prefix = f"{locus}*{first_field}"
46-
# TODO: validate all the mac codes with the prefix
47-
# show only the valid macs
48-
for code in sorted(similar_mac_names):
49-
print(f"{locus_prefix}:{code}")
50-
else:
51-
# Nothing after *
52-
sys.exit(2)
36+
def find_similar_alleles(prefix):
37+
alleles = ard.similar_alleles(prefix)
38+
if alleles:
39+
for allele in alleles:
40+
print(allele)
41+
sys.exit(0)
5342
else:
5443
# No *
5544
sys.exit(1)
5645

57-
# find similar alleles
58-
similar_allele_names = similar_alleles(ard.db_connection, prefix)
59-
if similar_allele_names:
60-
for allele in sorted(
61-
similar_allele_names,
62-
key=functools.cmp_to_key(smart_sort.smart_sort_comparator),
63-
):
64-
print(allele)
65-
sys.exit(0)
66-
6746

6847
def lookup_mac_codes():
6948
global e
@@ -202,7 +181,7 @@ if __name__ == "__main__":
202181

203182
# Handle --similar option
204183
if args.similar_allele:
205-
find_similar_alleles(ard, args.similar_allele)
184+
find_similar_alleles(args.similar_allele)
206185

207186
try:
208187
if args.cwd:

0 commit comments

Comments
 (0)