This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split VCF into separate VCFs by SNP count

Is there a way to run bcftools or another program to split up a vcf of a single scaffold/chromosome into chunks by SNP count? For example, how could I best split a VCF with 10,581 SNPs into ten chunks of length 1000 (snp 1 - 1000, snp 1001 - 2000, ...), and an 11th chunk SNP 10,001 - SNP 10,581. Is there a simple way to do this?

Thanks!

vcf bcftools next-gen

2 answers

There could be a more elegant way, but this could work. The first line splits up the snps into chunks of whatever size you want (-l) and then the next line loops over each file and subsets the vcf according.

bcftools query -f'%CHROM\t%POS\n' in.bcf | split -l 1000
for file in x*; do bcftools view -T $file -Ob in.bcf > in.$file.bcf; done

I wrote a tool: http://lindenb.github.io/jvarkit/Biostar497922.html

$ gunzip -c src/test/resources/rotavirus_rf.vcf.gz | java -jar dist/biostar497922.jar -o TMP -n 20 -m jeter.mf
[INFO][Biostar497922]Writing TMP/split.000001.vcf.gz
[INFO][Biostar497922]Writing TMP/split.000002.vcf.gz
[INFO][Biostar497922]Writing TMP/split.000003.vcf.gz
[INFO][Biostar497922]. Completed. N=45. That took:0 second

TMP/split.000001.vcf.gz RF01    970 A   RF04    1900    A   20
TMP/split.000002.vcf.gz RF04    1920    A   RF09    317 C   20
TMP/split.000003.vcf.gz RF09    414 T   RF11    74  CAAAAA  5

Log in to answer this question.