This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filter VCF with txt

Hi everyone.

I'm trying to filter a vcf with a txt that has only the ID column but I keep getting the following error message: [E::bcf_sr_regions_init] Could not parse the file list.txt, using the columns 1,2[,-1] Failed to read the targets: ^list.txt

I've been using the following command: bcftools view -T ^list.txt my.vcf > vcf_filtered

Is it possible to do such operation or should my txt have more fields? Keep in mind that my goal is to only keep the snp's listed in the txt.

Thank you in advance.

snp vcf filtering

2 answers

$ grep ^# snps.vcf > snps.header.vcf
$ grep -F -f list.txt snps.vcf > snps.filtered.noHeader.txt
$ cat snps.header.vcf snps.filtered.noHeader.txt > snps.filtered.withHeader.vcf

If you want to be fancypants and not waste time making intermediate files, this would be faster:

$ cat <(grep ^# snps.vcf) <(grep -F -f list.txt snps.vcf) > snps.filtered.withHeader.vcf

If you want to make it yet faster:

$ LC_ALL=C
$ cat <(grep ^# snps.vcf) <(grep -F -f list.txt snps.vcf) > snps.filtered.withHeader.vcf

It's probably unlikely that VCF files contain Unicode characters, and so limiting the character set to ASCII will make pattern matching with grep much faster.

Thank you, Alex. It worked!

$ grep -F -f list.txt snps.vcf > snps.filtered.txt

Hi Alex, thanks for your reply. It was really helpful!

I was able to filter the desired snp's but now I have an output file without the headers (## lines). How can I prevent that from happening? I ran the command:

grep -e -F -f id.txt my.vcf > filtered.vcf

Thanks.

Log in to answer this question.