Introduction

The treeio package supports parsing various phylogenetic tree file formats including software outputs that contain evolutionary evidences. Some of the formats are just log file (e.g. PAML and r8s outputs), while some of the others are non-standard formats (e.g. BEAST and MrBayes outputs that introduce square bracket, which was reserved to store comment in standard Nexus format, to store inferences). With treeio, we are now able to parse these files to extract phylogenetic tree and map associated data on the tree structure. Exporting tree structure is easy, users can use as.phyo method defined treeio to convert treedata object to phylo object then using write.tree or write.nexus implemented in ape package (Paradis, Claude, and Strimmer 2004) to export the tree structure as Newick text or Nexus file. This is quite useful for converting non-standard formats to standard format and for extracting tree from software outputs, such as log file.

However, exporting tree with associated data is still challenging. These associated data can be parsed from analysis programs or obtained from external sources (e.g. phenotypic data, experimental data and clinical data). The major obstacle here is that there is no standard format that designed for storing tree with data. NeXML (Vos et al. 2012) maybe the most flexible format, however it is currently not widely supported. Most of the analysis programs in this field rely extensively on Newick string and Nexus format. In my opinion, although BEAST Nexus format1 may not be the best solution, it is currently a good approach for storing heterogeneous associated data. The beauty of the format is that all the annotate elements are stored within square bracket, which is reserved for comments. So that the file can be parsed as standard Nexus by ignoring annotate elements and existing programs should be able to read them.

Exporting tree data to BEAST Nexus format

Exporting/converting software output

The treeio package provides write.beast to export treedata object as BEAST Nexus file (Bouckaert et al. 2014). With treeio, it is easy to convert software output to BEAST format if the output can be parsed by treeio. For example, we can convert NHX file to BEAST file and use NHX tags to color the tree using FigTree2 or convert CODEML output and use dN/dS, dN or dS to color the tree in FigTree.

#NEXUS
[R-package treeio, Mon Aug 13 21:27:01 2018]

BEGIN TAXA;
    DIMENSIONS NTAX = 16;
    TAXLABELS
        Prayidae_D27SS7@2825365
        Kephyes_ovata@2606431
        Chuniphyes_multidentata@1277217
        Apolemia_sp_@1353964
        Bargmannia_amoena@263997
        Bargmannia_elongata@946788
        Physonect_sp_@2066767
        Stephalia_dilata@2960089
        Frillagalma_vityazi@1155031
        Resomia_ornicephala@3111757
        Lychnagalma_utricularia@2253871
        Nanomia_bijuga@717864
        Cordagalma_sp_@1525873
        Rhizophysa_filiformis@3073669
        Hydra_magnipapillata@52244
        Ectopleura_larynx@3556167
    ;
END;
BEGIN TREES;
    TRANSLATE
        1   Prayidae_D27SS7@2825365,
        2   Kephyes_ovata@2606431,
        3   Chuniphyes_multidentata@1277217,
        4   Apolemia_sp_@1353964,
        5   Bargmannia_amoena@263997,
        6   Bargmannia_elongata@946788,
        7   Physonect_sp_@2066767,
        8   Stephalia_dilata@2960089,
        9   Frillagalma_vityazi@1155031,
        10  Resomia_ornicephala@3111757,
        11  Lychnagalma_utricularia@2253871,
        12  Nanomia_bijuga@717864,
        13  Cordagalma_sp_@1525873,
        14  Rhizophysa_filiformis@3073669,
        15  Hydra_magnipapillata@52244,
        16  Ectopleura_larynx@3556167
    ;
    TREE * UNTITLED = [&R] (((1[&Ev=S,S=58,ND=0]:0.0682841,(2[&Ev=S,S=69,ND=1]:0.0193941,3[&Ev=S,S=70,ND=2]:0.0121378)[&Ev=S,S=60,ND=3]:0.0217782)[&Ev=S,S=36,ND=4]:0.0607598,((4[&Ev=S,S=31,ND=9]:0.11832,(((5[&Ev=S,S=37,ND=10]:0.0144549,6[&Ev=S,S=38,ND=11]:0.0149723)[&Ev=S,S=33,ND=12]:0.0925388,7[&Ev=S,S=61,ND=13]:0.077429)[&Ev=S,S=24,ND=14]:0.0274637,(8[&Ev=S,S=52,ND=15]:0.0761163,((9[&Ev=S,S=53,ND=16]:0.0906068,10[&Ev=S,S=54,ND=17]:1e-06)[&Ev=S,S=45,ND=18]:1e-06,((11[&Ev=S,S=65,ND=19]:0.120851,12[&Ev=S,S=71,ND=20]:0.133939)[&Ev=S,S=56,ND=21]:1e-06,13[&Ev=S,S=64,ND=22]:0.0693814)[&Ev=S,S=46,ND=23]:1e-06)[&Ev=S,S=40,ND=24]:0.0333823)[&Ev=S,S=35,ND=25]:1e-06)[&Ev=D,S=24,ND=26]:0.0431861)[&Ev=S,S=19,ND=27]:1e-06,14[&Ev=S,S=26,ND=28]:0.22283)[&Ev=S,S=17,ND=29]:0.0292362)[&Ev=D,S=17,ND=8]:0.185603,(15[&Ev=S,S=16,ND=5]:0.0621782,16[&Ev=S,S=15,ND=6]:0.332505)[&Ev=S,S=12,ND=7]:0.185603)[&Ev=S,S=9,ND=30];
