pythonphylogenyete3r-ape

Python ete3 - Is there a way to stretch branches of phylogenetic trees?


I am trying to read a phylogenetic tree and stretch its branches to be bigger or smaller than the original, but I didn't find how. The stretch needs to be on the tree itself - not on its visualization.

For example, the following code reads a tree and presents it:

from ete3 import Tree

t = Tree("(2azaa:0.1871453443,1dz0a:0.1944528747,   (((1joi:0.1917345578,1nwpa:0.206793251):0.2050584423,"
     "(1jzga:0.3027313573,1rkra:0.2710518895):0.08148637118):0.06756061176,(1cuoa:0.2959705289,"
     "((1qhqa:0.585997308,1gy1a:2.509606787):0.1590837051,(1kdj:0.9427371887,"
     "((1iuz:0.1918780006,7pcy:0.2035503755):0.1750205426,((2plt:0.2727097306,(2b3ia:0.6259053315,"
     "(((1bawa:0.3036227494,1nin:0.5134587308):0.1375675558,((2raca:0.4617882857,1id2a:0.3274320042):0.7764884063,"
     "(1pmy:0.7017063073,(1bqk:0.2214168026,(1adwa:0.4171298259,1paz:0.4214910379):0.08599165577):0.2074622534):0.9354371144):0.4486761297)"
     ":0.1105387947,(1m9wa:0.4551681561,1bxva:0.3931722476):0.06879588421):0.1131812572):0.4242876607):0.1447393581,"
     "(1plb:0.2176281022,(1byoa:0.2314554253,(9pcy:0.2456728049,(1ag6:0.1776514893,1plc:0.318467746):0.02728470893)"
     ":0.07383541027):0.1260361833):0.2659408726):0.05013755844):0.2637791318):1.001560925):1.018869112):0.4609302267):0.1807238866);")

t.show()

The following link discusses how to use the library, but I didn't find what I was looking for:

http://etetoolkit.org/docs/latest/tutorial/tutorial_trees.html

Can anyone help?

Edit: If there are other Python libraries that can do that, I would love to hear which and how it's done.

Edit2: I know that in R there is a library named "ape" then can do it very simply... maybe someone who works with it knows the parallel operation in some python library?


Solution

  • After a long time I found a solution: As far as I know there are no built in functions to stretch trees in phylogentic python libraries. This is very strange and I hope I am wrong.

    However once you understand their data-structures there is in easy way to do it: all you need to do is just to run over all the edges in the trees and multiply them by the desired factor. This is done differently, depending on which library you use. Here are two examples how to stretch the trees twice their size using dendropy and ete3:

    from ete3 import Tree
    import dendropy as dp
    
    original_tree = "(2azaa:0.1871453443,1dz0a:0.1944528747,(((1joi:0.1917345578,1nwpa:0.206793251):0.2050584423,"\
         "(1jzga:0.3027313573,1rkra:0.2710518895):0.08148637118):0.06756061176,(1cuoa:0.2959705289,"\
         "((1qhqa:0.585997308,1gy1a:2.509606787):0.1590837051,(1kdj:0.9427371887,"\
         "((1iuz:0.1918780006,7pcy:0.2035503755):0.1750205426,((2plt:0.2727097306,(2b3ia:0.6259053315,"\
         "(((1bawa:0.3036227494,1nin:0.5134587308):0.1375675558,((2raca:0.4617882857,1id2a:0.3274320042):0.7764884063,"\
         "(1pmy:0.7017063073,(1bqk:0.2214168026,(1adwa:0.4171298259,1paz:0.4214910379):0.08599165577):0.2074622534):0.9354371144):0.4486761297)"\
         ":0.1105387947,(1m9wa:0.4551681561,1bxva:0.3931722476):0.06879588421):0.1131812572):0.4242876607):0.1447393581,"\
         "(1plb:0.2176281022,(1byoa:0.2314554253,(9pcy:0.2456728049,(1ag6:0.1776514893,1plc:0.318467746):0.02728470893)"\
         ":0.07383541027):0.1260361833):0.2659408726):0.05013755844):0.2637791318):1.001560925):1.018869112):0.4609302267):0.1807238866);"
    
    #dendropy test
    print("These are the dendropy results:")
    t1 = dp.Tree.get_from_string(original_tree,"newick")
    t2 = dp.Tree.get_from_string(original_tree,"newick")
    for edge in t2.levelorder_edge_iter():
        if(edge.length == None):
            continue
        edge.length *=2
    print(t1)
    print(t2)
    
    #ete3 test
    print("These are the ete3 results:")
    t3 = Tree(original_tree)
    t4 = Tree(original_tree)
    for node in t4.iter_descendants():
        node.dist*=2
    print(t3.write())
    print(t4.write())
    

    Another lesson we can learn from this case - always do your homework on the data-structure you work with before you search for a built in function...