YuLab-SMU / MicrobiotaProcess

:microbe: A comprehensive R package for deep mining microbiome
https://www.sciencedirect.com/science/article/pii/S2666675823000164
183 stars 37 forks source link

how to create a phyloseq object based on a given otu_table and sample_names #10

Open LuffyLuffy opened 4 years ago

LuffyLuffy commented 4 years ago

Hi, there, It is easy to reproduce example of ggrarecurve, however, when I try to create the phyloseq object based on my own data, errors come out. I wonder how to create such a formal phyloseq object for the ggrarecurve. Error in seq.default(0, sdepth, by = step) : 'to' must be a finite number data(test_otu_data) otu1<-as.data.frame(test_otu_data@otu_table) sd1<-as.data.frame(test_otu_data@sam_data) test_otu_data2<-phyloseq(otu_table(otu1,taxa_are_rows = T), sample_names(sd1)) ggrarecurve(test_otu_data2, indexNames=c("Chao1","ACE"), chunks=100) + theme(legend.spacing.y=unit(0.02,"cm"), legend.text=element_text(size=6))

xiangpin commented 4 years ago

You should use sample_data of phyloseq instead of sample_names. In fact, The functions of MicrobiotaProcess were designed to support the phyloseq class and data.frame (nrow sample * ncol feature). So you have two choose. For example in the following.

library(MicrobiotaProcess)
library(ggplot2)
library(phyloseq)

# For phyloseq class
data(test_otu_data)
otu1<-as.data.frame(test_otu_data@otu_table)
sd1<-as.data.frame(test_otu_data@sam_data)

test_otu_data2<-phyloseq(otu_table(otu1,taxa_are_rows = T),
                         sample_data(sd1))
# This is to reproduce
set.seed(1024)
p1 <- ggrarecurve(test_otu_data2,
            indexNames=c("Chao1","ACE"),
            chunks=100) +
      guides(color=guide_legend(keywidth=0.5, keyheight=0.5))+
      theme(legend.spacing.y=unit(0.02,"cm"),
            legend.text=element_text(size=6))

# For data.frame
otu2 <- data.frame(t(test_otu_data@otu_table), check.names=FALSE)
sd2 <- data.frame(test_otu_data@sam_data, check.names=FALSE)
set.seed(1024)
p2 <- ggrarecurve(obj=otu2,
                  sampleda=sd2,
                  indexNames=c("Chao1","ACE"),
                  chunks=100) +
      guides(color=guide_legend(keywidth=0.5, keyheight=0.5))+
      theme(legend.spacing.y=unit(0.02,"cm"),
            legend.text=element_text(size=6))
p <- p1 + p2
p

rare_curve

LuffyLuffy commented 4 years ago

Thanks ! it works! By the way, your rarefaction method is really fast! Previously, I try to use Rarefy in package GUniFrac, and make an iteration for 99 runs in different sampling depth, and the ACE and Chao1 index is not able to be smooth. Your method is great!

holypeggy commented 4 years ago

Hi, xiangpin I am appreciate if you can provide a sample in the ggrarecurve mapping setting, as i want to use the stat_smooth as you did in your recently publised paper. Thanks.

xiangpin commented 4 years ago

If you want to visualize the curve of group, first you should rare phyloseq object.

library(MicrobiotaProcess)
library(phyloseq)
library(ggplot2)

data(test_otu_data)
test_otu_data
set.seed(1024)
ps <- rarefy_even_depth(test_otu_data)
ps

p <- ggrarecurve(
                 ps,
                 factorNames="group",
                 indexNames=c("Observe", "Chao1", "ACE"),
                 chunks=800,
     ) +
     theme_bw()+
     theme(axis.text=element_text(size=8), panel.grid=element_blank(),
           strip.background = element_rect(colour=NA,fill="grey"),
           strip.text.x = element_text(face="bold"))
p
holypeggy commented 4 years ago

It works. Thank you very much!

Adamyazori commented 3 years ago

Hi there, I am getting the error below, meanwhile I have all my packages installed. Any idea on what am missing. Thanks

Error in get_rarecurve(obj = ps, chunks = 400) : could not find function "get_rarecurve"

xiangpin commented 3 years ago

The get_rarecurve is the function in newest(development) version. Please update MicrobiotaProcess following this.

Adamyazori commented 3 years ago

Thank you very much . That works.