joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
579 stars 188 forks source link

How to write phyloseq object into CSV file #1131

Open bioinfonext opened 5 years ago

bioinfonext commented 5 years ago

I am trying to write phyloseq object into csv file but it shows error?

physeq_soil phyloseq-class experiment-level object otu_table() OTU Table: [ 67405 taxa and 27 samples ] sample_data() Sample Data: [ 27 samples by 5 sample variables ] tax_table() Taxonomy Table: [ 67405 taxa by 7 taxonomic ranks ] phy_tree() Phylogenetic Tree: [ 67405 tips and 67229 internal nodes ]

write.csv( as.data.frame(physeq_soil), file="soil.csv" ) Error in as.data.frame.default(physeq_soil : cannot coerce class "structure("phyloseq", package = "phyloseq")" to a data.frame

erictleung commented 5 years ago

@yogeshgupt a phyloseq object is made up of multiple R objects, hence the last line of the error message saying

cannot coerce class "structure("phyloseq", package="phyloseq")" to data.frame

This previous issue here has some suggestions on how to save a phyloseq object into a text file. I don't have experience with writing out phylogenetic trees, but this may be a good start, which uses the ape package. Good luck!

RJ333 commented 4 years ago

There is also a function in the microbiome package for this: https://rdrr.io/github/microbiome/microbiome/man/write_phyloseq.html

However, for the manual way I want to strongly suggest future readers with larger data sets to use the way described in the linked issue, which is:

OTU1 = as(otu_table(ps), "matrix")
if(taxa_are_rows(ps)){OTU1 <- t(OTU1)}
# Coerce to data.frame
OTUdf = as.data.frame(OTU1)

This worked in seconds for my 90000 x 350 count table, whereas

OTUdf <- as.data.frame(otu_table(ps))

is still running after 5 hours.