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

Exclude samples #80

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion tests/test_bcftools_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def run_vcztools(args: str) -> str:
("view --no-version -s NA00001", "sample.vcf.gz"),
("view --no-version -s NA00001,NA00003", "sample.vcf.gz"),
("view --no-version -s HG00096", "1kg_2020_chrM.vcf.gz"),
("view --no-version -s '' --force-samples", "sample.vcf.gz")
("view --no-version -s '' --force-samples", "sample.vcf.gz"),
("view --no-version -s ^NA00001", "sample.vcf.gz"),
("view --no-version -s ^NA00003,NA00002", "sample.vcf.gz"),
("view --no-version -s ^NA00003,NA00002,NA00003", "sample.vcf.gz"),
("view --no-version -S ^tests/data/txt/samples.txt", "sample.vcf.gz"),
]
)
# fmt: on
Expand Down
10 changes: 9 additions & 1 deletion tests/test_vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def test_write_vcf__regions(tmp_path, regions, targets,
("NA00001", [[0, 0, True]]),
("NA00001,NA00003", [[0, 0, True], [0, 1, False]]),
("NA00003,NA00001", [[0, 1, False], [0, 0, True]]),
("^NA00002", [[0, 0, True], [0, 1, False]]),
("^NA00003,NA00002", [[0, 0, True]]),
("^NA00003,NA00002,NA00003", [[0, 0, True]]),
]
)
def test_write_vcf__samples(tmp_path, samples, expected_genotypes):
Expand All @@ -161,7 +164,12 @@ def test_write_vcf__samples(tmp_path, samples, expected_genotypes):

v = VCF(output)

assert v.samples == samples.split(",")
if samples.startswith("^"):
# There are three samples in the sample VCF.
assert len(set(v.samples)) + len(set(samples[1:].split(","))) == 3
assert len(set(v.samples) & set(samples[1:].split(","))) == 0
else:
assert v.samples == samples.split(",")

variant = next(v)

Expand Down
5 changes: 5 additions & 0 deletions vcztools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ def view(
raise ValueError(f"Output file extension must be .vcf, got: .{split[-1]}")

if samples_file:
exclude_samples_file = samples_file.startswith("^")
samples_file = samples_file.lstrip("^")
with open(samples_file) as file:
samples = samples or ""
assert samples == "" or samples_file.startswith("^") == exclude_samples_file
if exclude_samples_file and not samples.startswith("^"):
samples = "^" + samples
samples += ",".join(line.strip() for line in file.readlines())

# TODO: use no_update when fixing https://github.com/sgkit-dev/vcztools/issues/75
Expand Down
8 changes: 8 additions & 0 deletions vcztools/vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,18 @@ def write_vcf(
samples_selection = None
else:
all_samples = root["sample_id"][:]
exclude_samples = samples.startswith("^")
samples = samples.lstrip("^")
sample_ids = np.array(samples.split(","))
if np.all(sample_ids == np.array("")):
sample_ids = np.empty((0,))

samples_selection = search(all_samples, sample_ids)
if exclude_samples:
samples_selection = np.setdiff1d(
np.arange(all_samples.size), samples_selection
)
sample_ids = all_samples[samples_selection]

if not no_header and vcf_header is None:
if "vcf_header" in root.attrs:
Expand Down