How to collapse a clade of a tree?

466 views
Skip to first unread message

Anting Xu

unread,
Nov 30, 2010, 4:50:56 AM11/30/10
to The ETE toolkit
Hi,

I wonder what is the function I should use to collapse a clade of a
tree? Currently I can only do it with the show window. A.collapse()
does not work.

t = Tree()
A = t.add_child(name="A")
B = t.add_child(name="B")
C = A.add_child(name="C")
D = A.add_child(name="D")
print t

## /-C
## /---|
##----| \-D
## |
## \-B

t.show()
print t

## /-A
##----|
## \-B



Besides, Node.collapse() over-writes itself??? I wonder if this is a
bug. See the code and output below:

A.collapse
##<bound method TreeNode.collapse of <ete2.coretype.tree.TreeNode
object at 0x06634AB0>>
A.collapse()
A.collapse
##True
A.collapse()
##Traceback (most recent call last):
## File "<pyshell#77>", line 1, in <module>
## A.collapse()
##TypeError: 'bool' object is not callable

Jaime Huerta Cepas

unread,
Nov 30, 2010, 5:25:22 AM11/30/10
to eteto...@googlegroups.com
I see, It seems to be a small bug in the collapse function, sorry.
I will try to fix it soon. In the meantime, you can always do the same by setting
the node drawing style within a layout function (using the draw_descendants attribute).

from ete2 import layouts, Tree
def my_layout(node):
     layouts.basic(node)
     if node.name == "A":
          node.img_style["draw_descendants"] = False


t = Tree()
A = t.add_child(name="A")
B = t.add_child(name="B")
C = A.add_child(name="C")
D = A.add_child(name="D")
t.show(my_layout)

Jaime

BFT

unread,
Mar 21, 2013, 9:40:13 PM3/21/13
to eteto...@googlegroups.com
Hi Jaime, I wanted to collapse a clade on a tree, and have tried your suggested method, but keep getting this error message:

File "xyz.py", line 78, in <module>
    t.show(my_layout)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/coretype/tree.py", line 1283, in show
    drawer.show_tree(self, layout=layout, tree_style=tree_style)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/drawer.py", line 84, in show_tree
    tree_item, n2i, n2f = render(t, img)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 320, in render
    render_floatings(n2i, n2f, img, parent.float_layer, parent.float_behind_layer)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 759, in render_floatings
    item = n2i[node]
KeyError: Tree node 'NoName' (0x3aff05)

Also looked at the tutorial but couldnt find method to collapse a calde. Any help is much appreciated.

Jaime Huerta Cepas

unread,
Mar 22, 2013, 3:22:42 AM3/22/13
to eteto...@googlegroups.com
Could you provide an example that reproduces the error?
The code I sent to you seems to work fine in my computer, so I cannot debug it...


--
You received this message because you are subscribed to the Google Groups "The ETE toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to etetoolkit+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

BFT

unread,
Mar 22, 2013, 11:44:14 AM3/22/13
to eteto...@googlegroups.com


On Friday, March 22, 2013 1:22:42 AM UTC-6, Jaime Huerta Cepas wrote:
Could you provide an example that reproduces the error?
The code I sent to you seems to work fine in my computer, so I cannot debug it...

Jaime,
Thank you for the prompt reply. I have attached the input file here.

cheers,
xyz.py

Jaime Huerta Cepas

unread,
Mar 22, 2013, 12:11:28 PM3/22/13
to eteto...@googlegroups.com
Ok, I think I got the problem: 

I see that you are mixing static and dynamic NodeStyle instances. This is, first you initialize all nodes to use the **same** dictionary of node style properties:

nstyle = NodeStyle()
nstyle["shape"] = "sphere"
nstyle["size"] = 5
nstyle["fgcolor"] = "darkred"
for n in t.traverse():
   n.set_style(nstyle)

An the you use a layout function that dynamically modify node style when node name matches: 

def my_layout(node):
 if node.name=="contig06922":
    node.img_style["draw_descendants"] = False


The problem here is that, as you are recycling the same NodeStyle object in all nodes, when you set an attribute within the layout function you are changing such attribute in all nodes. (img_style is a pointer to the same dictionary in all nodes). Collapsing all nodes produces that weird error, basically because all nodes are expected to be collapsed. 

There are many different solutions. The easiest one would be to avoid recycling NodeStyle objects: 

for n in t.traverse():
  nstyle = NodeStyle()
  nstyle["shape"] = "sphere"
  nstyle["size"] = 5
  nstyle["fgcolor"] = "darkred"
  n.set_style(nstyle)

But if you really want to reuse NodeStyle instances, just make sure to create new instances for special nodes (the ones with differential attributes). 

Hope it helps! 
Jaime

BFT

unread,
Mar 22, 2013, 12:51:02 PM3/22/13
to eteto...@googlegroups.com
Thank you for the solution along with the explanation and it worked!
However, the
def my_layout(node):
 if node.name=="contig06922":
    node.img_style["draw_descendants"] = False

