R/densityPlate.R

Defines functions densityPlate

Documented in densityPlate

#' Generate for each well on the plate a density plot of the normalized probeset values
#' 
#' The grey densities displayed on each well plot
#' correspond to the individual densities of all other wells; the colored density
#' is the density of the given well (plate layout is respected).
#' 
#' @param eset ExpressionSet as generated by the 'preprocessing' function 
#' @param filePrefix prefix that will be used to generate the output files; for plate <i>, the default
#' name will be <filePrefix>Plate<i>.png; the default file prefix is "densityPlate"
#' @param title title name of the variable in the pData of the eset ExpresionSet that can be used to provide titles
#' on the individual well plots; if NULL no titles are plotted for the individual wells
#' @note the color codes correspond to the sampleColor column of the pData of the ExpressionSet passed to argument
#' 'eset'; if no sampleColor column is present, the density for a given sample will be plotted in 'blue'  
#' @return no return value; a graph is drawn to the current device
#' @export
densityPlate <- function(eset, filePrefix = "densityPlate", title = NULL) {
  
  if (is.null(pData(eset)$sampleColor))
    pData(eset)$sampleColor <- "blue"
  
  
  for (plate in sort(unique(eset$titanPlateNo))) {
    
    esettemp <- eset[,eset$titanPlateNo == plate]
    
    dplot <- list()
    maxval <- vector(mode = "numeric", length = ncol(exprs(eset)))
    for (k in 1:ncol(exprs(eset))) {
      dplot[[k]] <- density(exprs(eset)[,k])
      maxval[k] <- max(dplot[[k]]$y)
    }
    maxval <- max(maxval)

    png(file = paste(filePrefix, "Plate", plate, ".png", sep = ""), width = 10, height = 6, units = "in", res = 72) 
    par(mfrow = c(8, 12), mar = c(0.5, 0.5, 1, 0.5))
   
    for (iRow in c("A", "B", "C", "D", "E", "F", "G", "H")) {
      for (iCol in c(1:12)) {
        if (sum(esettemp$titanRow == iRow & esettemp$titanColumn == iCol) == 0) {
          plot(0, 0, type = "n", axes = FALSE)
        } else {
            plot(dplot[[1]], axes = FALSE, type = "n", ylim = c(0, maxval), main = "")
            axis(1, lwd = 0, labels = FALSE)
            axis(2, lwd = 0, labels = FALSE)
            box(bty = 'l', lwd = 1.5)
            for (k in 1:ncol(exprs(eset))) {
              lines(dplot[[k]], col = "lightgrey") 
            }
            col1 <- esettemp$sampleColor[esettemp$titanRow == iRow & esettemp$titanColumn == iCol]
            lines(density(exprs(esettemp)[,esettemp$titanRow == iRow & esettemp$titanColumn == iCol]), col = col1, lwd = 3)
            if(!is.null(title)) {
              title(pData(esettemp)[esettemp$titanRow == iRow & esettemp$titanColumn == iCol, title])
            }
         }
      } 
    }  
    dev.off()
  } 
}

Try the titanQC package in your browser

Any scripts or data that you put into this service are public.

titanQC documentation built on May 2, 2019, 5:55 p.m.