-
Notifications
You must be signed in to change notification settings - Fork 7
/
fourdigits.py
executable file
·318 lines (281 loc) · 13.8 KB
/
fourdigits.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
import os
import sys
import numpy as np
import config as cfg
"""
Determination of the 4-digit HLA type based on the 2-digit determination by seq2HLA.py
"""
def determine_four_digits_main(infile, bgVec, run_name, hla_class, hla_classification):
if hla_classification == "nonclassical":
result = determine_four_digits_minor_alleles(infile, bgVec, run_name)
else:
dbmhc_file = ""
dbmhc = []
if hla_class == 1:
dbmhc_file = cfg.dbmhc_I
else:
dbmhc_file = cfg.dbmhc_II
dbmhc = create_db_mhc_dict(dbmhc, dbmhc_file)
result = determine_four_digits(dbmhc, infile, bgVec, run_name)
return result
def create_db_mhc_dict(dbmhc, dbmhc_file):
population_dict = {}
header = True
with open(dbmhc_file) as inf:
for line in inf:
eles = line.split("\t")
if header:
#header
for i in range(1, len(eles)):
population_dict[i] = eles[i]
header = False
else:
if ":" in eles[0]:
l0_split = eles[0].split(":")
fourdigit = "{}:{}".format(l0_split[0], l0_split[1])
dbmhc.append(fourdigit)
return dbmhc
def mean_pan_pop_freq(freq_dict):
pan_list = []
for entry in freq_dict:
pan_list.append(float(freq_dict[entry]))
return np.mean(pan_list)
def determine_four_digits(dbmhc, infile, bgVec, run_name):
result_dict = {}
result_dict2 = {}
readcount = []
ambiguity_dict = {}
result = []
allele = ""
with open(infile) as inf:
for line in inf:
eles = line.split(":")
for i in range(0, len(eles) - 1):
allele += "{}:".format(eles[i])
result_dict[allele[0:-1]] = int(eles[len(eles) - 1])
allele = ""
readcount.append(int(eles[len(eles) - 1]))
writehandle = open("{}.solutions".format(infile), "w")
writehandle.write("#Full allele\tp-value\n")
readcount = [x for x in readcount if x != 0]
top = readcount[0]
readcount_copy = readcount[1:]
readcount_copy_str = str(readcount_copy)[1:-1].replace(" ","")
#call R-script "commmand_fourdigit.R" to calculate the confidence of the top-scoring allele (four-digits)
routput = os.popen("R --vanilla < {0} --args {1} {1},{2}".format(cfg.cmd_fourdigit, top, readcount_copy_str)).read()
parseOutput = routput.split("\n")
entries = []
repeat = True
for entry in parseOutput:
if entry[0:3] == "[1]":
entries.append(str(entry[4:len(entry)]))
#print entries[0]
if not entries[0] == "NA":
if float(entries[0]) < 0.001 and float(entries[0]) > 0.0:
for item in result_dict:
if result_dict[item] == top:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict[item], bgVec)))
repeat = False
mostProbableAllele = str(item.split(":")[0]) + ":" + str(item.split(":")[1])
numbersolutions = 1
percentile = 95
while repeat:
if percentile == 0:
return "no,0"
percentile -= 5
perc = int(np.percentile(readcount, percentile))
if perc == 0:
perc += 0.1
count = 0
for item in result_dict:
if result_dict[item] >= perc:
count += 1
#there is only 1 solution:
if count == 1:
count = 0
for item in result_dict:
if result_dict[item] >= perc:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict[item], bgVec)))
repeat = False
item_split = item.split(":")
mostProbableAllele = "{}:{}".format(item_split[0], item_split[1])
numbersolutions = 1
#there are more possible solutions
else:
count = 0
#check if allele (4-digits) occurs in dbMHC table. If not, is it a very rare allele in all populations
for item in result_dict:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
if result_dict[item] >= perc and item4digit in dbmhc:
result.append(item4digit)
result_dict2[item] = result_dict[item]
#only 1 solution (at 4-digit level) left
if len(set(result)) == 1:
#for item in result_dict:
for item in result_dict2:
item_split = item.split(":")
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict2[item], bgVec)))
repeat = False
mostProbableAllele = "{}:{}".format(item_split[0], item_split[1])
numbersolutions = 1
#more than one 2 solution possible (at 4-digit level).
elif len(set(result)) > 1:
numbersolutions = len(set(result))
max = 0.0
for item in result_dict2:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
if not item4digit in ambiguity_dict:
ambiguity_dict[item4digit] = result_dict2[item]
elif ambiguity_dict[item4digit] < result_dict2[item]:
ambiguity_dict[item4digit] = result_dict2[item]
if result_dict2[item] > max:
max = result_dict2[item]
mostProbableAllele = item4digit
ambiguity_handle = open("{}.ambiguity".format(run_name), "a")
ambiguity_handle.write("#################################################################################################\n")
ambiguity_handle.write("#Ambiguity:\n")
ambiguity_handle.write("#Based on the RNA-Seq reads and the dbMHC table, the following 4-digits alleles are possible:\n")
for ambi4DigitAllele in ambiguity_dict:
ambiguity_handle.write("{}\t{}\n".format(ambi4DigitAllele, calculate_outlier(ambiguity_dict[ambi4DigitAllele], bgVec)))
ambiguity_handle.write("#However, by taking into account the read data, the most probable 4-digit allele is:\n")
ambiguity_handle.write("{}\n\n".format(mostProbableAllele))
ambiguity_handle.close()
for item in result_dict2:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
if result_dict2[item] == max:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict2[item], bgVec)))
repeat = False
writehandle.close()
return "{},{}".format(mostProbableAllele, numbersolutions)
def calculate_outlier(top, bgVec):
#call R-script "commmand_fourdigit.R" to calculate the confidence of the top-scoring allele (four-digits)
routput = os.popen("R --vanilla < {} --args {} {}".format(cfg.cmd_fourdigit, top, bgVec)).read()
parseOutput = routput.split("\n")
entries = []
repeat = 1
for entry in parseOutput:
if entry[0:3] == "[1]":
entries.append(str(entry[4:len(entry)]))
return entries[0]
#method for determining the 4 digit HLA type for minor HLA alleles, for which no entry in dbMHC is available
def determine_four_digits_minor_alleles(infile, bgVec, run_name):
result_dict = {}
result_dict2 = {}
readcount = []
ambiguity_dict = {}
result = []
allele = ""
with open(infile) as inf:
for line in inf:
l = line.split(":")
for i in range(0, len(l) - 1):
allele += "{}:".format(l[i])
result_dict[allele[0:-1]] = int(l[len(l)-1])
allele = ""
readcount.append(int(l[len(l)-1]))
writehandle = open("{}.solutions".format(infile), "w")
writehandle.write("#Full allele\tp-value\n")
readcount = [x for x in readcount if x != 0]
top = readcount[0]
readcount_copy = readcount[1:]
readcount_copy_str = str(readcount_copy)[1:-1].replace(" ","")
#call R-script "commmand_fourdigit.R" to calculate the confidence of the top-scoring allele (four-digits)
routput = os.popen("R --vanilla < {0} --args {1} {1},{2}".format(cfg.cmd_fourdigit, top, top, readcount_copy_str)).read()
parseOutput = routput.split("\n")
entries = []
repeat = True
for entry in parseOutput:
if entry[0:3] == "[1]":
entries.append(str(entry[4:len(entry)]))
#print entries[0]
if not entries[0] == "NA":
if float(entries[0]) < 0.001 and float(entries[0]) > 0.0:
for item in result_dict:
if result_dict[item] == top:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict[item], bgVec)))
repeat = False
item_split = item.split(":")
mostProbableAllele = "{}:{}".format(item_split[0], item_split[1])
numbersolutions = 1
percentile = 95
while repeat:
if percentile == 0:
return "no,0"
percentile -= 5
perc = int(np.percentile(readcount, percentile))
if perc == 0:
perc += 0.1
count = 0
for item in result_dict:
if result_dict[item] >= perc:
count += 1
#there is only 1 solution:
if count == 1:
count = 0
for item in result_dict:
if result_dict[item] >= perc:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict[item], bgVec)))
repeat = False
item_split = item.split(":")
mostProbableAllele = "{}:{}".format(item_split[0], item_split[1])
numbersolutions = 1
#there are more possible solutions
else:
count = 0
#check if allele (4-digits) is in the defined percentile range
for item in result_dict:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
if result_dict[item] >= perc:
result.append(item4digit)
result_dict2[item] = result_dict[item]
#only 1 solution (at 4-digit level) left
if len(set(result)) == 1:
#for item in result_dict:
for item in result_dict2:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict2[item], bgVec)))
repeat = False
item_split = item.split(":")
mostProbableAllele = "{}:{}".format(item_split[0], item_split[1])
numbersolutions = 1
#more than one 2 solution possible (at 4-digit level).
elif len(set(result)) > 1:
numbersolutions = len(set(result))
max = 0.0
for item in result_dict2:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
if not item4digit in ambiguity_dict:
ambiguity_dict[item4digit] = result_dict2[item]
elif ambiguity_dict[item4digit] < result_dict2[item]:
ambiguity_dict[item4digit] = result_dict2[item]
if result_dict2[item] > max:
max = result_dict2[item]
#if meanPanPopFreq(dbmhc_prob[item4digit]) > max:
# max = meanPanPopFreq(dbmhc_prob[item4digit])
mostProbableAllele = item4digit
#if np.mean(dbmhc_prob[item4digit]) > max:
# max = np.mean(dbmhc_prob[item4digit])
# mostProbableAllele=item4digit
ambiguity_handle = open("{}.ambiguity".format(run_name), "a")
ambiguity_handle.write("#################################################################################################\n")
ambiguity_handle.write("#Ambiguity:\n")
ambiguity_handle.write("#Based on the RNA-Seq reads and the dbMHC table, the following 4-digits alleles are possible:\n")
for ambi4DigitAllele in ambiguity_dict:
ambiguity_handle.write("{}\t{}\n".format(ambi4DigitAllele, calculate_outlier(ambiguity_dict[ambi4DigitAllele], bgVec)))
ambiguity_handle.write("#However, by taking into account the read data, the most probable 4-digit allele is:\n")
ambiguity_handle.write("{}\n\n".format(mostProbableAllele))
ambiguity_handle.close()
for item in result_dict2:
item_split = item.split(":")
item4digit = "{}:{}".format(item_split[0], item_split[1])
#if np.mean(dbmhc_prob[item4digit]) == max:
#if meanPanPopFreq(dbmhc_prob[item4digit]) == max:
if result_dict2[item] == max:
writehandle.write("{}\t{}\n".format(item, calculate_outlier(result_dict2[item], bgVec)))
repeat = False
writehandle.close()
return "{},{}".format(mostProbableAllele, numbersolutions)