Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ In development

- Require Python >= 3.12 and vcztools >= 0.2

- The `[ancestral_state]` annotation is joined to the sources strictly: it
accepts `include`, `exclude`, `regions` and `targets` filters; more than one
annotation variant at a site is an error, as is any difference in REF allele
at a site between the sources, or between the sources and the annotation
(sources may have different ALT alleles); and sites with a missing ancestral state
(`.`, `N` or empty) are excluded from both inference and `augment_sites`.

## [0.5.1] - 2026-03-05

- Require tskit >= 1.0
Expand Down
16 changes: 16 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ Specifies where to read the ancestral allele for each variant position.
| `path` | string | (required) | Path to VCZ containing ancestral alleles |
| `field` | string | — | Array name in the store (e.g. `"variant_AA"`). Required unless `is_reference` is set. |
| `is_reference` | bool | `false` | Use the REF allele (`variant_allele[:, 0]`) as the ancestral state. Useful for simulations. `field` must not be set when this is `true`. |
| `contig` | string | — | Name of the contig in this store's header (see the top-level `contig` option) |
| `include` | string | — | bcftools-style filter expression selecting annotation variants (e.g. `'TYPE="snp"'`) |
| `exclude` | string | — | bcftools-style filter expression excluding annotation variants |
| `regions` | string | — | Genomic region, half-open (e.g. `"chr20:1000-50000"`) |
| `targets` | string | — | Exact target positions |

The annotation is joined to the sources by position. After filtering, there
must be at most one annotation variant at each site, and its REF allele must
equal the site's REF allele in the sources; otherwise an error is raised. All
sources with a variant at a site must also have the same REF allele there,
although their ALT alleles may differ. If
the annotation contains, for example, both a SNP and an indel at the same
position, use `include = 'TYPE="snp"'` to keep only the SNP. An ancestral
state of `.`, `N` or the empty string means the ancestral allele is unknown,
as does one that is not among the site's alleles; such sites are excluded from
inference and from `augment_sites`.


## `[[ancestors]]`
Expand Down
17 changes: 13 additions & 4 deletions tests/test_ancestors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,28 +1409,37 @@ def test_two_sources_combined_triallelic_raises(self):
with pytest.raises(ValueError, match="position 100 "):
ancestors.infer_ancestors(sources, _cfg(), _anc_state(store_a))

def test_two_sources_same_alleles(self):
def _two_sources(self, alleles_a, alleles_b):
store_a = _haploid_store(
[[0, 1, 0, 1]],
positions=[100],
alleles=[["A", "T"]],
alleles=[alleles_a],
anc_states=["A"],
sample_ids=np.array(["a0", "a1", "a2", "a3"]),
)
store_b = _haploid_store(
[[0, 1, 1, 0]],
positions=[100],
alleles=[["T", "A"]],
alleles=[alleles_b],
anc_states=["A"],
sample_ids=np.array(["b0", "b1", "b2", "b3"]),
)
sources = [
config.Source(path=store_a, name="a"),
config.Source(path=store_b, name="b"),
]
anc = ancestors.infer_ancestors(sources, _cfg(), _anc_state(store_a))
return sources, store_a

def test_two_sources_same_alleles(self):
sources, ann = self._two_sources(["A", "T"], ["A", "T"])
anc = ancestors.infer_ancestors(sources, _cfg(), _anc_state(ann))
np.testing.assert_array_equal(anc["variant_position"][:], [100])

def test_two_sources_swapped_ref_raises(self):
sources, ann = self._two_sources(["A", "T"], ["T", "A"])
with pytest.raises(ValueError, match="source 'b' has REF 'T'"):
ancestors.infer_ancestors(sources, _cfg(), _anc_state(ann))

def test_unknown_ancestral_allele_skipped(self, caplog):
gt = _haploid_store(
[[0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 2, 0]],
Expand Down
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ def test_ancestral_state(self, tmp_path):
assert cfg.ancestral_state is not None
assert cfg.ancestral_state.field == "variant_ancestral_allele"

def test_ancestral_state_filters(self, tmp_path):
toml = _STANDARD_TOML.replace(
'field = "variant_ancestral_allele"\n',
'field = "variant_ancestral_allele"\n'
"include = 'TYPE=\"snp\"'\n"
'exclude = "POS == 5"\n'
'regions = "20:1-1000"\n'
'targets = "^20:7"\n',
)
cfg = config.Config.from_toml(_write_toml(tmp_path, toml))
assert cfg.ancestral_state.include == 'TYPE="snp"'
assert cfg.ancestral_state.exclude == "POS == 5"
assert cfg.ancestral_state.regions == "20:1-1000"
assert cfg.ancestral_state.targets == "^20:7"

def test_ancestors(self, tmp_path):
cfg = config.Config.from_toml(_write_toml(tmp_path, _STANDARD_TOML))
assert len(cfg.ancestors) == 1
Expand Down Expand Up @@ -805,6 +820,16 @@ def test_includes_ancestral_state(self):
assert "ann.vcz" in text
assert "anc_allele" in text

def test_ancestral_state_filters(self):
cfg = _minimal_config(
ancestral_state=config.AncestralState(
path="ann.vcz", field="anc_allele", include='TYPE="snp"'
),
)
text = cfg.format()
assert 'include = TYPE="snp"' in text
assert "exclude" not in text.split("[ancestral_state]")[1].split("[")[0]

def test_includes_post_process(self):
cfg = _minimal_config(post_process=config.PostProcessConfig())
text = cfg.format()
Expand Down
31 changes: 31 additions & 0 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,37 @@ def test_adds_new_sites(self):
aug_positions = set(aug_ts.sites_position)
assert 200.0 in aug_positions

@pytest.mark.parametrize("ancestral", [".", "N", "", "C"])
def test_skips_unknown_ancestral(self, ancestral, caplog):
"""Sites with a missing ancestral state, or one not among the site's
alleles, are not added."""
main_store, sing_store, _ = self._make_hand_constructed_stores()
ts, _ = _run_pipeline_no_augment(main_store)
ann_store = helpers.make_sample_vcz(
genotypes=np.zeros((3, 1, 1), dtype=np.int8),
positions=np.array([100, 200, 300], dtype=np.int32),
alleles=np.array([["A", "T"], ["A", "G"], ["C", "G"]]),
ancestral_state=np.array(["A", ancestral, "C"]),
sequence_length=1000,
)
cfg = config.Config(
sources={
"test": config.Source(path=main_store, name="test"),
"singletons": config.Source(path=sing_store, name="singletons"),
},
ancestors=[],
match=config.MatchConfig(
sources={}, output="output.trees", reference_ts="ref.trees"
),
augment_sites=config.AugmentSitesConfig(sources=["singletons"]),
ancestral_state=_anc_state(ann_store),
)
with caplog.at_level(logging.INFO, logger="tsinfer"):
aug_ts = pipeline.augment_sites(ts, cfg)
assert aug_ts.num_sites == ts.num_sites
assert "skipping 1 sites with unknown ancestral allele" in caplog.text
assert "no new sites to add" in caplog.text

def test_skips_existing_sites(self):
"""Sites already in the TS are not duplicated."""
main_store, sing_store, ann_store = self._make_hand_constructed_stores()
Expand Down
Loading
Loading