This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Problem with Newick format - ggtree in R

Hello everyone,

I have a rookie question but unfortunately I am unable to find an answer myself. I would like to generate a cladrogram using ggtree package based on the taxonomy of my strains.

As I know the taxonomic classification of he strains I thought that I could use that to generate a tree in Newick format, for that I used the makeNewick function from the Taxonomizr package.

setwd("D:/R/phylogeny/Jemime_collection")
taxofile<-"cladotest.tab" 
Taxo <-  read.table (taxofile, check.names = FALSE, header = TRUE, dec = ".", sep = "\t", comment.char = "")

which got me this, so far so good:

Taxo

     family         genus                         species
     1  Actinomycetaceae Actinobaculum Actinobaculum_sp_oral_taxon_183
     2  Actinomycetaceae   Actinomyces  Actinomyces_bouchesdurhonensis
     3  Actinomycetaceae   Actinomyces            Actinomyces_dentalis
     4  Actinomycetaceae   Actinomyces        Actinomyces_gerencseriae
     5  Actinomycetaceae   Actinomyces        Actinomyces_graevenitzii
     6  Actinomycetaceae   Actinomyces            Actinomyces_israelii
     7  Actinomycetaceae   Actinomyces           Actinomyces_johnsonii
     8  Actinomycetaceae   Actinomyces        Actinomyces_massiliensis
     9  Actinomycetaceae   Actinomyces          Actinomyces_naeslundii
    10 Actinomycetaceae   Actinomyces                Actinomyces_oris
    11 Actinomycetaceae   Actinomyces            Actinomyces_SGB17154
    12 Actinomycetaceae   Actinomyces            Actinomyces_SGB17168
    13 Actinomycetaceae   Actinomyces            Actinomyces_sp_ICM47

After that I tried to generate a newick tree from this using the makenewick function, which seemed to work :

tree <- makeNewick(Taxo)
tree
      [1] "(((Actinobaculum_sp_oral_taxon_183)Actinobaculum,(Actinomyces_bouchesdurhonensis,Actinomyces_dentalis,Actinomyces_gerencseriae,Actinomyces_graevenitzii,Actinomyces_israelii,Actinomyces_johnsonii,Actinomyces_massiliensis,Actinomyces_naeslundii,Actinomyces_oris,Actinomyces_SGB17154,Actinomyces_SGB17168,Actinomyces_sp_ICM47)Actinomyces)Actinomycetaceae);"

However, when I tried to use this newick tree to generate the tree I got an error message:

ggtree(tree)

 > Error in `fortify()`:
! `data` must be a <data.frame>, or an object coercible by `fortify()`, not the string "(((Actinobaculum_sp_oral_ta...".
Run `rlang::last_trace()` to see where the error occurred.

I ran line indicated and got this:

<error/rlang_error>
Error in `fortify()`:
! `data` must be a <data.frame>, or an object coercible by `fortify()`, not the string "(((Actinobaculum_sp_oral_ta...".
---
Backtrace:
Run rlang::last_trace(drop = FALSE) to see 2 hidden frames.

Which did not help me understand how and why my tree was not data.frame and how to convert it into a data.frame.

Has anyone an idea on how to solve this problem?

r ggtree makenewick taxonomizr

Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or use one of (a) the option highlighted in the image below/ (b) fenced code blocks for multi-line code. Fenced code blocks are useful in syntax highlighting. If your code has long lines with a single command, break those lines into multiple lines with proper escape sequences so they're easier to read and still run when copy-pasted. I've done it for you this time.
code_formatting

Thank you, my apologies for the improper formating.

1 answer

The output of makeNewick() is a character string. It needs to be a data.frame or phylo class to work with ggtree. You have some options:

# Option 1: convert string to phylo within R
tree_string <- "(((Actinobaculum_sp_oral_taxon_183)Actinobaculum,(Actinomyces_bouchesdurhonensis,Actinomyces_dentalis,Actinomyces_gerencseriae,Actinomyces_graevenitzii,Actinomyces_israelii,Actinomyces_johnsonii,Actinomyces_massiliensis,Actinomyces_naeslundii,Actinomyces_oris,Actinomyces_SGB17154,Actinomyces_SGB17168,Actinomyces_sp_ICM47)Actinomyces)Actinomycetaceae);"
phylo_tree <- ape::read.tree(text = tree_string)
ggtree(phylo_tree)
# Option 2: write string to file, read in as tree
tree_string <- "(((Actinobaculum_sp_oral_taxon_183)Actinobaculum,(Actinomyces_bouchesdurhonensis,Actinomyces_dentalis,Actinomyces_gerencseriae,Actinomyces_graevenitzii,Actinomyces_israelii,Actinomyces_johnsonii,Actinomyces_massiliensis,Actinomyces_naeslundii,Actinomyces_oris,Actinomyces_SGB17154,Actinomyces_SGB17168,Actinomyces_sp_ICM47)Actinomyces)Actinomycetaceae);"
writeLines(tree_string, "tree.nwk")
tree_in <- treeio::read.newick("tree.nwk")
ggtree(tree_in)

I just tried Option 1 and it worked perfectly, thank you so much ! Overal I understood the format problem but was unable to find the ape::read.tree() function myself, thank you again !

Log in to answer this question.