diff --git a/CHANGELOG.md b/CHANGELOG.md index d62d5f8..d1482f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,11 @@ In development. -- Add `--no-mutation-updates` option to `postprocess` to skip pushing up - unary recombinant mutations and reversions. +- Fix `push_up_unary_recombinant_mutations` treating a recombinant node as + unary when it had one full-span child edge alongside other partial-span + child edges. Mutations were then pushed up onto the recombinant and + inherited by the other children, causing a `TSK_ERR_BAD_MUTATION_PARENT` + error in `postprocess`. ## [1.1.0] - 2026-10-08 diff --git a/sc2ts/cli.py b/sc2ts/cli.py index 731f4ce..2615e3a 100644 --- a/sc2ts/cli.py +++ b/sc2ts/cli.py @@ -536,11 +536,6 @@ def run_hmm( @click.argument("ts_in", type=click.Path(exists=True, dir_okay=False)) @click.argument("ts_out", type=click.Path(exists=False, dir_okay=False)) @click.option("--match-db", type=click.Path(exists=True, dir_okay=False)) -@click.option( - "--mutation-updates/--no-mutation-updates", - default=True, - help="Push up unary recombinant mutations and simple reversions", -) @click.option("--progress/--no-progress", default=True) @click.option("-v", "--verbose", count=True) @click.option("-l", "--log-file", default=None, type=click.Path(dir_okay=False)) @@ -548,7 +543,6 @@ def postprocess( ts_in, ts_out, match_db, - mutation_updates, progress, verbose, log_file, @@ -562,13 +556,12 @@ def postprocess( with si.MatchDb(match_db) as db: ts = si.append_exact_matches(ts, db, show_progress=progress) - if mutation_updates: - ts = si.push_up_unary_recombinant_mutations(ts) - # See if we can remove some of the reversions in a straightforward way. - mutations_is_reversion = si.find_reversions(ts) - ts = tree_ops.push_up_reversions( - ts, ts.mutations_node[mutations_is_reversion], date=None - ) + ts = si.push_up_unary_recombinant_mutations(ts) + # See if we can remove some of the reversions in a straightforward way. + mutations_is_reversion = si.find_reversions(ts) + ts = tree_ops.push_up_reversions( + ts, ts.mutations_node[mutations_is_reversion], date=None + ) ts.dump(ts_out) diff --git a/sc2ts/inference.py b/sc2ts/inference.py index 4569462..65bd90a 100644 --- a/sc2ts/inference.py +++ b/sc2ts/inference.py @@ -3142,13 +3142,17 @@ def push_up_unary_recombinant_mutations(ts): logger.info(f"Found {len(recomb_parent_edges)} edges with recombinant parent") for e in recomb_parent_edges: edge = ts.edge(e) - if edge.left == 0 and edge.right == ts.sequence_length: - by_parent[edge.parent].append(edge) - - # We're only interested in full-span edges with a single child. - child_to_parent = { - e[0].child: e[0].parent for e in by_parent.values() if len(e) == 1 - } + by_parent[edge.parent].append(edge) + + # We're only interested in recombinants with a single full-span child edge. + # Note: we must count all child edges here, not just full-span ones, as + # otherwise mutations would be inherited by other (partial) children. + child_to_parent = {} + for edges in by_parent.values(): + if len(edges) == 1: + edge = edges[0] + if edge.left == 0 and edge.right == ts.sequence_length: + child_to_parent[edge.child] = edge.parent logger.info(f"Of which {len(child_to_parent)} are unary") mutations_to_move = np.isin( ts.mutations_node, np.array(list(child_to_parent.keys()), dtype=np.int32) diff --git a/tests/test_cli.py b/tests/test_cli.py index 38b1b1b..0f36768 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -560,22 +560,6 @@ def test_example(self, tmp_path, fx_ts_map, fx_match_db): # dated otherwise assert out.edge(-1).parent == 0 - def test_no_mutation_updates(self, tmp_path, fx_ts_map, fx_match_db): - ts = fx_ts_map["2020-02-13"] - out_ts_path = tmp_path / "ts.ts" - runner = ct.CliRunner() - result = runner.invoke( - cli.cli, - f"postprocess {ts.path} {out_ts_path} --match-db={fx_match_db.path} " - "--no-mutation-updates", - catch_exceptions=False, - ) - assert result.exit_code == 0 - out = tskit.load(out_ts_path) - assert out.num_samples == ts.num_samples + 8 - assert out.num_provenances == ts.num_provenances + 1 - assert out.num_mutations == ts.num_mutations - class TestMinimiseMetadata: def test_example(self, tmp_path, fx_ts_map): diff --git a/tests/test_inference.py b/tests/test_inference.py index 9d433f6..99dfb0b 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -3406,6 +3406,42 @@ def test_recombinant_example_4(self, fx_recombinant_example_4): mut = tsp.site(site).mutations[0] assert mut.node == 56 + def recombinant_example(self, extra_partial_child): + # Root 0 with children 1 and 2. Recombinant node 3 inherits from + # 1 on [0, 5) and 2 on [5, 10). Sample 4 is a full-span child of 3 + # carrying a mutation. Optionally, sample 5 is also a child of 3 + # over [0, 5), so that 3 is not unary. + tables = tskit.TableCollection(10) + tables.nodes.add_row(time=4) + tables.nodes.add_row(time=3) + tables.nodes.add_row(time=3) + tables.nodes.add_row(flags=core.NODE_IS_RECOMBINANT, time=2) + tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0) + tables.edges.add_row(0, 10, 0, 1) + tables.edges.add_row(0, 10, 0, 2) + tables.edges.add_row(0, 5, 1, 3) + tables.edges.add_row(5, 10, 2, 3) + tables.edges.add_row(0, 10, 3, 4) + if extra_partial_child: + tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0) + tables.edges.add_row(0, 5, 3, 5) + tables.sites.add_row(2, "A") + tables.mutations.add_row(site=0, node=4, derived_state="T", time=0) + tables.sort() + return tables.tree_sequence() + + def test_unary_full_span_child(self): + ts = self.recombinant_example(extra_partial_child=False) + tsp = si.push_up_unary_recombinant_mutations(ts) + assert tsp.mutation(0).node == 3 + assert tsp.mutation(0).time == 2 + np.testing.assert_array_equal(ts.genotype_matrix(), tsp.genotype_matrix()) + + def test_full_span_child_with_partial_sibling(self): + ts = self.recombinant_example(extra_partial_child=True) + tsp = si.push_up_unary_recombinant_mutations(ts) + ts.tables.assert_equals(tsp.tables, ignore_provenance=True) + def test_provenance(self, fx_ts_map): ts = fx_ts_map["2020-02-13"] tsp = si.push_up_unary_recombinant_mutations(ts)