NEWICK-formatted tree manipulation

How to get the list of taxa from a NEWICK-formatted tree?

The following command lines allow the list of leaf names (one per line) to be extracted from a phylogenetic tree contained in a NEWICK-formatted tree file $treefile. To obtain the list on one unique line, these command lines should be ended by: | xargs echo. To sort the list according to the alphanumerical order, these command lines should be ended by: | sort.

gotree

The program gotree could be used to return the list of leaf names with the following command line:

[170403fl]

Bash

Alternatively, the following Bash command line returns the same list:

[181208ac]

How to get the number of leaves from a NEWICK-formatted tree?

The following command lines return the number of leaves from a phylogenetic tree contained in a NEWICK-formatted tree file $treefile.

gotree

The program gotree could be used with the following command line:

[170403fl]

Bash

Alternatively, the following Bash command line could also be used:

[170403ac]

How to compute the total branch length of a NEWICK-formatted tree?

The following command lines allow summing up the length of every branch of a phylogenetic tree contained in a NEWICK-formatted file $treefile.

gotree

The program gotree returns the total branch length of a tree with the following command line:

[170403fl]

Bash

Alternatively, the following Bash command line returns the same result:

[170403ac]

How to assess whether two NEWICK-formatted trees have the same set of leaf names?

The following command lines return the boolean true if the phylogenetic trees contained in the two NEWICK-formatted files $treefile1and $treefile2 are made up by the same leaf set.

gotree

The program gotree could be used with the following command line:

[170403fl]

Bash

The same result could be obtained with the following Bash command line:

[181208ac]

How to compute the matrix of patristic distances from a NEWICK-formatted tree?

The patristic distance is the sum of the length of all branches connecting two leaves in a phylogenetic tree. The program gotree could be used to compute the square matrix of patristic distances from the phylogenetic tree contained in the NEWICK-formatted file $treefile and write it into the PHYLIP-formatted file $outfile with the following command line:

[181225ac]

How to generate a binary matrix representation from a NEWICK-formatted tree?

Given a NEWICK-formatted tree file $treefile, the following command line writes into the text file $bmfile the sorted list of its leaf names (on the first line), followed by every non-trivial split in binary format (one per line).

Of note, a transposed binary representation $tbmfile (i.e. each raw corresponding to one leaf) could be obtained from the file $bmfile with the following awk one-liner:

Concerning the binary representation inside file $bmfile, it should stressed that: 1. the leaf names in the first line are sorted according to their alphanumerical order, 2. the first leaf name is always encoded by 1, and 3. the binary splits are sorted.

In consequence, every NEWICK representation of the same tree topology always leads to the same file $bmfile (branch lengths and support are not considered). The topology of any phylogenetic tree could then be easily hashed with e.g. the following command line:

If $treefile1 and $treefile2 are two files, each containing one NEWICK-formatted phylogenetic tree, that have been processed by the above command line to create the two binary matrix representation files $bmfile1 and $bmfile2, respectively, assessing that they have the same topology could therefore be easily performed with the following command line:

More generally, when the two trees are binary ones, a bipartition distance between them could be easily derived with the following command line:

[181208ac]