-
Notifications
You must be signed in to change notification settings - Fork 9
/
109_AddPhaseGFF3.py
237 lines (189 loc) · 6.52 KB
/
109_AddPhaseGFF3.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
#-----------------------------------------------------------+
# |
# 109_AddPhaseGFF3.py - Adds the GFF3 phase |
# |
#-----------------------------------------------------------+
# |
# AUTHOR: Vikas Gupta |
# CONTACT: [email protected] |
# STARTED: 26/06/2013 |
# UPDATED: 26/06/2013 |
# |
# DESCRIPTION: |
# Given CDS sequences in the GFF3 file this scripts adds the|
# phase based on the exon co-ordinates |
# |
# LICENSE: |
# GNU General Public License, Version 3 |
# http://www.gnu.org/licenses/gpl.html |
# |
#-----------------------------------------------------------+
# Example:
# python ~/script/python/1108_AddPhaseGFF3 -i GFF3
### import modules
import os,sys,getopt, re
### global variables
CDS = {}
mRNA_id = ''
CDS_strand = ''
### make a logfile
import datetime
now = datetime.datetime.now()
o = open(str(now.strftime("%Y-%m-%d_%H%M."))+'logfile','w')
### write logfile
def logfile(infile):
o.write("Program used: \t\t%s" % "100b_fasta2flat.py"+'\n')
o.write("Program was run at: \t%s" % str(now.strftime("%Y-%m-%d_%H%M"))+'\n')
o.write("Infile used: \t\t%s" % infile+'\n')
### main argument to
def options(argv):
infile = ''
gff3 = ''
try:
opts, args = getopt.getopt(argv,"hi:",["ifile="])
except getopt.GetoptError:
print '''
python 108_AddPhaseGFF3-i <ifile>
'''
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print '''
108_AddPhaseGFF3 -i <ifile>
'''
sys.exit()
elif opt in ("-i", "--ifile"):
infile = arg
logfile(infile)
return infile
### split line
def split_line(line):
return line.strip().split('\t')
### get ID
def get_ID(line):
line = line.strip()
match = re.search(r'ID=.+;',line)
if match:
return match.group().split(';')[0].replace('ID=','')
### http://www.sequenceontology.org/gff3.shtml
### make a class that returns columns of a GFF3 row
class GFF3:
def __init__(self, line):
tokens = split_line(line)
self.seqid = tokens[0]
self.source = tokens[1]
self.type = tokens[2]
self.start = tokens[3]
self.end = tokens[4]
self.score = tokens[5]
self.strand = tokens[6]
self.phase = tokens[7]
self.attribute = tokens[8]
self.id = get_ID(line)
def __str__(self):
return self.id
def seqids(self):
return self.seqid
def sources(self):
return self.source
def types(self):
return self.type
def starts(self):
return self.start
def ends(self):
return self.end
def scores(self):
return self.score
def strands(self):
return self.strand
def phases(self):
return self.phase
def attributes(self):
return self.attribute
def process_objs(obj,phase, attributes):
print obj.seqids() + '\t' + \
obj.sources() + '\t' + \
obj.types() + '\t' + \
obj.starts() + '\t' + \
obj.ends() + '\t' + \
obj.scores() + '\t' + \
obj.strands() + '\t' + \
str(phase) + '\t' + \
attributes
def getPhase(obj):
global CDS
CDS[int(obj.starts())] = obj
### returns a list containing phase values
def calcPhase(CDS, start_count):
phase = []
if start_count == 0:
for start in sorted(CDS.keys()):
end = CDS[start].ends()
if len(phase) == 0:
phase.append(0)
last_phase = phase[-1]
phase.append( ( 3 - ((int(end) - int(start) + 1 )%3) + last_phase )%3)
last_phase = phase[-1]
else:
phase.append( ( 3 - ((int(end) - int(start) + 1 )%3) + last_phase )%3)
last_phase = phase[-1]
return phase
else:
lst = sorted(CDS.keys())
for start in lst[::-1]:
end = CDS[start].ends()
if len(phase) == 0:
phase.append(0)
last_phase = phase[-1]
phase.append( (((int(end) - int(start) + 1 )%3) + last_phase )%3)
last_phase = phase[-1]
else:
phase.append( (((int(end) - int(start) + 1 )%3) + last_phase )%3)
last_phase = phase[-1]
return phase[::-1][1:]
def process_CDS(count):
start_count = count
phase = calcPhase(CDS, start_count)
i = 0
for start in sorted(CDS.keys()):
if start_count == 0:
count += 1
attributes = re.sub(r'ID.+?;','ID='+mRNA_id+'.CDS.'+str(count)+';',CDS[start].attributes())
process_objs(CDS[start], phase[i], attributes)
else:
attributes = re.sub(r'ID.+?;','ID='+mRNA_id+'.CDS.'+str(count)+';',CDS[start].attributes())
process_objs(CDS[start], phase[i], attributes)
count -= 1
i += 1
def printCDS():
if CDS_strand == '+':
count = 0
process_CDS(count)
else:
count = len(CDS)
process_CDS(count)
def parseGFF3(gff3):
global CDS, mRNA_id, CDS_strand
CDS_flag = False
for line in open(gff3,'r'):
if (len(line)>2) & (not line.startswith('#')):
obj = GFF3(line)
if obj.types() != 'CDS':
if CDS_flag == True:
printCDS()
CDS = {}
CDS_flag = False
print line.strip()
else:
CDS_flag = True
CDS_strand = obj.strands()
getPhase(obj)
if obj.types() == 'mRNA':
mRNA_id = str(obj)
first_line = False
printCDS()
if __name__ == "__main__":
file = options(sys.argv[1:])
parseGFF3(file)
### close the logfile
o.close()