-
Notifications
You must be signed in to change notification settings - Fork 9
/
HLAtoSequences.py
478 lines (310 loc) · 17 KB
/
HLAtoSequences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
import os, sys, re
import argparse, textwrap
########## < Core Global Variables > ##########
std_MAIN_PROCESS_NAME = "\n[%s]: " % (os.path.basename(__file__))
std_ERROR_MAIN_PROCESS_NAME = "\n[%s::ERROR]: " % (os.path.basename(__file__))
std_WARNING_MAIN_PROCESS_NAME = "\n[%s::WARNING]: " % (os.path.basename(__file__))
HLA_names = ["A", "B", "C", "DPA1", "DPB1", "DQA1", "DQB1", "DRB1"]
HLA_names2 = [0, 2, 1, 7, 5, 6, 3, 4] # Read in the order of `HLA_names`, Write in the order of `HLA_names2` (to be compatible with old version of Dictionary).
isREVERSE = {'A': False, 'C': True, 'B': True, 'DRB1': True, 'DQA1': False, 'DQB1': True, 'DPA1': True, 'DPB1': False}
def HLAtoSequences(_chped, _dictionary, _type, _out, __previous_version=False, __asLump=False):
### Intermediate path.
_out = _out if not _out.endswith('/') else _out.rstrip('/')
if bool(os.path.dirname(_out)):
INTERMEDIATE_PATH = os.path.dirname(_out)
os.makedirs(INTERMEDIATE_PATH, exist_ok=True)
else:
INTERMEDIATE_PATH = "./"
########## < Argument checking > ##########
# (1) ped file existence
if not os.path.isfile(_chped):
print(std_MAIN_PROCESS_NAME + "Given chped file doen't exist. Please check it again.\n")
sys.exit()
# (2) HLA DICTIONARY file
if not os.path.isfile(_dictionary):
print(std_MAIN_PROCESS_NAME + "Given dictionary file doen't exist. Please check it againg.\n")
sys.exit()
# (3) Chekcing `_type`
if not (_type == "AA" or _type == "SNPS"):
print(std_MAIN_PROCESS_NAME + "Given value for argument `_type` has wrong value. Please check it again.\n")
sys.exit()
##### < Core Variables > #####
HLA_DICTIONARY_byHLA = {HLA_names[i]: {} for i in range(0, len(HLA_names))} # Initialization.
HLA_SEQ_LENGTH = {HLA_names[i]: -1 for i in range(0, len(HLA_names))}
HLA_INS_byHLA = {HLA_names[i]: {} for i in range(0, len(HLA_names))} # Initialization.
haveInsertion= {HLA_names[i]: -1 for i in range(0, len(HLA_names))}
if __previous_version:
##### < [1] Loading HLA Dictionary > #####
with open(_dictionary, "r") as f_dictionary:
count = 0
for line in f_dictionary:
t_line = re.split(r'\s+', line.rstrip('\n'))
# ex1) (AA) ['B*58:01:01', 'MLVMAPRTVLLLLSAALALTETWAG...', 'x'] (len == 3)
# ex2) (SNPS) ['B*58:01:01', 'CTAGTCCTGCTTCAGGGTCCGGGGCCCG...'] (len == 2)
# print(t_line)
for i in range(0, len(HLA_names)):
if re.match(r'{}:'.format(HLA_names[i]), t_line[0]):
HLA_DICTIONARY_byHLA[HLA_names[i]][t_line[0]] = t_line[1] # Sequence information.
if HLA_SEQ_LENGTH[HLA_names[i]] == -1:
HLA_SEQ_LENGTH[HLA_names[i]] = len(t_line[1])
if _type == "AA":
HLA_INS_byHLA[HLA_names[i]][t_line[0]] = t_line[2] # Insertion information.
if haveInsertion[HLA_names[i]] == -1:
haveInsertion[HLA_names[i]] = bool(t_line[2])
break # If a line of given dictionary belongs to either HLA, then checking whether it belongs to other HLAs is useless.
count += 1
# if count > 5 : break
# # Result check
# for i in range(0, len(HLA_names)):
# print("\n{} :\n".format(HLA_names[i]))
# for k, v in HLA_DICTIONARY_byHLA[HLA_names[i]].items():
# print("{} : {}".format(k, v))
#
# for k, v in HLA_SEQ_LENGTH.items():
# print("The length of HLA-{} : {}".format(k, v))
#
# print("Insertion check : {}".format(haveInsertion))
##### < [2] Transforming each HLA alleles to corresponding sequences > #####
with open(_out + ".{}.ped".format(_type), 'w') as f_output:
f_output.writelines(GenerateLines(_chped, _type, HLA_DICTIONARY_byHLA, HLA_SEQ_LENGTH, HLA_INS_byHLA, haveInsertion,
__asLump=__asLump))
else:
##### < [1] Loading HLA Dictionary > #####
with open(_dictionary, "r") as f_dictionary:
count = 0
for line in f_dictionary:
t_line = re.split(r'\s+', line.rstrip('\n'))
# ex1) (AA) ['B*58:01:01', 'MLVMAPRTVLLLLSAALALTETWAG...'] (len == 2)
# ex2) (SNPS) ['B*58:01:01', 'CTAGTCCTGCTTCAGGGTCCGGGGCCCG...'] (len == 2)
# print(t_line)
for i in range(0, len(HLA_names)):
if re.match(r'{}\*'.format(HLA_names[i]), t_line[0]):
HLA_DICTIONARY_byHLA[HLA_names[i]][t_line[0]] = t_line[1] # Sequence information.
if HLA_SEQ_LENGTH[HLA_names[i]] == -1:
HLA_SEQ_LENGTH[HLA_names[i]] = len(t_line[1])
break # If a line of given dictionary belongs to either HLA, then checking whether it belongs to other HLAs is useless.
count += 1
# if count > 5 : break
# # Result check
# for i in range(0, len(HLA_names)):
# print("\n{} :".format(HLA_names[i]))
#
# idx = 0
# for k, v in HLA_DICTIONARY_byHLA[HLA_names[i]].items():
# print("{} : {}".format(k, v))
#
# idx += 1
# if idx > 10 : break
#
# for k, v in HLA_SEQ_LENGTH.items():
# print("The length of HLA-{} : {}".format(k, v))
# The insertion will be precessed in the dictionary generation in advance.
# print("Insertion check : {}".format(haveInsertion))
##### < [2] Transforming each HLA alleles to corresponding sequences > #####
with open(_out + ".{}.ped".format(_type), 'w') as f_output:
f_output.writelines(GenerateLines(_chped, _type, HLA_DICTIONARY_byHLA, HLA_SEQ_LENGTH, HLA_INS_byHLA, haveInsertion,
__previous_version=__previous_version, __asLump=__asLump))
def GenerateLines(_chped, _type, _dict_seq, _seq_length, _dict_ins, _haveIns,
__previous_version=True, __asLump=False):
with open(_chped, "r") as f:
for line in f:
t_line = re.split(r'\s+', line.rstrip('\n'))
# print(t_line)
"""
[0,1,2,3,4,5] := ped file information
[6,7] := HLA-A,
[8,9] := HLA-B,
...,
[20, 21] := HLA-DRB1
"""
t_iterator = HLA_names2 if __previous_version else range(0, len(HLA_names))
__ped_info__ = '\t'.join(t_line[:6])
__genomic_part__ = '\t'.join([
BringSequence2(t_line[(2 * i + 6)], t_line[(2 * i + 7)], _type, HLA_names[i],
_dict_seq[HLA_names[i]], _seq_length[HLA_names[i]],
_dict_ins[HLA_names[i]], _haveIns,
__previous_version=__previous_version, __asLump=__asLump) for i in t_iterator
])
# mem_p2 = process.memory_info().rss / 1024 ** 2
# print("{}(Mb)".format(mem_p2 - mem_p1))
yield '\t'.join([__ped_info__, __genomic_part__]) + "\n"
def BringSequence2(_HLA_allele1, _HLA_allele2, _type, _hla,
_dict_seq, _seq_length,
_dict_ins, _haveIns,
__previous_version = True,
__asLump=False):
if __previous_version:
# print("al1 : {}\nal2 : {}".format(_HLA_allele1, _HLA_allele2))
# Finding the corresponding sequence and insertion marker of `_HLA_allele1`.
try:
Seq1 = _dict_seq[_HLA_allele1]
if _type == "AA" and isREVERSE[_hla]:
Seq1 = Seq1[::-1]
except KeyError:
Seq1 = "-1" # fail
# Same job for `_HLA_allele2`.
try:
Seq2 = _dict_seq[_HLA_allele2]
if _type == "AA" and isREVERSE[_hla]:
Seq2 = Seq2[::-1]
except KeyError:
Seq2 = "-1" # fail
# print("Corresponding Seqs : \n{}\n{}".format(Seq1, Seq2))
### Main sequence information processing
if not __asLump:
if Seq1 != "-1" and Seq2 != "-1":
# Only when both HLA alleles can get the corresponding HLA sequence information.
l_temp = []
for i in range(0, len(Seq1)):
l_temp.append(Seq1[i])
l_temp.append(Seq2[i])
# Reversing
__return__ = '\t'.join(l_temp)
else:
__return__ = '\t'.join(["0" for z in range(0, 2 * _seq_length)])
### Insertion part processing.
if _type == "AA" and _haveIns[_hla]:
# One important assumption : "Every insertion part is just one, single character.(ex 'x', 'P', 'A')"
try:
ins1 = _dict_ins[_HLA_allele1]
except KeyError:
ins1 = ""
try:
ins2 = _dict_ins[_HLA_allele2]
except KeyError:
ins2 = ""
if bool(ins1) and bool(ins2):
__insertions__ = '\t'.join([ins1, ins2])
else:
__insertions__ = '\t'.join(["0", "0"])
__return__ = '\t'.join([__return__, __insertions__])
return __return__
else:
# As each Strings.
t_Seq1 = ""
t_Seq2 = ""
if Seq1 != "-1" and Seq2 != "-1":
t_Seq1 = Seq1
t_Seq2 = Seq2
else:
t_Seq1 = ["0" for z in range(0, _seq_length)]
t_Seq2 = ["0" for z in range(0, _seq_length)]
### Insertion part processing.
if _type == "AA" and _haveIns[_hla]:
# One important assumption : "Every insertion part is just one, single character.(ex 'x', 'P', 'A')"
try:
ins1 = _dict_ins[_HLA_allele1]
except KeyError:
ins1 = ""
try:
ins2 = _dict_ins[_HLA_allele2]
except KeyError:
ins2 = ""
if bool(ins1):
t_Seq1= ''.join([t_Seq1, ins1])
if bool(ins2):
t_Seq2 = ''.join([t_Seq2, ins2])
return str([[_HLA_allele1, t_Seq1], [_HLA_allele2, t_Seq2]])
else:
### Dealing with generalized 4-field HLA alleles.
# `_HLA_allele1`.
try:
Seq1 = _dict_seq[_HLA_allele1]
except KeyError:
Seq1 = "-1" # fail
# `_HLA_allele2`.
try:
Seq2 = _dict_seq[_HLA_allele2]
except KeyError:
Seq2 = "-1" # fail
# No reversing HLA sequences.
if not __asLump:
### Main sequence information processing
if Seq1 != "-1" and Seq2 != "-1":
# Only when both HLA alleles can get the corresponding HLA sequence information.
l_temp = []
for i in range(0, len(Seq1)):
l_temp.append(Seq1[i])
l_temp.append(Seq2[i])
# Reversing
__return__ = '\t'.join(l_temp)
else:
__return__ = '\t'.join(["0" for z in range(0, 2 * _seq_length)])
return __return__
else:
# As each Strings.
t_Seq1 = ""
t_Seq2 = ""
if Seq1 != "-1" and Seq2 != "-1":
t_Seq1 = Seq1
t_Seq2 = Seq2
else:
t_Seq1 = ''.join(["0" for z in range(0, _seq_length)])
t_Seq2 = ''.join(["0" for z in range(0, _seq_length)])
return str([[_HLA_allele1, t_Seq1], [_HLA_allele2, t_Seq2]])
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=textwrap.dedent('''\
#########################################################################################
Original Author: Sherman Jia, 2012
HLAtoSequences.py
- This script Converts HLA alleles (in .ped file format) to amino acid or DNA sequences
Input file should contain: FID, IID, pID, mID, sex, pheno, HLA-A (2), B (2), C (2),
DPA1 (2), DPB1 (2), DQA1 (2), DQB1 (2), DRB1 (2) ... Broad Order
#########################################################################################
'''),
add_help=False
)
parser._optionals.title = "OPTIONS"
# parser._optionals.description = "- Necessary main options.\n"
parser.add_argument("-h", "--help", help="\nShow this help message and exit\n\n", action='help')
parser.add_argument("-chped", help="\nHLA Type Data(Standard 4-field allele \"*.ped\" file).\n\n", required=True)
parser.add_argument("-dict", help="\nHLA dictonary file name(ex. 'HLA_DICTIONARY_AA.txt')\n\n", required=True)
parser.add_argument("-type", help="\nAA(for Amino Acid) or SNP(for SNPs)\n\n", choices=["AA", "SNPS"], required=True)
parser.add_argument("-o", help="\nOutput file prefix.\n\n", required=True)
parser.add_argument("--previous-version", help="\nIf you give this option, The MakeReference will work as original version.\n\n",
action='store_true')
parser.add_argument("--asLump", help="\n(for Testing) Not zipped result to check strings.\n\n",
action='store_true')
##### <for Test> #####
## (2019. 01. 06.) Introducinig compatibility to work with old version of dictionary
# # AA
# args = parser.parse_args(["-ped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.old.chped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference_old/HLA_DICTIONARY_AA.txt",
# "-type", "AA",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190106/HAPMAP_CEU.old.enCODED",
# "--previous-version"])
# # SNPS
# args = parser.parse_args(["-ped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.old.chped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference_old/HLA_DICTIONARY_SNPS.txt",
# "-type", "SNPS",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190106/HAPMAP_CEU.old.enCODED",
# "--previous-version"])
## (2019. 01. 08.) Generalized 4-field
# # AA
# args = parser.parse_args(["-chped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.4field.ped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HLA_DICTIONARY_AA.hg18.imgt370.txt",
# "-type", "AA",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190109/_1_HLAtoSequences/HAPMAP_CEU_HLA.4field"])
# # SNPS
# args = parser.parse_args(["-chped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.4field.ped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HLA_DICTIONARY_SNPS.hg18.imgt370.txt",
# "-type", "SNPS",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190109/_1_HLAtoSequences/HAPMAP_CEU_HLA.4field"])
# # AA
# args = parser.parse_args(["-chped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.4field.ped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HLA_DICTIONARY_AA.hg18.imgt370.txt",
# "-type", "AA",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190109/_1_HLAtoSequences/HAPMAP_CEU_HLA.4field.lumped",
# "--asLump"])
# # SNPS
# args = parser.parse_args(["-chped", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HAPMAP_CEU_HLA.4field.ped",
# "-dict", "/Users/wansun/Git_Projects/MakeReference_v2/data/MakeReference/HLA_DICTIONARY_SNPS.hg18.imgt370.txt",
# "-type", "SNPS",
# "-o", "/Users/wansun/Git_Projects/MakeReference_v2/tests/20190109/_1_HLAtoSequences/HAPMAP_CEU_HLA.4field.lumped",
# "--asLump"])
##### <for Publication> #####
args = parser.parse_args()
print(args)
HLAtoSequences(args.chped, args.dict, args.type, args.o, __previous_version=args.previous_version, __asLump=args.asLump)