-
Notifications
You must be signed in to change notification settings - Fork 2
/
VCF_dip2hap.py
41 lines (33 loc) · 1.11 KB
/
VCF_dip2hap.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
# -*- coding: utf-8 -*-
"""
Created on 2021-12-21
@author: Yudongcai
@Email: [email protected]
"""
import typer
import gzip
def main(in_vcf: str = typer.Argument(..., help="输入的二倍体vcf(.vcf.gz)"),
out_vcf: str = typer.Argument(..., help="输出的单倍体vcf(.vcf)")
):
"""
把二倍体的vcf拆分为单倍体。
"""
with gzip.open(in_vcf, 'rb') as f1, open(out_vcf, 'w') as f2:
for line in f1:
tline = line.decode()
if tline[:2] == '##':
f2.write(tline)
elif tline[:2] == '#C':
tline = tline.strip().split()
f2.write('\t'.join(tline[:9]))
for sample in tline[9:]:
f2.write(f'\t{sample}-1\t{sample}-2')
f2.write('\n')
else:
tline = tline.strip().split()
f2.write('\t'.join(tline[:9]))
for gt in tline[9:]:
f2.write(f'\t{gt[0]}\t{gt[2]}')
f2.write('\n')
if __name__ == '__main__':
typer.run(main)