Preparing Phyloseq amplicon data for LEfSe - meyermicrobiolab/Meyer_Lab_Resources GitHub Wiki
You can calculate the Linear Discriminant Analysis Effect Size with the Huttenhower galaxy page for LEfSe using amplicon data. However, you need to convert your tables from phyloseq into a format that can be uploaded to LEfSe.
This formatting can be started in R and finished with excel. I would love it if someone figured out how to do the whole thing in R.
First, you will have a phyloseq object for your project -- probably you want to use one that has removed all of the super low abundance ASVs. You will need to convert it to relative abundance. Then export the three matrices that are part of the phyloseq object as separate files. Here, I am also writing them out as text files, in case I need them later.
ps_ra<-transform_sample_counts(ps5, function(OTU) OTU/sum(OTU))
otu = as(otu_table(ps_ra), "matrix")
taxon = as(tax_table(ps_ra), "matrix")
metadata = as(sample_data(ps_ra), "matrix")
write.table(otu,"Fieldsites_ps5_RA_silva_nochloronomito_otu_table.txt",sep="\t",col.names=NA)
write.table(taxon,"Fieldsites_ps5_RA_silva_nochloronomito_taxa_table.txt",sep="\t",col.names=NA)
write.table(metadata,"Fieldsites_ps5_RA_silva_metadata.txt",sep="\t",col.names=NA)
The next step is to transpose the OTU (ASV) table and fix the column names. I had a lot of trouble with row and column names -- ended up fixing it in excel. Feel free to take on the challenge of getting this to work only with R.
otu <- read.table("Fieldsites_ps5_RA_silva_nochloronomito_otu_table.txt",sep="\t",header=TRUE)
names(otu)[1]<-"ASV"
otu.df <- as.data.frame(t(otu))
write.table(otu.df,"otu_table.txt",sep="\t",col.names=NA)
# delete first row in excel
otu <- read.table("otu_table.txt",sep="\t",header=TRUE)
Next, we need the taxonomy to be in one cell, not in hierarchical columns.
taxon <- read.table("Fieldsites_ps5_RA_silva_nochloronomito_taxa_table.txt",sep="\t",header=TRUE)
names(taxon)[1]<-"ASV"
taxon$Kingdom <- NULL
taxa<-unite(taxon, taxonomy, Phylum:Genus , sep = "|", remove = TRUE)
Finally, we merge the transposed otu table and condensed taxonomy:
lefse<-merge(taxa,otu,"ASV")
write.table(lefse,"lefse.txt",sep="\t",col.names=NA)
In excel, I added up to 2 rows of sample metadata at the top and removed the ASV column. I also had to fix my sample names that started with a number (R put an "X" in front of the sample name). DO NOT add more than 2 rows of metadata. Unused rows will throw an error in LEfSe.