END;

#NEXUS
[R-package treeio, Mon Aug 13 21:27:01 2018]

BEGIN TAXA;
    DIMENSIONS NTAX = 15;
    TAXLABELS
        A
        B
        C
        D
        E
        F
        G
        H
        I
        J
        K
        L
        M
        N
        O
    ;
END;
BEGIN TREES;
    TRANSLATE
        1   A,
        2   B,
        3   C,
        4   D,
        5   E,
        6   F,
        7   G,
        8   H,
        9   I,
        10  J,
        11  K,
        12  L,
        13  M,
        14  N,
        15  O
    ;
    TREE * UNTITLED = [&U] (11[&t=0.082,N=1514.9,S=633.1,dN_vs_dS=0.0224,dN=0.002,dS=0.0878,N_x_dN=3,S_x_dS=55.6]:0.081785,14[&t=0.062,N=1514.9,S=633.1,dN_vs_dS=0.0095,dN=7e-04,dS=0.0689,N_x_dN=1,S_x_dS=43.6]:0.062341,(4[&t=0.082,N=1514.9,S=633.1,dN_vs_dS=0.0385,dN=0.0033,dS=0.0849,N_x_dN=5,S_x_dS=53.8]:0.082021,(12[&t=0.006,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.0062,N_x_dN=0,S_x_dS=3.9]:0.005508,(10[&t=0.014,N=1514.9,S=633.1,dN_vs_dS=0.0457,dN=7e-04,dS=0.0143,N_x_dN=1,S_x_dS=9]:0.013996,(7[&t=0.046,N=1514.9,S=633.1,dN_vs_dS=0.1621,dN=0.006,dS=0.0373,N_x_dN=9.2,S_x_dS=23.6]:0.045746,((3[&t=0.028,N=1514.9,S=633.1,dN_vs_dS=0.0461,dN=0.0013,dS=0.0282,N_x_dN=2,S_x_dS=17.9]:0.02773,(5[&t=0.031,N=1514.9,S=633.1,dN_vs_dS=0.0641,dN=0.002,dS=0.0305,N_x_dN=3,S_x_dS=19.3]:0.031104,15[&t=0.048,N=1514.9,S=633.1,dN_vs_dS=0.0538,dN=0.0026,dS=0.0485,N_x_dN=4,S_x_dS=30.7]:0.048389)23[&t=0.008,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.0094,N_x_dN=0,S_x_dS=6]:0.008328)22[&t=0.016,N=1514.9,S=633.1,dN_vs_dS=0.0395,dN=7e-04,dS=0.0165,N_x_dN=1,S_x_dS=10.4]:0.015959,(8[&t=0.021,N=1514.9,S=633.1,dN_vs_dS=0.1028,dN=0.002,dS=0.0191,N_x_dN=3,S_x_dS=12.1]:0.021007,(9[&t=0.015,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.0167,N_x_dN=0,S_x_dS=10.6]:0.014739,(2[&t=0.032,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.0358,N_x_dN=0,S_x_dS=22.7]:0.031643,(1[&t=0.01,N=1514.9,S=633.1,dN_vs_dS=0.0646,dN=7e-04,dS=0.0101,N_x_dN=1,S_x_dS=6.4]:0.01034,(6[&t=0.007,N=1514.9,S=633.1,dN_vs_dS=0.298,dN=0.0013,dS=0.0044,N_x_dN=2,S_x_dS=2.8]:0.006649,13[&t=0.009,N=1514.9,S=633.1,dN_vs_dS=0.0738,dN=7e-04,dS=0.0088,N_x_dN=1,S_x_dS=5.6]:0.009195)28[&t=0.028,N=1514.9,S=633.1,dN_vs_dS=0.0453,dN=0.0013,dS=0.0289,N_x_dN=2,S_x_dS=18.3]:0.028303)27[&t=0.008,N=1514.9,S=633.1,dN_vs_dS=0.0863,dN=7e-04,dS=0.0076,N_x_dN=1,S_x_dS=4.8]:0.008072)26[&t=0.003,N=1514.9,S=633.1,dN_vs_dS=1.5591,dN=0.0013,dS=8e-04,N_x_dN=2,S_x_dS=0.5]:0.0035)25[&t=0.02,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.023,N_x_dN=0,S_x_dS=14.6]:0.020359)24[&t=0.001,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=6e-04,N_x_dN=0,S_x_dS=0.4]:0.000555)21[&t=0.024,N=1514.9,S=633.1,dN_vs_dS=0.0549,dN=0.0013,dS=0.0237,N_x_dN=2,S_x_dS=15]:0.023675)20[&t=0.046,N=1514.9,S=633.1,dN_vs_dS=0.0419,dN=0.002,dS=0.047,N_x_dN=3,S_x_dS=29.8]:0.045745)19[&t=0.015,N=1514.9,S=633.1,dN_vs_dS=1e-04,dN=0,dS=0.0166,N_x_dN=0,S_x_dS=10.5]:0.014684)18[&t=0.059,N=1514.9,S=633.1,dN_vs_dS=0.0964,dN=0.0053,dS=0.0545,N_x_dN=8,S_x_dS=34.5]:0.059308)17[&t=0.232,N=1514.9,S=633.1,dN_vs_dS=0.0129,dN=0.0033,dS=0.2541,N_x_dN=5,S_x_dS=160.9]:0.231628)16;
