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

Drop Genotypes #61

Merged
merged 6 commits into from
Aug 29, 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
8 changes: 5 additions & 3 deletions lib/vcf_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,11 @@ vcz_variant_encoder_write_format_fields(const vcz_variant_encoder_t *self,

if (all_missing) {
for (j = 0; j < num_samples + 1; j++) {
offset = append_string(buf, ".\t", 2, offset, buflen);
if (offset < 0) {
goto out;
if (num_samples > 0) {
offset = append_string(buf, ".\t", 2, offset, buflen);
if (offset < 0) {
goto out;
}
}
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_bcftools_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def run_vcztools(args: str) -> str:
"view --no-version -e '(FMT/DP >= 8 | FMT/GQ>40) && POS > 100000'",
"sample.vcf.gz"
),
(
"view --no-version -G",
"sample.vcf.gz"
),
]
)
# fmt: on
Expand Down
12 changes: 12 additions & 0 deletions tests/test_vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ def test_write_vcf__samples(tmp_path, samples, expected_genotypes):
assert variant.genotypes == expected_genotypes


def test_write_vcf__no_samples(tmp_path):
original = pathlib.Path("tests/data/vcf") / "sample.vcf.gz"
vcz = vcz_path_cache(original)
output = tmp_path.joinpath("output.vcf")

write_vcf(vcz, output, drop_genotypes=True)

v = VCF(output)

assert v.samples == []


@pytest.mark.parametrize(
("regions", "targets", "samples", "include", "expected_chrom_pos"),
[
Expand Down
9 changes: 9 additions & 0 deletions vcztools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def query(path, list_samples, format):
default=None,
help="Samples to include.",
)
@click.option(
"-G",
"--drop-genotypes",
type=bool,
is_flag=True,
help="Drop genotypes.",
)
@click.option(
"-t",
"--targets",
Expand All @@ -110,6 +117,7 @@ def view(
regions,
targets,
samples,
drop_genotypes,
include,
exclude,
):
Expand All @@ -121,6 +129,7 @@ def view(
variant_regions=regions,
variant_targets=targets,
samples=samples,
drop_genotypes=drop_genotypes,
include=include,
exclude=exclude,
)
Expand Down
21 changes: 16 additions & 5 deletions vcztools/vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def write_vcf(
variant_regions=None,
variant_targets=None,
samples=None,
drop_genotypes: bool = False,
include: Optional[str] = None,
exclude: Optional[str] = None,
) -> None:
Expand Down Expand Up @@ -138,7 +139,12 @@ def write_vcf(
root = zarr.open(vcz, mode="r")

with open_file_like(output) as output:
if samples is None:
if samples and drop_genotypes:
raise ValueError("Cannot select samples and drop genotypes.")
elif drop_genotypes:
sample_ids = []
samples_selection = np.array([])
elif samples is None:
sample_ids = root["sample_id"][:]
samples_selection = None
else:
Expand Down Expand Up @@ -300,7 +306,11 @@ def c_chunk_to_vcf(
info_fields = {}
num_samples = len(samples_selection) if samples_selection is not None else None
for name, array in root.items():
if name.startswith("call_") and not name.startswith("call_genotype"):
if (
name.startswith("call_")
and not name.startswith("call_genotype")
and num_samples != 0
):
vcf_name = name[len("call_") :]
format_fields[vcf_name] = get_vchunk_array(
array, v_chunk, v_mask_chunk, samples_selection
Expand All @@ -313,7 +323,7 @@ def c_chunk_to_vcf(

gt = None
gt_phased = None
if "call_genotype" in root:
if "call_genotype" in root and num_samples != 0:
array = root["call_genotype"]
gt = get_vchunk_array(array, v_chunk, v_mask_chunk, samples_selection)
if "call_genotype_phased" in root:
Expand Down Expand Up @@ -380,7 +390,7 @@ def _generate_header(ds, original_header, sample_ids):
info_fields = []
format_fields = []

if "call_genotype" in ds:
if "call_genotype" in ds and len(sample_ids) > 0:
# GT must be the first field if present, per the spec (section 1.6.2)
format_fields.append("GT")

Expand All @@ -395,7 +405,8 @@ def _generate_header(ds, original_header, sample_ids):
key = var[len("variant_") :]
info_fields.append(key)
elif (
var.startswith("call_")
len(sample_ids) > 0
and var.startswith("call_")
and not var.endswith("_fill")
and not var.endswith("_mask")
and dims(arr)[0] == "variants"
Expand Down