-
Notifications
You must be signed in to change notification settings - Fork 3
/
split_vcf_chr.py
35 lines (29 loc) · 997 Bytes
/
split_vcf_chr.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
import gzip
import argparse
def open_vcf(args, current_chrom):
chr_vcf = gzip.open(args.vcf.split('.vcf.gz')[0] + '_' + str(current_chrom) + '.vcf.gz', 'wt')
return chr_vcf
def main(args):
vcf = gzip.open(args.vcf)
header = []
last_chrom = ''
for line in vcf:
current_line = line.strip().decode('utf-8')
current_chrom = current_line.split()[0]
if current_line.startswith('#'):
header.append(current_line)
continue
if current_chrom != last_chrom:
if last_chrom.startswith('chr'):
chr_vcf.close()
chr_vcf = open_vcf(args, current_chrom)
for header_line in header:
chr_vcf.write((header_line + '\n'))
chr_vcf.write((current_line + '\n'))
last_chrom = current_chrom
chr_vcf.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--vcf')
args = parser.parse_args()
main(args)