[Biopython] Nexus format dialects in Biopython

Eric Talevich eric.talevich at gmail.com
Mon Sep 8 02:06:36 UTC 2014


On Sun, Sep 7, 2014 at 2:25 AM, Lior Glick <liorglic at mail.tau.ac.il> wrote:

> Dear list members,
> I'm trying to convert newick formated trees to the nexus format. I'm doing
> this since I'd like to use the trees as input for a software (BayesTraits
> V2) which will only accept nexus trees. It also required that the file is
> in a specific dialect of nexus in which the taxa names are translated to
> numbers, and the trees themselves only contain these numbers.
>
> I tried
> Bio.Phylo.convert(newick_trees_file,'newick',nexus_trees_file,'nexus'), but
> it produces nexus files with the taxa names within the trees. I couldn't
> find out a way to control the specific dialect of nexus to be used. Looking
> into the Bio.Nexus module wasn't very helpful either.
>
> Does anybody know of a way to do that?
>
> Thanks,
> Lior
>
>
Hi Lior,

While the standard Nexus format does support separately mapping taxon names
to integer identifiers and using those integers in the Newick-encoded
trees, I don't know if it's possible to do this automatically in Bio.Nexus.
Bio.Phylo doesn't directly support it.

You could try doing the mapping yourself (untested):

tree = Phylo.read(newick_trees_file, "newick")

# To label terminal nodes only:
names_to_ints = dict((name, str(i)) for i, name in
enumerate(tree.get_terminals()))
for clade in tree.find_clades(terminal=True):
    clade.name = names_to_ints[clade.name]

# To label internal nodes too:
names_to_ints = dict((name, i) for i, name in enumerate(tree.find_clades()))
for clade in tree.find_clades():
    clade.name = names_to_ints[clade.name]

Phylo.write(tree, nexus_trees_file, "nexus")

If BayesTraits requires a separate "taxa" block, you might need to use
another program to do the conversion, or just add the block manually if you
only need to run one or a few trees.

Best,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.open-bio.org/pipermail/biopython/attachments/20140907/71d826d7/attachment.html>


More information about the Biopython mailing list