END;

Combining tree with external data

Using the utilities provided by treeio, it is easy to link external data onto the corresponding phylogeny. The write.beast function enable users to combine the tree with external data to a single tree file.

[1] "(((Prayidae_D27SS7@2825365:0.0682841,(Kephyes_ovata@2606431:0.0193941,Chuniphyes_multidentata@1277217:0.0121378):0.0217782):0.0607598,((Apolemia_sp_@1353964:0.11832,(((Bargmannia_amoena@263997:0.0144549,Bargmannia_elongata@946788:0.0149723):0.0925388,Physonect_sp_@2066767:0.077429):0.0274637,(Stephalia_dilata@2960089:0.0761163,((Frillagalma_vityazi@1155031:0.0906068,Resomia_ornicephala@3111757:1e-06):1e-06,((Lychnagalma_utricularia@2253871:0.120851,Nanomia_bijuga@717864:0.133939):1e-06,Cordagalma_sp_@1525873:0.0693814):1e-06):0.0333823):1e-06):0.0431861):1e-06,Rhizophysa_filiformis@3073669:0.22283):0.0292362):0.185603,(Hydra_magnipapillata@52244:0.0621782,Ectopleura_larynx@3556167:0.332505):0.185603);"
#NEXUS
[R-package treeio, Mon Aug 13 21:27:01 2018]

BEGIN TAXA;
    DIMENSIONS NTAX = 16;
    TAXLABELS
        Prayidae_D27SS7@2825365
        Kephyes_ovata@2606431
        Chuniphyes_multidentata@1277217
        Apolemia_sp_@1353964
        Bargmannia_amoena@263997
        Bargmannia_elongata@946788
        Physonect_sp_@2066767
        Stephalia_dilata@2960089
        Frillagalma_vityazi@1155031
        Resomia_ornicephala@3111757
        Lychnagalma_utricularia@2253871
        Nanomia_bijuga@717864
        Cordagalma_sp_@1525873
        Rhizophysa_filiformis@3073669
        Hydra_magnipapillata@52244
        Ectopleura_larynx@3556167
    ;
