-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvs_gene_calc.py
329 lines (254 loc) · 5.9 KB
/
cvs_gene_calc.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#import
#
import re
import sys
from operator import itemgetter
import argparse
import csv
import os
#arguments
#
parser = argparse.ArgumentParser(description="Gene Calculator")
parser.add_argument("-in", "--infile", required=True,
help="name of file", type=str)
parser.add_argument("-out", "--outfile",
help="save to a file", type=str)
args = parser.parse_args()
try:
fileObj = open(args.infile, 'r') #try to open the file
except FileNotFoundError as errorObj:
print("File not found", errorObj)
line = fileObj.readlines()#read file
string = ''.join(line) #make a string out of a list
sublines = string.splitlines() #split into lines
data0 = ' '.join(sublines) #make a string out of each line
final = data0.split('>') #split on the appearance of ">"
my_line = final[1:] #remove '' before the first line of interest
ann = 0
pseudo = 0
count = 0
rev = 0
forw = 0
leng = 0
scaf = 0
cOne = 0
cTwo = 0
cThree = 0
cFour = 0
cFive = 0
cSix = 0
cSeven = 0
cEight = 0
cNine = 0
cTen = 0
cEleven = 0
cTwelve = 0
cX = 0
cY = 0
T = 0
loc = 0
c13 = 0
c14 = 0
c15 = 0
c16 = 0
c17 = 0
c18 = 0
c19 = 0
c20 = 0
c21 = 0
c22 = 0
c23 = 0
c24 = 0
c25 = 0
cTwoA = 0
cTwoB = 0
for s in my_line:
if re.search(r'NONE', s):
ann += 1
else:
pass
for s in my_line:
count += 1
for s in my_line:
if s.startswith(' (+)'):
forw += 1
else:
pass
for s in my_line:
if s.startswith(' (-)'):
rev += 1
else:
pass
for s in my_line:
if re.search(r'(?<=PSE)UDO', s):
pseudo += 1
else:
pass
for s in my_line:
if re.search(r' P ', s):
leng += 1
else:
pass
for s in my_line:
if re.search(r'unplaced', s):
scaf += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 1', s):
cOne += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 2', s):
cTwo += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 3', s):
cThree += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 4', s):
cFour += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 5', s):
cFive += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 6', s):
cSix += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 7', s):
cSeven += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 8', s):
cEight += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 9', s):
cNine += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 10', s):
cTen += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 11', s):
cEleven += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 12', s):
cTwelve += 1
else:
pass
for s in my_line:
if re.search(r'chromosome X', s):
cX += 1
else:
pass
for s in my_line:
if re.search(r'chromosome Y', s):
cY += 1
else:
pass
for s in my_line:
if re.search(r' T1 ', s):
T += 1
else:
pass
for s in my_line:
if re.search(r' unlocalized ', s):
loc += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 13', s):
c13 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 14', s):
c14 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 15', s):
c15 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 16', s):
c16 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 17', s):
c17 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 18', s):
c18 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 19', s):
c19 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 20', s):
c20 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 21', s):
c21 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 22', s):
c22 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 23', s):
c23 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 24', s):
c24 += 1
else:
pass
for s in my_line:
if re.search(r'chromosome 25', s):
c25 += 1
else:
pass
fname = args.outfile
if os.path.isfile(fname):
print("File updated", args.outfile)
with open(args.outfile, 'a') as csvfile:
writer1 = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer1.writerow([args.infile, ann, pseudo, forw, rev, scaf, loc, leng, count, T,cOne, cTwo, cThree,cFour, cFive,cSix,cSeven,cEight,cNine,cTen,cEleven,cTwelve, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23,c24,c25,cX,cY])
else:
print("New file created", args.outfile)
with open(args.outfile, 'w') as csvfile:
fieldnames = ['Species', 'No Annotation', 'Pseudo Annotation', 'Forward', 'Reverse', 'Unplaced', 'Unlocalised', 'Partial', 'Count', 'Transcript', 'Chr 1', 'Chr 2(A/B)', 'Chr 3', 'Chr 4', 'Chr 5', 'Chr 6','Chr 7' ,'Chr 8', 'Chr 9', 'Chr 10','Chr 11','Chr 12', 'Chr 13', 'Chr 14', 'Chr 15', 'Chr 16','Chr 17' ,'Chr 18', 'Chr 19', 'Chr 20','Chr 21','Chr 22', 'Chr 23', 'Chr 24', 'Chr 25', 'Chr X','Chr Y']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Species': args.infile, 'No Annotation': ann, 'Pseudo Annotation' : pseudo, 'Forward' : forw, 'Reverse' : rev, 'Unplaced' : scaf, 'Unlocalised': loc, 'Partial' : leng, 'Count': count, 'Transcript' : T, 'Chr 1' : cOne, 'Chr 2(A/B)' : cTwo, 'Chr 3': cThree, 'Chr 4' : cFour, 'Chr 5' : cFive, 'Chr 6' : cSix ,'Chr 7' : cSeven ,'Chr 8' : cEight, 'Chr 9' : cNine, 'Chr 10' : cTen, 'Chr 11' : cEleven, 'Chr 12' : cTwelve, 'Chr 13' : c13, 'Chr 14' : c14, 'Chr 15' : c15, 'Chr 16' : c16,'Chr 17' : c17 ,'Chr 18' : c18, 'Chr 19' : c19, 'Chr 20' : c20, 'Chr 21' : c21, 'Chr 22' : c22, 'Chr 23' : c23,'Chr 24' : c24,'Chr 25' : c25,'Chr X' : cX, 'Chr Y' : cY})