did not seem to have collapsed the descendent of contig06922, though. I have must made some mistakes else where in the script. Do you happen to have a section on your tutorial that talk about collapsing function? I am not able to find it though. I have also attached the test.newick file for you to look at.
Your help has been great, and I appreciate it.

Jaime Huerta Cepas

unread,
Mar 22, 2013, 1:01:00 PM3/22/13
to eteto...@googlegroups.com
does the attached simplified version of your script work? It collapses the node when I run it. 

I don't think there is a specific section about this in the tutorial.

Jaime

PS. your newick file was not attached, btw :)




--
xyz.py

BFT

unread,
Mar 22, 2013, 1:12:43 PM3/22/13
to eteto...@googlegroups.com
Ah, your response is faster than dhl delivery. gracious ;-)
Anyways, I have tried the simplified version of the script, and also "turn off" all the other function, but it didnt work. So i suspect the node.img_style["draw_descendants"] = False function might be affected by the topology? I am not sure.
Thanks!

BFT

unread,
Mar 22, 2013, 1:13:56 PM3/22/13
to eteto...@googlegroups.com
((((ckmer87_8517:0.005780000000001451,kmer67_4839782:0.0):1.011419,((ckmer83_42244:0.0,HCZQJRY02G6I6D:0.0):0.7036839999999991,((21910:0.00761500000000126,(kmer87_1644763:0.0,kmer87_1644761:0.0):0.0):0.17620099999999894,((ADJ51097:0.1412300000000002,(kmer87_2335415:0.06400000000000006,kmer87_2330975:0.030141000000000417):0.07937000000000083):0.03277899999999789,((YP_002431360:0.1505490000000016,(ABH11460:0.0,YP_002430896:0.0):0.08099200000000195):0.04232899999999873,((CAO03074:0.08376600000000067,CBK27727:0.0414740000000009):0.1339100000000002,(kmer87_1734471:0.02565699999999893,((kmer87_1678319:0.0,ckmer87_48508:1.0000000010279564E-6):0.03113700000000108,((kmer87_2145339:0.0,(kmer59_6584017:0.02071099999999859,(kmer87_1201809:0.0,(kmer63_4336173:9.999999974752427E-7,(contig01037:0.0,(kmer87_1201807:0.0,(kmer63_5178031:1.0000000010279564E-6,(kmer87_2143667:0.0,kmer67_4691864:0.0):0.0):0.0):0.0):9.999999974752427E-7):0.0):0.015284000000001186):0.0012599999999984846):2.0000000020559128E-6,kmer83_1962630:0.027512000000001535):0.018653000000000475):0.03551399999999916):0.18067100000000025):0.02799399999999963):0.038105999999999085):0.05107200000000134):0.2980039999999988):0.16676100000000105):0.313051999999999,(((kmer59_7248215:5.000000001587068E-6,kmer59_7027479:0.025289000000000783):2.1000000000270802E-5,(kmer87_1909901:0.06880699999999962,(ckmer87_62430:0.0,contig01010:0.0):0.00485400000000169):17.547894):0.27891500000000136,((kmer59_7248277:0.3673770000000012,(ADB04297:0.03330099999999803,(CAO72222:0.009859000000002283,(CAO72219:0.0,ZP_07203817:0.0011609999999997456):0.025013000000001284):0.026598999999997375):0.26455500000000143):0.18748000000000076,(ZP_09103342:0.2412390000000002,((ABO30979:0.06139999999999901,ADJ93876:0.05981799999999993):0.1256990000000009,(ZP_09103333:0.11300100000000057,(ABO30980:0.1448999999999998,(((YP_158060:0.017137000000001734,BSSA_THAAR:0.03086100000000158):0.09337599999999924,(BAD42366:0.0843560000000032,((AAK50372:0.004065000000000651,ABK15654:0.0):0.03356199999999987,(AAC38454:0.010329999999999728,BAC05501:0.00705199999999806):0.0070750000000003865):0.07899100000000203):0.028047999999998297):0.053288999999999476,((ACI45753:0.023058000000002465,(ckmer87_56747:0.0,(ABM92939:0.0,HCZQJRY02GJXZC:0.0):0.0):0.04862600000000228):0.08643299999999954,(YP_006759014:0.11179300000000225,(YP_002537892:0.11540400000000162,((ABM92936:0.008235000000002657,(YP_006720507:0.0,ABB31773:0.0):0.001660000000001105):0.018842999999996835,(ABM92938:0.015091000000001742,YP_002537902:0.013851999999999975):0.02700399999999803):0.06489000000000189):0.07548900000000103):0.025245999999999214):0.02271599999999907):0.061306999999999334):0.024881000000000597):0.036823000000001826):0.1381099999999975):0.13514200000000187):0.16068500000000085):0.3162269999999978):0.5847440000000006,((contig06922:0.6666550000000022,(kmer59_7256437:0.19328099999999893,(kmer59_7258257:0.05245599999999939,(1741:1.0000000010279564E-6,10834:0.05215499999999906):0.029274999999998386):0.1112080000000013):0.46714700000000065):0.19627699999999848,(((kmer59_7151817:0.7262960000000014,(kmer59_7210579:0.5171990000000015,(ZP_02948299:0.20320099999999996,(YP_003158645:0.2960029999999989,(kmer59_7167093:0.36203899999999933,YP_965941:0.13579700000000017):0.04094699999999918):0.04903600000000097):0.23241200000000006):0.3005139999999997):0.21055000000000135,(kmer87_2333523:0.8310430000000011,(kmer87_2339807:0.4118119999999976,ckmer87_57347:0.355753):0.2832080000000019):0.25201200000000057):0.22480999999999796,(15017:1.2390640000000026,(((ckmer83_42103:0.2279599999999995,(kmer59_7153253:0.12024999999999864,(kmer59_6885705:0.09461200000000147,(4002:0.05786700000000167,(8204:0.06677700000000186,(ckmer83_10395:0.02226699999999937,kmer67_4779042:0.006170000000000897):0.03942800000000091):0.022652999999998258):0.028870000000001284):0.052426999999998):0.11731500000000139):0.18847200000000086,(kmer79_3038427:0.3857779999999984,((22276:0.31541700000000006,(ckmer79_12054:0.28746500000000097,ckmer87_45602:0.08372699999999966):0.11080699999999766):0.19611700000000098,(ckmer79_33948:0.08988300000000038,ckmer79_23226:0.06237199999999987):0.12662399999999963):0.06499799999999922):0.06912800000000274):0.4719859999999976,((((kmer59_7134581:0.30026200000000003,(HCM3FMZ02GWL1I:0.0,'ckmer87_46009 1':9.999999974752427E-7):0.42742500000000305):0.3815169999999988,(kmer59_7206745:0.12613199999999836,kmer59_7202359:0.1902109999999979):0.556598000000001):0.05346399999999818,(6724:0.4737979999999986,(ckmer67_312:0.6118050000000004,(ckmer75_17625:0.21017600000000058,(kmer59_7111147:0.026611000000002605,((ckmer87_52247:0.0,((kmer59_6970915:0.05089500000000058,(kmer87_2179079:0.0,kmer87_2179081:0.003492999999998858):0.03800200000000231):1.0000000010279564E-6,(kmer87_1288159:1.0000000010279564E-6,kmer87_1287915:0.01133000000000095):3.200000000092018E-5):0.04752499999999671):0.9243000000000023,((kmer59_7267083:0.020201000000000136,8200:0.017172000000002186):0.03570099999999954,(ckmer75_12460:0.06419999999999959,kmer59_7099281:0.04830100000000215):0.0190570000000001):0.007348000000000354):0.01750900000000044):0.11892999999999887):0.1583509999999997):0.10473400000000055):0.11254899999999779):0.05899800000000255,((((kmer59_7269191:0.669551000000002,(9481:1.0095150000000004,((kmer59_7264125:0.16592899999999844,(ckmer75_29200:0.21899200000000008,(ckmer83_4924:0.1023159999999983,ckmer67_29029:0.07255800000000079):0.06692899999999824):0.06399100000000146):0.763812999999999,ckmer75_34719:0.5527789999999975):0.12966100000000225):0.13592900000000085):0.13642799999999866,kmer59_7218205:0.7644480000000016):0.03486699999999843,(kmer59_7126119:0.5134940000000014,16833:0.5560720000000003):0.11626499999999851):0.08402000000000243,(('kmer59_7165755 2':0.8006129999999985,(830:0.3098719999999986,kmer59_7167521:0.22842099999999732):0.30463400000000007):0.2485500000000016,((kmer59_7266875:0.8345780000000005,kmer59_7058677:0.5323910000000005):0.11062399999999784,(kmer71_4096458:0.0,kmer71_4096460:0.01956999999999809):1.4618579999999994):0.10773800000000122):0.1503690000000013):0.08033299999999954):0.05162199999999828):0.07974500000000262):0.20957899999999796):0.10795099999999991):0.5847440000000006);

Jaime Huerta Cepas

unread,
Mar 22, 2013, 1:23:07 PM3/22/13
to eteto...@googlegroups.com
Hey, but the problem now is that you are trying to collapse a leaf node!! :)

What you probably want is to collapse an internal node. For instance: 

node_to_collapse = t.get_common_ancestor("contig06922", "kmer59_7258257")
def my_layout(node):
   if node is node_to_collapse:
      node.name = "COLLAPSED NODE"
      node.img_style["size"] = 20
      node.img_style["draw_descendants"] = False


BFT

unread,
Mar 22, 2013, 1:35:06 PM3/22/13
to eteto...@googlegroups.com, jhu...@crg.es
Hey Jaime, Thanks for that. It worked. Would you consider adding "collapsing clades" in your tutorial? I bet that will be very helpful to alot of users who are not familiar with ETE or python. ETE is a great tool but for a lot of people as myself who do not regularly work on writting script, it can be a challenge at first. Good start for the weekend. Thanks!

Jaime Huerta Cepas

unread,
Mar 23, 2013, 9:46:24 AM3/23/13
to eteto...@googlegroups.com
Sure! I'll take it into account for the next tutorial updates

Reply all
Reply to author
Forward
0 new messages