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
5 changes: 5 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
and a window breakpoint falls within an internal missing interval.
(:user:`nspope`, :pr:`3176`, :issue:`3175`)

**Breaking changes**

- ``ltrim``, ``rtrim``, ``trim`` and ``shift`` raise an error if used on a tree sequence
containing a reference sequence (:user:`hyanwong`, :pr:`3210`, :issue:`2091`)

--------------------
[0.6.4] - 2025-05-21
--------------------
Expand Down
20 changes: 20 additions & 0 deletions python/tests/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -7074,6 +7074,18 @@ def test_failure_with_migrations(self):
with pytest.raises(ValueError):
ts.trim()

def test_reference_sequence(self):
# Test that we fail if there is a reference sequence
tables = tskit.TableCollection(3.0)
tables.reference_sequence.data = "ABC"
ts = tables.tree_sequence()
with pytest.raises(ValueError, match="reference sequence"):
ts.ltrim()
with pytest.raises(ValueError, match="reference sequence"):
ts.rtrim()
with pytest.raises(ValueError, match="reference sequence"):
ts.trim()


class TestShift:
"""
Expand Down Expand Up @@ -7140,6 +7152,14 @@ def test_bad_seq_len(self):
):
ts.shift(1, sequence_length=1)

def test_reference_sequence(self):
# Test that we fail if there is a reference sequence
tables = tskit.TableCollection(3.0)
tables.reference_sequence.data = "ABC"
ts = tables.tree_sequence()
with pytest.raises(ValueError, match="reference sequence"):
ts.shift(1)


class TestConcatenate:
def test_simple(self):
Expand Down
10 changes: 10 additions & 0 deletions python/tskit/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3887,6 +3887,11 @@ def keep_intervals(self, intervals, simplify=True, record_provenance=True):
)

def _check_trim_conditions(self):
if self.has_reference_sequence():
raise ValueError(
"Cannot trim if there is a reference sequence. Please remove the "
"reference sequence by calling `.reference_sequence.clear()` first."
)
if self.migrations.num_rows > 0:
if (np.min(self.migrations.left) < np.min(self.edges.left)) and (
np.max(self.migrations.right) > np.max(self.edges.right)
Expand Down Expand Up @@ -4005,6 +4010,11 @@ def shift(self, value, *, sequence_length=None, record_provenance=True):
:param sequence_length: The new sequence length of the tree sequence. If
``None`` (default) add `value` to the sequence length.
"""
if self.has_reference_sequence():
raise ValueError(
"Cannot shift if there is a reference sequence. Please remove the "
"reference sequence by calling `.reference_sequence.clear()` first."
)
self.drop_index()
self.edges.left += value
self.edges.right += value
Expand Down
Loading