annotate helper.py @ 6:8eb1751dcfe7 draft

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