Exporting otu_table() from phyloseq into either biom or text format

Hello all,

I am having trouble in trying to figure out how to export a phyloseq object as a text or biom file. Also, I have noticed that my tables are different if I import a biom file from MEGAN or export a text file from MEGAN and then convert that into a json biom table. When I do the former, I don't see the taxa names on the left. I do see them for the latter. I am having no problems running decontam in either case. I am able to use prune_taxa(!contamdf.prev$contaminant, phyloseq-object). And so I have the otu_table that I want. However, I am not sure how to export it as a text or biom file in R so that I can then import it into QIIME2 (i.e. a qza format). I have tried the following write.table() code but that is giving me a table without any taxonomy names.

otu_table(phyloseq-object)[1:5, 1:5]

write.table(t(otu_table(phyloseq-object)), "samples-decontam-filtered.txt", sep = "\t", row.names = TRUE, col.names = NA, quote = FALSE)

Samples 1, Sample 2, ...
2 11910 14206 12158 14498 7753 4949 7970 15989 18050 22519 19144 18724 23351 84 9037 11037 21407 21180 13367 20958 14329 16279 13851 16400 28022 12854 22574 10828 14208 24573 30444 16504 23558 20564 16114 13050 27812 18564 19158 13868 4326 8856 6720
57723 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
204434 0 0 0 0 0 0 0 0 0 0 0 0 0 5 671 485 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
658062 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1267533 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1476577 47385

Any help on this would greatly be appreciated!

Hi @SterlingWright2016 ,

Welcome to the forum!
I’m not experienced with MEGAN but exporting objects from phyloseq to QIIME 2 has been covered a few places around this forum. For example, you can try the tutorial here and let us know if it works?

1 Like

Hi Mehrbod,

Thank you for your help! I was able to figure it out. I had to use the phyloseq_to_df() function. Although it I have to tinker with the output a little (i.e. removing the first column and shifting the sample names over so that everything is in the proper format, it worked.

2 Likes

Glad to hear you got it working @SterlingWright2016!
If you feel like sharing the code you used here, I’m sure future users would be appreciative of it too :slight_smile:

Sure!
setwd("/~/wkdir")

Load Packages

library(phyloseq); packageVersion(“phyloseq”) ## ‘1.34.0’
library(ggplot2); packageVersion(“ggplot2”) ## ‘3.3.3’
library(decontam); packageVersion(“decontam”) ## ‘1.8.0’ #BiocManager::install(“decontam”)
library(plyr); packageVersion(“plyr”)
library(metagMisc); packageVersion(“metagMisc”)

Export taxonomy table (i.e. path-to-count) as a txt file.

Use biom convert to convert txt file into json-biom file

taxa<-import_biom(“taxa-json.biom”)
METADATA<-import_qiime_sample_data(“METADATA.txt”)
phyloseq<-merge_phyloseq(taxa,METADATA)
#Check to see if imported correctly
otu_table(phyloseq)[1:5, 1:5]
phyloseq
#phyloseq-class experiment-level object
#otu_table() OTU Table: [ 1810 taxa and 49 samples ]
#sample_data() Sample Data [ 49 samples by 25 sample variables ]

After running decontam to identify contaminants, use the prune_taxa to remove them. Then you can use the phyloseq_to_df() function to convert phyloseq object as a dataframe.

taxa.decontam.df<-phyloseq_to_df(phyloseq, addtax = FALSE, addtot = FALSE, addmaxrank = FALSE, sorting = “NULL”)
View(taxa.decontam.df)
write.table(taxa.decontam.df, “~/taxa-decontam.txt”, sep = “\t”)

You will need to open the file and remove the first column since it numbers each row. # # You will also need to shift sample names over one.

After you set up everything as a biom table, you can then convert it back into a qza file.

Not sure if this is the most efficient way of doing things but it allowed me to put everything from phyloseq back into the QIIME2 environment!

2 Likes

This is the code I use to export a phyloseq obj to text (biom format similar) file:

phyloseq::otu_table(phyloseq) %>%
  as.data.frame() %>%
  rownames_to_column("id") %>%
  left_join(phyloseq::tax_table(phyloseq)%>%as.data.frame()%>%
              rownames_to_column("id")) -> phyloseq_biom

write.csv(phyloseq_biom, file = "phyloseq_biom.csv")
2 Likes

This won't quite work to convert to a BIOM file since the biom tool has fairly strict requirements for file formatting. I've modified the excellent code posted by @sghignone so that the resulting file can be used with the biom convert command:

write_biom_csv <- function(ps, file, sep = "; ") {
  phyloseq::otu_table(ps) %>%
    as.data.frame() %>%
    rownames_to_column("#OTU ID") %>%
    left_join(phyloseq::tax_table(ps) %>% 
                as.data.frame() %>%
                rownames_to_column("#OTU ID") %>% 
                tidyr::unite("taxonomy", !`#OTU ID`, sep = sep)) -> phyloseq_biom
  
  write_tsv(phyloseq_biom, file = file)
}

Also available as a gist (if updates happen)

4 Likes