diff --git a/python/CHANGELOG.rst b/python/CHANGELOG.rst index 9243963df1..7e644377bd 100644 --- a/python/CHANGELOG.rst +++ b/python/CHANGELOG.rst @@ -12,6 +12,8 @@ In development **Bugfixes** +- Validate zero positions in VCF output using the converted Boolean + site mask. (:user:`rpinho`, :pr:`3512`) - Apply ``node_labels`` to sequence alignments when exporting NEXUS. (:user:`rpinho`, :pr:`3514`) diff --git a/python/tests/test_vcf.py b/python/tests/test_vcf.py index 4b25e840e3..edad7a346e 100644 --- a/python/tests/test_vcf.py +++ b/python/tests/test_vcf.py @@ -653,6 +653,23 @@ def test_site_0_masked(self): ) assert drop_header(actual) == expected + @pytest.mark.parametrize( + "mask", + [ + [True, False, False, False], + np.array([1, 0, 0, 0]), + np.array([True, False, False, False]), + ], + ) + def test_array_like_mask_skips_position_zero(self, mask): + actual = self.ts().as_vcf(site_mask=mask) + records = [line.split("\t") for line in actual.splitlines() if line[0] != "#"] + assert [record[1] for record in records] == ["2", "4", "6"] + + def test_integer_mask_rejects_unmasked_position_zero(self): + with pytest.raises(ValueError, match="A variant position of 0"): + self.ts().as_vcf(site_mask=np.array([0, 1, 0, 0])) + def test_site_0_masked_triploid(self): s = """\ #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\ttsk_0 diff --git a/python/tskit/vcf.py b/python/tskit/vcf.py index 196216da8b..c050c1a26a 100644 --- a/python/tskit/vcf.py +++ b/python/tskit/vcf.py @@ -105,7 +105,7 @@ def __init__( # The VCF spec does not allow for positions to be 0, so we error if one of the # transformed positions is 0 and allow_position_zero is False. if not allow_position_zero and np.any( - vcf_model.transformed_positions[~site_mask] == 0 + vcf_model.transformed_positions[~self.site_mask] == 0 ): raise ValueError( "A variant position of 0 was found in the VCF output, this is not "