This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Change column order in vcf file

Hello, I'm having troubles with a vcf file. Since the sample names are sorted alphabetically in the vcf after Mutect2, I want to arrange the samples in order of my choice..

[current]

CHROM POS ID REF ALT QUAL FILTER INFO FORMAT [sample A] [sample B]

[wish]

CHROM POS ID REF ALT QUAL FILTER INFO FORMAT [sample B] [sample A]

How can I change the order of the columns? Is it possible with gatk tool?

vcf gatk mutect2

1 answer

Python solution, for uncompressed vcf file (would be easy to implement also for compressed vcf).

import sys

with open(sys.argv[1], 'r') as vcf_input:
    for line in vcf_input:
        if line.startswith('#'):
            print(line.rstrip()) # remove line endings
        else:
            print("\t".join(
                line.rstrip().split('\t')[:-2], # all the columns minus samples
                line.rstrip().split('\t')[-1], # last sample
                line.rstrip().split('\t')[-2])) # other one

Usage:

python3 script.py input.vcf > output.vcf

If the wanted order is alphabetically, or something less specific, use bcftools as suggested here

Log in to answer this question.