convert nexus to newick

1,913 views
Skip to first unread message

monkey79

unread,
Mar 29, 2012, 7:51:02 AM3/29/12
to The ETE toolkit
Hi there,

I need to convert my trees from nexus format to newick format. Is that
possible with ete2?

thanks!

Daniel Pérez Martín

unread,
Mar 29, 2012, 11:56:48 AM3/29/12
to eteto...@googlegroups.com
Having found nothing in ETE2 docs, you might want to code a conversion utility using both ETE2 and a python library for nexus such as python-nexus:

Regards,
Daniel.

2012/3/29 monkey79 <barbara...@gmail.com>

Jaime Huerta Cepas

unread,
Mar 30, 2012, 5:08:50 AM3/30/12
to eteto...@googlegroups.com
There is no built-in nexus parser in ETE since Nexus (in the case of trees) seems to be a mere wrapper of the newick format (http://www.bioperl.org/wiki/Nexus_tree_format).

You can use any parser such as the one suggested by Daniel to extract the newick and then load it using ETE. Alternatively, you can just parse the Nexus file to extract lines starting with "tree". This is a simple example using regular expressions:

import re
from ete2 import Tree

nexus = """
#NEXUS
begin trees;
tree 'name' =(1:0.212481,8:0.297838,(9:0.222729,((6:0.201563,7:0.194547):0.282035,(4:1.146091,(3:1.008881,
(10:0.384105,(2:0.235682,5:0.353432):0.323680):0.103875):0.413540):0.254687):0.095341):0.079254):0.000000;

tree 'tree2' =(A:0.212481,B:0.297838,(C:0.222729,((D:0.201563,7:0.194547):0.282035,(4:1.146091,(3:1.008881,
(10:0.384105,(2:0.235682,5:0.353432):0.323680):0.103875):0.413540):0.254687):0.095341):0.079254):0.000000;


end;
"""

match  = re.search("begin trees;.+end;", nexus, re.MULTILINE | re.DOTALL)
if match:
    tree_block =  match.group()
    for match in re.finditer("\s*tree\s+([^=]+)=([^;]+;)", tree_block, re.MULTILINE | re.DOTALL):
        tree_name = match.groups()[0].strip()
        nw = match.groups()[1]
        t = Tree(nw)
        print t
   
If any of you has something more elaborated, I will be happy to include it in ETE.

Jaime

2012/3/29 Daniel Pérez Martín <dpe...@gmail.com>

monkey79

unread,
Apr 2, 2012, 3:19:26 AM4/2/12
to eteto...@googlegroups.com
Hey Jaime,

here is an example of what my nexus tree files likes. it is the result of a bayesian analysis. I have got about 1700 trees and I am trying to figure out how resolve into a newick format in order to use ete2. Any other suggestion?

thanks


#NEXUS

begin taxa;
dimensions ntax=5;
taxlabels
ENSTRUG00000000417
T18D3.7
ENSGACG00000013730
ENSLACG00000012925
ENSLACG00000003805
;
end;
begin trees;
translate
1 ENSTRUG00000000417,
2 T18D3.7,
3 ENSGACG00000013730,
4 ENSLACG00000012925,
5 ENSLACG00000003805
;
   tree con_all_compat = [&U] (ENSTRUG00000000417[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"]:1.418146859482858e-01[&length_mean=1.453808394263178e-01,length_median=1.418146859482858e-01,length_95%HPD={4.394102925423538e-02,2.398851568109936e-01}],ENSGACG00000013730[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"]:7.103231066152176e-01[&length_mean=7.193836401649210e-01,length_median=7.103231066152176e-01,length_95%HPD={5.013425489412863e-01,9.651804467297473e-01}],((T18D3.7[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"]:9.105574486435167e-01[&length_mean=9.257404980325880e-01,length_median=9.105574486435167e-01,length_95%HPD={5.933291691904896e-01,1.277730934852635e+00}],ENSLACG00000012925[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"]:5.505290596097889e-01[&length_mean=5.614284096694910e-01,length_median=5.505290596097889e-01,length_95%HPD={3.098368159543265e-01,8.231827588050069e-01}])[&prob=9.504132231404958e-01,prob_stddev=2.262138462873564e-03,prob_range={9.488136496934151e-01,9.520127965875766e-01},prob(percent)="95",prob+-sd="95+-0"]:2.232700123422474e-01[&length_mean=2.306815188221150e-01,length_median=2.232700123422474e-01,length_95%HPD={3.964294057691813e-02,4.360923788012644e-01}],ENSLACG00000003805[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"]:8.688248742304949e-02[&length_mean=9.037320533212091e-02,length_median=8.688248742304949e-02,length_95%HPD={6.775268382584457e-03,1.727273852136540e-01}])[&prob=4.964009597440682e-01,prob_stddev=5.655346157183772e-03,prob_range={4.924020261263663e-01,5.003998933617702e-01},prob(percent)="50",prob+-sd="50+-1"]:6.385319910354678e-02[&length_mean=7.043041817985121e-02,length_median=6.385319910354678e-02,length_95%HPD={1.049211220468756e-05,1.546138043511682e-01}])[&prob=1.000000000000000e+00,prob_stddev=0.000000000000000e+00,prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)="100",prob+-sd="100+-0"][&length_mean=0.000000000000000e+00,length_median=0.000000000000000e+00,length_95%HPD={0.000000000000000e+00,0.000000000000000e+00}];
end;

Jaime Huerta Cepas

unread,
Apr 2, 2012, 1:58:06 PM4/2/12
to eteto...@googlegroups.com
It doesn't look like a standard newick format at all :). You will probably need some regular-expression-magic to adapt the format to the extended newick and conserve the extra node info. 
If you just want the topology of the tree, the attached script seems to work with your example. 

Which program is generating the newick tree? 
nexus.txt
nexus.py

monkey79

unread,
Apr 4, 2012, 4:21:15 AM4/4/12
to eteto...@googlegroups.com
it is Mr.bayes

thanks Jaime

banta....@gmail.com

unread,
Jul 19, 2020, 9:41:05 AM7/19/20
to The ETE toolkit
Hello,

I ran into this post trying to solve the same problem. I figured it out! You need to use a program called TreeGraph 2 to perform the conversion. It's the only program (besides FigTree, which won't output the node labels) that can read Mr. Bayes output files properly. I made a tutorial detailing how to do the conversion here:

it is Mr.bayes

Daniel.


2012/3/29 monkey79 <barbar...@gmail.com>
Reply all
Reply to author
Forward
0 new messages