#' @export recipe.phyloseq
#' @method recipe phyloseq
#' @importFrom recipes recipe
#' @importClassesFrom phyloseq phyloseq
#' @title Setting up recipes for \code{phyloseq} objects
#' @param phylo Object of \code{phyloseq} class
#' @param formula A model formula
recipe.phyloseq <- function(phylo, formula = NULL,..., vars = NULL, roles = NULL){
if (!is.null(formula)){ # check if formula is not null
left <- rlang::f_lhs(formula)
right <- rlang::f_rhs(formula)
}
return(0)
}
#' @description Given a vector of variable names extract it from the phyloseq object from either otu_table or sample_data
#' @param vars Vector of variable names
#' @param phylo phyloseq type object
#' @return A tibble with all the data frames
#' @importFrom dplyr full_join
#' @importFrom tibble as_tibble rownames_to_column
#' @importFrom phyloseq taxa_names getslots.phyloseq sample_variables
extract_vars <- function(phylo, vars, ...){
if (class(phylo) != "phyloseq"){
rlang::abort(message = "Require phyloseq object")
}
extracted_df <- tibble(samp_id = sample_names(phylo))
slots <- phyloseq::getslots.phyloseq(phylo)
if ('otu_table' %in% slots){
select_idx <- match(vars, phyloseq::taxa_names(phylo))
if (length(na.omit(select_idx)) > 0){
if(phyloseq::taxa_are_rows(phylo) == TRUE){
df <- as(otu_table(phylo)[select_idx,], "matrix") # get all taxa from otu_table
df <- t(df)
} else {
df <- as(otu_table(phylo)[,select_idx], "Matrix")
}
df <- as.data.frame(df) %>% tibble::rownames_to_column(var = 'samp_id') %>% tibble::as_tibble() # convert to tibble
extracted_df <- dplyr::full_join(extracted_df, df, by = 'samp_id')
}
}
if ("sam_data" %in% slots){
select_idx <- match(vars, phyloseq::sample_variables(phylo))
if (length(na.omit(select_idx)) > 0){
df <- as(sample_data(phylo)[,select_idx], "matrix")
df <- as.data.frame(df) %>% rownames_to_column(var = 'samp_id') %>% as_tibble()
extracted_df <- dplyr::full_join(extracted_df, df, by = 'samp_id')
}
}
return(extracted_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.