-
Notifications
You must be signed in to change notification settings - Fork 15
/
sort_gff.py
executable file
·674 lines (628 loc) · 27.1 KB
/
sort_gff.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/usr/bin/python2.7
#Version: 1.4
#Authors: Allio Remi & Schomaker-Bastos Alex
#ISEM - CNRS - LAMPADA - IBQM - UFRJ
import sys
import os.path
import operator
import collections
from Bio import SeqIO, SeqFeature
from Bio.Alphabet import generic_dna, generic_protein, IUPAC
from Bio.Data import CodonTable
from Bio.Seq import Seq
def read_fasta(fp):
name, seq = None, []
for line in fp:
line = line.rstrip()
if line.startswith(">"):
if name: yield (name, ''.join(seq))
name, seq = line, []
else:
seq.append(line)
if name: yield (name, ''.join(seq))
file1=open(sys.argv[1])
fasta=open(sys.argv[1].split("_raw.gff")[0]+".fasta")
record = SeqIO.read(open(sys.argv[1].split("_raw.gff")[0]+".gb"), "genbank", generic_dna)
dico_start={}
dico_end={}
dico_lstart={}
dico_lend={}
for feature in record.features:
start=""
stop=""
if feature.type.lower() == 'cds':
if 'gene' in feature.qualifiers:
if 'translation' in feature.qualifiers:
if feature.location.strand == -1:
start=Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna)
if not dico_lstart.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(start.reverse_complement())
else:
if dico_lstart.get(feature.qualifiers['gene'][0].split("_")[0]) < feature.location.end:
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(start.reverse_complement())
stop=Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna)
if not dico_lend.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(stop.reverse_complement())
else:
if dico_lend.get(feature.qualifiers['gene'][0].split("_")[0]) > feature.location.start:
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(stop.reverse_complement())
else:
if not dico_lstart.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna))
else:
if dico_lstart.get(feature.qualifiers['gene'][0].split("_")[0]) > feature.location.start:
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna))
if not dico_lend.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna))
else:
if dico_lend.get(feature.qualifiers['gene'][0].split("_")[0]) < feature.location.end:
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna))
if 'product' in feature.qualifiers:
if feature.location.strand == -1:
start=Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna)
if not dico_lstart.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(start.reverse_complement())
else:
if dico_lstart.get(feature.qualifiers['gene'][0].split("_")[0]) < feature.location.end:
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(start.reverse_complement())
stop=Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna)
if not dico_lend.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(stop.reverse_complement())
else:
if dico_lend.get(feature.qualifiers['gene'][0].split("_")[0]) > feature.location.start:
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(stop.reverse_complement())
else:
if not dico_lstart.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna))
else:
if dico_lstart.get(feature.qualifiers['gene'][0].split("_")[0]) > feature.location.start:
dico_lstart[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.start
dico_start[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.start :feature.location.start+3]),IUPAC.unambiguous_dna))
if not dico_lend.has_key(feature.qualifiers['gene'][0].split("_")[0]):
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna))
else:
if dico_lend.get(feature.qualifiers['gene'][0].split("_")[0]) < feature.location.end:
dico_lend[feature.qualifiers['gene'][0].split("_")[0]]=feature.location.end
dico_end[feature.qualifiers['gene'][0].split("_")[0]]=str(Seq(str(record.seq[feature.location.end-3 :feature.location.end]),IUPAC.unambiguous_dna))
"""print feature.qualifiers['gene'][0].split("_")[0]
print dico_end[feature.qualifiers['gene'][0].split("_")[0]]
print dico_lend[feature.qualifiers['gene'][0].split("_")[0]]"""
tableToUse = CodonTable.unambiguous_dna_by_id[int(sys.argv[3])]
listOfStartCodons = []
listOfStopCodons = []
for startCodon in tableToUse.start_codons:
if startCodon not in listOfStartCodons:
listOfStartCodons.append(str(startCodon))
startCodons = listOfStartCodons
for stopCodon in tableToUse.stop_codons:
if stopCodon not in listOfStopCodons:
listOfStopCodons.append(str(stopCodon))
stopCodons = listOfStopCodons
"""print stopCodons
print startCodons"""
seqID=sys.argv[2]
dico={}
dicotrna={}
dicof={}
c=0
for name, seq in read_fasta(fasta):
length=len(seq)
for line in open(sys.argv[1]):
dico[line.rstrip().split("\t")[8]]=line.rstrip()
for line in open(sys.argv[1]):
t=1
line=line.rstrip()
chro=line.split("\t")[0]
gene=line.split("\t")[8]
start=int(line.split("\t")[3])
stop=int(line.split("\t")[4])
if "tRNA" in gene:
for k, v in dico.items():
v=v.rstrip()
tchro=v.split("\t")[0]
tgene=v.split("\t")[8]
tstart=int(v.split("\t")[3])
tstop=int(v.split("\t")[4])
if tgene != gene and tchro == chro and tstart <= start and tstop >= stop:
print gene+" = "+str(start)+","+str(stop)
print tgene+" = "+str(tstart)+","+str(tstop)
t=0
dicotrna[gene]=gene
break
elif tgene != gene and tchro == chro and tstart >= start and tstop <= stop:
print gene+" = "+str(start)+","+str(stop)
print tgene+" = "+str(tstart)+","+str(tstop)
t=0
dicotrna[gene]=gene
break
"""elif tgene != gene and tchro == chro and tstart <= start and tstop >= stop:
t=0
dicotrna[gene]=gene
break"""
dicof[start]=line.rstrip()
dicog={}
dicogl={}
for line in open(sys.argv[1]):
line=line.rstrip()
gene=line.split("\t")[8]
if not "_" in gene:
dicog[gene]=1
dicogl[gene]=line
for line in open(sys.argv[1]):
line=line.rstrip()
gene=line.split("\t")[8]
if sys.argv[4] == "no":
seqID=line.split("\t")[0]
if "_" in gene:
if line.split("\t")[6]== "-":
if int(line.split("\t")[3]) > int(dicogl.get(gene.split("_")[0]).split(";")[0].split("\t")[3]):
dicog[gene.split("_")[0]]+=1
dicogl[gene.split("_")[0]]=line+";"+dicogl.get(gene.split("_")[0])
else:
dicog[gene.split("_")[0]]+=1
dicogl[gene.split("_")[0]]=dicogl.get(gene.split("_")[0])+";"+line
elif line.split("\t")[6]== "+":
if int(line.split("\t")[3]) < int(dicogl.get(gene.split("_")[0]).split(";")[0].split("\t")[3]):
dicog[gene.split("_")[0]]+=1
dicogl[gene.split("_")[0]]=line+";"+dicogl.get(gene.split("_")[0])
else:
dicog[gene.split("_")[0]]+=1
dicogl[gene.split("_")[0]]=dicogl.get(gene.split("_")[0])+";"+line
sorted_x = sorted(dicof.items(), key=operator.itemgetter(0))
sorted_dict = collections.OrderedDict(sorted_x)
gout=sys.argv[1].split("_raw.gff")[0]+".gff"
gout=open(gout, "w")
gout.write(seqID+"\t"+"mitofinder\tsource\t1\t"+str(length)+"\t.\t+\t.\tName=source "+seqID.replace("_"," ")+"\n")
tout=sys.argv[1].split("_raw.gff")[0]+".tbl"
tout=open(tout, "w")
tout.write(">Feature "+seqID+"\n")
#tout.write("1\t"+str(length)+"\tREFERENCE\n")
#tout.write("\t\t\tMitoFinder\txxxxxx\n")
dico_product={}
dico_product["COX1"]="cytochrome c oxidase subunit I"
dico_product["COX2"]="cytochrome c oxidase subunit II"
dico_product["COX3"]="cytochrome c oxidase subunit III"
dico_product["ND1"]="NADH dehydrogenase subunit 1"
dico_product["ND2"]="NADH dehydrogenase subunit 2"
dico_product["ND3"]="NADH dehydrogenase subunit 3"
dico_product["ND4"]="NADH dehydrogenase subunit 4"
dico_product["ND4L"]="NADH dehydrogenase subunit 4L"
dico_product["ND5"]="NADH dehydrogenase subunit 5"
dico_product["ND6"]="NADH dehydrogenase subunit 6"
dico_product["CYTB"]="cytochrome b"
dico_product["ATP6"]="ATP synthase F0 subunit 6"
dico_product["ATP8"]="ATP synthase F0 subunit 8"
dico_product["rrnL"]="16S ribosomal RNA"
dico_product["rrnS"]="12S ribosomal RNA"
for k, v in sorted_dict.items():
if not dicotrna.has_key(v.split("\t")[8]):
col1=seqID
col2=v.split("\t")[1]
col3=v.split("\t")[2]
col4=v.split("\t")[3]
col5=v.split("\t")[4]
col6=v.split("\t")[5]
col7=v.split("\t")[6]
col8=v.split("\t")[7]
col9=v.split("\t")[8].rstrip()
ext=""
if not "trn" in col9 and not "tRNA" in col9 and not "rrn" in col9:
if dicog.get(col9) == 1:
size=int(col5)-int(col4)+1
if size%3 == 0:
if col7 == "+":
if dico_start[col9] in startCodons:
start=col4
else:
start="<"+col4
ext="5' Partial CDS"
if dico_end[col9] in stopCodons:
stop=col5
else:
stop=">"+col5
if ext != "":
ext="Partial CDS"
else:
ext="3' Partial CDS"
tout.write(start+"\t"+stop+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(start+"\t"+stop+"\t"+"CDS\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\tunknown\n")
tout.write("\t\t\ttransl_table\t"+sys.argv[3]+"\n")
if ext != "":
tout.write("\t\t\tnote\t"+ext+"\n")
if col7 == "-":
if dico_start[col9] in startCodons:
start=col5
else:
start="<"+col5
ext="5' Partial CDS"
if dico_end[col9] in stopCodons:
stop=col4
else:
stop=">"+col4
if ext != "":
ext="Partial CDS"
else:
ext="3' Partial CDS"
tout.write(start+"\t"+stop+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(start+"\t"+stop+"\t"+"CDS\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\t"+col9+"\n")
tout.write("\t\t\ttransl_table\t"+sys.argv[3]+"\n")
if ext != "":
tout.write("\t\t\tnote\t"+ext+"\n")
else:
ext="Note: Not a multiple of 3"
if col7 == "+":
tout.write(col4+"\t"+">"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+">"+col5+"\t"+"CDS\n")
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
tout.write("\t\t\ttransl_table\t"+sys.argv[3]+"\n")
tout.write("\t\t\tnote\tPartial sequence\n")
if col7 == "-":
tout.write("<"+col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write("<"+col5+"\t"+col4+"\t"+"CDS\n")
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
tout.write("\t\t\ttransl_table\t"+sys.argv[3]+"\n")
tout.write("\t\t\tnote\tPartial sequence\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene "+ext+"\n")
gout.write(col1+"\t"+col2+"\t"+"CDS"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" CDS "+ext+"\n")
if dicog.get(col9) > 1:
c=1
list_tmp_gene=[]
list_tmp_cds=[]
dico_tmp_gff={}
for line in dicogl.get(col9).split(";"):
col1=seqID
col2=line.split("\t")[1]
col3=line.split("\t")[2]
col4=line.split("\t")[3]
col5=line.split("\t")[4]
col6=line.split("\t")[5]
col7=line.split("\t")[6]
col8=line.split("\t")[7]
col9=line.split("\t")[8].split("_")[0].rstrip()
size=int(col5)-int(col4)+1
if c == 1:
if size%3 == 0:
if col7 == "+":
if dico_start[col9] in startCodons:
start=col4
else:
start="<"+col4
ext="5' Partial CDS"
stop=col5
if col7 == "-":
if dico_end[col9] in stopCodons:
stop=col4
else:
stop=">"+col4
if ext != "":
ext="Partial CDS"
else:
ext="3' Partial CDS"
start=col5
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop+"\t"+"CDS")
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"CDS"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 CDS "+ext
if c > 1 and c < int(dicog.get(col9)):
if size%3 == 0:
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
start=col5
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"CDS"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 CDS "+ext
if c > 1 and c == int(dicog.get(col9)):
if size%3 == 0:
if col7 == "+":
start=col4
if dico_end[col9] in stopCodons:
stop=col5
else:
stop=">"+col5
if ext != "":
ext="Partial CDS"
else:
ext="3' Partial CDS"
if col7 == "-":
if dico_start[col9] in startCodons:
start=col5
else:
start="<"+col5
ext="5' Partial CDS"
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"CDS"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 CDS "+ext
c+=1
list_tmp_gene=sorted(list_tmp_gene)
if col7 == "+":
tout.write(str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
if col7 == "-":
tout.write(str(list_tmp_gene[-1])+"\t"+str(list_tmp_gene[0])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
tout.write("\t\t\tgene\t"+col9+"\n")
for line in list_tmp_cds:
tout.write(line+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\t"+col9+"\n")
tout.write("\t\t\ttransl_table\t"+sys.argv[3]+"\n")
if ext != "":
tout.write("\t\t\tnote\t"+ext+"\n")
sorted_y = sorted(dico_tmp_gff.items(), key=operator.itemgetter(0))
sorted_dico = collections.OrderedDict(sorted_y)
for key, val in sorted_dico.items():
gout.write(val+"\n")
if "tRNA" in col9:
size=int(col5)-int(col4)+1
if size <= 120:
if col9 == "tRNA-???":
col9 = "tRNA-Xxx"
if col7 == "+":
tout.write(col4+"\t"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+col5+"\t"+"tRNA\n")
tout.write("\t\t\tproduct\t"+col9+"\n")
if col7 == "-":
tout.write(col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col5+"\t"+col4+"\t"+"tRNA\n")
tout.write("\t\t\tproduct\t"+col9+"\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene"+"\n")
gout.write(col1+"\t"+col2+"\t"+"tRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" "+"\n")
if "trn" in col9:
size=int(col5)-int(col4)+1
if size <= 120:
if col9 == "tRNA-???":
col9 = "tRNA-Xxx"
if col7 == "+":
tout.write(col4+"\t"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+col5+"\t"+"tRNA\n")
tout.write("\t\t\tproduct\t"+col9+"\n")
if col7 == "-":
tout.write(col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col5+"\t"+col4+"\t"+"tRNA\n")
tout.write("\t\t\tproduct\t"+col9+"\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene"+"\n")
gout.write(col1+"\t"+col2+"\t"+"tRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" tRNA"+"\n")
if "rrnL" in col9:
"""if col7 == "+":
tout.write(col4+"\t"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+col5+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write("\t\t\tproduct\t16S ribosomal RNA\n")
if col7 == "-":
tout.write(col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col5+"\t"+col4+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write("\t\t\tproduct\t16S ribosomal RNA\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene"+"\n")
gout.write(col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" rRNA"+"\n")"""
if dicog.get(col9) == 1:
if col7 == "+":
tout.write(col4+"\t"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+col5+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\tunknown\n")
if col7 == "-":
tout.write(col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col5+"\t"+col4+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\tunknown\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene"+"\n")
gout.write(col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" rRNA"+"\n")
if dicog.get(col9) > 1:
c=1
list_tmp_gene=[]
list_tmp_cds=[]
dico_tmp_gff={}
for line in dicogl.get(col9).split(";"):
col1=seqID
col2=line.split("\t")[1]
col3=line.split("\t")[2]
col4=line.split("\t")[3]
col5=line.split("\t")[4]
col6=line.split("\t")[5]
col7=line.split("\t")[6]
col8=line.split("\t")[7]
col9=line.split("\t")[8].split("_")[0].rstrip()
size=int(col5)-int(col4)+1
if c == 1:
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
stop=col4
start=col5
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop+"\t"+"rRNA")
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
if c > 1 and c < int(dicog.get(col9)):
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
start=col5
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
if c > 1 and c == int(dicog.get(col9)):
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
start=col5
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
c+=1
list_tmp_gene=sorted(list_tmp_gene)
if col7 == "+":
tout.write(str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
if col7 == "-":
tout.write(str(list_tmp_gene[-1])+"\t"+str(list_tmp_gene[0])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
tout.write("\t\t\tgene\t"+col9+"\n")
for line in list_tmp_cds:
tout.write(line+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\t"+col9+"\n")
if ext != "":
tout.write("\t\t\tnote\t"+ext+"\n")
sorted_y = sorted(dico_tmp_gff.items(), key=operator.itemgetter(0))
sorted_dico = collections.OrderedDict(sorted_y)
for key, val in sorted_dico.items():
gout.write(val+"\n")
if "rrnS" in col9:
if dicog.get(col9) == 1:
if col7 == "+":
tout.write(col4+"\t"+col5+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col4+"\t"+col5+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\tunknown\n")
if col7 == "-":
tout.write(col5+"\t"+col4+"\t"+"gene\n")
tout.write("\t\t\tgene\t"+col9+"\n")
tout.write(col5+"\t"+col4+"\t"+"rRNA\n")
tout.write("\t\t\tgene\t"+col9+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\tunknown\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" gene"+"\n")
gout.write(col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Name="+col9+" rRNA"+"\n")
if dicog.get(col9) > 1:
c=1
list_tmp_gene=[]
list_tmp_cds=[]
dico_tmp_gff={}
for line in dicogl.get(col9).split(";"):
col1=seqID
col2=line.split("\t")[1]
col3=line.split("\t")[2]
col4=line.split("\t")[3]
col5=line.split("\t")[4]
col6=line.split("\t")[5]
col7=line.split("\t")[6]
col8=line.split("\t")[7]
col9=line.split("\t")[8].split("_")[0].rstrip()
size=int(col5)-int(col4)+1
if c == 1:
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
stop=col4
start=col5
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop+"\t"+"rRNA")
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
if c > 1 and c < int(dicog.get(col9)):
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
start=col5
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
if c > 1 and c == int(dicog.get(col9)):
if col7 == "+":
start=col4
stop=col5
if col7 == "-":
start=col5
stop=col4
col9=line.split("\t")[8].split("_")[0].rstrip()
list_tmp_gene.append(int(start.replace("<","").replace(">","")))
list_tmp_gene.append(int(stop.replace("<","").replace(">","")))
list_tmp_cds.append(start+"\t"+stop)
dico_tmp_gff[int(col4)+1]=col1+"\t"+col2+"\t"+"rRNA"+"\t"+col4+"\t"+col5+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+".1 rRNA "
c+=1
list_tmp_gene=sorted(list_tmp_gene)
if col7 == "+":
tout.write(str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
if col7 == "-":
tout.write(str(list_tmp_gene[-1])+"\t"+str(list_tmp_gene[0])+"\tgene\n")
gout.write(col1+"\t"+col2+"\t"+"gene"+"\t"+str(list_tmp_gene[0])+"\t"+str(list_tmp_gene[-1])+"\t"+col6+"\t"+col7+"\t"+col8+"\t"+"Parent="+col9+"\n")
tout.write("\t\t\tgene\t"+col9+"\n")
for line in list_tmp_cds:
tout.write(line+"\n")
if dico_product.has_key(col9):
tout.write("\t\t\tproduct\t"+dico_product.get(col9)+"\n")
else:
tout.write("\t\t\tproduct\t"+col9+"\n")
if ext != "":
tout.write("\t\t\tnote\t"+ext+"\n")
sorted_y = sorted(dico_tmp_gff.items(), key=operator.itemgetter(0))
sorted_dico = collections.OrderedDict(sorted_y)
for key, val in sorted_dico.items():
gout.write(val+"\n")
gout.close()
tout.close()