-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcf_subset_by_chromosome.py
124 lines (85 loc) · 2.68 KB
/
vcf_subset_by_chromosome.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
#!/usr/local/bin/python
# Python 2.7.6
# vcf_subset_by_chromosome.py
# 13 April 2015
# Mathias Scharmann
# usage example
# python vcf_subset_by_chromosome.py --vcf raw.01.vcf.tmp6.recode.vcf --exclude_chrom fufuchroms.txt
"""
takes vcf made by freebayes and outputs a variant matrix in phylip format for the loci subset specified in --loci
"""
import argparse
import os
########################## HEAD
# this function checks if file exists and exits script if not
def extant_file(x):
"""
'Type' for argparse - checks that file exists but does not open.
"""
if not os.path.exists(x):
print "Error: {0} does not exist".format(x)
exit()
x = str(x)
return x
# breaks script if non-UNIX linebreaks in input file popmap
def linebreak_check(x):
if "\r" in open(x, "rb").readline():
print "Error: classic mac (CR) or DOS (CRLF) linebreaks in {0}".format(x)
exit()
######
# parses arguments from command line
def get_commandline_arguments ():
parser = argparse.ArgumentParser()
parser.add_argument("--vcf", required=True, type=extant_file,
help="name/path of vcf input file", metavar="FILE")
parser.add_argument("--exclude_chrom", required=True, type=extant_file,
help="name/path of the file with names of chromosomes to exclude", metavar="FILE")
args = parser.parse_args()
# finish
return args
################################## CORE
def parse_vcf(vcf_file, excl_chroms):
try:
os.remove(vcf_file+"subset.vcf")
except OSError:
None
# get total lines:
num_lines = sum(1 for line in open(vcf_file))
# now start the main business of walking through the vcf:
out_lines = []
with open(vcf_file, "r") as INFILE:
with open(vcf_file+"subset.vcf", "a") as OUTFILE:
linecnt = 0
tlinecnt = 0
retained_cnt = 0
for line in INFILE:
linecnt += 1
tlinecnt += 1
# print tlinecnt
if line.startswith("#"):
out_lines.append(line.strip("\n"))
else:
# throw away unwanted chromosomes:
fields = line.strip("\n").split("\t")
if fields[0] not in excl_chroms:
# throw away also all non-biallelic SNPs:
if "," not in fields[4]:
out_lines.append(line.strip("\n"))
retained_cnt += 1
if linecnt == 100000:
# write to file and flush memory
OUTFILE.write("\n".join(out_lines)+"\n")
linecnt = 0
out_lines = []
elif tlinecnt == num_lines:
OUTFILE.write("\n".join(out_lines))
print "retained\t" + str(retained_cnt)
################################## MAIN
args = get_commandline_arguments ()
#print args
excl_chrom = set()
with open(args.exclude_chrom, "r") as INFILE:
for line in INFILE:
excl_chrom.add(line.strip("\n"))
parse_vcf(args.vcf, excl_chrom)
print "Done!"