Skip to content

Instantly share code, notes, and snippets.

@smdabdoub
Last active January 18, 2024 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smdabdoub/f5451c654006426a70e22a13fc18b276 to your computer and use it in GitHub Desktop.
Save smdabdoub/f5451c654006426a70e22a13fc18b276 to your computer and use it in GitHub Desktop.
Export phyloseq object to BIOM-compatible TSV table
# Modified from https://forum.qiime2.org/t/exporting-otu-table-from-phyloseq-into-either-biom-or-text-format/19103/6
# to be directly exportable to a BIOM file using the official 'biom' command-line tool. The file-format requirements for
# a text table to be converted are listed here:
# https://forum.qiime2.org/t/csv-to-a-qiime2-readable-format/2284/7
# This version concatenates the taxonomy ranks into a single string that is added to the OTU table as a 'taxonomy' column.
# The resulting TSV table can be converted to a BIOM-format file with the following command (outside of R):
# #biom convert -i otu_table_biom.tsv -o otu_table.biom --to-hdf5 --header-key taxonomy
library(phyloseq)
library(readr)
library(tidyr)
write_biom_tsv <- 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)
}
@eveliinahanski
Copy link

Hello! I tried using this code but so far no luck. Am I right in understanding that the only alterations I should make are: change all 'ps' in the code to my phyloseq object (e.g. 'phyloseq') and then change the last 'file' to the pathname of a file I want to create? Thanks!

@hank00000
Copy link

No errors thrown but also no tsv written. No luck for me either.

@smdabdoub
Copy link
Author

@eveliinahanski Apologies, I somehow missed your post! Let me know if you still have issues.

@hank00000 I would first try commenting out line 21, and add a new line below:

return (phyloseq_biom)

since that object is a data.frame, take a look and see if it's empty or what's going on there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment