Skip to main content
`Ctrl`+`K`

# skbio.tree.TreeNode.bipart

TreeNode.bipart()

  Return a bipartition of the tree at the current branch.

  Added in version 0.6.3.

  A bipartition, partition or split of a tree is the division of all taxa (tip names) into two complementary subsets, separated at a given branch. In this context, it is the branch connecting self and its parent. One subset consists of all taxa descending from self and the other consists of all remaining taxa. The smaller subset of the two is returned.

  Returns:
    frozenset of str

      The set of names at the tips on the smaller side of the current branch.

  See also

  `subset`
  `biparts`

  Notes

  A bipartition describes the topological placement of a branch regardless of other branches and the root of the tree.

  The returned value is a set of tip names on the smaller side of the branch, as determined by the number of tips. If a tie is observed, the tip names on both sides are sorted lexicographically and the first set is returned.

  The returned value (a frozenset) is unordered and hashable, making it efficient for lookup and comparison. For example, one can check whether two branches in two unrooted trees with the same taxa agree with each other.

  Rerooting a tree will not change the bipartition of a branch. However, one should be cautious because this method applies to a node, and rerooting may change the branch above the current node.

  Applying this method to a root node will return an empty set. Applying this method to a tip will return a single-element set containing the tip name. These two situations produce outputs independent of the topology of the tree.

  Duplicate tip names, if separated by the target branch, can cause an unexpected result. This function does not validate the uniqueness of tip names though.

  Examples

      >>> from skbio import TreeNode
      >>> tree = TreeNode.read(["(((a,(b,c)X)Y,d)Z,(e,f),g);"])
      >>> print(tree.ascii_art())
                                    /-a
                          /Y-------|
                         |         |          /-b
                /Z-------|          \X-------|
               |         |                    \-c
               |         |
               |          \-d
      ---------|
               |          /-e
               |---------|
               |          \-f
               |
                \-g

  Clade has less than half taxa, return them.

      >>> sorted(tree.find('X').bipart())
      ['b', 'c']

  Clade has more than half taxa, return remaining taxa.

      >>> sorted(tree.find('Z').bipart())
      ['e', 'f', 'g']

  Clade has exactly half taxa, return the lexicographically smaller side.

      >>> sorted(tree.find('Y').bipart())
      ['a', 'b', 'c']

  A second tree with the same topology but different root position.

      >>> tree2 = TreeNode.read(["((c,b)X2,a,(((f,e),g)Y2,d));"])
      >>> print(tree2.ascii_art())
                          /-c
                /X2------|
               |          \-b
               |
               |--a
      ---------|
               |                              /-f
               |                    /--------|
               |          /Y2------|          \-e
               |         |         |
                \--------|          \-g
                         |
                          \-d

  Although the tree has been re-positioned, the corresponding branches have the same bipartitions, whereas non-corresponding branches don’t.

      >>> tree.find('X').bipart() == tree2.find('X2').bipart()
      True

      >>> tree.find('Y').bipart() == tree2.find('Y2').bipart()
      False