END;
BEGIN TREES;
    TRANSLATE
        1   Prayidae_D27SS7@2825365,
        2   Kephyes_ovata@2606431,
        3   Chuniphyes_multidentata@1277217,
        4   Apolemia_sp_@1353964,
        5   Bargmannia_amoena@263997,
        6   Bargmannia_elongata@946788,
        7   Physonect_sp_@2066767,
        8   Stephalia_dilata@2960089,
        9   Frillagalma_vityazi@1155031,
        10  Resomia_ornicephala@3111757,
        11  Lychnagalma_utricularia@2253871,
        12  Nanomia_bijuga@717864,
        13  Cordagalma_sp_@1525873,
        14  Rhizophysa_filiformis@3073669,
        15  Hydra_magnipapillata@52244,
        16  Ectopleura_larynx@3556167
    ;
    TREE * UNTITLED = [&R] (((1[&fake_trait=0.0966299134186797,another_trait=0.647292237961665]:0.0682841,(2[&fake_trait=0.947483989667702,another_trait=0.862706780899316]:0.0193941,3[&fake_trait=-0.275959396586473,another_trait=0.680975269293413]:0.0121378)[&fake_trait=-1.4790928538732,another_trait=0.252711398992687]:0.0217782)[&fake_trait=-1.04620304918213,another_trait=0.798957669176161]:0.0607598,((4[&fake_trait=1.45619665406011,another_trait=0.320004595909268]:0.11832,(((5[&fake_trait=0.170603453237509,another_trait=0.0621879519894719]:0.0144549,6[&fake_trait=-1.34667935507119,another_trait=0.933772765332833]:0.0149723)[&fake_trait=0.0766903407263227,another_trait=0.257852485636249]:0.0925388,7[&fake_trait=-0.36263738301459,another_trait=0.699447236256674]:0.077429)[&fake_trait=0.270459715221035,another_trait=0.49641826050356]:0.0274637,(8[&fake_trait=-0.93391069407322,another_trait=0.636195978615433]:0.0761163,((9[&fake_trait=-0.503501786660506,another_trait=0.604290389223024]:0.0906068,10[&fake_trait=0.180615271163822,another_trait=0.65591481141746]:1e-06)[&fake_trait=1.10792119922326,another_trait=0.617437205510214]:1e-06,((11[&fake_trait=1.34130427334812,another_trait=0.598855313379318]:0.120851,12[&fake_trait=-0.261491820699991,another_trait=0.889982002321631]:0.133939)[&fake_trait=0.246154642100281,another_trait=0.888543640263379]:1e-06,13[&fake_trait=0.136960834599048,another_trait=0.741116506746039]:0.0693814)[&fake_trait=1.95055075279903,another_trait=0.990469428477809]:1e-06)[&fake_trait=-0.0798357428488587,another_trait=0.356383193749934]:0.0333823)[&fake_trait=-0.0486475662032578,another_trait=0.45021412265487]:1e-06)[&fake_trait=0.700142987183249,another_trait=0.0867538603488356]:0.0431861)[&fake_trait=1.12996253721286,another_trait=0.588001284515485]:1e-06,14[&fake_trait=0.201436776909679,another_trait=0.228193773888052]:0.22283)[&fake_trait=0.198396378515684,another_trait=0.254944651620463]:0.0292362)[&fake_trait=0.236338670113736,another_trait=0.892709236126393]:0.185603,(15[&fake_trait=-2.55312502159514,another_trait=0.356084818951786]:0.0621782,16[&fake_trait=1.28109838301749,another_trait=0.0802781076636165]:0.332505)[&fake_trait=-2.05656381827616,another_trait=0.502227429300547]:0.185603)[&fake_trait=1.05068617529501,another_trait=0.922268101712689];
END;

Merging tree data from different sources

Not only Newick tree text can be combined with associated data, but also tree data obtained from software output can be combined with external data, as well as different tree objects can be merged together. For details, please refer to the Importer vignette.

## 'treedata' S4 object that stored information of
##  '/tmp/Rtmpc0Tpwl/Rinst21857df23c40/treeio/extdata/NHX/phyldog.nhx'.
## 
## ...@ phylo: 
## Phylogenetic tree with 16 tips and 15 internal nodes.
## 
## Tip labels:
##  Prayidae_D27SS7@2825365, Kephyes_ovata@2606431, Chuniphyes_multidentata@1277217, Apolemia_sp_@1353964, Bargmannia_amoena@263997, Bargmannia_elongata@946788, ...
## 
## Rooted; includes branch lengths.
## 
## with the following features available:
##  'Ev',   'S',    'ND',   'fake_trait',   'another_trait'.
## 'treedata' S4 object that stored information of
##  '/tmp/Rtmpc0Tpwl/Rinst21857df23c40/treeio/extdata/NHX/phyldog.nhx'.
## 
## ...@ phylo: 
## Phylogenetic tree with 16 tips and 15 internal nodes.
## 
## Tip labels:
##  Prayidae_D27SS7@2825365, Kephyes_ovata@2606431, Chuniphyes_multidentata@1277217, Apolemia_sp_@1353964, Bargmannia_amoena@263997, Bargmannia_elongata@946788, ...
## 
## Rooted; includes branch lengths.
## 
## with the following features available:
##  'Ev',   'S',    'ND',   'fake_trait',   'another_trait'.
## [1] TRUE

After merging data from different sources, the tree with the associated data can be exported into a single file.

#NEXUS
[R-package treeio, Mon Aug 13 21:27:01 2018]

BEGIN TAXA;
    DIMENSIONS NTAX = 16;
    TAXLABELS
        Prayidae_D27SS7@2825365
        Kephyes_ovata@2606431
        Chuniphyes_multidentata@1277217
        Apolemia_sp_@1353964
        Bargmannia_amoena@263997
        Bargmannia_elongata@946788
        Physonect_sp_@2066767
        Stephalia_dilata@2960089
        Frillagalma_vityazi@1155031
        Resomia_ornicephala@3111757
        Lychnagalma_utricularia@2253871
        Nanomia_bijuga@717864
        Cordagalma_sp_@1525873
        Rhizophysa_filiformis@3073669
        Hydra_magnipapillata@52244
        Ectopleura_larynx@3556167
    ;
