etetoolkit / ete

Python package for building, comparing, annotating, manipulating and visualising trees. It provides a comprehensive API and a collection of command line tools, including utilities to work with the NCBI taxonomy tree.
http://etetoolkit.org
GNU General Public License v3.0
790 stars 214 forks source link

can a tree+alignment figure include internal node sequences? #226

Closed argriffing closed 8 years ago

argriffing commented 8 years ago

I tried modifying the example in https://github.com/etetoolkit/ete/issues/219#issuecomment-221196256 as follows, but I was not able to make ete3 show the sequences corresponding to internal nodes.

Of course in the traditional phylogenetics setting you do not have access to ancestral sequence data, but it could still be useful to visualize imputed or conditionally sampled ancestral states.

from ete3 import Tree, TreeStyle, PhyloTree, TextFace, add_face_to_node, SeqMotifFace
fasta_txt = """
>seqA
MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH
>Internal1
MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH
>seqB
MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH
>Internal2
MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH
>seqC
MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH
>seqD
MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAH
"""

# Load a tree and link it to an alignment.
t = PhyloTree("(((seqA,seqB)Internal1,seqC)Internal2,seqD);", format=1)
t.link_to_alignment(fasta_txt) # it could also be a file name
ts = TreeStyle()
ts.show_leaf_name = False
def my_layout(node):
    F = TextFace(node.name, tight_text=True)
    add_face_to_node(F, node, column=0, position="branch-top")
    #if node.is_leaf():
    if not node.is_root():
        seq_face = SeqMotifFace(node.sequence, seqtype='aa', seq_format='seq')
        add_face_to_node(seq_face, node, column=0, position='aligned')
        print node.name
ts.layout_fn = my_layout
t.show(tree_style=ts)

A workaround might be to add new leaves that are connected to internal nodes by a branch of length zero. It would probably look awkward.

jhcepas commented 8 years ago

aligned faces are only allowed for tip nodes. You could still add SeqMotifFaces to branches in internal nodes, but it will probably look awkward with large sequences. Check this example: https://gist.github.com/jhcepas/868b1057d9c0b72a57b809a67ad6e4cc

argriffing commented 8 years ago

Thank you!