Skip to content

Commit 578c697

Browse files
committed
Consolidate alleles and code mappings into their own.
1 parent 84a7dc1 commit 578c697

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

pyard/ard.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ def __init__(
9696
self.ars_mappings = dr.generate_ars_mapping(self.db_connection, imgt_version)
9797
# Load Alleles and XX Codes
9898
(
99-
self.valid_alleles,
100-
self.who_alleles,
101-
self.xx_codes,
102-
self.who_group,
103-
self.exp_alleles,
99+
self.code_mappings,
100+
self.allele_group,
104101
) = dr.generate_alleles_and_xx_codes_and_who(
105102
self.db_connection, imgt_version, self.ars_mappings
106103
)
107104

108105
# Generate short nulls from WHO mapping
109-
self.shortnulls = dr.generate_short_nulls(self.db_connection, self.who_group)
106+
self.shortnulls = dr.generate_short_nulls(
107+
self.db_connection, self.code_mappings.who_group
108+
)
110109

111110
# Load Serology mappings
112111
broad_splits.broad_splits_ser_mapping = (
@@ -214,8 +213,10 @@ def _redux_allele(
214213
# new redux_type which is full WHO expansion
215214
if self._is_who_allele(allele):
216215
return allele
217-
if allele in self.who_group:
218-
return self.redux("/".join(self.who_group[allele]), redux_type)
216+
if allele in self.code_mappings.who_group:
217+
return self.redux(
218+
"/".join(self.code_mappings.who_group[allele]), redux_type
219+
)
219220
else:
220221
return allele
221222
elif redux_type == "exon":
@@ -354,11 +355,13 @@ def redux(self, glstring: str, redux_type: VALID_REDUCTION_TYPES) -> str:
354355
if self.is_XX(glstring, loc_antigen, code):
355356
if is_hla_prefix:
356357
reduced_alleles = self.redux(
357-
"/".join(self.xx_codes[loc_antigen]), redux_type
358+
"/".join(self.code_mappings.xx_codes[loc_antigen]), redux_type
358359
)
359360
return "/".join(["HLA-" + a for a in reduced_alleles.split("/")])
360361
else:
361-
return self.redux("/".join(self.xx_codes[loc_antigen]), redux_type)
362+
return self.redux(
363+
"/".join(self.code_mappings.xx_codes[loc_antigen]), redux_type
364+
)
362365

363366
# Handle MAC
364367
if self._config["reduce_MAC"] and self.is_mac(glstring):
@@ -403,7 +406,7 @@ def is_XX(self, glstring: str, loc_antigen: str = None, code: str = None) -> boo
403406
loc_antigen, code = loc_allele[0], loc_allele[1]
404407
else:
405408
return False
406-
return code == "XX" and loc_antigen in self.xx_codes
409+
return code == "XX" and loc_antigen in self.code_mappings.xx_codes
407410

408411
def is_serology(self, allele: str) -> bool:
409412
"""
@@ -469,15 +472,15 @@ def _is_who_allele(self, allele):
469472
:param allele: Allele to test
470473
:return: bool to indicate if allele is valid
471474
"""
472-
return allele in self.who_alleles
475+
return allele in self.allele_group.who_alleles
473476

474477
def _is_valid_allele(self, allele):
475478
"""
476479
Test if allele is valid in the current imgt database
477480
:param allele: Allele to test
478481
:return: bool to indicate if allele is valid
479482
"""
480-
return allele in self.valid_alleles
483+
return allele in self.allele_group.alleles
481484

482485
def is_shortnull(self, allele):
483486
"""
@@ -494,7 +497,7 @@ def is_exp_allele(self, allele):
494497
:param allele: Allele to test
495498
:return: bool to indicate if allele is valid
496499
"""
497-
return allele in self.exp_alleles
500+
return allele in self.allele_group.exp_alleles
498501

499502
def _get_alleles(self, code, locus_antigen) -> Iterable[str]:
500503
"""

pyard/data_repository.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
load_latest_version,
3636
)
3737
from .constants import expression_chars
38-
from .mappings import ars_mapping_tables, ARSMapping, code_mapping_tables
38+
from .mappings import (
39+
ars_mapping_tables,
40+
ARSMapping,
41+
code_mapping_tables,
42+
AlleleGroups,
43+
CodeMappings,
44+
allele_tables,
45+
)
3946
from .misc import (
4047
get_2field_allele,
4148
get_3field_allele,
@@ -63,7 +70,7 @@ def expression_reduce(df):
6370
return None
6471

6572

66-
def generate_ars_mapping(db_connection: sqlite3.Connection, imgt_version):
73+
def generate_ars_mapping(db_connection: sqlite3.Connection, imgt_version) -> ARSMapping:
6774
if db.tables_exist(db_connection, ars_mapping_tables):
6875
return db.load_ars_mappings(db_connection)
6976

@@ -162,7 +169,7 @@ def generate_ars_mapping(db_connection: sqlite3.Connection, imgt_version):
162169
def generate_alleles_and_xx_codes_and_who(
163170
db_connection: sqlite3.Connection, imgt_version, ars_mappings
164171
):
165-
if db.tables_exist(db_connection, code_mapping_tables):
172+
if db.tables_exist(db_connection, code_mapping_tables + allele_tables):
166173
return db.load_code_mappings(db_connection)
167174

168175
import pandas as pd
@@ -256,7 +263,14 @@ def generate_alleles_and_xx_codes_and_who(
256263
who_alleles,
257264
)
258265

259-
return valid_alleles, who_alleles, xx_codes, who_group, exp_alleles
266+
return (
267+
CodeMappings(xx_codes=xx_codes, who_group=who_group),
268+
AlleleGroups(
269+
alleles=valid_alleles, who_alleles=who_alleles, exp_alleles=exp_alleles
270+
),
271+
)
272+
273+
# return valid_alleles, who_alleles, xx_codes, who_group, exp_alleles
260274

261275

262276
def generate_short_nulls(db_connection, who_group):

pyard/db.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import sqlite3
2525
from typing import Tuple, Dict, Set, List
2626

27-
from .mappings import ARSMapping
27+
from .mappings import ARSMapping, CodeMappings, AlleleGroups
2828
from .misc import get_imgt_db_versions, get_default_db_directory
2929

3030

@@ -467,7 +467,12 @@ def load_code_mappings(db_connection):
467467
xx_codes = {k: v.split("/") for k, v in xx_codes.items()}
468468
exp_alleles = load_dict(db_connection, "exp_alleles", ("exp_allele", "allele_list"))
469469
exp_alleles = {k: v.split("/") for k, v in exp_alleles.items()}
470-
return valid_alleles, who_alleles, xx_codes, who_group, exp_alleles
470+
return (
471+
CodeMappings(xx_codes=xx_codes, who_group=who_group),
472+
AlleleGroups(
473+
alleles=valid_alleles, who_alleles=who_alleles, exp_alleles=exp_alleles
474+
),
475+
)
471476

472477

473478
def load_shortnulls(db_connection):

pyard/mappings.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
"exon_group",
1010
"p_not_g",
1111
]
12+
1213
code_mapping_tables = [
14+
"xx_codes",
15+
"who_group",
16+
]
17+
18+
allele_tables = [
1319
"alleles",
1420
"exp_alleles",
15-
"xx_codes",
1621
"who_alleles",
17-
"who_group",
1822
]
1923

2024
ARSMapping = namedtuple("ARSMapping", ars_mapping_tables)
25+
CodeMappings = namedtuple("CodeMappings", code_mapping_tables)
26+
AlleleGroups = namedtuple("AlleleGroups", allele_tables)

scripts/pyard-status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ if __name__ == "__main__":
9696
for table in (
9797
pyard.mappings.ars_mapping_tables
9898
+ pyard.mappings.code_mapping_tables
99+
+ pyard.mappings.allele_tables
99100
+ ["mac_codes"]
100101
):
101102
if db.table_exists(db_connection, table):

0 commit comments

Comments
 (0)