Change column order in vcf file
1
0
Entering edit mode
9 weeks ago
gms03223 • 0

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 • 185 views
ADD COMMENT
0
Entering edit mode
9 weeks ago
Shred ▴ 990

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

ADD COMMENT

Login before adding your answer.

Traffic: 3221 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6