END;
BEGIN TREES;
    TRANSLATE
        1   Prayidae_D27SS7@2825365,
        2   Kephyes_ovata@2606431,
        3   Chuniphyes_multidentata@1277217,
        4   Apolemia_sp_@1353964,
        5   Bargmannia_amoena@263997,
        6   Bargmannia_elongata@946788,
        7   Physonect_sp_@2066767,
        8   Stephalia_dilata@2960089,
        9   Frillagalma_vityazi@1155031,
        10  Resomia_ornicephala@3111757,
        11  Lychnagalma_utricularia@2253871,
        12  Nanomia_bijuga@717864,
        13  Cordagalma_sp_@1525873,
        14  Rhizophysa_filiformis@3073669,
        15  Hydra_magnipapillata@52244,
        16  Ectopleura_larynx@3556167
    ;
    TREE * UNTITLED = [&R] (((1[&Ev=S,S=58,ND=0,fake_trait=0.0966299134186797,another_trait=0.647292237961665]:0.0682841,(2[&Ev=S,S=69,ND=1,fake_trait=0.947483989667702,another_trait=0.862706780899316]:0.0193941,3[&Ev=S,S=70,ND=2,fake_trait=-0.275959396586473,another_trait=0.680975269293413]:0.0121378)[&Ev=S,S=60,ND=3,fake_trait=-1.4790928538732,another_trait=0.252711398992687]:0.0217782)[&Ev=S,S=36,ND=4,fake_trait=-1.04620304918213,another_trait=0.798957669176161]:0.0607598,((4[&Ev=S,S=31,ND=9,fake_trait=1.45619665406011,another_trait=0.320004595909268]:0.11832,(((5[&Ev=S,S=37,ND=10,fake_trait=0.170603453237509,another_trait=0.0621879519894719]:0.0144549,6[&Ev=S,S=38,ND=11,fake_trait=-1.34667935507119,another_trait=0.933772765332833]:0.0149723)[&Ev=S,S=33,ND=12,fake_trait=0.0766903407263227,another_trait=0.257852485636249]:0.0925388,7[&Ev=S,S=61,ND=13,fake_trait=-0.36263738301459,another_trait=0.699447236256674]:0.077429)[&Ev=S,S=24,ND=14,fake_trait=0.270459715221035,another_trait=0.49641826050356]:0.0274637,(8[&Ev=S,S=52,ND=15,fake_trait=-0.93391069407322,another_trait=0.636195978615433]:0.0761163,((9[&Ev=S,S=53,ND=16,fake_trait=-0.503501786660506,another_trait=0.604290389223024]:0.0906068,10[&Ev=S,S=54,ND=17,fake_trait=0.180615271163822,another_trait=0.65591481141746]:1e-06)[&Ev=S,S=45,ND=18,fake_trait=1.10792119922326,another_trait=0.617437205510214]:1e-06,((11[&Ev=S,S=65,ND=19,fake_trait=1.34130427334812,another_trait=0.598855313379318]:0.120851,12[&Ev=S,S=71,ND=20,fake_trait=-0.261491820699991,another_trait=0.889982002321631]:0.133939)[&Ev=S,S=56,ND=21,fake_trait=0.246154642100281,another_trait=0.888543640263379]:1e-06,13[&Ev=S,S=64,ND=22,fake_trait=0.136960834599048,another_trait=0.741116506746039]:0.0693814)[&Ev=S,S=46,ND=23,fake_trait=1.95055075279903,another_trait=0.990469428477809]:1e-06)[&Ev=S,S=40,ND=24,fake_trait=-0.0798357428488587,another_trait=0.356383193749934]:0.0333823)[&Ev=S,S=35,ND=25,fake_trait=-0.0486475662032578,another_trait=0.45021412265487]:1e-06)[&Ev=D,S=24,ND=26,fake_trait=0.700142987183249,another_trait=0.0867538603488356]:0.0431861)[&Ev=S,S=19,ND=27,fake_trait=1.12996253721286,another_trait=0.588001284515485]:1e-06,14[&Ev=S,S=26,ND=28,fake_trait=0.201436776909679,another_trait=0.228193773888052]:0.22283)[&Ev=S,S=17,ND=29,fake_trait=0.198396378515684,another_trait=0.254944651620463]:0.0292362)[&Ev=D,S=17,ND=8,fake_trait=0.236338670113736,another_trait=0.892709236126393]:0.185603,(15[&Ev=S,S=16,ND=5,fake_trait=-2.55312502159514,another_trait=0.356084818951786]:0.0621782,16[&Ev=S,S=15,ND=6,fake_trait=1.28109838301749,another_trait=0.0802781076636165]:0.332505)[&Ev=S,S=12,ND=7,fake_trait=-2.05656381827616,another_trait=0.502227429300547]:0.185603)[&Ev=S,S=9,ND=30,fake_trait=1.05068617529501,another_trait=0.922268101712689];
