skbio.tree.TreeNode.shuffle#
- TreeNode.shuffle(k=None, names=None, shuffle_f=<bound method RandomState.shuffle of RandomState(MT19937)>, n=1)[source]#
Yield trees with shuffled tip names.
- Parameters:
- kint, optional
The number of tips to shuffle. If k is not None, k tips are randomly selected, and only those names will be shuffled.
- nameslist, optional
The specific tip names to shuffle. k and names cannot be specified at the same time.
- shuffle_ffunc
Shuffle method, this function must accept a list and modify inplace.
- nint, optional
The number of iterations to perform. Value must be > 0 and np.inf can be specified for an infinite number of iterations.
- Yields:
- TreeNode
Tree with shuffled tip names.
- Raises:
- ValueError
If k is < 2 If n is < 1
- ValueError
If both k and names are specified
- MissingNodeError
If names is specified but one of the names cannot be found
Notes
Tip names are shuffled inplace. If neither k nor names are provided, all tips are shuffled.
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));