Skip to content

Commit 3f601e5

Browse files
committed
Refactor similar_alleles code to ARD.
1 parent 5b6037f commit 3f601e5

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,
@@ -851,3 +851,47 @@ def get_db_version(self) -> str:
851851
@return:
852852
"""
853853
return dr.get_db_version(self.db_connection)
854+
855+
def similar_alleles(self, prefix: str) -> Union[List, None]:
856+
"""
857+
Given a prefix, find similar alleles or MACs starting with the prefix.
858+
The minimum prefix needs to specify is the locus with a `*`,
859+
and a first field of the allele/MAC.
860+
861+
@param prefix: The prefix for allele or MAC
862+
@return: List of alleles/MACs that start with the prefix
863+
"""
864+
865+
if "*" not in prefix: # Only for those that have locus
866+
return None
867+
868+
locus, fields = prefix.split("*")
869+
# if at least a field is specified after *
870+
if fields:
871+
# Will check only for and after 2 fields
872+
if len(fields.split(":")) == 2:
873+
first_field, mac_prefix = fields.split(":")
874+
if mac_prefix.isalpha(): # Check for MACs
875+
similar_mac_names = db.similar_mac(self.db_connection, mac_prefix)
876+
if similar_mac_names:
877+
locus_prefix = f"{locus}*{first_field}"
878+
# Build all the mac codes with the prefix
879+
mac_codes = [
880+
f"{locus_prefix}:{code}" for code in similar_mac_names
881+
]
882+
# show only the valid macs
883+
real_mac_codes = sorted(
884+
filter(lambda mac: self.is_mac(mac), mac_codes)
885+
)
886+
return real_mac_codes
887+
888+
# find similar alleles
889+
similar_allele_names = db.similar_alleles(self.db_connection, prefix)
890+
if similar_allele_names:
891+
alleles = sorted(
892+
similar_allele_names,
893+
key=functools.cmp_to_key(smart_sort.smart_sort_comparator),
894+
)
895+
return alleles
896+
897+
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)