This function writes one or more trees in Newick-with-Attributes (NWKA) format to a file or to the standard output.

write_nwka_tree(trees, file = "", append = FALSE, nwka = TRUE, quotes = FALSE)

Arguments

trees

An object of class "phylo" or "multiPhylo".

file

A file name. If this is "" (the default), the tree will be written on the standard output.

append

If this is FALSE (the default), the output file (if it exists already) is truncated before writing trees (i.e. overwritten). If this is TRUE, the trees are appended at the end of the output file.

nwka

If this is TRUE (the default), the tree will be written in Newick-with-Attributes (NWKA) format. Otherwise, the tree will be written in Newick format (and attributes that cannot be represented in this format will be lost).

quotes

If nwka = FALSE, this argument determines whether names in the tree file will be enclosed within single quotes (if this is TRUE) or not (if this is FALSE).

Details

All of the available attributes are written to the file if nwka = TRUE. Otherwise, (if available) the tip names and lenghts are always written, as well as the internal nodes' lenghts and support values. If nodes have a name, this is only included if they do not have a support value as well.

The tip names can be specified either as a Name element in the tree's tip.attributes element, or as the tip.label element of the tree. If both are specified, the values stored in the Name attribute take precedence (this allows backward compatibility for trees created using ape).

The node names and support values can similarly be specified either with a Name or Support element in the tree's node.attributes, or as the tree's node.label. If all the node labels can be parsed as numbers, they will be assumed to contain support values; otherwise, they will be assumed to contain node names. If the node.attributes already contain a Name or Support element, the node labels will be ignored.

No attempt is made to fix problematic labels. Thus, if the tip or node names contain special characters, an invalid output may be produced. For example, if the labels contain spaces or commas and enwk and quotes are both FALSE, the output tree may not be parsed correctly. If you wish to produce a tree conforming to the Newick format while fixing problematic tip labels, you should look into the write.tree function of the ape package.

References

https://github.com/arklumpus/TreeNode/blob/master/NWKA.md

See also

Examples

# Parse a tree string # Topology from https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=207598 tree <- read_nwka_tree(text="(((('Homo sapiens'[rank=species])'Homo'[rank=genus]) 'Hominina'[rank=subtribe],(('Pan paniscus'[rank=species],'Pan troglodytes' [rank=species])'Pan'[rank=genus])'Panina'[rank=subtribe])'Hominini'[rank=tribe], (('Gorilla gorilla'[rank=species],'Gorilla beringei'[rank=species])'Gorilla' [rank=genus])'Gorillini'[rank=tribe])'Homininae'[rank=subfamily];") # Print the tree to the standard output in NWKA format cat(write_nwka_tree(tree))
#> (((('Homo sapiens'[rank='species'])'Homo'[rank='genus'])'Hominina'[rank='subtribe'],(('Pan paniscus'[rank='species'],'Pan troglodytes'[rank='species'])'Pan'[rank='genus'])'Panina'[rank='subtribe'])'Hominini'[rank='tribe'],(('Gorilla gorilla'[rank='species'],'Gorilla beringei'[rank='species'])'Gorilla'[rank='genus'])'Gorillini'[rank='tribe'])'Homininae'[rank='subfamily',TreeName='tree'];
# Print the tree to the standard output in Newick format without quotes cat(write_nwka_tree(tree, nwka = FALSE))
#> ((((Homo sapiens)Homo)Hominina,((Pan paniscus,Pan troglodytes)Pan)Panina)Hominini,((Gorilla gorilla,Gorilla beringei)Gorilla)Gorillini)Homininae;
# Print the tree to the standard output in Newick format with quotes cat(write_nwka_tree(tree, nwka = FALSE, quotes = TRUE))
#> (((('Homo sapiens')'Homo')'Hominina',(('Pan paniscus','Pan troglodytes')'Pan')'Panina')'Hominini',(('Gorilla gorilla','Gorilla beringei')'Gorilla')'Gorillini')'Homininae';