mikemc / phyloseqpp

Phyloseq extensions and functions for tidier analysis of microbiome data
Other
2 stars 0 forks source link

Enable purrr-style anonymous functions in phyloseq functions #8

Open mikemc opened 5 years ago

mikemc commented 5 years ago

Idea is to be able to do

ps.ra <- transform_sample_counts(ps, ~ . / sum(.))
# or simply
ps.ra <- transform_sample_counts(ps, . / sum(.))
# and
ps.filt <- filter_taxa(ps, ~ sum(. > 0) > 5, prune = TRUE)

see https://purrr.tidyverse.org/articles/other-langs.html

mikemc commented 5 years ago

For transform_sample_counts(), a simple wrapper like

transform_sample_counts <- function(physeq, .f, ...){
    fun <- purrr::as_mapper(.f)
    phyloseq::transform_sample_counts(physeq, fun, ...)
}

works for single-argument functions, which might be all we really want, though should make sure the failure on multi-arg anonymous functions doesn't indicate any potential issues.

mikemc commented 5 years ago

should make sure the failure on multi-arg anonymous functions doesn't indicate any potential issues.

It turns out phyloseq's transform_sample_counts currently doesn't work on multi-arg functions due to a missing ... in the test call to fun.

transform_sample_counts <- function(physeq, fun, ...){
    # Test the user-provided function returns a vector of the same length as input.
    if( !identical(length(fun(1:10)), 10L) ){stop("`fun` not valid function.")} # Should be fun(1:10, ...)