Skip to content
Snippets Groups Projects
Select Git revision
  • 56d5a95e949773f82ba0e7c6c75aee80ba40bd1c
  • master default
  • patch-1
3 results

countsBoxplots.R

Blame
  • countsBoxplots.R 2.41 KiB
    #' Box-plots of (normalized) counts distribution per sample
    #'
    #' Box-plots of raw and normalized counts distributions per sample to assess the effect of the normalization
    #'
    #' @name countsBoxplots
    #' @param results results produced by DESeq2 or limma with ChIPuana
    #' @param conditions factor vector of the condition from which each sample belongs
    #' @param method "DESeq2" or "limma"
    #' @param col colors of the boxplots (one per biological condition)
    #' @param outfile TRUE to export the figure in a png file
    #' @return A boxplots of the raw and normalized counts
    #' @author Marie-Agnes Dillies, Hugo Varet and Maëlle Daunesse
    
    countsBoxplots <- function(results, conditions, method,
                               col = c("#f3c300", "#875692", "#f38400", "#a1caf1", "#be0032", "#c2b280", "#848482", "#008856", "#e68fac", "#0067a5", "#f99379", "#604e97"), 
                               outfile=FALSE){
      # extract counts and norm counts
      idx <- which(names(results[[1]]) == "baseMean")
      tmp <- results[[1]][,1:(idx-1)]
      nbSamples <- ncol(tmp)/2
      counts <- removeNull(tmp[,1:nbSamples])
      norm.counts <- tmp[,(nbSamples+1):ncol(tmp)]
      norm.count <- removeNull(norm.counts)
      colnames(norm.counts) <- gsub("norm.", "", colnames(norm.counts))
      
      if (outfile) png(filename="countsBoxplots.png", width=2*min(2200, 1800+800*ncol(norm.counts)/10), height=1800, res=300)
      # raw counts
      d <- stack(as.data.frame(log2(as.matrix(counts))))
      d$conditions <- rep(conditions, each=nrow(counts))
      p1 <- ggplot(d) + 
        geom_boxplot(aes(x=.data$ind, y=.data$values+1, fill=.data$conditions), show.legend=FALSE) +
        labs(fill="") +
        scale_fill_manual(values=col) +
        xlab("Samples") +
        ylab("Raw counts (log scale)") +
        ggtitle("Raw counts distribution") +
        theme_light() +
        theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5))
      # norm counts
      if (method=="DESeq2") norm.counts <- log2(as.matrix(norm.counts))
      d <- stack(as.data.frame(norm.counts))
      d$conditions <- rep(conditions, each=nrow(norm.counts))
      p2 <- ggplot(d) + 
        geom_boxplot(aes(x=.data$ind, y=.data$values+1, fill=.data$conditions), show.legend=FALSE) +
        labs(fill="") +
        scale_fill_manual(values=col) +
        xlab("Samples") +
        ylab("Normalized counts (log scale)") +
        ggtitle("Normalized counts distribution") +
        theme_light() +
        theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5))
      grid.arrange(p1, p2, nrow=1, ncol=2)
      if (outfile) dev.off()
    }