END;

The output BEAST Nexus file can be imported into R using the read.beast function and all the associated data can be used to annotate the tree using ggtree (Yu et al. 2017).

## 'treedata' S4 object that stored information of
##  '/tmp/RtmpLnnP56/file22a8203a2de5.tree'.
## 
## ...@ phylo: 
## Phylogenetic tree with 16 tips and 15 internal nodes.
## 
## Tip labels:
##  Prayidae_D27SS7@2825365, Kephyes_ovata@2606431, Chuniphyes_multidentata@1277217, Apolemia_sp_@1353964, Bargmannia_amoena@263997, Bargmannia_elongata@946788, ...
## 
## Rooted; includes branch lengths.
## 
## with the following features available:
##  'Ev',   'ND',   'S',    'another_trait',    'fake_trait'.

Exporting tree data to jtree format

The treeio package provides write.beast to export treedata to BEAST Nexus file. This is quite useful to convert file format, combine tree with data and merging tree data from different sources as we demonstrated in Exporting tree data to BEAST Nexus format session. The treeio package also supplies read.beast function to parse output file of write.beast. Although with treeio, the R community has the ability to manipulate BEAST Nexus format and process tree data, there is still lacking library/package for parsing BEAST file in other programming language.

JSON (JavaScript Object Notation)3 is a lightweight data-interchange format and widely supported in almost all modern programming languages. To make it easy to import tree with data in other programming languages, treeio supports exporting tree with data in jtree format, which is JSON-based and can be easy to parse using any languages that supports JSON.

