test_xia8:py处理vcf文件

2020-06-07  本文已影响0人  夕颜00

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
上一篇下一篇

猜你喜欢

热点阅读