R/combine_abundances.R

Defines functions HotLoadings.combine_abundances

Documented in HotLoadings.combine_abundances

#' Combine abundances between groups
#'
#' @param top_features A data frame containing \code{n_top} features generated by \code{\link{HotLoadings.top_features}}.
#' @param mean_relative_abundances A data frame obtained with \code{\link{HotLoadings.mean_relative_abundances}} containings mean relative abundances for each feature.
#' @param Y_name A character string indicating which is the variable associated with component specified. Must be a dicothomous variable.
#' @return A data frame containings \code{n_top} feature associated with a specified \code{component}, information about their mean relative abundance in the 2 \code{Y_name} categories, loading values, and proportions useful for plot.
#' @seealso \code{\link{HotLoadings.mean_relative_abundances}} to create mean relative abundances data frame and \code{\link{HotLoadings.plot_loadings}} for plot.


HotLoadings.combine_abundances <- function(top_features,mean_relative_abundances,Y_name){
  # Calculate proportion between 2 conditions.
  Y_levels <- unique(top_features[,Y_name])
  other_indexes <- c(nrow(top_features)+1,2*(nrow(top_features)+1))
  mean_relative_abundances <- mean_relative_abundances[-other_indexes,]
  tot_prob <- mean_relative_abundances$Relative_Abundance[mean_relative_abundances[,Y_name]==Y_levels[1]]+mean_relative_abundances$Relative_Abundance[mean_relative_abundances[,Y_name]==Y_levels[2]]

  # For level 1 associated features
  prob_level_1 <- mean_relative_abundances$Relative_Abundance[mean_relative_abundances[,Y_name]==Y_levels[1]] / tot_prob * top_features$loadings
  # For level 2 associated taxa
  prob_level_2 <- top_features$loadings-prob_level_1

  # Repeat observations
  top_features_divided <- rbind(top_features,top_features)

  # portion of loading length to give to level1 and level2
  top_features_divided$proportions <- c(prob_level_1,prob_level_2)
  # Adding Relative Abundances from compute_mean_relative_abundances()
  top_features_divided$Relative_Abundance <- mean_relative_abundances$Relative_Abundance
  # Levels for Y_name in Relative Abundances, maybe different from top_feature oreder
  top_features_divided$class <- mean_relative_abundances[,Y_name]
  # Combinig top_feature Y_name with class
  top_features_divided$combined <- as.factor(paste(top_features_divided[,Y_name], top_features_divided$class))
  pos1 <- paste(Y_levels[1],Y_levels[1])
  pos2 <- paste(Y_levels[1],Y_levels[2])
  pos3 <- paste(Y_levels[2],Y_levels[2])
  pos4 <- paste(Y_levels[2],Y_levels[1])
  top_features_divided$combined <- factor(top_features_divided$combined,levels = c(pos1,pos2,pos3,pos4), ordered = TRUE)
  offset <- 0.005
  top_features_divided$just <- as.numeric(as.character(factor(top_features_divided$combined,levels = c(pos2, pos1, pos3, pos4),labels = c(offset,-offset,offset,-offset))))
  return(top_features_divided[order(top_features_divided$combined),])
}
mcalgaro93/HotLoadings documentation built on June 13, 2021, 10:01 p.m.