Treatment of unrooted trees

11 views
Skip to first unread message

krumpelstiltskin

unread,
Oct 24, 2018, 12:46:15 PM10/24/18
to DendroPy Users
Hello,

I'm using Dendropy with unrooted trees. I have to manipulate the trees so am interested in details regarding the representation of unrooted trees in Dendropy.

Thing seems to be awkward when it comes to unrooted trees. For instance, all the tree manipulation methods are coined in terms of children and parents (e.g. new_child(), child_nodes(), etc.). What does it mean to have a "child" in an unrooted tree?

I also was surprised to see the following:

In [10]: from dendropy import Tree

In [11]: tree = Tree.get(data='[&U] (a, (b,c));', schema='newick')

In [12]: tree.print_plot()
/---------------------------------------------------------------------------- a
+                                                                              
|                                     /-------------------------------------- b
\-------------------------------------+                                        
                                     
\-------------------------------------- c
                                                                               
                                                                               

In [13]: print(len(tree.nodes()))
5

In [14]: print(tree.internal_nodes())
[<Node object at 0x7f949fe79860: 'None' (None)>, <Node object at 0x7f949fe799e8: 'None' (None)>]

Although I've specified an unrooted tree using the "[&U]" syntax, there seems to be an extra internal node. This tree looks rooted to me. Is there a way to avoid this?

Thank you.

Alexey Morozov

unread,
Oct 24, 2018, 12:52:39 PM10/24/18
to dendrop...@googlegroups.com
All trees are inherently rooted in DendroPy. I'm afraid you'll have to hack around it if your analysis is somehow relying on tree being unrooted. In most cases it's enough to stuck somethung like `if node is not tree.seed_node:` on top of your node processing code, and edge set is the same on rooted tree and its unrooted counterpart anyway.

чт, 25 окт. 2018 г., 0:46 krumpelstiltskin <theks...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "DendroPy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dendropy-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

krumpelstiltskin

unread,
Oct 24, 2018, 1:31:11 PM10/24/18
to DendroPy Users
Thanks for the help!  I'll work around it.

Could you please clarify one more thing for me?
I see the following:
In [9]: print(len(tree.edges()))
5

In [10]: print(len(tree.nodes()))
5

How could a rooted tree have 5 edges and 5 nodes?

Alexey Morozov

unread,
Oct 24, 2018, 11:02:03 PM10/24/18
to dendrop...@googlegroups.com
Not sure where the fifth edge comes from. If it's your tree from below, I would expect it ro have five nodes and four edges, rooted. Have you tried displaying what these edges are?

чт, 25 окт. 2018 г., 1:31 krumpelstiltskin <theks...@gmail.com>:

krumpelstiltskin

unread,
Oct 25, 2018, 5:05:40 AM10/25/18
to DendroPy Users
It looks like there's an "edge" connected to the "root" (i.e. tree.seed_node) which has no "Tail Node".

VERY Strange.

In [5]: print(tree.seed_node)
<Node object at 0x7fc1400595c0: 'None' (None)>

In [6]: for e in tree.edges():
   ...:     print(e.description())

Edge object at 0x7fc1400595f8 (140467979523576, Length=None)
    [Length]
        None
    [Tail Node]
        None
    [Head Node]
        Node object at 0x7fc1400595c0<Node object at 0x7fc1400595c0: 'None' (None)>
.
.
.

krumpelstiltskin

unread,
Oct 25, 2018, 5:18:42 AM10/25/18
to DendroPy Users
Things get even stranger when I initialize a tree with a single node.  There are now TWO edges!  One connected to nothing and another to itself.  :/

In [1]: from dendropy import Tree

In [2]: tree = Tree.get(data='[&U] (a);', schema='newick')

In [3]: for e in tree.edges():
   
...:     print(e.description())
   
...:    
Edge object at 0x7f4aeccb0780 (139959777036160, Length=None)

   
[Length]
       
None
   
[Tail Node]
       
None
   
[Head Node]

       
Node object at 0x7f4aeccb06d8<Node object at 0x7f4aeccb06d8: 'None' (None)>
Edge object at 0x7f4aeccb07f0 (139959777036272, Length=None)
   
[Length]
       
None
   
[Tail Node]
       
Node object at 0x7f4aeccb06d8<Node object at 0x7f4aeccb06d8: 'None' (None)>
   
[Head Node]
       
Node object at 0x7f4aeccb07b8<Node object at 0x7f4aeccb07b8: 'None' (<Taxon 0x7f4aeccb0828 'a'>)>




Reply all
Reply to author
Forward
0 new messages