{
    "tree": "(((Prayidae_D27SS7@2825365:0.0682841{1},(Kephyes_ovata@2606431:0.0193941{2},Chuniphyes_multidentata@1277217:0.0121378{3}):0.0217782{20}):0.0607598{19},((Apolemia_sp_@1353964:0.11832{4},(((Bargmannia_amoena@263997:0.0144549{5},Bargmannia_elongata@946788:0.0149723{6}):0.0925388{25},Physonect_sp_@2066767:0.077429{7}):0.0274637{24},(Stephalia_dilata@2960089:0.0761163{8},((Frillagalma_vityazi@1155031:0.0906068{9},Resomia_ornicephala@3111757:1{10}e-06):1{28}e-06,((Lychnagalma_utricularia@2253871:0.120851{11},Nanomia_bijuga@717864:0.133939{12}):1{30}e-06,Cordagalma_sp_@1525873:0.0693814{13}):1{29}e-06):0.0333823{27}):1{26}e-06):0.0431861{23}):1{22}e-06,Rhizophysa_filiformis@3073669:0.22283{14}):0.0292362{21}):0.185603{18},(Hydra_magnipapillata@52244:0.0621782{15},Ectopleura_larynx@3556167:0.332505{16}):0.185603{31}){17};",
    "data":[
  {
    "edge_num": 1,
    "Ev": "S",
    "S": "58",
    "ND": 0,
    "fake_trait": 0.0966,
    "another_trait": 0.6473
  },
  {
    "edge_num": 2,
    "Ev": "S",
    "S": "69",
    "ND": 1,
    "fake_trait": 0.9475,
    "another_trait": 0.8627
  },
  {
    "edge_num": 3,
    "Ev": "S",
    "S": "70",
    "ND": 2,
    "fake_trait": -0.276,
    "another_trait": 0.681
  },
  {
    "edge_num": 4,
    "Ev": "S",
    "S": "31",
    "ND": 9,
    "fake_trait": 1.4562,
    "another_trait": 0.32
  },
  {
    "edge_num": 5,
    "Ev": "S",
    "S": "37",
    "ND": 10,
    "fake_trait": 0.1706,
    "another_trait": 0.0622
  },
  {
    "edge_num": 6,
    "Ev": "S",
    "S": "38",
    "ND": 11,
    "fake_trait": -1.3467,
    "another_trait": 0.9338
  },
  {
    "edge_num": 7,
    "Ev": "S",
    "S": "61",
    "ND": 13,
    "fake_trait": -0.3626,
    "another_trait": 0.6994
  },
  {
    "edge_num": 8,
    "Ev": "S",
    "S": "52",
    "ND": 15,
    "fake_trait": -0.9339,
    "another_trait": 0.6362
  },
  {
    "edge_num": 9,
    "Ev": "S",
    "S": "53",
    "ND": 16,
    "fake_trait": -0.5035,
    "another_trait": 0.6043
  },
  {
    "edge_num": 10,
    "Ev": "S",
    "S": "54",
    "ND": 17,
    "fake_trait": 0.1806,
    "another_trait": 0.6559
  },
  {
    "edge_num": 11,
    "Ev": "S",
    "S": "65",
    "ND": 19,
    "fake_trait": 1.3413,
    "another_trait": 0.5989
  },
  {
    "edge_num": 12,
    "Ev": "S",
    "S": "71",
    "ND": 20,
    "fake_trait": -0.2615,
    "another_trait": 0.89
  },
  {
    "edge_num": 13,
    "Ev": "S",
    "S": "64",
    "ND": 22,
    "fake_trait": 0.137,
    "another_trait": 0.7411
  },
  {
    "edge_num": 14,
    "Ev": "S",
    "S": "26",
    "ND": 28,
    "fake_trait": 0.2014,
    "another_trait": 0.2282
  },
  {
    "edge_num": 15,
    "Ev": "S",
    "S": "16",
    "ND": 5,
    "fake_trait": -2.5531,
    "another_trait": 0.3561
  },
  {
    "edge_num": 16,
    "Ev": "S",
    "S": "15",
    "ND": 6,
    "fake_trait": 1.2811,
    "another_trait": 0.0803
  },
  {
    "edge_num": 17,
    "Ev": "S",
    "S": "9",
    "ND": 30,
    "fake_trait": 1.0507,
    "another_trait": 0.9223
  },
  {
    "edge_num": 18,
    "Ev": "D",
    "S": "17",
    "ND": 8,
    "fake_trait": 0.2363,
    "another_trait": 0.8927
  },
  {
    "edge_num": 19,
    "Ev": "S",
    "S": "36",
    "ND": 4,
    "fake_trait": -1.0462,
    "another_trait": 0.799
  },
  {
    "edge_num": 20,
    "Ev": "S",
    "S": "60",
    "ND": 3,
    "fake_trait": -1.4791,
    "another_trait": 0.2527
  },
  {
    "edge_num": 21,
    "Ev": "S",
    "S": "17",
    "ND": 29,
    "fake_trait": 0.1984,
    "another_trait": 0.2549
  },
  {
    "edge_num": 22,
    "Ev": "S",
    "S": "19",
    "ND": 27,
    "fake_trait": 1.13,
    "another_trait": 0.588
  },
  {
    "edge_num": 23,
    "Ev": "D",
    "S": "24",
    "ND": 26,
    "fake_trait": 0.7001,
    "another_trait": 0.0868
  },
  {
    "edge_num": 24,
    "Ev": "S",
    "S": "24",
    "ND": 14,
    "fake_trait": 0.2705,
    "another_trait": 0.4964
  },
  {
    "edge_num": 25,
    "Ev": "S",
    "S": "33",
    "ND": 12,
    "fake_trait": 0.0767,
    "another_trait": 0.2579
  },
  {
    "edge_num": 26,
    "Ev": "S",
    "S": "35",
    "ND": 25,
    "fake_trait": -0.0486,
    "another_trait": 0.4502
  },
  {
    "edge_num": 27,
    "Ev": "S",
    "S": "40",
    "ND": 24,
    "fake_trait": -0.0798,
    "another_trait": 0.3564
  },
  {
    "edge_num": 28,
    "Ev": "S",
    "S": "45",
    "ND": 18,
    "fake_trait": 1.1079,
    "another_trait": 0.6174
  },
  {
    "edge_num": 29,
    "Ev": "S",
    "S": "46",
    "ND": 23,
    "fake_trait": 1.9506,
    "another_trait": 0.9905
  },
  {
    "edge_num": 30,
    "Ev": "S",
    "S": "56",
    "ND": 21,
    "fake_trait": 0.2462,
    "another_trait": 0.8885
  },
  {
    "edge_num": 31,
    "Ev": "S",
    "S": "12",
    "ND": 7,
    "fake_trait": -2.0566,
    "another_trait": 0.5022
  }
],
    "metadata": {"info": "R-package treeio", "data": "Mon Aug 13 21:27:01 2018"}
}

The jtree format is based on JSON and can be parsed using JSON parser.

