Skip to content

Commit

Permalink
Add samples file option to view CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Will-Tyler authored and tomwhite committed Sep 5, 2024
1 parent 33018eb commit 6f97ac8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/data/txt/samples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NA00001
NA00003
5 changes: 5 additions & 0 deletions tests/test_bcftools_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def run_vcztools(args: str) -> str:
"view --no-version -G",
"sample.vcf.gz"
),
(
"view --no-update --no-version --samples-file "
"tests/data/txt/samples.txt",
"sample.vcf.gz"),
("view -I --no-version -S tests/data/txt/samples.txt", "sample.vcf.gz"),
]
)
# fmt: on
Expand Down
22 changes: 22 additions & 0 deletions vcztools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ def query(path, list_samples, format):
default=None,
help="Regions to include.",
)
@click.option(
"-I",
"--no-update",
is_flag=True,
help="Do not recalculate INFO fields for the sample subset.",
)
@click.option(
"-s",
"--samples",
type=str,
default=None,
help="Samples to include.",
)
@click.option(
"-S",
"--samples-file",
type=str,
default=None,
help="File of sample names to include.",
)
@click.option(
"-G",
"--drop-genotypes",
Expand Down Expand Up @@ -120,7 +133,9 @@ def view(
no_version,
regions,
targets,
no_update,
samples,
samples_file,
drop_genotypes,
include,
exclude,
Expand All @@ -129,6 +144,13 @@ def view(
split = output.split(".")
raise ValueError(f"Output file extension must be .vcf, got: .{split[-1]}")

if samples_file:
with open(samples_file) as file:
samples = samples or ""
samples += ",".join(line.strip() for line in file.readlines())

# TODO: use no_update when fixing https://github.com/sgkit-dev/vcztools/issues/75

vcf_writer.write_vcf(
path,
output,
Expand Down

0 comments on commit 6f97ac8

Please sign in to comment.