diff --git a/docs/topological-analysis.md b/docs/topological-analysis.md index 2f20454f6a..d9f756d691 100644 --- a/docs/topological-analysis.md +++ b/docs/topological-analysis.md @@ -122,6 +122,30 @@ on high performance methods including the `numba` approach. ::: +(sec_simplification_roots)= + +## Roots after simplification + +With the default options, {meth}`TreeSequence.simplify` removes ancestry above +the most recent common ancestors (MRCAs) of the selected samples. With +`keep_unary=True`, it retains nodes that have one child in the ancestry of +the selected samples. This can include nodes above their MRCAs, even with +`keep_input_roots=False`. + +In this three-sample tree, samples 1 and 2 share an MRCA at time 1. Simplifying +to those two samples makes their MRCA the root with the default options. With +`keep_unary=True`, the original root at time 2 remains and has one child. + +```{code-cell} ipython3 +comb_ts = tskit.Tree.generate_comb(3).tree_sequence +for keep_unary in (False, True): + simplified_tree = comb_ts.simplify( + [1, 2], keep_unary=keep_unary, keep_input_roots=False + ).first() + print(f"{keep_unary=}, root time = {simplified_tree.time(simplified_tree.root)}") +``` + + (sec_combinatorics)= ## Identifying and counting topologies diff --git a/python/tskit/tables.py b/python/tskit/tables.py index fadae7435c..c9093c19a1 100644 --- a/python/tskit/tables.py +++ b/python/tskit/tables.py @@ -3651,11 +3651,11 @@ def simplify( associated with an individual in the individuals table. Cannot be specified at the same time as ``keep_unary``. (Default: ``None``, equivalent to False) - :param bool keep_input_roots: Whether to retain history ancestral to the - MRCA of the samples. If ``False``, no topology older than the MRCAs of the - samples will be included. If ``True`` the roots of all trees in the returned - tree sequence will be the same roots as in the original tree sequence. - (Default: False) + :param bool keep_input_roots: If ``True``, retain the roots of the input trees + that are ancestral to the selected samples. If ``False``, ancestry above the + most recent common ancestors of the samples can still be retained by + ``keep_unary`` or ``keep_unary_in_individuals``. + See :ref:`sec_simplification_roots` for an example. (Default: False) :param bool record_provenance: If True, record details of this call to simplify in the returned tree sequence's provenance information (Default: True). diff --git a/python/tskit/trees.py b/python/tskit/trees.py index 168cead7e0..8b29e7ee8a 100644 --- a/python/tskit/trees.py +++ b/python/tskit/trees.py @@ -7082,11 +7082,11 @@ def simplify( associated with an individual in the individuals table. Cannot be specified at the same time as ``keep_unary``. (Default: ``None``, equivalent to False) - :param bool keep_input_roots: Whether to retain history ancestral to the - MRCA of the samples. If ``False``, no topology older than the MRCAs of the - samples will be included. If ``True`` the roots of all trees in the returned - tree sequence will be the same roots as in the original tree sequence. - (Default: False) + :param bool keep_input_roots: If ``True``, retain the roots of the input trees + that are ancestral to the selected samples. If ``False``, ancestry above the + most recent common ancestors of the samples can still be retained by + ``keep_unary`` or ``keep_unary_in_individuals``. + See :ref:`sec_simplification_roots` for an example. (Default: False) :param bool record_provenance: If True, record details of this call to simplify in the returned tree sequence's provenance information (Default: True).