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.
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?
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.
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
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.
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: