<p><strong>Brief answer</strong> Firstly this is not a final solution. The simple answer is to use TreeNode.add_feature() in ETE3 which would enable NHX format to be inserted into the tree. There are two possible ways to implement the solution, but I'm not aware of a 'reader'.  I'll return to this later under the section 'Question (again)'.</p>
<p><strong>Note</strong>  I assume that the OP made a typo with 'witg ETE3' and probably means 'with ETE3'?</p>
<p><strong>Interest</strong> However as an approach to molecular phylogeny it is interesting theory and has clear application. Not in a simple tree, but when the volume of data overwhelms optical analysis. I am not aware of this being deployed in phylogeny - not done a literature search though, but would readily automate processing of complex trees.</p>
<p><strong>Theory</strong> The OP uses the term 'edge' rather than branch, so this is referring to depth first search (DFS) type style calculations. In Phylogenetics there are 'branches' and 'nodes' and these are distinct, in this line of theory, I think, 'edges' can be nodes ... possibly, it is possible an 'edge' connects two nodes, in which an 'edge' is synonymous with a 'branch'.</p>
<p>The difficult I have with the information is that edge 0 is not defined. In Phylogenetics edge 0 would probably be the root. It is possibly the 'missing branch' in the trifurcation at the base of the tree. In this line of theory however 0 may simply be a null category, because an edge is not necessarily the same thing as a branch.</p>
<p><strong>Data structure</strong> The information present is paired. thus ... salamander is 7,0 ... frog is 6,0 ... turtle is 4, 2... We can work this out because the data structure is 7,6,5,4,3,2,1 for odd numbers.</p>
<p>7-&gt;0, 6-&gt;0, 5-&gt;2 and so on ...</p>
<p>Moreover, 7 is at the top of the tree whilst, 1 is at the bottom and the 'edge' is an arbitrary designation. Phylogenetics wouldn't label nodes in this format, labelling would usually be with respect to the base of the tree (root). If I look at this from my world-view there's no 0-&gt; ... but 5 the ancestor of 7 and 6 goes to turtles.</p>
<p>Thus far the information isn't the DFS, its presented as the input, for example to calculate DFS, and can be used calculate all sorts of things in this style of graph theory.</p>
<p><strong>Question (again)</strong> Back to the question, I am not aware of DFS style theory being used in trees, therefore I would not be aware of an interpreter that would read it. If the OP was just wanting to stash the data into a tree via  NHX it could be stacked onto the root lineage. There isn't a root lineage in this tree so I've just added a root at '1:0.01', i.e. a nominal distance to provide the OP with a place to stash the data there. This will read as a tree and the additional 1:0.01 doesn't affect the integrity of the tree - especially in this given phylogeny where the location of the root is not in dispute.</p>
<p><strong>Idea 1</strong></p>
<p>As described above the Edge data gets stored in the 1:0.01 inserted as an additional bracket at the end of the tree.</p>
<pre><code>((mammal:0.14,turtle:0.02,(rayfinfish:0.25,(frog:0.01,salamander:0.01)50:0.12)95:0.09)1:0.01);
</code></pre>
<p><strong>Idea 2</strong></p>
<p>This is pairwise data and NXH format could insert the pairwise data for each edge (via TreeNode.add_feature()) for each 'edge' thus salamander would have 7-0 or 7,0 inserted using NHX. That is absolutely technically where the data belongs, but how this read later is more challenging.</p>
<p><strong>If I get chance</strong> I'll update the question later with specific NHX output and possibly code to achieve it.</p>
<hr />
<p>OP upvoted. Its a good question with interesting application.</p>

CC BY-SA 4.0
2022-03-27T19:34:45.647Z
2022-03-28T00:47:25.197Z
2022-03-28T00:47:25.197Z
M__
<p>The treedata book has a <a href="https://yulab-smu.top/treedata-book/chapter5.html" rel="nofollow noreferrer">section</a> on tree annotations that should work for you.</p>

CC BY-SA 4.0
2021-03-02T11:57:17.793Z
2021-03-02T11:57:17.793Z
winni2k
<p>I need help to add information in my phylogenetic tree on each branch.</p>
<p>I have two pieces of information at my disposal:</p>
<ol>
<li>A phylogenetic tree in newick format :</li>
</ol>
<p>(mammal:0.14,turtle:0.02,(rayfinfish:0.25,(frog:0.01,salamander:0.01)50:0.12)95:0.09);</p>
<ol start="2">
<li>A matrix with the edge number and information in the Information column:</li>
</ol>
<blockquote>
<pre><code>Edge    Inf
7        0
6        0
5        2
4        1
3        2
2        2
1        2
</code></pre>
</blockquote>
<p>Does someone have an idea in order to add these informations by creating a <code>NHX format</code> in python (with <strong>ete3</strong> for instance) or with an <strong>R package</strong> ?</p>
<p>Here is the tree :
<a href="https://i.sstatic.net/Xdpch.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Xdpch.png" alt="enter image description here" /></a></p>

