skbio.tree.TreeNode.shuffle

TreeNode.shuffle(k=None, names=None, shuffle_f=<built-in method shuffle of numpy.random.mtrand.RandomState object>, n=1)[source]

Yield trees with shuffled tip names

State: Experimental as of 0.4.0.

Parameters
  • k (int, optional) – The number of tips to shuffle. If k is not None, k tips are randomly selected, and only those names will be shuffled.

  • names (list, optional) – The specific tip names to shuffle. k and names cannot be specified at the same time.

  • shuffle_f (func) – Shuffle method, this function must accept a list and modify inplace.

  • n (int, optional) – The number of iterations to perform. Value must be > 0 and np.inf can be specified for an infinite number of iterations.

Notes

Tip names are shuffled inplace. If neither k nor names are provided, all tips are shuffled.

Yields

TreeNode – Tree with shuffled tip names.

Raises

Examples

Alternate the names on two of the tips, ‘a’, and ‘b’, and do this 5 times.

>>> from skbio import TreeNode
>>> tree = TreeNode.read(["((a,b),(c,d));"])
>>> rev = lambda items: items.reverse()
>>> shuffler = tree.shuffle(names=['a', 'b'], shuffle_f=rev, n=5)
>>> for shuffled_tree in shuffler:
...     print(shuffled_tree)
((b,a),(c,d));

((a,b),(c,d));

((b,a),(c,d));

((a,b),(c,d));

((b,a),(c,d));