$tree
[1] "(((Prayidae_D27SS7@2825365:0.0682841{1},(Kephyes_ovata@2606431:0.0193941{2},Chuniphyes_multidentata@1277217:0.0121378{3}):0.0217782{20}):0.0607598{19},((Apolemia_sp_@1353964:0.11832{4},(((Bargmannia_amoena@263997:0.0144549{5},Bargmannia_elongata@946788:0.0149723{6}):0.0925388{25},Physonect_sp_@2066767:0.077429{7}):0.0274637{24},(Stephalia_dilata@2960089:0.0761163{8},((Frillagalma_vityazi@1155031:0.0906068{9},Resomia_ornicephala@3111757:1{10}e-06):1{28}e-06,((Lychnagalma_utricularia@2253871:0.120851{11},Nanomia_bijuga@717864:0.133939{12}):1{30}e-06,Cordagalma_sp_@1525873:0.0693814{13}):1{29}e-06):0.0333823{27}):1{26}e-06):0.0431861{23}):1{22}e-06,Rhizophysa_filiformis@3073669:0.22283{14}):0.0292362{21}):0.185603{18},(Hydra_magnipapillata@52244:0.0621782{15},Ectopleura_larynx@3556167:0.332505{16}):0.185603{31}){17};"

$data
   edge_num Ev  S ND fake_trait another_trait
1         1  S 58  0     0.0966        0.6473
2         2  S 69  1     0.9475        0.8627
3         3  S 70  2    -0.2760        0.6810
4         4  S 31  9     1.4562        0.3200
5         5  S 37 10     0.1706        0.0622
6         6  S 38 11    -1.3467        0.9338
7         7  S 61 13    -0.3626        0.6994
8         8  S 52 15    -0.9339        0.6362
9         9  S 53 16    -0.5035        0.6043
10       10  S 54 17     0.1806        0.6559
11       11  S 65 19     1.3413        0.5989
12       12  S 71 20    -0.2615        0.8900
13       13  S 64 22     0.1370        0.7411
14       14  S 26 28     0.2014        0.2282
15       15  S 16  5    -2.5531        0.3561
16       16  S 15  6     1.2811        0.0803
17       17  S  9 30     1.0507        0.9223
18       18  D 17  8     0.2363        0.8927
19       19  S 36  4    -1.0462        0.7990
20       20  S 60  3    -1.4791        0.2527
21       21  S 17 29     0.1984        0.2549
22       22  S 19 27     1.1300        0.5880
23       23  D 24 26     0.7001        0.0868
24       24  S 24 14     0.2705        0.4964
25       25  S 33 12     0.0767        0.2579
26       26  S 35 25    -0.0486        0.4502
27       27  S 40 24    -0.0798        0.3564
28       28  S 45 18     1.1079        0.6174
29       29  S 46 23     1.9506        0.9905
30       30  S 56 21     0.2462        0.8885
31       31  S 12  7    -2.0566        0.5022

$metadata
$metadata$info
[1] "R-package treeio"

$metadata$data
[1] "Mon Aug 13 21:27:01 2018"

The jtree file can be directly imported as a treedata object using read.jtree provided also in treeio package.

## 'treedata' S4 object that stored information of
##  '/tmp/RtmpLnnP56/file22a86428cfb8.jtree'.
## 
## ...@ phylo: 
## Phylogenetic tree with 16 tips and 15 internal nodes.
## 
## Tip labels:
##  Prayidae_D27SS7@2825365, Kephyes_ovata@2606431, Chuniphyes_multidentata@1277217, Apolemia_sp_@1353964, Bargmannia_amoena@263997, Bargmannia_elongata@946788, ...
## 
## Rooted; includes branch lengths.
## 
## with the following features available:
##  'Ev',   'S',    'ND',   'fake_trait',   'another_trait'.

References

Bouckaert, Remco, Joseph Heled, Denise Kühnert, Tim Vaughan, Chieh-Hsi Wu, Dong Xie, Marc A. Suchard, Andrew Rambaut, and Alexei J. Drummond. 2014. “BEAST 2: A Software Platform for Bayesian Evolutionary Analysis.” PLoS Comput Biol 10 (4):e1003537. https://doi.org/10.1371/journal.pcbi.1003537.

Paradis, Emmanuel, Julien Claude, and Korbinian Strimmer. 2004. “APE: Analyses of Phylogenetics and Evolution in R Language.” Bioinformatics 20 (2):289–90. https://doi.org/10.1093/bioinformatics/btg412.

Vos, Rutger A., James P. Balhoff, Jason A. Caravas, Mark T. Holder, Hilmar Lapp, Wayne P. Maddison, Peter E. Midford, et al. 2012. “NeXML: Rich, Extensible, and Verifiable Representation of Comparative Data and Metadata.” Systematic Biology 61 (4):675–89. https://doi.org/10.1093/sysbio/sys025.

Yu, Guangchuang, David K. Smith, Huachen Zhu, Yi Guan, and Tommy Tsan-Yuk Lam. 2017. “Ggtree: An R Package for Visualization and Annotation of Phylogenetic Trees with Their Covariates and Other Associated Data.” Methods in Ecology and Evolution 8 (1):28–36. https://doi.org/10.1111/2041-210X.12628.


  1. http://beast.community/nexus_metacomments

  2. http://beast.community/figtree

  3. https://www.json.org/