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

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