labelling (and weighting?) internal nodes

953 views
Skip to first unread message

Jacob Scott

unread,
Jan 12, 2016, 10:05:54 AM1/12/16
to The ETE toolkit
Hi -

I'm sort of new to ete3, but am struggling with something...  this is probably quite simple, but i've looked through the docs all i can!  any help would be appreciated.

I'm successfully drawing trees, but can't manage to label my 'non-leaf' nodes, I can t.show_internal = True when I display as ASCII, but can't manage in a graphical representation.  

The same question goes for weighting internal nodes...

thanks in advance (and thanks for the great package).

jake

Jaime Huerta Cepas

unread,
Jan 12, 2016, 10:13:42 AM1/12/16
to eteto...@googlegroups.com
Hi Jacob, 
thanks for your email - to draw labels in internal nodes, you need to add them manually. It's done through the use of "nodeFaces". There are many types of Faces, and you can learn more about them here and here:  
The simplest example would consist of adding TextFaces to nodes. For instance: 

from ete3 import Tree, TextFace
....
for node in tree.traverse():
     node.add_face(TextFace(node.dist), column=0, position="branch-top")
...
tree.show()

another simple example is here

cheers,
-jaime


--
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.
To post to this group, send email to eteto...@googlegroups.com.
Visit this group at https://groups.google.com/group/etetoolkit.
For more options, visit https://groups.google.com/d/optout.

Jacob Scott

unread,
Jan 12, 2016, 11:58:13 AM1/12/16
to The ETE toolkit
Thanks so much...  this works, but I can't seem to modify the attributes using fsize and position - nothing i put in there either effects the output or raises an error...


for n in t.traverse():
    n.add_face(TextFace(n.name, fsize = 16), column=0, position="branch-bottom")

and seems to be the same syntax as:

thanks again...

Jaime Huerta Cepas

unread,
Jan 12, 2016, 12:03:57 PM1/12/16
to eteto...@googlegroups.com
could you share the piece of code that is not working? 

The following example works well for me:

from ete3 import Tree, TreeStyle, TextFace
t = Tree( "((a,b),c);" )

# Basic tree style
ts = TreeStyle()
ts.show_leaf_name = True

# Add two text faces to different columns
t.add_face(TextFace("hello ", fsize=20), column=0, position = "branch-bottom")
t.show(tree_style=ts)

--

Jacob Scott

unread,
Jan 12, 2016, 3:49:26 PM1/12/16
to eteto...@googlegroups.com
Here's the whole thing.;

thanks for your help!

i'm deriving the trees from other data types..

''' Build phylogenies from parent,child lists

Jacob Scott 26 Dec 2015 '''

*******************

import numpy as np
from ete3 import Tree, TreeStyle, NodeStyle, faces, AttrFace, CircleFace, TextFace
from collections import Counter
from math import log as ln
import random as random

# read_path1 = '../../../../Thesis/phylogenies/experiment/non-stem/'
read_path = '../andrea_test/stem/'


def layout(node):
    # if node.is_leaf():
    #     # Add node name to leaf nodes
    #     N = AttrFace("name", fsize=14, fgcolor="black")
    #     faces.add_face_to_node(N, node, 0)
    if "weight" in node.features:
        # Creates a sphere face whose size is proportional to node's
        # feature "weight"
        C = CircleFace(radius=node.weight, color="RoyalBlue", style="sphere")
        # Let's make the sphere transparent
        C.opacity = 0.6
        # And place as a float face over the tree
        faces.add_face_to_node(C, node, 0, position="float")

def sort_pairs(pair):
    # Extract integer after "r".
    return int(pair[0][1:])

data = open(read_path+'stemsym6l4t5k.txt').read().replace(',',' ').replace('\n',' ')
x = data.split()
ParentChild = np.array(x).astype('str')
y = len(ParentChild)/3
ParentChild1 = np.reshape(ParentChild, (y,3))
firsttwo = ParentChild1[:,0:2] #chops off first line which encodes parameters of simulation and third column which is not yet used
parents = []
children = []

