-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvcf_pos_count.py
More file actions
executable file
·66 lines (57 loc) · 2.25 KB
/
vcf_pos_count.py
File metadata and controls
executable file
·66 lines (57 loc) · 2.25 KB
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
#!/usr/bin/env python
"""
Counts SNP occurrence frequency for each POS in `.vcf` file.
"""
import sys
import argparse
__author__ = 'Pim Bongaerts'
__copyright__ = 'Copyright (C) 2016 Pim Bongaerts'
__license__ = 'GPL'
HEADER_CHAR = '#'
MAX_READ_LENGTH = 1000
MAX_STATUS_BAR_LENGTH = 60
def main(vcf_filename):
# Initialise dict with SNP positions
pos_occurrence = dict.fromkeys(range(1, MAX_READ_LENGTH), 0)
# Open VCF and iterate over SNPs
vcf_file = open(vcf_filename, 'r')
highest_count = highest_pos = 0
for line in vcf_file:
if line[0] != HEADER_CHAR:
pos = int(line.split()[1])
if pos in pos_occurrence:
pos_occurrence[pos] += 1
if pos_occurrence[pos] > highest_count:
highest_count = pos_occurrence[pos]
if pos > highest_pos:
highest_pos = pos
else:
print('Error - unexpected pos: {0}'.format(pos))
# Initialise file and variables for outputting
output_file = open(vcf_filename.replace('.vcf', '') + '_occur.txt', 'w')
group_number = group_count = 0
last_group_number = 1
graph_multiplier = MAX_STATUS_BAR_LENGTH / (highest_count * 4)
# Iterate over occurrence dict and output result
for pos in pos_occurrence:
# Output to file
output_file.write('{0}\t{1}\n'.format(pos, pos_occurrence[pos]))
# Output to screen every 5 bp
group_count += pos_occurrence[pos]
group_number += 1
graph_bar = '*' * int(group_count * graph_multiplier)
if group_number == 5:
print('{0}-{1}\t{2}\t{3}'.format(last_group_number,
pos, group_count, graph_bar))
last_group_number = pos + 1
group_number = group_count = 0
elif pos >= highest_pos:
print('{0}-{1}\t{2}\t{3}'.format(last_group_number,
pos, group_count, graph_bar))
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('vcf_filename', metavar='vcf_file',
help='input file with SNP data (`.vcf`)')
args = parser.parse_args()
main(args.vcf_filename)