-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_ncbi.py
executable file
·336 lines (249 loc) · 8.95 KB
/
format_ncbi.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
#!/usr/bin/env python3
# format_ncbi.py
# SK
import sys
def error(string, error_type=1):
sys.stderr.write(f'ERROR: {string}\n')
sys.exit(error_type)
def log(string, newline_before=False):
if newline_before:
sys.stderr.write('\n')
sys.stderr.write(f'LOG: {string}\n')
#####
infile = sys.argv[1]
log('==============================================================')
log(f'Started formatting NCBI annotation {infile}')
gfftoggle = False
gtftoggle = False
abbr = infile.rsplit('/',1)[-1].split('.',1)[0].upper()
NID = '_NCBI_ID_'
if len(abbr) != 3:
error(f'Could not infer bat abbreviation: {infile}')
num_lines = 0
del_lines = {}
warn_types = []
####
def print_line(lr):
# print line in gtf format
global num_lines
field9 = '; '.join([f'{l} "{r}"' for l, r in lr[-1]]) + ';'
print(*lr[:-1], field9, sep='\t')
num_lines += 1
def get_ID(lr):
tags = lr[-1]
ID = [v for t, v in tags if t == 'ID']
if ID == []:
error(f'No ID in line: {lr}')
return ID[0]
def get_Parent(lr):
tags = lr[-1]
par = [v for t, v in tags if t == 'Parent']
if par == []:
return None
return par[0]
def handle_gene_rec(gr):
global del_lines
maintype = gr[0][2]
ID = get_ID(gr[0])
gene_id = abbr + 'G' + NID + ID
if maintype in ['gene']:
# add tags to all lines
# gene line
assert get_Parent(gr[0]) is None
gr[0][-1].append(('gene_id', gene_id))
print_line(gr[0])
# rest
tid = None
TID = None
for line in gr[1:]:
if line[2] != 'exon':
# transcript of some sort
try:
assert get_Parent(line) == ID
except AssertionError:
log(f'ID: {ID} - Parent: {get_Parent(line)}')
for l in gr:
log(l)
error('')
TID = get_ID(line)
tid = abbr + 'T' + NID + get_ID(line)
line[-1] += [('gene_id', gene_id), ('transcript_id', tid)]
print_line(line)
else:
# exon
if tid is None:
# add transcript line
assert get_Parent(line) == ID
TID = ID
tid = abbr + 'T' + NID + ID
tline = gr[0].copy()
tline[2] = 'transcript'
tline[-1] = gr[0][-1].copy() + [('gene_id', gene_id), ('transcript_id', tid)]
print_line(tline)
assert get_Parent(line) == TID
eid = abbr + 'E' + NID + get_ID(line)
line[-1] += [('gene_id', gene_id), ('transcript_id', tid), ('exon_id', eid)]
print_line(line)
elif maintype in ['tRNA', 'rRNA']:
# add gene line
gline = gr[0].copy()
gline[2] = 'gene'
gline[-1] = gr[0][-1].copy() + [('gene_id', gene_id)]
print_line(gline)
# fix transcript line
assert get_Parent(gr[0]) is None
tid = abbr + 'T' + NID + ID
gr[0][-1] += [('gene_id', gene_id), ('transcript_id', tid)]
print_line(gr[0])
# exon line(s)
for line in gr[1:]:
assert get_Parent(line) == ID
eid = abbr + 'E' + NID + get_ID(line)
line[-1] += [('gene_id', gene_id), ('transcript_id', tid), ('exon_id', eid)]
print_line(line)
elif maintype == 'pseudogene':
# keep as is
for line in gr:
print_line(line)
# # add a 'gene' line
# gline = gr[0].copy()
# gline[2] = 'gene'
# gline[-1] = gline[-1].copy() + [('gene_id', gene_id)]
# print_line(gline)
# # add tags to rest of lines
# tline = gr[0]
# tid = abbr + 'T' + NID + get_ID(tline)
# tline[-1] += [('gene_id', gene_id), ('transcript_id', tid)]
# print_line(tline)
# for line in gr[1:]:
# try:
# assert get_Parent(line) == ID
# except AssertionError:
# log(f'{gr}')
# exit()
# eid = abbr + 'E' + NID + get_ID(line)
# line[-1] += [('gene_id', gene_id), ('transcript_id', tid), ('exon_id', eid)]
# print_line(line)
# all other types
else:
if maintype not in warn_types:
log(f'Warning: Found unhandled main feature type: {maintype}')
warn_types.append(maintype)
# keep as is
for line in gr:
print_line(line)
#### main loop over file
linectr = 0
gene_records = {}
parents = {}
main_ftypes = {}
# iterate over file
with open(infile) as infh:
for ln, line in enumerate(infh):
if line.startswith('##gff-version'):
gfftoggle = True
log('gff format detected.')
continue
if line.startswith('#') or line == '\n':
continue
# status
linectr += 1
if linectr % 100 == 0:
sys.stderr.write(f'LOG: Parsing lines: {linectr}\r')
# normal record, split line
try:
contig, source, ftype, start, stop, _, strand, _, description = line.strip().split('\t')
except:
error(f'Line {ln} does not have 9 fields:\n{line}')
# ignore some feature types
if ftype in ['region', 'CDS', 'cDNA_match']:
if ftype in del_lines:
del_lines[ftype] += 1
else:
del_lines[ftype] = 1
continue
# read description
if not gfftoggle and not gtftoggle:
tag = description.split(';')[0]
if '=' in tag:
gfftoggle = True
log('gff format detected.')
elif ' "' in tag:
gtftoggle = True
log('gtf format detected.')
else:
error(f'Could not determine format from field 9: {description}')
if gfftoggle:
tags = [(f.split('=')) for f in description.split(';')]
elif gtftoggle:
# read in to check format
tspl = description.split(';')
if tspl[-1] == '':
tspl = tspl[:-1]
pairs = [t.strip().split(' ',1) for t in tspl]
tags = [(t, v.strip('"')) for t, v in pairs]
else:
error(f'Could not determine format from field 9: {description}')
# add contig
if contig not in gene_records:
gene_records[contig] = {}
# line record
lrec = [contig, source, ftype, int(start), int(stop),
'.', strand, '.', tags]
ID = get_ID(lrec)
if get_Parent(lrec) is None:
# new gene record
try:
assert ID not in gene_records[contig]
except AssertionError:
error(f'ID {ID} already in gene records.')
gene_records[contig][ID] = {'gline': lrec, 'trans': {}}
# track main types
if ftype in main_ftypes:
main_ftypes[ftype] += 1
else:
main_ftypes[ftype] = 1
else:
par = get_Parent(lrec)
if par in gene_records[contig]:
# add as transcript
gene_records[contig][par]['trans'][ID] = {'tline': lrec, 'exons': []}
try:
assert ID not in parents
except AssertionError:
# WTF?
log(f'WARNING: Duplicate ID {ID} in file!?')
continue
# set parent
parents[ID] = par
elif par in parents:
# add as exon
gene = parents[par]
gene_records[contig][gene]['trans'][par]['exons'].append(lrec)
else:
error(f'Line parent not found: {par}\n{lrec}')
sys.stderr.write(f'LOG: Parsing lines: {linectr}\r')
log('Parsed input file.', newline_before=True)
log(f'Main feature types: \n{sorted([(k, main_ftypes[k]) for k in main_ftypes], key=lambda x: x[1], reverse=True)}')
#####
# handle all records
log('Printing records ...')
for contig in gene_records:
data = gene_records[contig]
# sort by start pos on contig
for ID in sorted(data.keys(), key=lambda x: data[x]['gline'][3]):
# start with gene line
gr = []
gr.append(data[ID]['gline'])
# add transcripts
tras = data[ID]['trans']
for tra in sorted(tras.keys(), key=lambda x: tras[x]['tline'][3]):
gr.append(tras[tra]['tline'])
# add exons
for exon in sorted(tras[tra]['exons'], key=lambda x: x[3]):
gr.append(exon)
# print this gene record
handle_gene_rec(gr)
log(f'Records printed: {num_lines} lines.')
log(f'Deleted {sum([del_lines[k] for k in del_lines])} lines:\n{sorted([(k, del_lines[k]) for k in del_lines], key=lambda x: x[1], reverse=True)}')
log('Done.')