Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for index -n/--nrecords #59

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/test_bcftools_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def test_vcf_output(tmp_path, args, vcf_file):
@pytest.mark.parametrize(
("args", "vcf_name"),
[
("index -n", "sample.vcf.gz"),
("index --nrecords", "1kg_2020_chrM.vcf.gz"),
("query -l", "sample.vcf.gz"),
("query --list-samples", "1kg_2020_chrM.vcf.gz"),
],
Expand Down
15 changes: 15 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pathlib
from io import StringIO

from vcztools.stats import nrecords

from .utils import vcz_path_cache


def test_nrecords():
original = pathlib.Path("tests/data/vcf") / "sample.vcf.gz"
vcz = vcz_path_cache(original)

output_str = StringIO()
nrecords(vcz, output_str)
assert output_str.getvalue() == "9\n"
15 changes: 12 additions & 3 deletions vcztools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click

from . import query as query_module
from . import regions, vcf_writer
from . import regions, stats, vcf_writer


class NaturalOrderGroup(click.Group):
Expand All @@ -17,8 +17,17 @@ def list_commands(self, ctx):

@click.command
@click.argument("path", type=click.Path())
def index(path):
regions.create_index(path)
@click.option(
"-n",
"--nrecords",
is_flag=True,
help="Print the number of records (variants).",
)
def index(path, nrecords):
if nrecords:
stats.nrecords(path, sys.stdout)
else:
regions.create_index(path)


@click.command
Expand Down
11 changes: 11 additions & 0 deletions vcztools/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import zarr

from vcztools.utils import open_file_like


def nrecords(vcz, output):
root = zarr.open(vcz, mode="r")

with open_file_like(output) as output:
num_variants = root["variant_position"].shape[0]
print(num_variants, file=output)