Extracting Columns From Vcf File Using Vcftools/Perl
3
3
Entering edit mode
11.7 years ago
bioinfo ▴ 830

What's the easiest way to extract 2 or more columns simultaneously from my vcf SNP file and export them in CSV format from command line in linux? Any commands in vcftools or do I have to use perl?

vcftools snp perl • 18k views
ADD COMMENT
0
Entering edit mode

You may want to specify if you're trying to extract 2 or more samples or simply arbitrary columns. It looks like matted's answer applies to the former, and Dk and Rm's apply to the latter.

ADD REPLY
8
Entering edit mode
11.7 years ago

This linux command will output column 1,2, and 3 of your file into csv. Change the 1,2,3 according to the column (1-based) you want to extract. You can extract as many columns as you want.

cut -f 1,2,3 fileName.vcf | sed 's/[\t]/,/g' > cols.csv
ADD COMMENT
0
Entering edit mode

I tried with the command but not sure why i m getting it wrong..!!

cut -f 2,6,9 file.vcf | sed 's/[\t]/,/g' > out.csv
sed: -e expression #1, char 1: unknown command: `\ufffd'

sed: -e expression #1, char 1: unknown command: `\ufffd'

By the way what else can I do for INFO column (column 9) if I want to get just DP from a list of things in that column 9 (INFO column contains e,g. AN=2;DP=87;Dels=0.00..)

ADD REPLY
4
Entering edit mode
11.7 years ago
matted 7.8k

For completeness, here's a vcftools solution:

vcf-subset -c name1,name3,name5 in.vcf.gz | tr "\t" "," > out.csv
ADD COMMENT
0
Entering edit mode
vcf-subset -c POS,QUAL my.vcf.gz | tr "\t" "," > out.csv

Its giving me just the header line of the two columns in the VCF file POS and QUAL

#CHROM,POS,ID,REF,ALT,QUAL,FILTER,INFO,FORMAT,POS,QUAL

by the way I don't want any other info except the data in the columns with header

ADD REPLY
1
Entering edit mode
11.7 years ago
Rm 8.3k

Using Awk :

awk 'BEGIN {OFS ="," ; FS = "\t"};{print $1, $2, $4}' input.vcf > output.csv

In perl:

perl -F"\t" -lane '$, = ","; print $F[0], $F[1] , $F[3]' input.vcf > output.csv
ADD COMMENT

Login before adding your answer.

Traffic: 1508 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