R/deltaSNPindex_plot.R

Defines functions deltaSNPindex_plot

Documented in deltaSNPindex_plot

#delta(SNP-index): wrapper function

#' @title delta(SNP-index) Wrapper Function
#' @description This wrapper function is used to fully run the delta(SNP-index) method, by calling all the funcitons involved in plotting the delta(SNP-index) values from each bulk against the mid position of the corresponding window of a specific chromosome. 
#'
#' \deqn{mean_M_SNPindex - mean_WT_SNPindex.WT}
#'
#' @param vcf.list object containing meta information and vcf data frame
#' @param wtBulk Wild-Type pool
#' @param mBulk Mutant pool 
#' @param variants variants to be considered. Default is "SNP" (allowed: "SNP" or "all")
#' @param min.SNPindex min value allowed for the SNP index (default=0.3)
#' @param max.SNPindex max value allowed for the SNP index (default=0.9)
#' @param min.DP min value allowed for the read depth (default=50)
#' @param max.DP max value allowed for the read depth (default=200)
#' @param min.GQ min value allowed for the genotype quality (default=99)
#' @param chrID chromosome ID of interest
#' @param chr chromosome name printed on the plot
#' @param windowSize window size (default=1000000)
#' @param windowStep window step (default=10000)
#' @param filename file name under which the file will be saved (default="plot_deltaSNPindex_chX")
#' @param path path where the file will be saved (default=current working directory)
#' @param dpi resolution value. If no value is given, plots will be generated but not saved
#' @param width width value (default=7.5)
#' @param height height value (default=5)
#' @param units size units (default="in")
#' 
#' @details Wrapper function that sequentially calls the required functions involved in generating the delta(SNP-index) plot:
#' calc_SNPindex(), filter_SNPindex(), extract_chrIDs(), slidingWindow(), calc_deltaSNPindex() and plot_deltaSNPindex(). 
#' The resulting plot will show (for each bulk) mean SNP-index values against the mid position of the corresponding window of a specific chromosome.
#'
#' @importFrom dplyr %>%
#' @export deltaSNPindex_plot
#' @examples
#' ## Use default values WITHOUT saving the plot
#' deltaSNPindex_plot(vcf.list=vcf_list, 
#'               wtBulk="pool_S3781_minus", 
#'               mBulk="pool_S3781_plus", 
#'               chrID="SL4.0ch03", 
#'               chr=3)
#' ## OR use default values AND save the plot
#' deltaSNPindex_plot(vcf.list=vcf_list, 
#'               wtBulk="pool_S3781_minus", 
#'               mBulk="pool_S3781_plus", 
#'               chrID="SL4.0ch03", 
#'               chr=3, 
#'               dpi=1200)
#' #OR customise default parameters
#' deltaSNPindex_plot(vcf.list=vcf_list, 
#'               wtBulk="pool_S3781_minus", 
#'               mBulk="pool_S3781_plus", 
#'               variants="all",
#'               min.SNPindex=0.25, 
#'               max.SNPindex=0.8, 
#'               min.DP=60, 
#'               max.DP=250, 
#'               min.GQ=98,
#'               chrID="SL4.0ch03", 
#'               chr=3,
#'               windowSize=2000000, 
#'               windowStep=20000,
#'               filename="deltaSNPindex_chrom03", 
#'               path="Document/Plots", 
#'               dpi=1200,
#'               width=20, 
#'               height=12, 
#'               units="cm")   


deltaSNPindex_plot<- function(vcf.list, wtBulk, mBulk, variants="SNP",
                          min.SNPindex=0.3, max.SNPindex=0.9, min.DP=50, max.DP=200, min.GQ=99,
                          chrID, chr, windowSize=1000000, windowStep=10000, 
                          filename=paste0("plot_deltaSNPindex_ch", chr), path=getwd(), 
                          dpi, width=7.5, height=5, units="in"){
  
  #Calculate SNP-index of each variant in each bulk
  vcf_df_SNPindex <- BSAvis::calc_SNPindex(vcf.list$df, wtBulk, mBulk, variants)
  
  #Filter variants
  vcf_df_SNPindex_filt <- BSAvis::filter_SNPindex(vcf_df_SNPindex, min.SNPindex, max.SNPindex, min.DP, max.DP, min.GQ)
  
  #Create list of chromosome IDs in the way they appear in the VCF file
  chrList <- BSAvis::extract_chrIDs(vcf.list$meta)
  
  #Apply sliding window to calculate mean SNP-index in each window of a specifc size and step for each bulk
  SNPindex_windows <- BSAvis::slidingWindow(vcf.list$meta, chrList, chrID, windowSize, windowStep, vcf_df_SNPindex_filt)
  
  #Calculate delta(SNP-index)
  deltaSNPindex_windows <- BSAvis::calc_deltaSNPindex(SNPindex_windows)
  
  #Plot delta(SNP-index) across the positions of a given chromosome
  BSAvis::plot_deltaSNPindex(deltaSNPindex_windows, chr, filename, path, dpi, width, height, units)
}
EG-lisy/BSAvis documentation built on Dec. 17, 2021, 5:38 p.m.