Skip to content

Commit b37cbe4

Browse files
dpark01claude
andcommitted
Fix FreeBayes VCF output: bgzip and tabix-index when .vcf.gz requested
FreeBayes always writes plain VCF regardless of output filename, but downstream VcfReader (pysam.TabixFile) requires bgzipped + tabix-indexed input. Write to temp .vcf, then compress/index with pysam. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0feb58f commit b37cbe4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/viral_ngs/assemble/freebayes.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
'''
77

88
import logging
9+
import os
910
import shutil
1011
import subprocess
1112

13+
import pysam
14+
1215
from ..core import Tool, PrexistingUnixCommand
16+
from ..core.file import mkstempfname
1317

1418
_log = logging.getLogger(__name__)
1519

@@ -23,15 +27,30 @@ def __init__(self):
2327
Tool.__init__(self, install_methods=install_methods)
2428

2529
def call(self, inBam, refFasta, outVcf, options=None):
26-
"""Call variants with FreeBayes, emitting all sites."""
30+
"""Call variants with FreeBayes, emitting all sites.
31+
32+
If outVcf ends with .vcf.gz, the output is bgzipped and tabix-indexed
33+
(FreeBayes always writes plain VCF, so we compress and index afterward).
34+
"""
2735
options = options or ["--min-base-quality", "15", "--pooled-continuous"]
36+
37+
if outVcf.endswith('.vcf.gz'):
38+
plainVcf = mkstempfname('.vcf')
39+
else:
40+
plainVcf = outVcf
41+
2842
opts = [
2943
'-b', inBam,
3044
'-f', refFasta,
31-
'-v', outVcf,
45+
'-v', plainVcf,
3246
'--use-best-n-alleles', '0',
3347
'--report-monomorphic',
3448
]
3549
tool_cmd = [self.install_and_get_path()] + opts + options
3650
_log.debug(' '.join(tool_cmd))
3751
subprocess.check_call(tool_cmd)
52+
53+
if outVcf.endswith('.vcf.gz'):
54+
pysam.tabix_compress(plainVcf, outVcf, force=True)
55+
pysam.tabix_index(outVcf, preset='vcf', force=True)
56+
os.unlink(plainVcf)

0 commit comments

Comments
 (0)