<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Sep 7, 2014 at 2:25 AM, Lior Glick <span dir="ltr"><<a href="mailto:liorglic@mail.tau.ac.il" target="_blank">liorglic@mail.tau.ac.il</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="rtl"><div dir="ltr">Dear list members,</div><div dir="ltr">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.</div><div dir="ltr"><br></div><div dir="ltr">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.</div><div dir="ltr"><br></div><div dir="ltr">Does anybody know of a way to do that?</div><div dir="ltr"><br></div><div dir="ltr">Thanks,</div><div dir="ltr">Lior<br></div></div><br></blockquote><div><br><div><div><div><div>Hi Lior,<br><br></div>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.<br><br></div>You could try doing the mapping yourself (untested):<br><br></div><div>tree = Phylo.read(newick_trees_file, "newick")<br></div><div><br># To label terminal nodes only:<br></div>names_to_ints = dict((name, str(i)) for i, name in enumerate(tree.get_terminals()))<br></div><div>for clade in tree.find_clades(terminal=True):<br></div><div> <a href="http://clade.name">clade.name</a> = names_to_ints[<a href="http://clade.name">clade.name</a>]<br></div><div><div><br></div># To label internal nodes too:<br>names_to_ints = dict((name, i) for i, name in enumerate(tree.find_clades()))<br></div>for clade in tree.find_clades():<br><div><div> <a href="http://clade.name">clade.name</a> = names_to_ints[<a href="http://clade.name">clade.name</a>]<br></div><div><br></div></div><div>Phylo.write(tree, nexus_trees_file, "nexus")<br></div><div><br></div>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.<br><div><div><br></div><div>Best,<br></div><div>Eric<br></div></div></div></div></div></div>