<p>If we are talking pointers only then the pseudo code I use the ETE3 method</p>
<pre><code>t = Tree((A:0.1,B:0.2,(C:0.3,(D:0.4, (E:0.6, F:0.6))));)
clade = t.get_common_ancestor( .... ) # name the descendants
</code></pre>
<p>Thats it, what its doing is cool, but the code is simple</p>
<hr />
<p>From comments &quot;clade&quot; is the subtree defined by the descendants of the common ancestor</p>
<hr />
<p>The OPs solution</p>
<pre><code>subtree = node.up.up.up
</code></pre>
<p>This is fine if you've a fixed tree, rather than trees that might have a changed topology (for example bootstrap replicates). For example if the paraphyly changes in another &quot;bootstrap replicate', <code>up.up.up</code> may not be the clade of interest. <code>get_common_ancestor</code> ensures the code is generalised. For example if the topology changes the approach above will still identify the correct subtree.</p>
<p>Thus if on one tree you need <code>up.up.up</code>, another tree <code>up.up</code> and a different tree <code>up.up.up.up</code> the above solution solves all scenarios.</p>

CC BY-SA 4.0
2024-05-07T18:54:58.450Z
so the `clade` object will be of type `Tree` ? Need to try in the terminal. Thanks!
El Dude
CC BY-SA 4.0
2024-05-07T19:02:57.027Z
Addressed above
M__
CC BY-SA 4.0
2024-05-08T15:19:23.803Z
Yes, the `up.up.up` chain has to be adapted to the location of the searched node because it could error out if its hardcoded
El Dude
CC BY-SA 4.0
2024-05-07T18:44:14.697Z
2024-05-07T23:16:04.533Z
2024-05-07T23:16:04.533Z
M__
<p>Actually ended up doing this to get the neighborhood of a node</p>
<pre class="lang-py prettyprint-override"><code>tree = Tree(newick_file_path, format=1)
node = tree.search_nodes(name=node_name)[0]
subtree = node.up.up.up
</code></pre>
<p>but <code>get_common_ancestor</code> worked too, thanks!</p>

CC BY-SA 4.0
2024-05-07T23:16:39.187Z
Thanks for your answer. I've responded above about this approach. This can definitely work in some situations, as long as you understand why thats cool.
M__
CC BY-SA 4.0
2024-05-07T20:09:41.430Z
2024-05-08T19:49:51.587Z
2024-05-08T19:49:51.587Z
El Dude
<p>I can not find a good example how to extract a subtree from a phylogenetic tree with either library.
I can load <code>newick</code> files etc, but not sure how to search for a node and extract all children with the right topology. Already thought of parsing the <code>newick</code> file and extract on raw data but there must be a way...
How?</p>
<p>This is a sample tree</p>
<pre><code>(A:0.1,B:0.2,(C:0.3,(D:0.4, (E:0.6, F:0.6))));
</code></pre>
<p>and I would like to get everything below <code>C</code>, so <code>D,E,F</code> . I see the ETE3 <code>get_dependants</code> function but it returns a list, not a tree... ? Please correct me if I'm wrong.</p>
<p>I understand and do not expect anyone to write code for me, but just a pointer to the right function ete3 would be enough. I can get all children for C but not sure how to preserve the subtree relationships with that command ete3 : <code>get_dependants()</code>.</p>

<python><phylogenetics><phylogeny><biopython><ete3>
Biopython / ETE3 get subtree of a newick tree
# Biopython / ETE3 get subtree of a newick tree

(Asked by: username_0 on 2024-05-06)

I can not find a good example how to extract a subtree from a phylogenetic tree with either library.
I can load newick files etc, but not sure how to search for a node and extract all children with the right topology. Already thought of parsing the newick file and extract on raw data but there must be a way...
How?
This is a sample tree
(A:0.1,B:0.2,(C:0.3,(D:0.4, (E:0.6, F:0.6))));

and I would like to get everything below C, so D,E,F . I see the ETE3 get_dependants function but it returns a list, not a tree... ? Please correct me if I'm wrong.
I understand and do not expect anyone to write code for me, but just a pointer to the right function ete3 would be enough. I can get all children for C but not sure how to preserve the subtree relationships with that command ete3 : get_dependants().

**Tags:** python, phylogenetics, phylogeny, biopython, ete3

---

## 2 answers

### Answer by username_1 on 2024-05-07. Score: 2 | Accepted answer

If we are talking pointers only then the pseudo code I use the ETE3 method
t = Tree((A:0.1,B:0.2,(C:0.3,(D:0.4, (E:0.6, F:0.6))));)
clade = t.get_common_ancestor( .... ) # name the descendants

Thats it, what its doing is cool, but the code is simple

From comments "clade" is the subtree defined by the descendants of the common ancestor

The OPs solution
subtree = node.up.up.up

This is fine if you've a fixed tree, rather than trees that might have a changed topology (for example bootstrap replicates). For example if the paraphyly changes in another "bootstrap replicate', up.up.up may not be the clade of interest. get_common_ancestor ensures the code is generalised. For example if the topology changes the approach above will still identify the correct subtree.
Thus if on one tree you need up.up.up, another tree up.up and a different tree up.up.up.up the above solution solves all scenarios.

> **Comments:**
> **username_0**: so the `clade` object will be of type `Tree` ? Need to try in the terminal. Thanks!  
> **username_1**: Addressed above  
> **username_0**: Yes, the `up.up.up` chain has to be adapted to the location of the searched node because it could error out if its hardcoded  

---

### Answer by username_0 on 2024-05-07. Score: 2

Actually ended up doing this to get the neighborhood of a node
tree = Tree(newick_file_path, format=1)
node = tree.search_nodes(name=node_name)[0]
subtree = node.up.up.up

but get_common_ancestor worked too, thanks!

> **Comments:**
> **username_1**: Thanks for your answer. I've responded above about this approach. This can definitely work in some situations, as long as you understand why thats cool.  

---

