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/
580 stars 187 forks source link

Converting from Dataframe back to Phyloseq Object #1479

Open dylan-lawrence opened 3 years ago

dylan-lawrence commented 3 years ago

I find for most of the work I do I use psmelt to generate dataframes before doing any plotting or manipulation. However, I have a dataset that need ordination plots and I've run into an issue trying to convert this data back into a phyloseq object. It does not seem seem trivial to generate the otu_table and tax_table plus metadata to build the phyloseq object the long way. Do you have a script or example of how to convert the resulting dataframe from psmelt back to a phyloseq object?

And either way, might I suggest a potential for a dataframe_to_phyloseq function in the future to facilitate this transition?

ycl6 commented 3 years ago

Hi @dylan-lawrence, it shouldn't be too difficult if you have the correct data structures. Have you checked out this tutorial on creating a phyloseq object?

dylan-lawrence commented 3 years ago

I did end up figuring this out, but it was not trivial. Requires a not-insignificant amount of reshaping and formatting data as well as re-generating the tree.

Dnman-code commented 2 years ago

Hi @dylan-lawrence, I am also trying to convert my data into a phyloseq object. Care to share how you did yours? @ycl6 I am also looking at the tutorial now too. The type of files I am working with is from PICRUSt2 outputs and the mapping file.

ycl6 commented 2 years ago

Hi @Dnman-code You can find some useful materials on Picrust2 and Phyloseq here: https://usfomicshub.github.io/Workshops/Microbiome_Workshop_Materials/microbiome_workshop_demos/day3/Ranalysis/PartII https://rstudio-pubs-static.s3.amazonaws.com/566863_687400bd7e8742568e73bf167fc42d3d.html

Dnman-code commented 2 years ago

Hi Hsuan-Lin

I have been trying the Picrust2 downstream stuff for my analysis and have struggled to convert a CSV file into phyloseq objects. I also saw you created a tutorial for a similar study. I haven't tried applying it to my data because of your workflows from previous parts. I will try following the USF tutorial, and thanks always for your help.

Kind regards

BellaTsch commented 1 year ago

@dylan-lawrence any chance you could share how you converted your dataframe back to phyloseq? Thank you in advance

cpavloud commented 7 months ago

I was browsing the internet for a solution to this, but I ended up finding a way to do it quite easily.

Assuming that pd <- psmelt(physeq) and assuming that the physeq table did not contain metadata (so no sample_data)

Then

pd_wider <- pd %>% pivot_wider(names_from = Sample, values_from = Abundance) pd_wider[is.na(pd_wider)] <- 0

The TAXONOMY table can be retrieved as pd_wider_tax <- select(pd_wider, OTU, Root, Kingdom, Phylum, Class, Order, Family, Genus, Species) and the OTU table can be retrieved as pd_wider_bio <- select(pd_wider, -Root, -Kingdom, -Phylum, -Class, -Order, -Family, -Genus, -Species)

Then you just need to re-create the rownames pd_wider_tax <- pd_wider_tax %>% remove_rownames %>% column_to_rownames(var="OTU") and pd_wider_bio <- pd_wider_bio %>% remove_rownames %>% column_to_rownames(var="OTU")

Transform the data frames to matrices pd_wider_tax <- as.matrix(pd_wider_tax) pd_wider_bio <- as.matrix(pd_wider_bio)

And just re-create the phyloseq object TAX = tax_table(pd_wider_tax) OTU = otu_table(pd_wider_bio, taxa_are_rows = TRUE)

physeq_recreated <- phyloseq(OTU, TAX)