write_nwka_tree.Rd
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)
trees | An object of class |
---|---|
file | A file name. If this is |
append | If this is |
nwka | If this is |
quotes | If |
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.
https://github.com/arklumpus/TreeNode/blob/master/NWKA.md
Other functions to write trees:
keep_writing_binary_trees()
,
write_binary_trees()
,
write_nwka_nexus()
# 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';