Trying to generate ASV table from phyloseq

Viewed 14

I recognize most people have the opposite problem. But I'm trying to create an ASV table, with column names as "identified OTUs" (aka the column name is drawn from the taxonomy information from GlobalPatterns@tax.table, rather than just being the assigned OTU code that's encoded in GlobalPatterns@otu.table), and row names as sample name.

I also want to append the metadata to the end of the ASV table, to allow for analysis based on said metadata.

I managed to generate a table without the taxonomic information with this code, using GlobalPatterns for reproducibility:

data(GlobalPatterns)
asv.matrix <- as.matrix(GlobalPatterns@otu_table@.Data)
asv <- data.frame(t(asv.matrix))                                   #transposing to make sample name the row name
meta.df <- as.data.frame(GlobalPatterns@sam_data)
asv.full <- data.frame(asv,meta.df)
write.csv(asv.full, file = "full_asv.csv",quote = FALSE,sep = ",")

However, I can't figure out how to force taxonomy information into the column names, which makes the ASV table functionally useless for analysis.

EDIT: My preferred format is (abbreviated with faked metadata appended) as below. Tried to make a table, failed, have a fake code chunk.

Sample-ID / Species1 / Species2 / ...etc... / Metadata1 / Metadata2 /...etc... /
--------- / -------- / -------- / --------- / --------- / --------- /--------- /
Sample1   / 1        / 5        / ...etc... / lake      / summer    /...etc... /
Sample2   / 4        / 0        / ...etc... / bog       / spring    /...etc... /
1 Answers

I think you're looking for the phyloseq::psmelt function, which combines the otu_table, tax_table and sample_data tables into a single, long format table that is suitable for analysis.

One way of dealing with unresolved taxonomy is to assign the highest known taxonomy to any unresolved level. You can use the name_na_taxa function from the fantaxtic package for this, prior to using psmelt.

Related