3
$\begingroup$

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().

$\endgroup$
0

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ so the clade object will be of type Tree ? Need to try in the terminal. Thanks! $\endgroup$
    – El Dude
    Commented May 7 at 18:54
  • 1
    $\begingroup$ Addressed above $\endgroup$
    – M__
    Commented May 7 at 19:02
  • 1
    $\begingroup$ 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 $\endgroup$
    – El Dude
    Commented May 8 at 15:19
2
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – M__
    Commented May 7 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.