Skip to content

Commit dbfac4d

Browse files
authored
Merge pull request #190 from pbashyal-nmdp/db_version
Store IMGT DB Version Number
2 parents d327445 + cf78863 commit dbfac4d

File tree

8 files changed

+136
-6
lines changed

8 files changed

+136
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ A*01:01/A*01:02
189189
$ pyard --gl 'DRB1*08:XX' -r G
190190
DRB1*08:01:01G/DRB1*08:02:01G/DRB1*08:03:02G/DRB1*08:04:01G/DRB1*08:05/ ...
191191

192-
$ pyard -v 3290 --gl 'A1' -r lgx # For a particular version of DB
192+
$ pyard -i 3290 --gl 'A1' -r lgx # For a particular version of DB
193193
A*01:01/A*01:02/A*01:03/A*01:06/A*01:07/A*01:08/A*01:09/A*01:10/A*01:12/ ...
194194
```
195195
### Batch Reduce a CSV file

api-spec.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,31 @@ info:
66
servers:
77
- url: 'http://localhost:8080'
88
tags:
9+
- name: Database
10+
description: IPD-IMGT/HLA DB Information
911
- name: ARD Reduction
1012
description: Reduce GL String to ARD
1113
- name: MAC Expansion
1214
description: Expand MAC to alleles
1315
- name: DRBX Blender
1416
description: Blend DRBX based on DRB1 and DRB3/4/5
1517
paths:
18+
/version:
19+
get:
20+
tags:
21+
- Database
22+
operationId: api.version_controller
23+
summary: IPD-IMGT/HLA Version
24+
description: |
25+
Get IPD-IMGT/HLA DB Version used for this service
26+
responses:
27+
200:
28+
description: IPD-IMGT/HLA version number
29+
content:
30+
application/json:
31+
schema:
32+
type: integer
33+
example: 3440
1634
/redux:
1735
post:
1836
tags:

api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ def drbx_blender_controller():
7979
return {"DRBX_blend": blended_drbx}
8080
except DRBXBlenderError as e:
8181
return {"found": e.found, "expected": e.expected}
82+
83+
84+
def version_controller():
85+
version = ard.get_db_version()
86+
return {"version": version}, 200

pyard/data_repository.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,40 @@ def generate_v2_to_v3_mapping(db_connection: sqlite3.Connection, imgt_version):
672672
dictionary=v2_to_v3_example,
673673
columns=("v2", "v3"),
674674
)
675+
676+
677+
def set_db_version(db_connection: sqlite3.Connection, imgt_version):
678+
"""
679+
Set the IMGT database version number as a user_version string in
680+
the database itself.
681+
682+
:param db_connection: Active SQLite Connection
683+
:param imgt_version: current imgt_version
684+
"""
685+
# If version already exists, don't reset
686+
version = db.get_user_version(db_connection)
687+
if version:
688+
return version
689+
690+
version = imgt_version
691+
692+
if imgt_version == "Latest":
693+
from urllib.request import urlopen
694+
695+
response = urlopen(
696+
"https://raw.githubusercontent.com/ANHIG/IMGTHLA/Latest/release_version.txt"
697+
)
698+
for line in response:
699+
l = line.decode("utf-8")
700+
if l.find("version:") != -1:
701+
# Version line looks like
702+
# # version: IPD-IMGT/HLA 3.51.0
703+
version = l.split()[-1].replace(".", "")
704+
705+
db.set_user_version(db_connection, int(version))
706+
print("Version:", version)
707+
return db.get_user_version(db_connection)
708+
709+
710+
def get_db_version(db_connection: sqlite3.Connection):
711+
return db.get_user_version(db_connection)

pyard/db.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,36 @@ def similar_alleles(connection: sqlite3.Connection, allele_name: str) -> Set[str
323323
# Get out the first value of the tuple from the result list
324324
alleles = set(map(lambda t: t[0], result))
325325
return alleles
326+
327+
328+
def get_user_version(connection: sqlite3.Connection) -> int:
329+
"""
330+
Retrieve user_version from db
331+
332+
:connection: sqlite3.Connection: SQLite DB Connection
333+
"""
334+
query = "PRAGMA user_version"
335+
cursor = connection.execute(query)
336+
result = cursor.fetchone()
337+
version = result[0]
338+
cursor.close()
339+
340+
if version:
341+
return version
342+
return None
343+
344+
345+
def set_user_version(connection: sqlite3.Connection, version: int):
346+
"""
347+
Save the version number as user_version in db
348+
349+
:connection: sqlite3.Connection:
350+
:version: int: version number to store
351+
@return:
352+
"""
353+
query = f"PRAGMA user_version={version}"
354+
cursor = connection.execute(query)
355+
# commit transaction - writes to the db
356+
connection.commit()
357+
# close the cursor
358+
cursor.close()

pyard/pyard.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def __init__(
123123
dr.generate_serology_mapping(self.db_connection, imgt_version)
124124
# Load V2 to V3 mappings
125125
dr.generate_v2_to_v3_mapping(self.db_connection, imgt_version)
126+
# Save IMGT database version
127+
dr.set_db_version(self.db_connection, imgt_version)
126128

127129
# Close the current read-write db connection
128130
self.db_connection.close()
@@ -713,3 +715,10 @@ def refresh_mac_codes(self) -> None:
713715
:return: None
714716
"""
715717
dr.generate_mac_codes(self.db_connection, True)
718+
719+
def get_db_version(self) -> str:
720+
"""
721+
Get the IMGT DB Version Number
722+
@return:
723+
"""
724+
return dr.get_db_version(self.db_connection)

scripts/pyard

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# > http://www.opensource.org/licenses/lgpl-license.php
2323
#
2424
import argparse
25+
import sys
2526

2627
import pyard
2728

@@ -39,13 +40,29 @@ def get_imgt_version(imgt_version):
3940

4041
if __name__ == "__main__":
4142
parser = argparse.ArgumentParser(
42-
usage="""[-v <IMGT DB Version>] [gl-string redux_type]""",
43-
description="""py-ard tool to redux GL String""",
43+
description="""
44+
py-ard tool to redux GL String
45+
""",
4446
)
45-
parser.add_argument("-v", "--imgt-version", dest="imgt_version")
46-
parser.add_argument("--gl", required=True, dest="gl_string")
4747
parser.add_argument(
48-
"-r", choices=pyard.pyard.reduction_types, required=True, dest="redux_type"
48+
"-v",
49+
"--version",
50+
dest="version",
51+
action="store_true",
52+
help="IPD-IMGT/HLA DB Version number",
53+
)
54+
parser.add_argument(
55+
"-i",
56+
"--imgt-version",
57+
dest="imgt_version",
58+
help="IPD-IMGT/HLA db to use for redux",
59+
)
60+
parser.add_argument("--gl", dest="gl_string", help="GL String to reduce")
61+
parser.add_argument(
62+
"-r",
63+
choices=pyard.pyard.reduction_types,
64+
dest="redux_type",
65+
help="Reduction Method",
4966
)
5067

5168
args = parser.parse_args()
@@ -56,5 +73,10 @@ if __name__ == "__main__":
5673
else:
5774
ard = pyard.ARD()
5875

76+
if args.version:
77+
version = ard.get_db_version()
78+
print(f"IPD-IMGT/HLA version:", version)
79+
sys.exit(0)
80+
5981
print(ard.redux_gl(args.gl_string, args.redux_type))
6082
del ard

tests/test_pyard.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939

4040
class TestPyArd(unittest.TestCase):
41+
db_version = None
42+
ard = None
43+
4144
@classmethod
4245
def setUpClass(cls) -> None:
4346
cls.db_version = "3440"
@@ -157,3 +160,6 @@ def test_allele_duplicated(self):
157160
allele_code = "C*02:ACMGS"
158161
allele_code_rx = self.ard.redux_gl(allele_code, "lgx")
159162
self.assertEqual(allele_code_rx, "C*02:02")
163+
164+
def test_imgt_db_version(self):
165+
self.assertEqual(self.ard.get_db_version(), int(TestPyArd.db_version))

0 commit comments

Comments
 (0)