Skip to content

Commit

Permalink
Add support for index -n/--nrecords
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Aug 19, 2024
1 parent 3e1c238 commit 846b759
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
16 changes: 16 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 @@ -2,7 +2,7 @@

import click

from . import regions, vcf_writer
from . import regions, stats, vcf_writer


class NaturalOrderGroup(click.Group):
Expand All @@ -16,8 +16,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
15 changes: 15 additions & 0 deletions vcztools/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from contextlib import ExitStack
from pathlib import Path

import zarr


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

with ExitStack() as stack:
if isinstance(output, str) or isinstance(output, Path):
output = stack.enter_context(open(output, mode="w"))

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

0 comments on commit 846b759

Please sign in to comment.