-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyVarcheck.py
executable file
·295 lines (263 loc) · 8.43 KB
/
pyVarcheck.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
#!/usr/bin/env python
from __future__ import division
__doc__ = """
SYNOPSIS
pypy pyVarcheck.py -i <input.mpileup.gz> -o <output.gz> -h <help>
or
pypy pyVarcheck.py -o <output.gz> <input.mpileup.gz>
DESCRIPTION
Tab delimited output file with:
chromosome, position, ref_base, depth, mutations, mutation fractions, support reads, chain fractions, support reads for each chain
EXAMPLES
pypy pyVarcheck.py -h
pypy pyVarcheck.py -i input.mpileup.gz -o output.varcheck.gz
pypy pyVarcheck.py -o output.varcheck.gz input.mpileup.gz
"""
__author__ = "Hao Guo"
__version__ = '0.1'
import os, sys, time, getopt, re
import gzip
# read .gz file
def read_gz_file(path):
if os.path.exists(path):
with gzip.open(path,'r') as pf:
for line in pf:
yield line
else:
print('The path \'{}\' is not exist!'.format(path))
# write .gz file
def write_gz_file(path,content):
try:
with gzip.open(path,'w') as f:
f.write(content)
except:
print("gz file write error!")
#split file into chunks
def splitter(l, n):
i = 0
chunk = l[:n]
while chunk:
yield chunk
i += n
chunk = l[i:i+n]
# test '+' existence
def test_in(s,valid):
test = 0
for x in valid:
if x+'+' in s:
test = 1
break
if test == 1:
return 1
else:
return 0
# test '-' existence
def test_del(s,valid):
test = 0
for x in valid:
if x+'-' in s:
test = 1
break
if test == 1:
return 1
else:
return 0
#find first '+'
def find_first_in(s):
pin = s.index('+')
numb = ""
i = 0
while 1:
if s[pin + 1 + i].isdigit():
numb += s[pin + 1 + i]
i += 1
else:
break
take = int(numb)
total = s[pin - 1:pin + len(numb) + 1 + take]
return(total,numb)
#find first '-'
def find_first_del(s):
pin = s.index('-')
numb = ""
i = 0
while 1:
if s[pin + 1 + i].isdigit():
numb += s[pin + 1 + i]
i += 1
else:
break
take = int(numb)
total = s[pin - 1:pin + len(numb) + 1 + take]
return(total,numb)
# merge lower and upper key values
def merge_indels(dic):
keys = list(set([s.upper() for s in dic.keys()]))
inserts_ig = {}
for k in keys:
try:
m = dic[k]
except:
m=0
try:
n = dic[k.lower()]
except:
n = 0
inserts_ig.setdefault(k, m + n)
return(inserts_ig)
# construct indel list
def construct_indelgeno(inserts_ig,inserts):
scan=[]
inlist = map(lambda x: [x, inserts_ig[x]], inserts_ig.keys())
inlist.sort(lambda x, y: cmp(y[1], x[1]))
for il in inlist:
inname = il[0]
incount = il[1]
try:
pcount = inserts[inname]
except:
pcount = 0
try:
ncount = inserts[inname.lower()]
except:
ncount = 0
scan += [[inname, incount, pcount, ncount]]
return(scan)
# parse information of the 5th column from mpileup file
def parse_info(info_string, ref, depth):
inserts = {}
dels = {}
quals = {'a': 0, 'A': 0, 'c': 0, 'C': 0, 'g': 0, 'G': 0, 't': 0, 'T': 0, '.': 0, ',': 0, '*': 0}
valid = ['a', 'A', 'c', 'C', 'g', 'G', 't', 'T', '.', ',', '*']
x = 0
temp = info_string
temp = re.sub(r'\^\S', '', temp)
#temp = temp.replace('^+', '')
temp = temp.replace('$', '')
while test_in(temp, valid) == 1:
(total,numb) = find_first_in(temp)
si=2+len(numb)
inserts.setdefault('+' + total[si:], temp.count(total))
temp = temp.replace(total, '')
while test_del(temp, valid) == 1:
(total,numb) = find_first_del(temp)
si = 2 + len(numb)
dels.setdefault('-' + total[si:], temp.count(total))
temp = temp.replace(total, '')
while 1:
if x >= len(temp):
break
elif temp[x] in valid:
quals[temp[x]] += 1
elif temp[x] == "^":
x += 1
elif temp[x] == '+' or temp[x] == '-':
temp1 = ""
i = 0
while 1:
if temp[x + 1 + i].isdigit():
temp1 += temp[x + 1 + i]
i += 1
else:
break
x += int(temp1) + len(temp1)
x += 1
del_ig = merge_indels(dels)
inserts_ig = merge_indels(inserts)
Aa_HQ = quals['A'] + quals['a']
Tt_HQ = quals['T'] + quals['t']
Cc_HQ = quals['C'] + quals['c']
Gg_HQ = quals['G'] + quals['g']
match_HQ = quals['.'] + quals[',']
dls = quals['*']
if len(inserts_ig) > 0 and len(del_ig) > 0:
scan1 = construct_indelgeno(inserts_ig, inserts)
scan2 = construct_indelgeno(del_ig, dels)
scan = scan1 + scan2
elif len(del_ig) > 0:
scan = construct_indelgeno(del_ig, dels)
elif len(inserts_ig) > 0:
scan = construct_indelgeno(inserts_ig, inserts)
else:
scan = []
scan += [['A', Aa_HQ, quals['A'], quals['a']], \
['T', Tt_HQ, quals['T'], quals['t']], \
['C', Cc_HQ, quals['C'], quals['c']], \
['G', Gg_HQ, quals['G'], quals['g']], \
['*', dls, 0, 0]]
for w in scan:
if w[0] == ref:
w[1] += match_HQ
w[2] += quals['.']
w[3] += quals[',']
scan.sort(lambda x, y: cmp(float(y[1]), float(x[1])))
scan2 = map(lambda x: [x[0], \
round(x[1] / float(depth), 4), \
round(x[2] / float(x[1]), 4) if x[1] != 0 else 0, \
round(x[3] / float(x[1]), 4) if x[1] != 0 else 0], scan)
scan11 = filter(lambda x: x[1] > 0, scan)
scan22 = filter(lambda x: x[1] > 0, scan2)
name = '|'.join([x[0] for x in scan11])
freq = '|'.join([str(x[1] * 100) for x in scan22])
support = '|'.join([str(x[1]) for x in scan11])
chainF = '|'.join([str(x[2] * 100)+','+str(x[3]*100) for x in scan22])
chainS = '|'.join([str(x[2]) + ',' + str(x[3]) for x in scan11])
info = name + '\t' + freq + '\t' + support + '\t' + chainF + '\t' + chainS + '\n'
return info
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
# print opts
# print ('args:%s'%(args))
except (getopt.GetoptError) as e:
print('Option error, please use -h for help!')
sys.exit(0)
input = None
for op, value in opts:
if op == "-h":
print('Usage: pypy pyVarcheck.py -i <input.mpileup.gz> -o <output.gz> -h <help>')
print(' Options:\n -h\t: help message\n -i\t: input mpileup file\n -o\t: output varcheck file')
print(__doc__)
sys.exit(0)
elif op == "-o":
output = value
# print output
elif op == "-i":
input = value
else:
print('Wrong option specified.')
sys.exit(0)
try:
output_file = output
except:
output_file = re.split(r'\.', args[0])[0] + '.varcheck.gz'
try:
fileDic = input or args[0]
# print fileDic
if fileDic != "":
con = read_gz_file(fileDic)
with gzip.open(output_file, 'w') as f:
if getattr(con, '__iter__', None):
i=0
for line in con:
chrom = re.split(r'\s', line)[0]
pos = re.split(r'\s', line)[1]
ref = re.split(r'\s', line)[2].upper()
depth = re.split(r'\s', line)[3]
bases = re.split(r'\s', line)[4]
if int(depth)>0:
info = parse_info(bases, ref, depth)
# print("%s\t%s\t%s\t%s\t%s" % (chrom, pos, ref, depth, info))
f.write("%s\t%s\t%s\t%s\t%s" % (chrom, pos, ref, depth, info))
i += 1
else:
f.write("%s\t%s\t%s\t%s\n" % (chrom, pos, ref, depth))
except IndexError as e:
print('Error, try to use ""pypy pyVarcheck.py -i /path/file.mpileup.gz ""')
sys.exit(1)
if __name__ == '__main__':
starttime = time.time()
# print('--------------Parsing information from mpileup file -----------------')
main()
# print('%s was done!'%(sys.argv[1]))
print('Processing time is : %s' % (time.time() - starttime))