Repository navigation
Expand file tree
/
Copy pathinit_table.py
More file actions
483 lines (454 loc) · 12.9 KB
/
Copy pathinit_table.py
File metadata and controls
483 lines (454 loc) · 12.9 KB
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
479
480
481
482
483
###############################################################################
###############################################################################
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###############################################################################
###############################################################################
def error_check(line, length):
"""
Description: Error check to make sure Annotation Table is being filled out
Input: Annotation Table Line, Line Length
"""
errors = {
3:'Row length error in init_table.py - add_fasta() TranscriptID: ',
5:'Row length error in init_table.py - add_swissprot_top_blastx() TranscriptID: ',
7:'Row length error in init_table.py - add_protid() TranscriptID: ',
8:'Row length error in init_table.py - add_swissprot_top_blastp() TranscriptID: ',
9:'Row length error in init_table.py - add_pfam() TranscriptID: ',
10:'Row length error in init_table.py - add_signalp() TranscriptID: ',
11:'Row length error in init_table.py - add_tmhmm() TranscriptID: ',
12:'Row length error in init_table.py - add_rnammer() TranscriptID: ',
13:'Row length error in init_table.py - add_trembl_top_blastx() TranscriptID: ',
14:'Row length error in init_table.py - add_trembl_top_blastp() TranscriptID: ' }
if len(line) != length:
error_message = 'Annotation Table line incorrect size\n' + errors[length] + line[0] + '\nLineLength: ' + str(len(line)) + ' ' + '\t'.join(line)
raise ValueError(error_message)
return
def add_space(annot_table):
"""
Description: Space generator for empty files
Input: Annotation Table
Return: Annotation Table
"""
for index in range(len(annot_table)):
annot_table[index].append(".")
return annot_table
def compare(transcriptid_file, transcriptid_table):
"""
Description: Compares if transcriptid from a file is > transcriptid in the row of the annot_table
Input: TranscriptID, TranscriptID
Return: Boolean
"""
transcriptid_file = [int(x[1:]) for x in (transcriptid_file.split('_'))]
transcriptid_table = [int(x[1:]) for x in (transcriptid_table.split('_'))]
for x, y in zip(transcriptid_file, transcriptid_table):
if x > y:
return True
return False
def conversion_transcriptid_geneid(conversion_file):
"""
Description: Creates dictionary for TranscriptID -> GeneID conversion
Input: GeneTransMap File
Return: Dictionary
"""
convDt = {}
file = open(conversion_file)
for line in file:
line = line.strip('\n').split()
first = line[0]
second = line[1]
convDt[second] = first
file.close()
return convDt
def add_fasta(fasta_file, transmap_file, nr_file):
"""
Description: Initiates Table with Columns 0,1,2 - TranscriptID, GeneID, TranscriptLength
Input: Fasta File, GeneTransMap File
Return: Annotation Table
"""
annot_table = []
file = open(fasta_file)
transcriptid = ''
length = 0
geneid = conversion_transcriptid_geneid(transmap_file)
lengths = {}
for line in file:
if '>' == line[0]:
if length > 0:
annot_table += [[transcriptid, geneid[transcriptid], str(length)]]
lengths[transcriptid] = length
length = 0
transcriptid = line.split(' ')[0][1:]
else:
length += len(line.strip())
annot_table += [[transcriptid, geneid[transcriptid], str(length)]]
file.close()
if nr_file == 'NONE':
return annot_table, 0
return annot_table, lengths
def add_swissprot_top_blastx(annot_table, swissprotx_file):
"""
Description: Adds Columns 3,4 - SwissProtBlastX, SwissProtBlastXLength
Input: Annotation Table, SwissProtBlastX File
Return: Annotation Table
"""
file = open(swissprotx_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
if prev == line[0]:
continue
prev = line[0]
while (True):
if index == length:
break
error_check(annot_table[index], 3)
if line[0] == annot_table[index][0]:
annot_table[index].append(line[1] + ' ' + line[12])
annot_table[index].append(line[13])
index += 1
break
if compare(line[0], annot_table[index][0]):
annot_table[index].append('.')
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 3)
annot_table[index].append('.')
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_protid(annot_table, trans_file):
"""
Description: Adds Columns 5,6 - ProteinID, ProteinCoordinates
Input: Annotation Table, Transdecoder File
Return: Annotation Table
"""
file = open(trans_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
if line[0] != '>':
continue
line = line.strip('\n').split()
transcriptid = line[8].split(':')[0]
if prev == transcriptid:
continue
prev = transcriptid
while (index < length):
error_check(annot_table[index], 5)
if transcriptid == annot_table[index][0]:
proteinid = line[0][1:]
protein_coordinate = line[8].split(':')[1].replace('(', '[').replace(')', ']')
annot_table[index].append(proteinid)
annot_table[index].append(protein_coordinate)
index += 1
break
if compare(transcriptid, annot_table[index][0]):
annot_table[index].append('.')
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 5)
annot_table[index].append('.')
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_swissprot_top_blastp(annot_table, swissprotp_file):
"""
Description: Adds Column 7 - SwissProtBlastP
Input: Annotation Table, SwissProtBlastP File
Return: Annotation Table
"""
file = open(swissprotp_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
transcriptid = line[0].split('|')[0][len('cds.'):]
if prev == transcriptid:
continue
prev = transcriptid
while (True):
if index == length:
break
error_check(annot_table[index], 7)
if transcriptid == annot_table[index][0]:
annot_table[index].append(line[1]) ###could change to 12###
index += 1
break
if compare(transcriptid, annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 7)
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_pfam(annot_table, pfam_file):
"""
Description: Adds Column 8 - PFAM
Input: Annotation Table, PFAM File
Return: Annotation Table
"""
file = open(pfam_file)
index = 0
length = len(annot_table)
first = True
for line in file:
if index == length:
break
if '#' == line[0]:
continue
line = line.strip('\n').split()
transcriptid = line[3].split('|')[0][len('cds.'):]
while (True):
if first:
error_check(annot_table[index], 8)
if transcriptid == annot_table[index][0]:
if first:
#temp = line[1] + '^' + line[0] + '^' + ' '.join(line[22:]) + '^' + line[15] + '-' + line[16] + '^E:' + line[10]
temp = line[1] + '^' + ' '.join(line[22:])
annot_table[index].append(temp)
first = False
break
else:
#temp = line[1] + '^' + line[0] + '^' + ' '.join(line[22:]) + '^' + line[15] + '-' + line[16] + '^E:' + line[10]
if line[1] not in annot_table[index][-1]:
temp = line[1] + '^' + ' '.join(line[22:])
annot_table[index][-1] = annot_table[index][-1] + '`' + temp
break
if compare(transcriptid, annot_table[index][0]):
#prevents extra '.' from being added
if first:
annot_table[index].append('.')
first = True
index += 1
else:
break
if len(annot_table[index]) > 8:
index += 1
while (index < length):
error_check(annot_table[index], 8)
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_signalp(annot_table, signalp_file):
"""
Description: Adds Column 9 - SignalP
Input: Annotation Table, SignalP File
Return: Annotation Table
"""
file = open(signalp_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
if '#' == line[0]:
continue
line = line.strip('\n').split('\t')
transcriptid = line[0].split('|')[0]
if prev == transcriptid:
continue
prev = transcriptid
while (True):
error_check(annot_table[index], 9)
if transcriptid == annot_table[index][0]:
temp = 'sigP:' + line[3] + '^' + line[4] + '^' + line[5] + '^' + line[8]
annot_table[index].append(temp)
index += 1
break
if compare(transcriptid, annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 9)
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_tmhmm(annot_table, tmhmm_file):
"""
Description: Adds Column 10 - TMHMM
Input: Annotation Table, TMHMM File
Return: Annotation Table
"""
file = open(tmhmm_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
transcriptid = line[0].split('|')[0]
if prev == transcriptid:
continue
prev = transcriptid
while (True):
error_check(annot_table[index], 10)
if transcriptid == annot_table[index][0]:
temp = line[2] + '^' + line[4] + '^' + line[5]
annot_table[index].append(temp)
index += 1
break
if compare(transcriptid, annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 10)
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_rnammer(annot_table, rnammer_file):
"""
Description: Adds Column 11 - RNAMMER
Input: Annotation Table, RNAMMER File
Return: Annotation Table
"""
file = open(rnammer_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
if prev == line[0]:
continue
prev = line[0]
while (True):
error_check(annot_table[index], 11)
if line[0] == annot_table[index][0]:
temp = line[7] + '^' + line[4] + '-' + line[5]
annot_table[index].append(temp)
index += 1
break
if compare(line[0], annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
file.close()
while (index < length):
error_check(annot_table[index], 11)
annot_table[index].append('.')
index += 1
return annot_table
def add_trembl_top_blastx(annot_table, unirefx_file):
"""
Description: Adds Column 12 - Uniref90BlastX
Input: Annotation Table, Uniref90BlastX File
Return: Annotation Table
"""
file = open(unirefx_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
if prev == line[0]:
continue
prev = line[0]
while (True):
error_check(annot_table[index], 12)
if line[0] == annot_table[index][0]:
annot_table[index].append(line[1]) ###could change to 12###
index += 1
break
if compare(line[0], annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 12)
annot_table[index].append('.')
index += 1
file.close()
return annot_table
def add_trembl_top_blastp(annot_table, unirefp_file):
"""
Description: Adds Column 12 - Uniref90BlastP
Input: Annotation Table, Uniref90BlastP File
Return: Annotation Table
"""
if unirefp_file != None:
file = open(unirefp_file)
index = 0
length = len(annot_table)
prev = ''
for line in file:
if index == length:
break
line = line.strip('\n').split('\t')
transcriptid = line[0].split('|')[0]
if prev == transcriptid:
continue
prev = transcriptid
while (True):
error_check(annot_table[index], 13)
if transcriptid == annot_table[index][0]:
annot_table[index].append(line[12])
index += 1
break
if compare(transcriptid, annot_table[index][0]):
annot_table[index].append('.')
index += 1
else:
break
while (index < length):
error_check(annot_table[index], 13)
annot_table[index].append('.')
index += 1
file.close()
for index in range(len(annot_table)):
error_check(annot_table[index], 14)
annot_table[index] += ['.'] * 15
else:
for index in range(len(annot_table)):
error_check(annot_table[index], 13)
annot_table[index] += ['.'] * 16
return annot_table