rbioinformaticsphyloseq

R phyloseq tax_glom used in a function give a error


I'm trying to make a function to obtain a table in a relative abundance of any given taxrank with PHYLOSEQ, something like:

Relative_Table <- function (PhyloObj, TRank) {
    GROUP <- tax_glom(PhyloObj, taxrank="TRank")
    Percent <- transform_sample_counts(GROUP, function(x)100* x / sum(x))
    OTUglom <- otu_table(Percent)
    TAXglom <- tax_table(Percent)[,"TRank"]
    GroupTable <- merge(TAXglom, OTUglom, by=0, all=TRUE)
    GroupTable$Row.names = NULL
    return(GroupTable)
}

so when I use it like: TABLE <- Relative_Table(PHYLO_Obj, Phylum) it give me an error:

Error in tax_glom(PhyloObj, taxrank = "TaxonRank") : Bad taxrank argument. Must be among the values of rank_names(physeq)

nevertheless when I use the the taxrank inside of the function, it works well:

Relative_Table <- function (PhyloObj) {
    GROUP <- tax_glom(PhyloObj, taxrank="Phylum")
    Percent <- transform_sample_counts(GROUP, function(x)100* x / sum(x))
    OTUglom <- otu_table(Percent)
    TAXglom <- tax_table(Percent)[,"Phylum"]
    GroupTable <- merge(TAXglom, OTUglom, by=0, all=TRUE)
    GroupTable$Row.names = NULL
    return(GroupTable)
}

What its wrong with the first option ??, I just Want to use any given taxrank (Phylum, Class....... Genus) in the function and generate a Table !!!!

Thanks


Solution

  • It's hard to say without a reproducible example, but I strongly suspect that the problem is that you are treating TRank as a string rather than a symbol names. In other words, try taking out the quotation marks around "TRank" in these two lines:

    GROUP <- tax_glom(PhyloObj, taxrank=TRank)
    

    and

    TAXglom <- tax_table(Percent)[,TRank]
    

    The error you cite makes it sound like you're using TaxonRank as the argument name in your actual function, while one you showed us uses TRank (that probably doesn't matter, but it's a little confusing for the reader)