-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathags_to_gff_conv.py
executable file
·292 lines (281 loc) · 13.3 KB
/
ags_to_gff_conv.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
#!/usr/bin/env python
"""
Program to convert gene structure in mGene AGS format to GFF3.
Usage: python ags_to_gff_conv.py in.mat > out.gff
Requirements:
NumPy:- http://www.numpy.org/
SciPy:- http://www.scipy.org/
"""
import sys
import numpy as NP
import scipy.io as SIO
def ags_to_gff(fname):
"""
read the mGene AGS data format.
@args fname: mat file stores the AGS structure
@type fname: str
"""
mat_info = SIO.loadmat(fname, squeeze_me=True, struct_as_record=False)
gene_details = mat_info['genes'] #TODO automatically detect the struct identifier.
for each_entry in gene_details: #Iterate over the matlab struct
SOURCE = '.'
#SOURCE = str(NP.atleast_1d(each_entry.strain)[0]) if each_entry.strain.size else SOURCE
SOURCE = str(each_entry.gene_info.Source) if each_entry.gene_info.Source else SOURCE
geneLine = [str(each_entry.chr),
SOURCE,
str(each_entry.gene_type),
str(each_entry.start),
str(each_entry.stop),
'.',
str(each_entry.strand),
'.',
'ID=%s;Name=%s' % (each_entry.name, each_entry.name)]
print '\t'.join(geneLine) ## gene line in GFF3
tidx = 0
for transcript in NP.atleast_1d(each_entry.transcripts):
try:
try:
start = int(NP.atleast_1d(each_entry.exons)[tidx][0][0])
stop = int(NP.atleast_1d(each_entry.exons)[tidx][-1][1])
except:
start = int(NP.atleast_1d(each_entry.exons)[tidx][0])
stop = int(NP.atleast_1d(each_entry.exons)[tidx][1])
except:
try:
start = int(NP.atleast_1d(each_entry.exons)[0][0])
stop = int(NP.atleast_1d(each_entry.exons)[-1][1])
except:
start = NP.atleast_1d(each_entry.exons)[0]
stop = NP.atleast_1d(each_entry.exons)[1]
TYPE = '.'
TYPE = str(NP.atleast_1d(each_entry.transcript_type)[tidx]) if each_entry.transcript_type.size else TYPE
tLine = [str(each_entry.chr),
SOURCE,
TYPE,
str(start),
str(stop),
'.',
str(each_entry.strand),
'.',
'ID=%s;Parent=%s' % (transcript, each_entry.name)]
print '\t'.join(tLine) ##
if each_entry.utr5_exons.size:## UTR5
try:
try:
if len(NP.atleast_1d(each_entry.transcripts))==1:
for eidx in range(len(each_entry.utr5_exons)):
u5Line = [str(each_entry.chr),
SOURCE,
'five_prime_UTR',
str(each_entry.utr5_exons[eidx][0]),
str(each_entry.utr5_exons[eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent=%s' % transcript]
print '\t'.join(u5Line)
else:
u5_start = int(each_entry.utr5_exons[tidx][0])
u5_stop = int(each_entry.utr5_exons[tidx][1])
u5Line = [str(each_entry.chr),
SOURCE,
'five_prime_UTR',
str(u5_start),
str(u5_stop),
'.',
str(each_entry.strand),
'.',
'Parent=%s' % transcript]
print '\t'.join(u5Line)
except:
u5_start = int(each_entry.utr5_exons[0])
u5_stop = int(each_entry.utr5_exons[1])
u5Line = [str(each_entry.chr),
SOURCE,
'five_prime_UTR',
str(u5_start),
str(u5_stop),
'.',
str(each_entry.strand),
'.',
'Parent=%s' % transcript]
print '\t'.join(u5Line)
except:
for eidx in range(len(each_entry.utr5_exons[tidx])):
u5Line = [str(each_entry.chr),
SOURCE,
'five_prime_UTR',
str(each_entry.utr5_exons[tidx][eidx][0]),
str(each_entry.utr5_exons[tidx][eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent=%s' % transcript]
print '\t'.join(u5Line)
if each_entry.cds_exons.size:## CDS
try:
try:
if len(NP.atleast_1d(each_entry.transcripts))==1:
for eidx in range(len(each_entry.cds_exons)):
cLine = [str(each_entry.chr),
SOURCE,
'CDS',
str(each_entry.cds_exons[eidx][0]),
str(each_entry.cds_exons[eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(cLine)
else:
c_start = int(each_entry.cds_exons[tidx][0])
c_stop = int(each_entry.cds_exons[tidx][1])
cLine = [str(each_entry.chr),
SOURCE,
'CDS',
str(c_start),
str(c_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(cLine)
except:
c_start = int(each_entry.cds_exons[0])
c_stop = int(each_entry.cds_exons[1])
cLine = [str(each_entry.chr),
SOURCE,
'CDS',
str(c_start),
str(c_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(cLine)
except:
for eidx in range(len(each_entry.cds_exons[tidx])):
cLine = [str(each_entry.chr),
SOURCE,
'CDS',
str(each_entry.cds_exons[tidx][eidx][0]),
str(each_entry.cds_exons[tidx][eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(cLine)
if each_entry.utr3_exons.size: ## UTR3
try:
try:
if len(NP.atleast_1d(each_entry.transcripts))==1:
for eidx in range(len(each_entry.utr3_exons)):
u3Line = [str(each_entry.chr),
SOURCE,
'three_prime_UTR',
str(each_entry.utr3_exons[eidx][0]),
str(each_entry.utr3_exons[eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(u3Line)
else:
u3_start = int(each_entry.utr3_exons[tidx][0])
u3_stop = int(each_entry.utr3_exons[tidx][1])
u3Line = [str(each_entry.chr),
SOURCE,
'three_prime_UTR',
str(u3_start),
str(u3_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(u3Line)
except:
u3_start = int(each_entry.utr3_exons[0])
u3_stop = int(each_entry.utr3_exons[1])
u3Line = [str(each_entry.chr),
SOURCE,
'three_prime_UTR',
str(u3_start),
str(u3_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(u3Line)
except:
for eidx in range(len(each_entry.utr3_exons[tidx])):
u3Line = [str(each_entry.chr),
SOURCE,
'three_prime_UTR',
str(each_entry.utr3_exons[tidx][eidx][0]),
str(each_entry.utr3_exons[tidx][eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(u3Line)
if each_entry.exons.size:## Exons
try:
try:
if len(NP.atleast_1d(each_entry.transcripts))==1:
for eidx in range(len(each_entry.exons)):
eLine = [str(each_entry.chr),
SOURCE,
'exon',
str(each_entry.exons[eidx][0]),
str(each_entry.exons[eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(eLine)
else:
e_start = int(each_entry.exons[tidx][0])
e_stop = int(each_entry.exons[tidx][1])
eLine = [str(each_entry.chr),
SOURCE,
'exon',
str(e_start),
str(e_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(eLine)
except:
e_start = int(each_entry.exons[0])
e_stop = int(each_entry.exons[1])
eLine = [str(each_entry.chr),
SOURCE,
'exon',
str(e_start),
str(e_stop),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(eLine)
except:
for eidx in range(len(each_entry.exons[tidx])):
eLine = [str(each_entry.chr),
SOURCE,
'exon',
str(each_entry.exons[tidx][eidx][0]),
str(each_entry.exons[tidx][eidx][1]),
'.',
str(each_entry.strand),
'.',
'Parent='+str(transcript)]
print '\t'.join(eLine)
tidx += 1
if __name__== "__main__":
try:
mat_fname = sys.argv[1]
except:
print __doc__
sys.exit(-1)
ags_to_gff(mat_fname)