Answers.Body: <p>The example given a 3 taxa tree, isn't an ideal example. What is required is a raw treefile in a common format (nexus, philip, newark). The example below uses ETE3 for a 5 taxa tree A,B,C,D,E. Its ROOTED - very important to know the direction.</p>
  <p>ETE3 is providing a means of traversing down the tree from the root, however the coder needs to interrogate the path it has taken.</p>
  <ol>
  <li>Tree file imported</li>
  <li>Ladderizing ... so you know the horizontal direction of the tree (see *)</li>
  <li>Ask the ETE3 to search down the nodes ... each child is a node from the root.</li>
  <li>Parse the output and identify the clade.</li>
  </ol>
  <p>The two outputs from point 3 are either it encounters a node - in this case it returns all the children below it (taxa names). The alternative is that it encounters a terminal node or a leaf, like a species name or isolate. In this case the taxa name is returned.</p>
  <p>The caveat is the terminal node (taxa label or leaf) is always bifurcation and the taxa names are split into [1] and [0] on the ETE3 object (see code) ... you have to interrogate both.</p>
  <p>Thats what the OP is looking for and this part of the code is marked <strong>'Part 1'</strong></p>
  <p>Parsing <strong>Part 2</strong> is more complicated, because usually the coder wants the clade a particular taxa (or leaf) belongs to. Essentially, the coder needs to ask what the leaf name is, if the target leaf name is found then the clade is then identified, i.e. the other stuff next to it.</p>
  <p>The code is below. I agree if you don't think trees, this is a rushed explanation.</p>
  <pre><code>from ete3 import Tree
  
  # Part 1
  tree = Tree(intree, format=1)
  tree.ladderize(direction=1)
  clade1_ = tree.children[0].children[0].children[0].name
  leaf1_ = tree.children[0].children[0].children[0].children[0].name 
  leaf2_ = tree.children[0].children[0].children[0].children[1].name
  
  # Part 2
  if leaf1 == 'A' or leaf2 == 'A':
      CladeA = clade1_
  if leaf2 == 'B' or leaf2 == 'B':
      CladeA = clade1_
  </code></pre>
  <p>*, ladderizing that can be important for classifying [0] and [1] in ETE3 ... its a weird one in trees 'cause it shouldn't matter</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-15T21:05:37.173Z
Answers.LastActivityDate: 2022-08-16T12:26:48.513Z
Answers.LastEditDate: 2022-08-16T12:26:48.513Z
Answers.UserDisplayName: M__
Body: <p>I am using Biopython Phylo and <a href="https://www.rdkit.org/docs/source/rdkit.ML.Cluster.Butina.html" rel="nofollow noreferrer">RDkit Cluster</a> to obtain a UPGMA tree from a distance matrix of 5k x 5k entries. How can I extract the taxa names within a given clade(s)?</p>
  <p>For example, Bio.Phylo can return plain-text representation of a tree, so I think I can parse it by the number of tabs:</p>
  <pre><code>Phylogeny()
       Clade()
           Clade(branch_length=0.06)
               Clade(branch_length=0.102, name='A')
               Clade(branch_length=0.23, name='B')
           Clade(branch_length=0.4, name='C') 
  ...
  </code></pre>
  <p>or, ASCII format:</p>
  <pre><code>          _____________ A
    _______|
  _|       |_______________________________ B
   |
   |_______________________________________________________ C
  </code></pre>
  <p>Which, I think is more difficult to parse but possible.
  Either way, those are not established methods for this kind of task, so I would like a hint.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-11T02:23:12.190Z
Comments.Text: There are two packages that do this well, `ETE3` and `dendropy`. I will put a ETE3 solution forward.
Comments.UserDisplayName: M__
Tags: <phylogenetics><python><phylogeny><clustering>
Title: How to extract clusters from a phylogenetic tree?
ThreadText: # How to extract clusters from a phylogenetic tree?
  
  (Asked by: username_0 on 2020-12-15)
  
  I am using Biopython Phylo and [RDkit Cluster](https://www.rdkit.org/docs/source/rdkit.ML.Cluster.Butina.html) to obtain a UPGMA tree from a distance matrix of 5k x 5k entries. How can I extract the taxa names within a given clade(s)?
  
  For example, Bio.Phylo can return plain-text representation of a tree, so I think I can parse it by the number of tabs:
  
  ```
  Phylogeny()
       Clade()
           Clade(branch_length=0.06)
               Clade(branch_length=0.102, name='A')
               Clade(branch_length=0.23, name='B')
           Clade(branch_length=0.4, name='C') 
  ...
  
  ```
  
  or, ASCII format:
  
  ```
            _____________ A
    _______|
  _|       |_______________________________ B
   |
   |_______________________________________________________ C
  
  ```
  
  Which, I think is more difficult to parse but possible.
  Either way, those are not established methods for this kind of task, so I would like a hint.
  
  **Tags:** phylogenetics, python, phylogeny, clustering
  
  > **Comments:**
  > **username_1**: There are two packages that do this well, `ETE3` and `dendropy`. I will put a ETE3 solution forward.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-15. Score: 2 | Accepted answer
  
  The example given a 3 taxa tree, isn't an ideal example. What is required is a raw treefile in a common format (nexus, philip, newark). The example below uses ETE3 for a 5 taxa tree A,B,C,D,E. Its ROOTED - very important to know the direction.
  
  ETE3 is providing a means of traversing down the tree from the root, however the coder needs to interrogate the path it has taken.
  
  1. Tree file imported
  2. Ladderizing ... so you know the horizontal direction of the tree (see \*)
  3. Ask the ETE3 to search down the nodes ... each child is a node from the root.
  4. Parse the output and identify the clade.
  
  The two outputs from point 3 are either it encounters a node - in this case it returns all the children below it (taxa names). The alternative is that it encounters a terminal node or a leaf, like a species name or isolate. In this case the taxa name is returned.
  
  The caveat is the terminal node (taxa label or leaf) is always bifurcation and the taxa names are split into [1] and [0] on the ETE3 object (see code) ... you have to interrogate both.
  
  Thats what the OP is looking for and this part of the code is marked **'Part 1'**
  
  Parsing **Part 2** is more complicated, because usually the coder wants the clade a particular taxa (or leaf) belongs to. Essentially, the coder needs to ask what the leaf name is, if the target leaf name is found then the clade is then identified, i.e. the other stuff next to it.
  
  The code is below. I agree if you don't think trees, this is a rushed explanation.
  
  ```
  from ete3 import Tree
  
  # Part 1
  tree = Tree(intree, format=1)
  tree.ladderize(direction=1)
  clade1_ = tree.children[0].children[0].children[0].name
  leaf1_ = tree.children[0].children[0].children[0].children[0].name 
  leaf2_ = tree.children[0].children[0].children[0].children[1].name
  
  # Part 2
  if leaf1 == 'A' or leaf2 == 'A':
      CladeA = clade1_
  if leaf2 == 'B' or leaf2 == 'B':
      CladeA = clade1_
  
  ```
  
  \*, ladderizing that can be important for classifying [0] and [1] in ETE3 ... its a weird one in trees 'cause it shouldn't matter
  
  
  ---
  
  