Skip to content

Commit

Permalink
Add output to query, and clarify semantics of -l
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromekelleher committed Jan 14, 2025
1 parent 1354105 commit f5ac455
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
23 changes: 23 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,26 @@ def test_path_required(self):
def test_list(self, vcz_path):
result = run_vcztools(f"query -l {vcz_path}")
assert list(result.splitlines()) == ["NA00001", "NA00002", "NA00003"]

def test_list_ignores_output(self, vcz_path, tmp_path):
output = tmp_path / "tmp.txt"
result = run_vcztools(f"query -l {vcz_path} -o {output}")
assert list(result.splitlines()) == ["NA00001", "NA00002", "NA00003"]
assert not output.exists()

def test_output(self, vcz_path, tmp_path):
output = tmp_path / "tmp.txt"
result = run_vcztools(f"query -f '%POS\n' {vcz_path} -o {output}")
assert list(result.splitlines()) == []
assert output.exists()


def test_top_level():
runner = ct.CliRunner(mix_stderr=False)
result = runner.invoke(
cli.vcztools_main,
catch_exceptions=False,
)
assert result.exit_code == 0
assert len(result.stdout) > 0
assert len(result.stderr) == 0
32 changes: 19 additions & 13 deletions vcztools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def handle_broken_pipe(output):
exclude = click.option(
"-e", "--exclude", type=str, help="Filter expression to exclude variant sites."
)
output = click.option(
"-o",
"--output",
type=click.File("w"),
default="-",
help="File path to write output to (defaults to stdout '-').",
)


class NaturalOrderGroup(click.Group):
Expand Down Expand Up @@ -68,6 +75,7 @@ def index(path, nrecords, stats):

@click.command
@click.argument("path", type=click.Path())
@output
@click.option(
"-l",
"--list-samples",
Expand All @@ -83,27 +91,25 @@ def index(path, nrecords, stats):
)
@include
@exclude
def query(path, list_samples, format, include, exclude):
def query(path, output, list_samples, format, include, exclude):
if list_samples:
query_module.list_samples(path)
# bcftools query -l ignores the --output option and always writes to stdout
output = sys.stdout
with handle_broken_pipe(output):
query_module.list_samples(path, output)
return

if format is None:
raise click.UsageError("Missing option -f / --format")

query_module.write_query(
path, query_format=format, include=include, exclude=exclude
)
with handle_broken_pipe(output):
query_module.write_query(
path, output, query_format=format, include=include, exclude=exclude
)


@click.command
@click.argument("path", type=click.Path())
@click.option(
"-o",
"--output",
type=click.File("w"),
default="-",
help="File path to write output to (defaults to stdout '-').",
)
@output
@click.option(
"-h",
"--header-only",
Expand Down
16 changes: 7 additions & 9 deletions vcztools/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

from vcztools import constants
from vcztools.filter import FilterExpressionEvaluator, FilterExpressionParser
from vcztools.utils import open_file_like, vcf_name_to_vcz_name
from vcztools.utils import vcf_name_to_vcz_name


def list_samples(vcz_path, output=None):
def list_samples(vcz_path, output):
root = zarr.open(vcz_path, mode="r")

with open_file_like(output) as output:
sample_ids = root["sample_id"][:]
print("\n".join(sample_ids), file=output)
sample_ids = root["sample_id"][:]
print("\n".join(sample_ids), file=output)


class QueryFormatParser:
Expand Down Expand Up @@ -328,7 +327,7 @@ def generate(root) -> str:

def write_query(
vcz,
output=None,
output,
*,
query_format: str,
include: Optional[str] = None,
Expand All @@ -342,6 +341,5 @@ def write_query(
root = zarr.open(vcz, mode="r")
generator = QueryFormatGenerator(query_format, include=include, exclude=exclude)

with open_file_like(output) as output:
for result in generator(root):
print(result, sep="", end="", file=output)
for result in generator(root):
print(result, sep="", end="", file=output)

0 comments on commit f5ac455

Please sign in to comment.