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, Fri Jan 25 21:41:39 2019]

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, Fri Jan 25 21:41:39 2019]

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);"
Warning: `data_frame()` is deprecated, use `tibble()`.
This warning is displayed once per session.
#NEXUS
[R-package treeio, Fri Jan 25 21:41:39 2019]

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.490212524946883,another_trait=0.0819537870120257]:0.0682841,(2[&fake_trait=0.677130429609548,another_trait=0.359371040482074]:0.0193941,3[&fake_trait=-0.210930792615615,another_trait=0.00831776461564004]:0.0121378)[&fake_trait=1.52863997484518,another_trait=0.699250750243664]:0.0217782)[&fake_trait=-0.830002937708463,another_trait=0.512827570084482]:0.0607598,((4[&fake_trait=0.792660621974838,another_trait=0.79678917909041]:0.11832,(((5[&fake_trait=0.021758970220006,another_trait=0.675522632198408]:0.0144549,6[&fake_trait=0.29852929601607,another_trait=0.928076567593962]:0.0149723)[&fake_trait=0.388160354626184,another_trait=0.519057192839682]:0.0925388,7[&fake_trait=-1.97052205388628,another_trait=0.753102293936536]:0.077429)[&fake_trait=1.15726248012076,another_trait=0.243390332441777]:0.0274637,(8[&fake_trait=-0.198576283620219,another_trait=0.472857442451641]:0.0761163,((9[&fake_trait=-1.08984062435629,another_trait=0.583236675476655]:0.0906068,10[&fake_trait=1.06296534881541,another_trait=0.307273282669485]:1e-06)[&fake_trait=1.03281389825159,another_trait=0.679154373938218]:1e-06,((11[&fake_trait=1.33253482301363,another_trait=0.368328508688137]:0.120851,12[&fake_trait=0.556560169824714,another_trait=0.502828885568306]:0.133939)[&fake_trait=-1.52993654311419,another_trait=0.699205168057233]:1e-06,13[&fake_trait=0.517818501905603,another_trait=0.837434149812907]:0.0693814)[&fake_trait=0.107593036146107,another_trait=0.848443516530097]:1e-06)[&fake_trait=-1.48227842991736,another_trait=0.0549788442440331]:0.0333823)[&fake_trait=-1.58802157126508,another_trait=0.365696684690192]:1e-06)[&fake_trait=-0.834709208415877,another_trait=0.414465259760618]:0.0431861)[&fake_trait=0.924712865518581,another_trait=0.648664495907724]:1e-06,14[&fake_trait=-1.58066896740599,another_trait=0.175387351540849]:0.22283)[&fake_trait=-0.379754758685323,another_trait=0.0288192296866328]:0.0292362)[&fake_trait=0.796414471624802,another_trait=0.764943784102798]:0.185603,(15[&fake_trait=-1.21569149006564,another_trait=0.0801574073266238]:0.0621782,16[&fake_trait=0.716092737131118,another_trait=0.647288323845714]:0.332505)[&fake_trait=-0.611572158411749,another_trait=0.796943147899583]:0.185603)[&fake_trait=0.707588898589443,another_trait=0.805669415043667];
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/Rtmp7QPIfB/Rinst4a8f79c6f322/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/Rtmp7QPIfB/Rinst4a8f79c6f322/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, Fri Jan 25 21:41:39 2019]

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.490212524946883,another_trait=0.0819537870120257]:0.0682841,(2[&Ev=S,S=69,ND=1,fake_trait=0.677130429609548,another_trait=0.359371040482074]:0.0193941,3[&Ev=S,S=70,ND=2,fake_trait=-0.210930792615615,another_trait=0.00831776461564004]:0.0121378)[&Ev=S,S=60,ND=3,fake_trait=1.52863997484518,another_trait=0.699250750243664]:0.0217782)[&Ev=S,S=36,ND=4,fake_trait=-0.830002937708463,another_trait=0.512827570084482]:0.0607598,((4[&Ev=S,S=31,ND=9,fake_trait=0.792660621974838,another_trait=0.79678917909041]:0.11832,(((5[&Ev=S,S=37,ND=10,fake_trait=0.021758970220006,another_trait=0.675522632198408]:0.0144549,6[&Ev=S,S=38,ND=11,fake_trait=0.29852929601607,another_trait=0.928076567593962]:0.0149723)[&Ev=S,S=33,ND=12,fake_trait=0.388160354626184,another_trait=0.519057192839682]:0.0925388,7[&Ev=S,S=61,ND=13,fake_trait=-1.97052205388628,another_trait=0.753102293936536]:0.077429)[&Ev=S,S=24,ND=14,fake_trait=1.15726248012076,another_trait=0.243390332441777]:0.0274637,(8[&Ev=S,S=52,ND=15,fake_trait=-0.198576283620219,another_trait=0.472857442451641]:0.0761163,((9[&Ev=S,S=53,ND=16,fake_trait=-1.08984062435629,another_trait=0.583236675476655]:0.0906068,10[&Ev=S,S=54,ND=17,fake_trait=1.06296534881541,another_trait=0.307273282669485]:1e-06)[&Ev=S,S=45,ND=18,fake_trait=1.03281389825159,another_trait=0.679154373938218]:1e-06,((11[&Ev=S,S=65,ND=19,fake_trait=1.33253482301363,another_trait=0.368328508688137]:0.120851,12[&Ev=S,S=71,ND=20,fake_trait=0.556560169824714,another_trait=0.502828885568306]:0.133939)[&Ev=S,S=56,ND=21,fake_trait=-1.52993654311419,another_trait=0.699205168057233]:1e-06,13[&Ev=S,S=64,ND=22,fake_trait=0.517818501905603,another_trait=0.837434149812907]:0.0693814)[&Ev=S,S=46,ND=23,fake_trait=0.107593036146107,another_trait=0.848443516530097]:1e-06)[&Ev=S,S=40,ND=24,fake_trait=-1.48227842991736,another_trait=0.0549788442440331]:0.0333823)[&Ev=S,S=35,ND=25,fake_trait=-1.58802157126508,another_trait=0.365696684690192]:1e-06)[&Ev=D,S=24,ND=26,fake_trait=-0.834709208415877,another_trait=0.414465259760618]:0.0431861)[&Ev=S,S=19,ND=27,fake_trait=0.924712865518581,another_trait=0.648664495907724]:1e-06,14[&Ev=S,S=26,ND=28,fake_trait=-1.58066896740599,another_trait=0.175387351540849]:0.22283)[&Ev=S,S=17,ND=29,fake_trait=-0.379754758685323,another_trait=0.0288192296866328]:0.0292362)[&Ev=D,S=17,ND=8,fake_trait=0.796414471624802,another_trait=0.764943784102798]:0.185603,(15[&Ev=S,S=16,ND=5,fake_trait=-1.21569149006564,another_trait=0.0801574073266238]:0.0621782,16[&Ev=S,S=15,ND=6,fake_trait=0.716092737131118,another_trait=0.647288323845714]:0.332505)[&Ev=S,S=12,ND=7,fake_trait=-0.611572158411749,another_trait=0.796943147899583]:0.185603)[&Ev=S,S=9,ND=30,fake_trait=0.707588898589443,another_trait=0.805669415043667];
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/Rtmp3CI0xy/file4beb4dd22384.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.4902,
    "another_trait": 0.082
  },
  {
    "edge_num": 2,
    "Ev": "S",
    "S": "69",
    "ND": 1,
    "fake_trait": 0.6771,
    "another_trait": 0.3594
  },
  {
    "edge_num": 3,
    "Ev": "S",
    "S": "70",
    "ND": 2,
    "fake_trait": -0.2109,
    "another_trait": 0.0083
  },
  {
    "edge_num": 4,
    "Ev": "S",
    "S": "31",
    "ND": 9,
    "fake_trait": 0.7927,
    "another_trait": 0.7968
  },
  {
    "edge_num": 5,
    "Ev": "S",
    "S": "37",
    "ND": 10,
    "fake_trait": 0.0218,
    "another_trait": 0.6755
  },
  {
    "edge_num": 6,
    "Ev": "S",
    "S": "38",
    "ND": 11,
    "fake_trait": 0.2985,
    "another_trait": 0.9281
  },
  {
    "edge_num": 7,
    "Ev": "S",
    "S": "61",
    "ND": 13,
    "fake_trait": -1.9705,
    "another_trait": 0.7531
  },
  {
    "edge_num": 8,
    "Ev": "S",
    "S": "52",
    "ND": 15,
    "fake_trait": -0.1986,
    "another_trait": 0.4729
  },
  {
    "edge_num": 9,
    "Ev": "S",
    "S": "53",
    "ND": 16,
    "fake_trait": -1.0898,
    "another_trait": 0.5832
  },
  {
    "edge_num": 10,
    "Ev": "S",
    "S": "54",
    "ND": 17,
    "fake_trait": 1.063,
    "another_trait": 0.3073
  },
  {
    "edge_num": 11,
    "Ev": "S",
    "S": "65",
    "ND": 19,
    "fake_trait": 1.3325,
    "another_trait": 0.3683
  },
  {
    "edge_num": 12,
    "Ev": "S",
    "S": "71",
    "ND": 20,
    "fake_trait": 0.5566,
    "another_trait": 0.5028
  },
  {
    "edge_num": 13,
    "Ev": "S",
    "S": "64",
    "ND": 22,
    "fake_trait": 0.5178,
    "another_trait": 0.8374
  },
  {
    "edge_num": 14,
    "Ev": "S",
    "S": "26",
    "ND": 28,
    "fake_trait": -1.5807,
    "another_trait": 0.1754
  },
  {
    "edge_num": 15,
    "Ev": "S",
    "S": "16",
    "ND": 5,
    "fake_trait": -1.2157,
    "another_trait": 0.0802
  },
  {
    "edge_num": 16,
    "Ev": "S",
    "S": "15",
    "ND": 6,
    "fake_trait": 0.7161,
    "another_trait": 0.6473
  },
  {
    "edge_num": 17,
    "Ev": "S",
    "S": "9",
    "ND": 30,
    "fake_trait": 0.7076,
    "another_trait": 0.8057
  },
  {
    "edge_num": 18,
    "Ev": "D",
    "S": "17",
    "ND": 8,
    "fake_trait": 0.7964,
    "another_trait": 0.7649
  },
  {
    "edge_num": 19,
    "Ev": "S",
    "S": "36",
    "ND": 4,
    "fake_trait": -0.83,
    "another_trait": 0.5128
  },
  {
    "edge_num": 20,
    "Ev": "S",
    "S": "60",
    "ND": 3,
    "fake_trait": 1.5286,
    "another_trait": 0.6993
  },
  {
    "edge_num": 21,
    "Ev": "S",
    "S": "17",
    "ND": 29,
    "fake_trait": -0.3798,
    "another_trait": 0.0288
  },
  {
    "edge_num": 22,
    "Ev": "S",
    "S": "19",
    "ND": 27,
    "fake_trait": 0.9247,
    "another_trait": 0.6487
  },
  {
    "edge_num": 23,
    "Ev": "D",
    "S": "24",
    "ND": 26,
    "fake_trait": -0.8347,
    "another_trait": 0.4145
  },
  {
    "edge_num": 24,
    "Ev": "S",
    "S": "24",
    "ND": 14,
    "fake_trait": 1.1573,
    "another_trait": 0.2434
  },
  {
    "edge_num": 25,
    "Ev": "S",
    "S": "33",
    "ND": 12,
    "fake_trait": 0.3882,
    "another_trait": 0.5191
  },
  {
    "edge_num": 26,
    "Ev": "S",
    "S": "35",
    "ND": 25,
    "fake_trait": -1.588,
    "another_trait": 0.3657
  },
  {
    "edge_num": 27,
    "Ev": "S",
    "S": "40",
    "ND": 24,
    "fake_trait": -1.4823,
    "another_trait": 0.055
  },
  {
    "edge_num": 28,
    "Ev": "S",
    "S": "45",
    "ND": 18,
    "fake_trait": 1.0328,
    "another_trait": 0.6792
  },
  {
    "edge_num": 29,
    "Ev": "S",
    "S": "46",
    "ND": 23,
    "fake_trait": 0.1076,
    "another_trait": 0.8484
  },
  {
    "edge_num": 30,
    "Ev": "S",
    "S": "56",
    "ND": 21,
    "fake_trait": -1.5299,
    "another_trait": 0.6992
  },
  {
    "edge_num": 31,
    "Ev": "S",
    "S": "12",
    "ND": 7,
    "fake_trait": -0.6116,
    "another_trait": 0.7969
  }
],
    "metadata": {"info": "R-package treeio", "data": "Fri Jan 25 21:41:39 2019"}
}

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.4902        0.0820
2         2  S 69  1     0.6771        0.3594
3         3  S 70  2    -0.2109        0.0083
4         4  S 31  9     0.7927        0.7968
5         5  S 37 10     0.0218        0.6755
6         6  S 38 11     0.2985        0.9281
7         7  S 61 13    -1.9705        0.7531
8         8  S 52 15    -0.1986        0.4729
9         9  S 53 16    -1.0898        0.5832
10       10  S 54 17     1.0630        0.3073
11       11  S 65 19     1.3325        0.3683
12       12  S 71 20     0.5566        0.5028
13       13  S 64 22     0.5178        0.8374
14       14  S 26 28    -1.5807        0.1754
15       15  S 16  5    -1.2157        0.0802
16       16  S 15  6     0.7161        0.6473
17       17  S  9 30     0.7076        0.8057
18       18  D 17  8     0.7964        0.7649
19       19  S 36  4    -0.8300        0.5128
20       20  S 60  3     1.5286        0.6993
21       21  S 17 29    -0.3798        0.0288
22       22  S 19 27     0.9247        0.6487
23       23  D 24 26    -0.8347        0.4145
24       24  S 24 14     1.1573        0.2434
25       25  S 33 12     0.3882        0.5191
26       26  S 35 25    -1.5880        0.3657
27       27  S 40 24    -1.4823        0.0550
28       28  S 45 18     1.0328        0.6792
29       29  S 46 23     0.1076        0.8484
30       30  S 56 21    -1.5299        0.6992
31       31  S 12  7    -0.6116        0.7969

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

$metadata$data
[1] "Fri Jan 25 21:41:39 2019"

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/Rtmp3CI0xy/file4beb6aa57f6e.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/