<r><python><phylogenetics><phylogeny>
Create a NHX phylogeny format with r or python
# Create a NHX phylogeny format with r or python

(Asked by: username_0 on 2021-03-02)

I need help to add information in my phylogenetic tree on each branch.
I have two pieces of information at my disposal:

A phylogenetic tree in newick format :

(mammal:0.14,turtle:0.02,(rayfinfish:0.25,(frog:0.01,salamander:0.01)50:0.12)95:0.09);

A matrix with the edge number and information in the Information column:


Edge    Inf
7        0
6        0
5        2
4        1
3        2
2        2
1        2


Does someone have an idea in order to add these informations by creating a NHX format in python (with ete3 for instance) or with an R package ?
Here is the tree :

**Tags:** r, python, phylogenetics, phylogeny

---

## 2 answers

### Answer by username_1 on 2022-03-27. Score: 1 | Accepted answer

Brief answer Firstly this is not a final solution. The simple answer is to use TreeNode.add_feature() in ETE3 which would enable NHX format to be inserted into the tree. There are two possible ways to implement the solution, but I'm not aware of a 'reader'.  I'll return to this later under the section 'Question (again)'.
Note  I assume that the OP made a typo with 'witg ETE3' and probably means 'with ETE3'?
Interest However as an approach to molecular phylogeny it is interesting theory and has clear application. Not in a simple tree, but when the volume of data overwhelms optical analysis. I am not aware of this being deployed in phylogeny - not done a literature search though, but would readily automate processing of complex trees.
Theory The OP uses the term 'edge' rather than branch, so this is referring to depth first search (DFS) type style calculations. In Phylogenetics there are 'branches' and 'nodes' and these are distinct, in this line of theory, I think, 'edges' can be nodes ... possibly, it is possible an 'edge' connects two nodes, in which an 'edge' is synonymous with a 'branch'.
The difficult I have with the information is that edge 0 is not defined. In Phylogenetics edge 0 would probably be the root. It is possibly the 'missing branch' in the trifurcation at the base of the tree. In this line of theory however 0 may simply be a null category, because an edge is not necessarily the same thing as a branch.
Data structure The information present is paired. thus ... salamander is 7,0 ... frog is 6,0 ... turtle is 4, 2... We can work this out because the data structure is 7,6,5,4,3,2,1 for odd numbers.
7->0, 6->0, 5->2 and so on ...
Moreover, 7 is at the top of the tree whilst, 1 is at the bottom and the 'edge' is an arbitrary designation. Phylogenetics wouldn't label nodes in this format, labelling would usually be with respect to the base of the tree (root). If I look at this from my world-view there's no 0-> ... but 5 the ancestor of 7 and 6 goes to turtles.
Thus far the information isn't the DFS, its presented as the input, for example to calculate DFS, and can be used calculate all sorts of things in this style of graph theory.
Question (again) Back to the question, I am not aware of DFS style theory being used in trees, therefore I would not be aware of an interpreter that would read it. If the OP was just wanting to stash the data into a tree via  NHX it could be stacked onto the root lineage. There isn't a root lineage in this tree so I've just added a root at '1:0.01', i.e. a nominal distance to provide the OP with a place to stash the data there. This will read as a tree and the additional 1:0.01 doesn't affect the integrity of the tree - especially in this given phylogeny where the location of the root is not in dispute.
Idea 1
As described above the Edge data gets stored in the 1:0.01 inserted as an additional bracket at the end of the tree.
((mammal:0.14,turtle:0.02,(rayfinfish:0.25,(frog:0.01,salamander:0.01)50:0.12)95:0.09)1:0.01);

Idea 2
This is pairwise data and NXH format could insert the pairwise data for each edge (via TreeNode.add_feature()) for each 'edge' thus salamander would have 7-0 or 7,0 inserted using NHX. That is absolutely technically where the data belongs, but how this read later is more challenging.
If I get chance I'll update the question later with specific NHX output and possibly code to achieve it.

OP upvoted. Its a good question with interesting application.


---

### Answer by username_2 on 2021-03-02. Score: 0

The treedata book has a [section](https://yulab-smu.top/treedata-book/chapter5.html) on tree annotations that should work for you.


---

