How to subset vcf by sample and write?
Hi. I have a vcf file containing variants information of multiple samples. How can I subset it by sample ID and write and save them separately?
• 3,298 views
•
link
2 answers
If you want to only subset several samples, try:
bcftools view combined.vcf.gz -s sample_ID -Oz > sample_ID.vcf.gz
If you want to subset every sample, try:
bcftools +split input.vcf.gz -Oz -o vcf_per_sample
• 0 views
•
link
Here's a Python API solution using the pyvcf submodule I wrote:
>>> from fuc import pyvcf
>>> data = {
... 'CHROM': ['chr1', 'chr1'],
... 'POS': [100, 101],
... 'ID': ['.', '.'],
... 'REF': ['G', 'T'],
... 'ALT': ['A', 'C'],
... 'QUAL': ['.', '.'],
... 'FILTER': ['.', '.'],
... 'INFO': ['.', '.'],
... 'FORMAT': ['GT:DP', 'GT:DP'],
... 'A': ['0/1:30', '0/1:29'],
... 'B': ['0/1:24', '0/1:30'],
... 'C': ['0/1:18', '0/1:24'],
... }
>>> vf = pyvcf.VcfFrame.from_dict([], data)
>>> # vf = pyvcf.VcfFrame.from_file('in.vcf')
>>> vf.df
CHROM POS ID REF ALT QUAL FILTER INFO FORMAT A B C
0 chr1 100 . G A . . . GT:DP 0/1:30 0/1:24 0/1:18
1 chr1 101 . T C . . . GT:DP 0/1:29 0/1:30 0/1:24
>>> a_vf = vf.subset('A')
>>> a_vf.df
CHROM POS ID REF ALT QUAL FILTER INFO FORMAT A
0 chr1 100 . G A . . . GT:DP 0/1:30
1 chr1 101 . T C . . . GT:DP 0/1:29
>>> a_vf.to_file('A.vcf')
• 0 views
•
link
Log in to answer this question.
Splitting vcf files to individual samples