-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_RATT_gff.py
157 lines (140 loc) · 5.29 KB
/
fix_RATT_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
#!/usr/bin/env python
import sys, csv
infile = sys.argv[1]
outfile = sys.argv[2]
class Gene:
def __init__(self):
self.gene = []
self.rna = []
self.exons = []
self.cds = []
def printgff(self):
print(self.gene)
print(self.rna)
print(self.exons)
print(self.cds)
def check_output(self):
if self.rna == []:
print("RNA missing for gene:")
print(self.gene)
return True
return False
def output_gene(self, handle):
if self.check_output() == True:
return
self.fix_gene()
self.fix_rna()
self.fix_exons()
self.fix_cds()
gff.fix_nonsense_genes()
gff.write_gff(handle)
def fix_gene(self):
qualifiers = self.gene[8].split(';')
outqual = []
for qual in qualifiers:
if qual.startswith('Name='):
outqual.append('ID=' + qual.replace('Name=',''))
self.gene = self.gene[:8] + [';'.join(outqual)]
def fix_gene_from_rna(self, rna_row):
qualifiers = rna_row[8].split(';')
outqual = []
for qual in qualifiers:
if qual.startswith('note=Parent:gene-'):
outqual.append('Name=' + qual.replace('note=Parent:gene-',''))
return (rna_row[:2] + ['gene'] + rna_row[3:8] + [';'.join(outqual)])
def fix_rna(self):
qualifiers = self.rna[8].split(';')
outqual = []
for qual in qualifiers:
if qual.startswith('note=ID:rna-'):
outqual.append('ID=' + qual.replace('note=ID:rna-',''))
if qual.startswith('note=Parent:gene-'):
outqual.append('Parent=' + qual.replace('note=Parent:gene-',''))
if qual.startswith('product='):
outqual.append(qual)
self.rna = self.rna[:8] + [';'.join(outqual)]
def fix_exons(self):
outexons = []
for exon in self.exons:
qualifiers = exon[8].split(';')
outqual = []
for qual in qualifiers:
if qual.startswith('note=ID:exon-'):
outqual.append('ID=' + qual.replace('note=ID:exon-',''))
if qual.startswith('note=Parent:rna-'):
outqual.append('Parent=' + qual.replace('note=Parent:rna-',''))
outexons.append(exon[:8] + [';'.join(outqual)])
self.exons = outexons
def fix_cds(self):
outcds = []
for cds in self.cds:
qualifiers = cds[8].split(';')
outqual = []
for qual in qualifiers:
if qual.startswith('note=ID:cds-'):
outqual.append('ID=' + qual.replace('note=ID:cds-',''))
if qual.startswith('note=Parent:rna-'):
outqual.append('Parent=' + qual.replace('note=Parent:rna-',''))
outcds.append(cds[:8] + [';'.join(outqual)])
self.cds = outcds
def fix_nonsense_genes(self):
if self.gene[6] == '-':
# self.gene = self.gene[:3] + self.fix_nonsense_start_end(self.gene[3], self.gene[4]) + self.gene[5:]
# self.rna = self.rna[:3] + self.fix_nonsense_start_end(self.rna[3], self.rna[4]) + self.rna[5:]
# print(self.exons)
# outexons = []
# for exon in self.exons:
# outexons.append(exon[:3] + self.fix_nonsense_start_end(exon[3], exon[4]) + exon[5:])
# self.exons = outexons
# print(self.exons)
# outcds = []
# for cds in self.cds:
# outcds.append(cds[:3] + self.fix_nonsense_start_end(cds[3], cds[4]) + cds[5:])
# self.cds = outcds
self.exons.sort(key=lambda x: int(x[3]), reverse=True)
self.cds.sort(key=lambda x: int(x[3]), reverse=True)
# def fix_nonsense_start_end(self, s, e):
# h = max(int(s), int(e))
# l = min(int(s), int(e))
# return [str(h), str(l)]
def write_gff(self, handle):
handle.writerow(gff.gene)
handle.writerow(gff.rna)
for exon in gff.exons:
handle.writerow(exon)
for cds in gff.cds:
handle.writerow(cds)
with open(sys.argv[1]) as infile, open(sys.argv[2], 'w') as outfile:
csv_in = csv.reader(infile, delimiter='\t')
csv_out = csv.writer(outfile, delimiter='\t')
gff = Gene()
no_gene = False
for i, row in enumerate(csv_in):
if row[0].startswith('#'):
csv_out.writerow(row)
continue
if row[2] == 'gene' and gff.gene != []:
gff.output_gene(csv_out)
gff = Gene()
gff.gene = row
no_gene = False
elif row[2] == 'gene':
gff.gene = row
no_gene = False
elif row[2].endswith('RNA'):
if gff.rna != []:
gff.output_gene(csv_out)
gff = Gene()
gff.gene = gff.fix_gene_from_rna(row)
gff.rna = row
elif row[2] == 'exon':
if no_gene == True:
continue
gff.exons.append(row)
elif row[2] == 'CDS':
if no_gene == True:
continue
gff.cds.append(row)
else:
sys.exit('\t'.join(row))
gff.output_gene(csv_out)