Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove samples from phyloseq object that have less than n taxa? #969

Closed
nextgenusfs opened this issue Jul 18, 2018 · 2 comments
Closed

remove samples from phyloseq object that have less than n taxa? #969

nextgenusfs opened this issue Jul 18, 2018 · 2 comments

Comments

@nextgenusfs
Copy link

I feel like there should be an easy answer to this question, but I can't seem to find it. All I would like to do is to drop samples from a phyloseq object where there are less than n number of taxa. Anybody know how to do this? ntaxa() yields total number of OTUs. I know that samples can be dropped with the prune_samples() function requiring a list of TRUE/FALSE, but not sure how to generate that list.

@vmikk
Copy link

vmikk commented Jul 18, 2018

Hello Jon!
You can try to use estimate_richness to count the number of OTUs per sample.

library(phyloseq)
data("esophagus")

phyloseq_richness_filter <- function(physeq, mintaxa = 10){
  sp <- estimate_richness(physeq, measures = "Observed")
  samples_to_keep <- rownames(sp)[ which(sp$Observed >= mintaxa) ]
  
  
  if(length(samples_to_keep) == 0){
    stop("All samples will be removed.\n")  
  }
  
  if(length(samples_to_keep) == nsamples(physeq)){
    cat("All samples will be preserved\n")
    res <- physeq
  }
  
  if(length(samples_to_keep) < nsamples(physeq)){
    res <- prune_samples(samples = samples_to_keep, x = physeq)
  }
 
  return(res)
}


phyloseq_richness_filter(esophagus, mintaxa = 30)
phyloseq_richness_filter(esophagus, mintaxa = 10)
phyloseq_richness_filter(esophagus, mintaxa = 100)

But keep in mind that estimate_richness will work only with count data (not proportions).
I made the pull-request to fix this behaviour (#891), but it was not merged yet.

HTH,
Vladimir

@nextgenusfs
Copy link
Author

Thanks @vmikk! Hadn't thought of the species richness function - as I kept looking for something to sum() or count(), etc. Much appreciated!

-Jon

@joey711 joey711 closed this as completed Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants