R/CIBERSORT_abs_NF.R

Defines functions CIBERSORT_abs_NF

Documented in CIBERSORT_abs_NF

##' CIBERSORT_abs_NF
##'
##' Function that generates the absolute deconvolution plot with the CIBERSORT method, with the non-fractional plot, and returns a df resulting from the deconvolution.
##' @param matrix expression matrix, this matrix must be in .txt format, organized in tabulated columns with one column containing the list of genes, with the name Gene.symbol, Gene.Symbol or Geneid, and in the rest of the columns the names of the samples and the expression values for the genes.  (Example. Counts.HUMAN_nodup_Sal_Veh_IPs_INPUTs.txt).
##' @param sig.matrix Signature matrix, this matrix must be in .txt format, organized in tabulated columns with one column containing the list of genes, with the name Gene.symbol, Gene.Symbol or Geneid, and in the rest of the columns the cell types and the expression values for the genes.  (Example. Monaco-ABIS_Immune11_Microarray.txt).
##' @param results_dir Directory to which the graphics will be generated.
##' @param height Number that defines the height of the graph, to increase or decrease the height, which has a default value of 10.
##' @param name Alternative name for the chart, which by default has the structure Sig_type_method.matrixName_Plot.png (Example. "graph_1")
##' @return Returns the generated graph and the df of the deconvolution.
##' @author Nidia Barco Armengol
##' @export
##'
##' @examples
##' CIBERSORT_abs_NF("Counts.HUMAN_nodup_Sal_Veh_IPs_INPUTs.txt", "Monaco-ABIS_Immune11_Microarray.txt", results_dir = "/bicoh/nidia/Deconv/Brain_MMartin/test", height= 8, name= "MONACO")

CIBERSORT_abs_NF <- function(matrix, sig.matrix, results_dir, height = 10, name= FALSE){
  require(usethis)
  require(devtools)
  require(gplots)
  require(data.table)
  require(ggplot2)
  require(dplyr)
  require(tidyr)
  require(immunedeconv)
  require(RColorBrewer)
  source("CIBERSORT.R")
  # Construcció de la matriu de deconvolució absoluta i conversió d'aquesta en un dataframe
  res_ciber = CIBERSORT(sig_matrix=sig.matrix,
                        mixture_file = matrix,
                        QN = F,perm=100,absolute = T,abs_method='no.sumto1')
  res_ciber = as.data.frame(t(res_ciber))
  res_ciber$cell_type=rownames(res_ciber)
  C_abs <- res_ciber[1:(length(rownames(res_ciber))-4),]

  # Defineix el nom de l'arxiu depenent de si l'usuari introdueix el nom o si es predeterminat
  if (name==FALSE){
    title <- gsub(".txt","_",sig.matrix)
    file <- c("CIBERSORT.abs_", title , "_","plot.png")
  }else{
    title <- name
    file <- c("CIBERSORT.abs_", title , "_","plot.png")
  }
  file <- paste(file, collapse = "")

  # Selecció de les paletes de colors per als grafics
  colors <- c(brewer.pal(12, "Set3"), brewer.pal(12, "Paired"))
  getPalette = colorRampPalette(colors)

  # Creació del grafic i guardat d'aquest al directori de resultats
  p=res_ciber[1:((grep("P-value",rownames(res_ciber)))-1),] %>%
    gather(sample, fraction, -cell_type) %>%
    # plot as stacked bar chart
    ggplot(aes(x=sample, y=fraction, fill=cell_type)) +
    geom_bar(stat='identity') +
    coord_flip() +
    scale_fill_manual(values = getPalette(length(rownames(res_ciber)))) +
    scale_x_discrete(limits = rev(levels(res_ciber)))
  ggsave(filename=paste(results_dir,file, sep = "/"),dpi=300,width =9, height = height)
  return(C_abs)
}
margenomics/Deconvolution documentation built on March 19, 2022, 5:44 a.m.