pycea.tl.sort

Contents

pycea.tl.sort#

pycea.tl.sort(tdata, key, reverse=False, tree=None)#

Reorders branches based on a node attribute.

For every internal node with multiple children, reorders outgoing edges (child branches) based on a given node attribute. The order is applied in-place preserving all node and edge metadata.

Sorting allows for consistent or meaningful ordering of descendants in tree visualizations, e.g., ordering by inferred ancestral state values or other numeric or categorical metrics.

Parameters:
  • tdata (TreeData) – TreeData object.

  • key (str) – Attribute of tdata.obst[tree].nodes to sort by.

  • reverse (bool (default: False)) – If True, sort in descending order.

  • tree (str | Sequence[str] | None (default: None)) – The obst key or keys of the trees to use. If None, all trees are used.

Return type:

None

Returns:

Returns None and does not set any fields.

Examples

Sort branches by number of descendant leaves:

>>> tdata = py.datasets.yang22()
>>> tdata.obs["n"] = 1
>>> py.tl.ancestral_states(tdata, keys="n", method="sum")
>>> py.tl.sort(tdata, key="n")