comparison helper.py @ 5:738310b31de0 draft

Uploaded
author brigidar
date Thu, 08 Oct 2015 22:48:23 -0400
parents
children
comparison
equal deleted inserted replaced
4:f2506dfbc7f7 5:738310b31de0
1 #!/usr/bin/env python
2 """
3 Common utility functions
4 """
5
6 import os
7 import re
8 import sys
9 import bz2
10 import gzip
11 import numpy
12
13 def init_gene():
14 """
15 Initializing the gene structure
16 """
17
18 gene_det = [('id', 'f8'),
19 ('anno_id', numpy.dtype),
20 ('confgenes_id', numpy.dtype),
21 ('name', 'S25'),
22 ('source', 'S25'),
23 ('gene_info', numpy.dtype),
24 ('alias', 'S15'),
25 ('name2', numpy.dtype),
26 ('strand', 'S2'),
27 ('score', 'S15'),
28 ('chr', 'S15'),
29 ('chr_num', numpy.dtype),
30 ('paralogs', numpy.dtype),
31 ('start', 'f8'),
32 ('stop', 'f8'),
33 ('transcripts', numpy.dtype),
34 ('transcript_type', numpy.dtype),
35 ('transcript_info', numpy.dtype),
36 ('transcript_score', numpy.dtype),
37 ('transcript_status', numpy.dtype),
38 ('transcript_valid', numpy.dtype),
39 ('exons', numpy.dtype),
40 ('exons_confirmed', numpy.dtype),
41 ('cds_exons', numpy.dtype),
42 ('utr5_exons', numpy.dtype),
43 ('utr3_exons', numpy.dtype),
44 ('tis', numpy.dtype),
45 ('tis_conf', numpy.dtype),
46 ('tis_info', numpy.dtype),
47 ('cdsStop', numpy.dtype),
48 ('cdsStop_conf', numpy.dtype),
49 ('cdsStop_info', numpy.dtype),
50 ('tss', numpy.dtype),
51 ('tss_info', numpy.dtype),
52 ('tss_conf', numpy.dtype),
53 ('cleave', numpy.dtype),
54 ('cleave_info', numpy.dtype),
55 ('cleave_conf', numpy.dtype),
56 ('polya', numpy.dtype),
57 ('polya_info', numpy.dtype),
58 ('polya_conf', numpy.dtype),
59 ('is_alt', 'f8'),
60 ('is_alt_spliced', 'f8'),
61 ('is_valid', numpy.dtype),
62 ('transcript_complete', numpy.dtype),
63 ('is_complete', numpy.dtype),
64 ('is_correctly_gff3_referenced', 'S5'),
65 ('splicegraph', numpy.dtype) ]
66
67 return gene_det
68
69 def open_file(fname):
70 """
71 Open the file (supports .gz .bz2) and returns the handler
72
73 @args fname: input file name for reading
74 @type fname: str
75 """
76
77 try:
78 if os.path.splitext(fname)[1] == ".gz":
79 FH = gzip.open(fname, 'rb')
80 elif os.path.splitext(fname)[1] == ".bz2":
81 FH = bz2.BZ2File(fname, 'rb')
82 else:
83 FH = open(fname, 'rU')
84 except Exception as error:
85 sys.exit(error)
86
87 return FH
88
89 def add_CDS_phase(strand, cds):
90 """
91 Calculate CDS phase and add to the CDS exons
92
93 @args strand: feature strand information
94 @type strand: +/-
95 @args cds: coding exon coordinates
96 @type cds: numpy array [[int, int, int]]
97 """
98
99 cds_region, cds_flag = [], 0
100 if strand == '+':
101 for cdspos in cds:
102 if cds_flag == 0:
103 cdspos = (cdspos[0], cdspos[1], 0)
104 diff = (cdspos[1]-(cdspos[0]-1))%3
105 else:
106 xy = 0
107 if diff == 0:
108 cdspos = (cdspos[0], cdspos[1], 0)
109 elif diff == 1:
110 cdspos = (cdspos[0], cdspos[1], 2)
111 xy = 2
112 elif diff == 2:
113 cdspos = (cdspos[0], cdspos[1], 1)
114 xy = 1
115 diff = ((cdspos[1]-(cdspos[0]-1))-xy)%3
116 cds_region.append(cdspos)
117 cds_flag = 1
118 elif strand == '-':
119 cds.reverse()
120 for cdspos in cds:
121 if cds_flag == 0:
122 cdspos = (cdspos[0], cdspos[1], 0)
123 diff = (cdspos[1]-(cdspos[0]-1))%3
124 else:
125 xy = 0
126 if diff == 0:
127 cdspos = (cdspos[0], cdspos[1], 0)
128 elif diff == 1:
129 cdspos = (cdspos[0], cdspos[1], 2)
130 xy = 2
131 elif diff == 2:
132 cdspos = (cdspos[0], cdspos[1], 1)
133 xy = 1
134 diff = ((cdspos[1]-(cdspos[0]-1))-xy)%3
135 cds_region.append(cdspos)
136 cds_flag = 1
137 cds_region.reverse()
138 return cds_region
139
140 def buildUTR(cc, ec, strand):
141 """
142 Build UTR regions from a given set of CDS and exon coordiantes of a gene
143
144 @args cc: coding exon coordinates
145 @type cc: numpy array [[int, int, int]]
146 @args ec: exon coordinates
147 @type ec: numpy array [[int, int]]
148 @args strand: feature strand information
149 @type strand: +/-
150 """
151
152 utr5 = []
153 utr3 = []
154 if strand == '+':
155 cds_s = cc[0][0]
156 for ex in ec:
157 if ex[0] <= cds_s and cds_s <= ex[1]:
158 if ex[0] != cds_s:utr5.append((ex[0], cds_s-1))
159 break
160 else:
161 utr5.append(ex)
162 cds_e = cc[-1][1]
163 for i in range(len(ec)):
164 i += 1
165 if ec[-i][0] <= cds_e and cds_e <= ec[-i][1]:
166 if ec[-i][1] != cds_e:utr3.append((cds_e +1, ec[-i][1]))
167 break
168 else:
169 utr3.append(ec[-i])
170 utr3.reverse()
171 elif strand == '-':
172 cds_s = cc[-1][1]
173 for i in range(len(ec)):
174 i += 1
175 if ec[-i][0] <= cds_s and cds_s <= ec[-i][1]:
176 if ec[-i][1] != cds_s:utr5.append((cds_s+1, ec[-i][1]))
177 break
178 else:
179 utr5.append(ec[-i])
180 utr5.reverse()
181 cds_e = cc[0][0]
182 for ex in ec:
183 if ex[0] <= cds_e and cds_e <= ex[1]:
184 if ex[0] != cds_e:utr3.append((ex[0], cds_e-1))
185 break
186 else:
187 utr3.append(ex)
188 return utr5, utr3
189
190 def make_Exon_cod(strand_p, five_p_utr, cds_cod, three_p_utr):
191 """
192 Create exon cordinates from UTR's and CDS region
193
194 @args strand_p: feature strand information
195 @type strand_p: +/-
196 @args five_p_utr: five prime utr exon coordinates
197 @type five_p_utr: numpy array [[int, int]]
198 @args cds_cod: coding exon coordinates
199 @type cds_cod: numpy array [[int, int, int]]
200 @args three_p_utr: three prime utr exon coordinates
201 @type three_p_utr: numpy array [[int, int]]
202 """
203
204 exon_pos = []
205 if strand_p == '+':
206 utr5_start, utr5_end = 0, 0
207 if five_p_utr != []:
208 utr5_start, utr5_end = five_p_utr[-1][0], five_p_utr[-1][1]
209 cds_5start, cds_5end = cds_cod[0][0], cds_cod[0][1]
210 jun_exon = []
211 if cds_5start-utr5_end == 0 or cds_5start-utr5_end == 1:
212 jun_exon = [utr5_start, cds_5end]
213 if len(cds_cod) == 1:
214 five_prime_flag = 0
215 if jun_exon != []:
216 five_p_utr = five_p_utr[:-1]
217 five_prime_flag = 1
218 for utr5 in five_p_utr:
219 exon_pos.append(utr5)
220 jun_exon = []
221 utr3_start, utr3_end = 0, 0
222 if three_p_utr != []:
223 utr3_start = three_p_utr[0][0]
224 utr3_end = three_p_utr[0][1]
225 if utr3_start-cds_5end == 0 or utr3_start-cds_5end == 1:
226 jun_exon = [cds_5start, utr3_end]
227 three_prime_flag = 0
228 if jun_exon != []:
229 cds_cod = cds_cod[:-1]
230 three_p_utr = three_p_utr[1:]
231 three_prime_flag = 1
232 if five_prime_flag == 1 and three_prime_flag == 1:
233 exon_pos.append([utr5_start, utr3_end])
234 if five_prime_flag == 1 and three_prime_flag == 0:
235 exon_pos.append([utr5_start, cds_5end])
236 cds_cod = cds_cod[:-1]
237 if five_prime_flag == 0 and three_prime_flag == 1:
238 exon_pos.append([cds_5start, utr3_end])
239 for cds in cds_cod:
240 exon_pos.append(cds)
241 for utr3 in three_p_utr:
242 exon_pos.append(utr3)
243 else:
244 if jun_exon != []:
245 five_p_utr = five_p_utr[:-1]
246 cds_cod = cds_cod[1:]
247 for utr5 in five_p_utr:
248 exon_pos.append(utr5)
249 exon_pos.append(jun_exon) if jun_exon != [] else ''
250 jun_exon = []
251 utr3_start, utr3_end = 0, 0
252 if three_p_utr != []:
253 utr3_start = three_p_utr[0][0]
254 utr3_end = three_p_utr[0][1]
255 cds_3start = cds_cod[-1][0]
256 cds_3end = cds_cod[-1][1]
257 if utr3_start-cds_3end == 0 or utr3_start-cds_3end == 1:
258 jun_exon = [cds_3start, utr3_end]
259 if jun_exon != []:
260 cds_cod = cds_cod[:-1]
261 three_p_utr = three_p_utr[1:]
262 for cds in cds_cod:
263 exon_pos.append(cds)
264 exon_pos.append(jun_exon) if jun_exon != [] else ''
265 for utr3 in three_p_utr:
266 exon_pos.append(utr3)
267 elif strand_p == '-':
268 utr3_start, utr3_end = 0, 0
269 if three_p_utr != []:
270 utr3_start = three_p_utr[-1][0]
271 utr3_end = three_p_utr[-1][1]
272 cds_3start = cds_cod[0][0]
273 cds_3end = cds_cod[0][1]
274 jun_exon = []
275 if cds_3start-utr3_end == 0 or cds_3start-utr3_end == 1:
276 jun_exon = [utr3_start, cds_3end]
277 if len(cds_cod) == 1:
278 three_prime_flag = 0
279 if jun_exon != []:
280 three_p_utr = three_p_utr[:-1]
281 three_prime_flag = 1
282 for utr3 in three_p_utr:
283 exon_pos.append(utr3)
284 jun_exon = []
285 (utr5_start, utr5_end) = (0, 0)
286 if five_p_utr != []:
287 utr5_start = five_p_utr[0][0]
288 utr5_end = five_p_utr[0][1]
289 if utr5_start-cds_3end == 0 or utr5_start-cds_3end == 1:
290 jun_exon = [cds_3start, utr5_end]
291 five_prime_flag = 0
292 if jun_exon != []:
293 cds_cod = cds_cod[:-1]
294 five_p_utr = five_p_utr[1:]
295 five_prime_flag = 1
296 if three_prime_flag == 1 and five_prime_flag == 1:
297 exon_pos.append([utr3_start, utr5_end])
298 if three_prime_flag == 1 and five_prime_flag == 0:
299 exon_pos.append([utr3_start, cds_3end])
300 cds_cod = cds_cod[:-1]
301 if three_prime_flag == 0 and five_prime_flag == 1:
302 exon_pos.append([cds_3start, utr5_end])
303 for cds in cds_cod:
304 exon_pos.append(cds)
305 for utr5 in five_p_utr:
306 exon_pos.append(utr5)
307 else:
308 if jun_exon != []:
309 three_p_utr = three_p_utr[:-1]
310 cds_cod = cds_cod[1:]
311 for utr3 in three_p_utr:
312 exon_pos.append(utr3)
313 if jun_exon != []:
314 exon_pos.append(jun_exon)
315 jun_exon = []
316 (utr5_start, utr5_end) = (0, 0)
317 if five_p_utr != []:
318 utr5_start = five_p_utr[0][0]
319 utr5_end = five_p_utr[0][1]
320 cds_5start = cds_cod[-1][0]
321 cds_5end = cds_cod[-1][1]
322 if utr5_start-cds_5end == 0 or utr5_start-cds_5end == 1:
323 jun_exon = [cds_5start, utr5_end]
324 if jun_exon != []:
325 cds_cod = cds_cod[:-1]
326 five_p_utr = five_p_utr[1:]
327 for cds in cds_cod:
328 exon_pos.append(cds)
329 if jun_exon != []:
330 exon_pos.append(jun_exon)
331 for utr5 in five_p_utr:
332 exon_pos.append(utr5)
333 return exon_pos