-
Notifications
You must be signed in to change notification settings - Fork 1
/
vcf_subset_by_chromosome.py
144 lines (107 loc) · 3.77 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/local/bin/python
# Python 2.7.6
# vcf_subset_by_chromosome.py
# a python script that filters .vcf files for the CHROM field, i.e. extracts RAD-tags by their names
# Copyright (C) 2017, ETH Zurich, Mathias Scharmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# If you use this code please cite:
#
# "Scharmann M, Grafe TU, Metali F, Widmer A. (2017) Sex-determination
# and sex chromosomes are shared across the radiation of dioecious
# Nepenthes pitcher plants. XXX"
# AND/OR
# "Scharmann M, Metali F, Grafe TU, Widmer A. (2017) Divergence with
# gene flow among multiple sympatric carnivorous Nepenthes pitcher
# plants is linked to trap morphology. XXX"
#
# contact: mathias.scharmann[-at-]env.ethz.ch or msph52[-at-]gmail.com
# usage example
# python vcf_subset_by_chromosome.py --vcf raw.01.vcf.tmp6.recode.vcf --exclude_chrom fufuchroms.txt
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!"