Skip to content

C port followup: split_disjoint_nodes mutation relabelling and constrain_ages with fixed nodes #540

Backlog

Description

@nspope

These behaviours of the tree-sequence utilities (lib/util.c, tsdate/util.py) were kept as-is when the numerical code moved from numba to C (#534), and are worth a look. The reproducers use this helper:

import numpy as np, tskit, tsdate

def make_ts(nodes, edges, mutations, length=10):
    tables = tskit.TableCollection(length)
    for flags, time in nodes:
        tables.nodes.add_row(flags=flags, time=time)
    for left, right, parent, child in edges:
        tables.edges.add_row(left, right, parent, child)
    for position, node in mutations:
        site = tables.sites.add_row(position, "0")
        tables.mutations.add_row(site, node, "1")
    tables.sort()
    return tables.tree_sequence()

split_disjoint_nodes fails for mutations with no edges to their right

tsd_relabel_mutations_node (called by tsdate.util.split_disjoint_nodes, which tsdate.preprocess_ts runs by default) sweeps from 0 to the right end of the last edge, not to the sequence length. Mutations beyond that point, or all mutations when there are no edges, are never visited and keep the node -1, so building the output tree sequence fails with a tskit error:

ts = make_ts([(1, 0), (1, 0)], [], [(1, 0)])                       # no edges
tsdate.util.split_disjoint_nodes(ts)
# tskit.LibraryError: Node out of bounds. (TSK_ERR_NODE_OUT_OF_BOUNDS)

ts = make_ts([(1, 0), (1, 0), (0, 1)], [(0, 5, 2, 0), (0, 5, 2, 1)], [(7, 0)])
tsdate.util.split_disjoint_nodes(ts)                               # same error

split_disjoint_nodes keeps nodes that have left the tree

tsd_relabel_mutations_node maps each original node to its current (possibly split) copy as edges are inserted, but never clears the map when edges are removed. A mutation on a node that has left the tree is therefore relabelled to whichever copy was seen last, while a mutation on a node that has not yet appeared raises an error:

# node 3 is in the tree on [0, 5) only; a mutation on it at position 7
ts = make_ts([(1, 0), (1, 0), (0, 1), (0, 2)],
             [(0, 10, 2, 0), (0, 10, 2, 1), (0, 5, 3, 2)], [(7, 3)])
tsdate.util.split_disjoint_nodes(ts).mutations_node      # [3], no error

# node 3 never in the tree before the mutation
ts = make_ts([(1, 0), (1, 0), (0, 1), (0, 2)],
             [(0, 10, 2, 0), (0, 10, 2, 1)], [(7, 3)])
tsdate.util.split_disjoint_nodes(ts)
# _tsdate.LibraryError: A mutation is above a node that is not in any tree before it

For a node that is split into several copies, a mutation in a gap between its pieces is assigned to the copy on its left.

constrain_ages with inconsistent fixed nodes

tsdate.util.constrain_ages (tsd_constrain_ages) treats sample nodes as fixed. If two fixed nodes on an edge violate the constraint (e.g. inconsistent ancient sample ages), the iterative step fails with TSD_ERR_FIXED_NODES_VIOLATE_CONSTRAINT; with max_iterations=0 (the default) the final forcing step instead silently changes the age of a fixed node:

ts = make_ts([(1, 0), (1, 1), (0, 2)], [(0, 10, 1, 0), (0, 10, 2, 1)], [])
times = np.array([3.0, 1.0, 2.0])   # sample 0 older than its sample parent 1
tsdate.util.constrain_ages(ts, times, max_iterations=5)
# LibraryError: An edge between two fixed nodes violates the age constraint
tsdate.util.constrain_ages(ts, times, max_iterations=0)
# [3.0, 3.000001, 3.000002]   <- sample node 1 moved from 1.0

The forcing step is commented in the code as able to change the ages of fixed nodes.

Activity

  1. self-assigned this
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions