Skip to content

Commit

Permalink
Implement %SAMPLE tag in vcztools query
Browse files Browse the repository at this point in the history
  • Loading branch information
Will-Tyler authored and jeromekelleher committed Nov 27, 2024
1 parent 5aa10a2 commit 598c441
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tests/test_bcftools_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ def test_vcf_output_with_output_option(tmp_path, args, vcf_file):
(r"query -f '%POS\n' -e 'POS=112'", "sample.vcf.gz"),
(r"query -f '[%CHROM\t]\n'", "sample.vcf.gz"),
(r"query -f '[%CHROM\t]\n' -i 'POS=112'", "sample.vcf.gz"),
(r"query -f '%CHROM\t%POS\t%REF\t%ALT[\t%GT]\n'", "sample.vcf.gz"),
(r"query -f '%CHROM\t%POS\t%REF\t%ALT[\t%SAMPLE=%GT]\n'", "sample.vcf.gz"),
(r"query -f 'GQ:[ %GQ] \t GT:[ %GT]\n'", "sample.vcf.gz"),
(r"query -f '[%CHROM:%POS %GT\n]'", "sample.vcf.gz"),
(r"query -f '[%GT %DP\n]'", "sample.vcf.gz"),
(r"query -f '[%CHROM:%POS %SAMPLE %GT\n]'", "sample.vcf.gz"),
(r"query -f '[%SAMPLE %GT %DP\n]'", "sample.vcf.gz"),
],
)
def test_output(tmp_path, args, vcf_name):
Expand Down
11 changes: 11 additions & 0 deletions vcztools/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def stringify(gt_and_phase: tuple):

return generate

def _compose_sample_ids_generator(self) -> Callable:
def generate(root):
variant_count = root["variant_position"].shape[0]
sample_ids = root["sample_id"][:].tolist()
yield from itertools.repeat(sample_ids, variant_count)

return generate

def _compose_tag_generator(
self, tag: str, *, subfield=False, sample_loop=False
) -> Callable:
Expand All @@ -129,6 +137,9 @@ def _compose_tag_generator(
if tag == "GT":
return self._compose_gt_generator()

if tag == "SAMPLE":
return self._compose_sample_ids_generator()

def generate(root):
vcz_names = set(name for name, _zarray in root.items())
vcz_name = vcf_name_to_vcz_name(vcz_names, tag)
Expand Down

0 comments on commit 598c441

Please sign in to comment.