Skip to content

Commit b07b3e2

Browse files
hyanwongmergify[bot]
authored andcommitted
Add site.alleles method
Deliberately returns an (unordered) set
1 parent 072b214 commit b07b3e2

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

python/CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- SVG drawing routines now return a special string object that is automatically
88
rendered in a Jupyter notebook (:user:`hyanwong`, :pr:`2377`)
99

10+
**Features**
11+
12+
- New ``Site.alleles()`` method (:user:`hyanwong`, :issue:`2380`, :pr:`2385`)
13+
1014

1115
--------------------
1216
[0.5.0] - 2022-06-22

python/tests/test_highlevel.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,29 @@ def test_individual_properties(self, n):
25922592
self.verify_individual_properties(ts)
25932593

25942594

2595+
class TestSiteAlleles:
2596+
def test_no_mutations(self):
2597+
tables = tskit.TableCollection(sequence_length=1)
2598+
tables.sites.add_row(0, ancestral_state="")
2599+
site = tables.tree_sequence().site(0)
2600+
assert site.alleles == {""}
2601+
2602+
@pytest.mark.parametrize("k", range(5))
2603+
def test_k_mutations(self, k):
2604+
tables = tskit.TableCollection(sequence_length=1)
2605+
tables.sites.add_row(0, ancestral_state="ABC")
2606+
tables.nodes.add_row(1, 0)
2607+
tables.nodes.add_row(1, 0) # will not have any mutations => missing
2608+
for j in range(k):
2609+
tables.mutations.add_row(site=0, node=0, derived_state=str(j))
2610+
ts = tables.tree_sequence()
2611+
variant = next(ts.variants())
2612+
assert variant.has_missing_data
2613+
assert len(variant.site.alleles) == k + 1
2614+
assert "ABC" in variant.site.alleles
2615+
assert variant.site.alleles == set(variant.alleles[:-1])
2616+
2617+
25952618
class TestEdgeDiffs:
25962619
@pytest.mark.parametrize("ts", get_example_tree_sequences())
25972620
def test_correct_trees_forward(self, ts):

python/tskit/trees.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,22 @@ def __eq__(self, other):
357357
and self.metadata == other.metadata
358358
)
359359

360+
@property
361+
def alleles(self) -> set[str]:
362+
"""
363+
Return the set of all the alleles defined at this site
364+
365+
.. note::
366+
This deliberately returns an (unordered) *set* of the possible allelic
367+
states (as defined by the site's ancestral allele and its associated
368+
mutations). If you wish to obtain an (ordered) *list* of alleles, for
369+
example to translate the numeric genotypes at a site into allelic states,
370+
you should instead use ``.alleles`` attribute of the :class:`Variant` class,
371+
which unlike this attribute includes ``None`` as a state when there is
372+
missing data at a site.
373+
"""
374+
return {self.ancestral_state} | {m.derived_state for m in self.mutations}
375+
360376

361377
@metadata_module.lazy_decode()
362378
@dataclass

0 commit comments

Comments
 (0)