change leaf name

341 views
Skip to first unread message

yann.b...@dpes.gu.se

unread,
Nov 21, 2014, 7:25:51 PM11/21/14
to dendrop...@googlegroups.com
Hi,
I have been using dendropy to make very complex analyses and there is a very simple task I can't seem to be able to do.

I want to find the leaf node that bear a given label (this bit is easy using leaf_iter and checking if node.label matches my request or simply with find_node_with_taxon_label).
But then?
create a taxon object and use it to replace the current taxon associated with the leaf node?
I tried to create a taxon object and set the name and fail miserably.
please help

Cheers,
Yann

Jeet Sukumaran

unread,
Nov 22, 2014, 1:37:25 AM11/22/14
to dendrop...@googlegroups.com
Nodes can have both labels and taxa associated with them, and the label
of a node is independent of the label of the taxon object associated
with the node.

You need to establish first if you are dealing with node labels or node
taxon labels and what you want to change.

With respect to what you are changing, you need to be clear if you are
changing to label of a taxon (in which case it will be changed
throughout all trees), or changing the taxon itself (in which case it
will be changed only in one specific tree).

Posting some sample code would probably make things easier.

-- jeet

On 11/21/14, 7:25 PM, yann.b...@dpes.gu.se wrote:
> Hi,
> I have been using dendropy to make very complex analyses and there is a
> very simple task I can't seem to be able to do.
>
> I want to find the leaf node that bear a given label (this bit is easy
> usingleaf_iter and checking if node.label matches my
> request<https://pythonhosted.org/DendroPy/library/tree.html#dendropy.dataobject.tree.Tree.leaf_iter>
> or simply with find_node_with_taxon_label).
> But then?
> create a taxon object and use it to replace the current taxon associated
> with the leaf node?
> I tried to create a taxon object and set the name and fail miserably.
> please help
>
> Cheers,
> Yann
>
> --
> 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
> <mailto:dendropy-user...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--



--------------------------------------
Jeet Sukumaran
--------------------------------------
jeetsu...@gmail.com
--------------------------------------
Blog/Personal Pages:
http://jeetworks.org/
GitHub Repositories:
http://github.com/jeetsukumaran
Photographs (as stream):
http://www.flickr.com/photos/jeetsukumaran/
Photographs (by galleries):
http://www.flickr.com/photos/jeetsukumaran/sets/
--------------------------------------

yann.b...@dpes.gu.se

unread,
Nov 22, 2014, 8:28:59 AM11/22/14
to dendrop...@googlegroups.com
Thanks Jeet for your prompt reply,
I do understand the theory behind node.taxon and Taxon objects but I still can't figure out the practical part.
say I have a tree "((A,B),(C,D))" and want to output "((A,Z),(C,D))"
T = dendropy.Tree.get_from_string("((A,B),(C,D))", schema="newick")
So I start to find node with taxon labelled "B"
node_to_change = T.find_node_with_taxon_label("B")

Then create a new taxon object with the right label and attached it to the targetted node.
new_Taxon = dendropy.Taxon(label="Z")
node_to_change.taxon = new_Taxon

This doesn't seem to do it since I cannot update the tree without getting an index error: IndexError: Taxon with ID 'd24256336' and label 'Z' not found
Additionally I assume I should be modifying/updating manually the taxon_set associated with the tree, shouldn't I?
Cheers,
YAnn

Jeet Sukumaran

unread,
Nov 23, 2014, 1:34:36 AM11/23/14
to dendrop...@googlegroups.com
Well, this code works fine for me (after commenting out the non-code bits).

In fact, all of the following work as well:

```
import dendropy

def f1():
"""
This works, but the problem is that the new taxon is not added
to the taxon namespace.
"""
print("\n## f1")
T = dendropy.Tree.get_from_string("((A,B),(C,D));", schema="newick")
node_to_change = T.find_node_with_taxon_label("B")

# This works, but does not add the new taxon to the taxon namespace
(i.e.
# taxon set)
new_Taxon = dendropy.Taxon(label="Z")
node_to_change.taxon = new_Taxon

print(T.as_string("newick"))
print("Taxon namespace: {}".format(", ".join(taxon.label for taxon
in T.taxon_set)))

def f2():
"""
This works and the taxon is added to the taxon namespace, but the
old taxon
is still there.
"""
print("\n## f2")
T = dendropy.Tree.get_from_string("((A,B),(C,D));", schema="newick")
node_to_change = T.find_node_with_taxon_label("B")

# Add a new taxon to the taxon namespace
new_Taxon = T.taxon_set.new_taxon("Z")
node_to_change.taxon = new_Taxon

print(T.as_string("newick"))
print("Taxon namespace: {}".format(", ".join(taxon.label for taxon
in T.taxon_set)))

def f3():
"""
This works and the taxon is added to the taxon namespace and the
old taxon
is removed. A little tedious though ...
"""
print("\n## f3")
T = dendropy.Tree.get_from_string("((A,B),(C,D));", schema="newick")
node_to_change = T.find_node_with_taxon_label("B")

# delete old taxon and add new one
T.taxon_set.remove(node_to_change.taxon)
new_Taxon = T.taxon_set.new_taxon("Z")

node_to_change.taxon = new_Taxon
print(T.as_string("newick"))
print("Taxon namespace: {}".format(", ".join(taxon.label for taxon
in T.taxon_set)))


def f4():
"""
This is the most efficient if all you want to do is change the
label of the
taxon instead of the the entire taxon. It is also propagates the change
across all data that reference the same taxon namespace.
"""
print("\n## f4")
T = dendropy.Tree.get_from_string("((A,B),(C,D));", schema="newick")
node_to_change = T.find_node_with_taxon_label("B")

# Change label of taxon in-situ
node_to_change.taxon.label = "Z"

print(T.as_string("newick"))
print("Taxon namespace: {}".format(", ".join(taxon.label for taxon
in T.taxon_set)))

f1()
f2()
f3()
f4()
```

On 11/22/14, 8:28 AM, yann.b...@dpes.gu.se wrote:
> say I have a tree "((A,B),(C,D))" and want to output "((A,Z),(C,D))"
> T = dendropy.Tree.get_from_string("((A,B),(C,D))", schema="newick")
> So I start to find node with taxon labelled "B"
> node_to_change = T.find_node_with_taxon_label("B")
>
> Then create a new taxon object with the right label and attached it to
> the targetted node.
> new_Taxon = dendropy.Taxon(label="Z")
> node_to_change.taxon = new_Taxon

yann.b...@dpes.gu.se

unread,
Nov 25, 2014, 2:47:28 AM11/25/14
to dendrop...@googlegroups.com
Thanks so much Jeet.
f4() has my preferences.
It is very simple and propagates the change.
I was stucked with a trying to find my way around f1().
Now I see it I am relieved it is straightforward to do.

I appreciate the time you spent writing a complete answer.
Cheer,
Yann
Reply all
Reply to author
Forward
0 new messages