1、输入文件:
image.png
2、提取特定的列。
并将ALT有多个值的情况,分成多行输出;
image.png
3、脚本1: 将ALT的值按照多行输出
import vcf
import csv
input = 'E:/Script/python/xia_test/8/demo.vcf'
output = 'E:/Script/python/xia_test/8/out.csv'
vcf_reader = vcf.Reader(open(input, 'r'))
with open(output, 'w', newline="") as file:
writer = csv.writer(file, quotechar='"')
header = ['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'GT']
writer.writerow(header)
for record in vcf_reader:
for i in record.ALT:
line = record.CHROM, record.POS, record.ID, record.REF, i, record.QUAL, record.samples[0]['GT']
writer.writerow(line)
4、脚本2:
将ALT的值按照分隔符“”|“”,输出
import vcf
import csv
input = 'E:/Script/python/xia_test/8/demo.vcf'
output = 'E:/Script/python/xia_test/8/out.csv'
vcf_reader = vcf.Reader(open(input, 'r'))
with open(output, 'w', newline="") as file:
writer = csv.writer(file, quotechar='"')
header = ['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'GT']
writer.writerow(header)
for record in vcf_reader:
# print(type(record.ALT))
# break
line = record.CHROM, record.POS, record.ID, record.REF, "|".join(list(map(str,record.ALT))), record.QUAL, record.samples[0]['GT']
writer.writerow(line)
image.png
网友评论