for row in range(0, len(firsttwo)): 
for column in range(0,2): 
firsttwo[row,column] = 'r'+firsttwo[row,column]

t = Tree() # Creates an empty tree

r1 = t.add_child(name="r1")
lookup = {"r1": r1}
prune_list = ['r1']

for pair in sorted(firsttwo, key=sort_pairs):
    parentname = pair[0]
    childname = pair[1]
    if childname not in lookup:
        if parentname in lookup:
            # print parentname
            newchild = lookup[parentname].add_child(name = childname)
            lookup.update({childname: newchild})
            if parentname not in parents:
                prune_list.append(lookup[parentname])
            parents.append(parentname) #make list of unique terminal nodes (no children of children)
            children.append(newchild)
        else:
            raise RuntimeError('Must not happen.')

'''make a list of all leaves with no children, count them and then prune the tree of all leaves '''
prune_count = Counter(children) #counter than contains the number of children that each terminal node has]
print(parents)
# print(prune_list)
# for key in prune_count.keys():
#     # node_weight = ln(prune_count[key])
#     # node_weight = ln(n_children)
#     # print(node_weight,n_children)
#     # node = lookup[key]
#     node.add_features(weight=random.randint(50))
for n in t.traverse():
    n.add_face(TextFace(n.name, fsize = 16), column=0, position="branch-bottom")
    n.add_features(weight=random.randint(0,20))
# t.prune(prune_list)
# Create an empty TreeStyle
ts = TreeStyle()
# Set our custom layout function
ts.layout_fn = layout
# Draw a tree
ts.mode = "c"
# False need to add node names manually
ts.show_leaf_name = False

# Show branch data
ts.show_branch_length = False
ts.show_branch_support = True


# print (t.get_ascii(show_internal=True))

t.show(tree_style=ts)

**********************

Jacob Scott, MD
Department of Radiation Oncology and Integrative Mathematical Oncology
H. Lee Moffitt Cancer Center and,
Oxford University Centre for Mathematical Biology

personal email: jacob....@gmail.com
medical information: jacob...@moffitt.org
twitter: @CancerConnector

--
You received this message because you are subscribed to a topic in the Google Groups "The ETE toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/etetoolkit/ptU41idLF5E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to etetoolkit+...@googlegroups.com.

Jacob Scott

unread,
Jan 12, 2016, 4:05:32 PM1/12/16
to eteto...@googlegroups.com
here's the file i'm using if it helps.

thanks so much for the assistance.

Jacob Scott, MD
Department of Radiation Oncology and Integrative Mathematical Oncology
H. Lee Moffitt Cancer Center and,
Oxford University Centre for Mathematical Biology

personal email: jacob....@gmail.com
medical information: jacob...@moffitt.org
twitter: @CancerConnector

stemsym6l4t5k.txt

Jaime Huerta Cepas

unread,
Jan 13, 2016, 5:26:47 AM1/13/16
to eteto...@googlegroups.com
Hi Jacob, 
your code seems to run fine. TextFaces are actually setting the fontsize to 16, but you may not notice it due to zooming issues. To fit custom text on branches without overlaps in the circular representations, ETE increases the branch scale (similar effect as a large zoom-out factor), making the 16-fontsize text look smaller. But if you zoom in, you will see the relative size of those textFaces is larger than other. 
I know this is an annoying effect in large trees (derived from the non-overlapping features in ETE) and we are working on better approaches. 
 
the rectangular view mode should not be affected

Jacob Scott

unread,
Jan 13, 2016, 9:40:50 AM1/13/16
to The ETE toolkit
ah, gotcha. this explains my first debugging attempt (setting the fsize to 100) not changing things either, as you are saying that this would rescale everything for no overlap as well...

thanks again.

will play with rectangular view too.
Reply all
Reply to author
Forward
0 new messages