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

how to include phyloseq functions arguments into a custom function? #1336

Open Gian77 opened 4 years ago

Gian77 commented 4 years ago

Hello,

I am trying to pass arguments to a functions that has some Phyloseq function within it. However, I didn't understand why it is not working. Please see the code below

> FilterGroup <- function(physeq, var1, var2){
+   physeq %>% 
+     subset_samples(Status%in%c(var1) & Species%in%c(var2)) -> physeq_filt
+     otu_table(physeq_filt) <- otu_table(physeq_filt)[which(
+       rowSums(otu_table(physeq_filt)) > 0),] 
+ return(physeq_filt)
+ }

This is the error I am getting below

> FilterGroup(physeq_bact, "bleached", "Acropora millepora")
 Error in Status %in% c(var1) : object 'var1' not found 

I saw there was another issue open on this but not solved https://github.com/joey711/phyloseq/issues/487 and I found a workaround (see below) but, has been found a real fix yet in Phyloseq?

FilterGroup <- function(physeq, var1, var2){
  physeq1 <-subset(sample_data(physeq),Status==var1)
  physeq2<-subset(sample_data(physeq1),Species==var2)
  phy_subset<-merge_phyloseq(tax_table(physeq),
                             otu_table(physeq),
                             refseq(physeq),
                             physeq2)
  otu_table(phy_subset) <- otu_table(phy_subset)[which(
    rowSums(otu_table(phy_subset)) > 0),] 
  return(phy_subset)
}
FilterGroup(physeq_fungi_filt, "bleached", "Acropora millepora")

Thanks a lot in advance, Gan

samd1993 commented 4 years ago

HI Gian,

Thanks for posting the workaround. I am having a similar issue. If I understand correctly, your workaround works because the arguments of your function are no longer within phyloseq specific functions but instead are within the subset function which isn't phyloseq specific?

A simplified version of the function I am trying to make is:

subset_function<- function(Data,Rank,Taxa) {

subset_taxa(Data, Rank == Taxa)

}
sf= subset_function(physeq,Phylum,"Acidobacteria")  

But I get the same error as you where R cannot find object "Rank". I guess I can do a workaround like you did with using the subset function first.

Gian77 commented 4 years ago

You're welcome @samd1993, if you use subset() and the re-create a Phyloseq object it will work.