Skip to content

Commit c86884d

Browse files
nspopejeromekelleher
authored andcommitted
Fix bug in pair coal counts when window breakpoint is in missing interval
1 parent 4195624 commit c86884d

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

c/tskit/trees.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9586,8 +9586,9 @@ tsk_treeseq_pair_coalescence_stat(const tsk_treeseq_t *self, tsk_size_t num_samp
95869586
window_span = windows[w + 1] - windows[w] - missing_span;
95879587
missing_span = 0.0;
95889588
if (num_edges == 0) {
9589+
/* missing interval, so remove overcounted missing span */
95899590
remaining_span = right - windows[w + 1];
9590-
window_span -= remaining_span;
9591+
window_span += remaining_span;
95919592
missing_span += remaining_span;
95929593
}
95939594
for (i = 0; i < (tsk_id_t) num_set_indexes; i++) {

python/CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
- ``TreeSequence.map_to_vcf_model`` now also returns the transformed positions and
88
contig length. (:user:`benjeffery`, :pr:`XXXX`, :issue:`3173`)
99

10+
**Bugfixes**
11+
12+
- Fix bug in ``TreeSequence.pair_coalescence_counts`` when ``span_normalise=True``
13+
and a window breakpoint falls within an internal missing interval.
14+
(:user:`nspope`, :pr:`3176`, :issue:`3175`)
15+
1016
--------------------
1117
[0.6.4] - 2025-05-21
1218
--------------------

python/tests/test_coalrate.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,9 +1294,9 @@ def test_span_normalise(self):
12941294
proto = proto_pair_coalescence_counts(ts, windows=windows, span_normalise=False)
12951295
np.testing.assert_allclose(proto, check)
12961296

1297-
def test_span_normalise_with_missing(self):
1297+
def test_span_normalise_with_missing_flanks(self):
12981298
"""
1299-
test case where span is normalised and there are intervals without trees
1299+
test case where span is normalised and there are flanking intervals without trees
13001300
"""
13011301
ts = self.example_ts()
13021302
missing = np.array([[0.0, 0.1], [0.8, 1.0]]) * ts.sequence_length
@@ -1313,6 +1313,30 @@ def test_span_normalise_with_missing(self):
13131313
proto = proto_pair_coalescence_counts(ts, windows=windows, span_normalise=True)
13141314
np.testing.assert_allclose(proto, check)
13151315

1316+
def test_span_normalise_with_missing_interior(self):
1317+
"""
1318+
test that span normalisation correctly calculates internal missing data
1319+
"""
1320+
ts = msprime.sim_ancestry(samples=1, discrete_genome=False)
1321+
missing_interval = np.array([[0.3, 0.6]]) * ts.sequence_length
1322+
windows = np.array([0.0, 0.31, 1.0]) * ts.sequence_length
1323+
time_windows = np.array([0.0, np.inf])
1324+
ts = ts.delete_intervals(missing_interval)
1325+
check = np.ones(windows.size - 1)
1326+
implm = ts.pair_coalescence_counts(
1327+
windows=windows,
1328+
time_windows=time_windows,
1329+
span_normalise=True,
1330+
).flatten()
1331+
np.testing.assert_array_almost_equal(implm, check)
1332+
proto = proto_pair_coalescence_counts(
1333+
ts,
1334+
windows=windows,
1335+
time_windows=time_windows,
1336+
span_normalise=True,
1337+
).flatten()
1338+
np.testing.assert_array_almost_equal(proto, check)
1339+
13161340
def test_empty_windows(self):
13171341
"""
13181342
test that windows without nodes contain zeros

0 commit comments

Comments
 (0)