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

Add contig_lengths dataset attribute if found in the VCF file #946

Merged
merged 2 commits into from
Nov 3, 2022
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
4 changes: 4 additions & 0 deletions sgkit/io/vcf/vcf_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ def vcf_to_zarr_sequential(
ds.attrs["filters"] = filters
ds.attrs["vcf_zarr_version"] = "0.1"
ds.attrs["vcf_header"] = vcf.raw_header
try:
ds.attrs["contig_lengths"] = vcf.seqlens
except AttributeError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the cyvcf2 source I see that this raises if the lengths are absent in the header, so seem ok to then omit them in our output.

pass

for field_handler in field_handlers:
field_handler.update_dataset(ds)
Expand Down
3 changes: 3 additions & 0 deletions sgkit/tests/io/vcf/test_vcf_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_vcf_to_zarr__small_vcf(shared_datadir, is_path, tmp_path):
ds = xr.open_zarr(output)

assert ds.attrs["contigs"] == ["19", "20", "X"]
assert "contig_lengths" not in ds.attrs
assert_array_equal(ds["variant_contig"], [0, 0, 1, 1, 1, 1, 1, 1, 2])
assert_array_equal(
ds["variant_position"],
Expand Down Expand Up @@ -155,6 +156,8 @@ def test_vcf_to_zarr__large_vcf(shared_datadir, is_path, tmp_path):
vcf_to_zarr(path, output, chunk_length=5_000)
ds = xr.open_zarr(output)

assert ds.attrs["contigs"] == ["20", "21"]
assert ds.attrs["contig_lengths"] == [63025520, 48129895]
assert ds["sample_id"].shape == (1,)
assert ds["call_genotype"].shape == (19910, 1, 2)
assert ds["call_genotype_mask"].shape == (19910, 1, 2)
Expand Down
3 changes: 3 additions & 0 deletions sgkit/tests/io/vcf/test_vcf_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ def test_all_fields(
assert allel_ds_contigs <= sg_ds_contigs
del allel_ds.attrs["contigs"]
del sg_ds.attrs["contigs"]
# scikit-allel doesn't store contig lengths
if "contig_lengths" in sg_ds.attrs:
del sg_ds.attrs["contig_lengths"]

if allel_ds_contigs < sg_ds_contigs:
# variant_contig variables are not comparable, so remove them before comparison
Expand Down