R/ReferenceScore_Plot.R

Defines functions check.input.plot_reference_scores dacomp.plot_reference_scores

Documented in dacomp.plot_reference_scores

#' Plot histogram of median SD statistics from a reference selection result object
#'
#' @param ref_select  Computation result of \code{dacomp.select_references}
#' @param label Histogram label.
#' @param quantiles_to_plot Percentiles marked by vertical lines. Default value is \code{c(0.5,0.7,0.9)}.
#' @param breaks_param Passed as argument with the same name to the \code{hist} function.
#'
#' @return
#' @export
#'
#' @examples
#' \dontrun{
#' library(dacomp)
#' 
#' set.seed(1)
#' 
#' data = dacomp.generate_example_dataset.two_sample(m1 = 100,
#'        n_X = 50,
#'        n_Y = 50,
#'        signal_strength_as_change_in_microbial_load = 0.1)
#' 
#' #select references: (may take a minute)
#' result.selected.references = dacomp.select_references(X = data$counts,
#'                                                      median_SD_threshold = 0.6, #APPLICATION SPECIFIC
#'                                                      verbose = T)
#' 
#' length(result.selected.references$selected_references)
#'
#' #plot the reference selection scores (can also be used to better set the median SD threshold)
#' dacomp.plot_reference_scores(result.selected.references)
#'
#' } 
dacomp.plot_reference_scores = function(ref_select,label="Histogram of medianSD statistic, across taxa",quantiles_to_plot = c(0.5,0.7,0.9), breaks_param = 30){
  
  #check inputs:
  input_check_result = check.input.plot_reference_scores(ref_select,label,quantiles_to_plot,breaks_param)
  if(!input_check_result)
    stop('Input check failed on dacomp.plot_reference_scores')
  
  #plot histogram
  hist(ref_select$scores,breaks = breaks_param,main = label,xlab = "medianSD statistic")
  sorted_scores = sort(ref_select$scores)
  threshold_ind = 1
  
  #draw quantiles:
  qunatiles_in_scores = quantile(ref_select$scores,probs = quantiles_to_plot)
  if(length(quantiles_to_plot)>0)
    for(i in 1:length(quantiles_to_plot)){
      abline(v = qunatiles_in_scores,col = 1,lty = 2,lwd = 3)
    }
}

#internal function for checking inputs
check.input.plot_reference_scores = function(ref_select,label,quantiles_to_plot,breaks_param){
  # ref_select - is class
  if(class(ref_select) != CLASS.LABEL.REFERENCE_SELECTION_OBJECT)
    stop(paste0('ref_select must be a valid object of class ',CLASS.LABEL.REFERENCE_SELECTION_OBJECT,', generated by the function dacomp.select_references(...)'))
  # label
  if(!is.character(label))
    stop('label must be of type character')
  # quantiles_to_plot - are between 0 and 1
    if(!is.null(quantiles_to_plot)){
      MSG_quantiles_to_plot = 'quantiles_to_plot must be a vector of numerics between 0 at 1'
      if(any(!is.numeric(quantiles_to_plot)))
        stop(MSG_quantiles_to_plot)  
      if(any(quantiles_to_plot<0) | any(quantiles_to_plot>1))
        stop(MSG_quantiles_to_plot)  
    }
    
  # breaks_param- breaks on the graph
  
  return(TRUE)  
}
barakbri/dacomp documentation built on June 17, 2021, 11:20 p.m.