R/get_descriptives_ImprintingCarreras.R

Defines functions get_descriptives_ImprintingCarreras

Documented in get_descriptives_ImprintingCarreras

#' Get descriptives Carreras-Gallo IRC
#'
#' Get counts, proportions and plots for Carreras-Gallo IRCs attending to any criteria as
#' Bonferroni, FDR
#'
#' @param position string. IRC
#' @param criteria boolean. Boolean with criteria like Bonferroni, FDR,...
#' @param namecriteria string, criteria name for plot and to add to descriptives
#' @param outputdir string. Output path to store file with results, by default results
#' are written in current dir
#' @param outputfile string. File name to store results if no name is provided results
#' are not written.The suffix "_DescCarrerasIRC.txt" and "_PlotCarrerasIRC.pdf" are added
#' respectively for descriptive file and plots.
#'
#' @return File with descriptive and plots from CpG Annotation
#'
#'
#' @export
get_descriptives_ImprintingCarreras <- function(position, criteria, namecriteria, outputdir = ".", outputfile = NULL)
{

   if( is.null(outputfile) ){
      stop("output file is needed to write descriptives")
   }else {
      if(!is.null(outputdir) & !is.na(outputdir) & outputdir!='.')
         dir.create(outputdir, showWarnings = FALSE, recursive = TRUE)
      # Output filename
      filename <- tools::file_path_sans_ext(basename(outputfile))
   }

   ctable <- as.data.frame.matrix(table(position, criteria))
   ptable <- as.data.frame.matrix(prop.table(ctable))

   desc.fname <- paste0(outputdir,"/",filename,"_",namecriteria,"_DescCarrerasIRC.txt")

   write(sprintf('\t\t=================================================\n\t\t  Descriptive Resume for Carreras-Gallo IRCs with %s criteria \n\t\t=================================================\n', namecriteria), file = desc.fname)
   write(sprintf('-------------------\n Model : %s\n-------------------\n',filename), file = desc.fname, append = TRUE)
   write(sprintf('\nCount table : \n '), file = desc.fname, append = TRUE)
   suppressWarnings(write.table(ctable, file = desc.fname, append = TRUE))
   write(sprintf('\nProportion table : \n'), file = desc.fname, append = TRUE)
   suppressWarnings(write.table(ptable, file = desc.fname, append = TRUE))
   write(sprintf('\nChi-Square Test : \n'), file = desc.fname, append = TRUE)
   write(sprintf('\tX-squared : %f',chisq.test(ctable)[1]), file = desc.fname, append = TRUE)
   write(sprintf('\tdf : %f',chisq.test(ctable)[2]), file = desc.fname, append = TRUE)
   write(sprintf('\tp-value : %f',chisq.test(ctable)[3]), file = desc.fname, append = TRUE)

   pt <- as.data.frame(cbind(position, criteria))
   colnames(pt) <- c('position',namecriteria)
   ptc <- ggplot(pt, aes(position)) +
      geom_bar(aes(fill = criteria)) +
      scale_x_discrete(guide = guide_axis(angle = 90))

   plot.fname <- paste0(outputdir,"/",filename,"_",namecriteria,"_PlotCarrerasIRC.pdf")

   pdf(plot.fname)
      print(ptc)
   dev.off()

}
isglobal-brge/EASIER documentation built on July 17, 2025, 2:14 a.m.