美文网首页
test_xia8:py处理vcf文件

test_xia8:py处理vcf文件

作者: 夕颜00 | 来源:发表于2020-06-07 14:35 被阅读0次

    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

    相关文章

      网友评论

          本文标题:test_xia8:py处理vcf文件

          本文链接:https://www.haomeiwen.com/subject/yxhnzhtx.html