R/filter_and_qc.R

Defines functions plot_correl plot_gates plot_qc filter_data

Documented in filter_data plot_correl plot_gates plot_qc

#
# Authors: Florent Chuffart, INSERM and Magali Richard, CNRS
# florent.chuffart@univ-grenoble-alpes.fr
# mag.richardt@ens-lyon.fr
#
#---------------------------------------------
#' Plot the correlation between  FL1H, FSC and SSC
#'
#' This function works on FL1H, FSC and SSC data acquired on 96-well plates
#' @param gated_data A particular element of the list data$gdat (correspond to a single well)
#' @return A figure of 3 plots to control gating efficiency
#' @examples
#' load("data/data.rda")
#' gated_data=data$gdat[[1]]
#' cur_anno = data$anno[1, ]
#' main = paste(cur_anno$plate, cur_anno$well, sep = "")
#' plot_correl(gated_data, main)
#' @export

plot_correl=function(gated_data, main){
  n = dim(gated_data)[1]
  plot(gated_data, pch = ".", main = main)
}

#---------------------------------------------
#' Plot the gates
#'
#' This function works on FL1H, FSC and SSC data acquired on 96-well plates
#' @param data The gated data generated by gate_facs_yeast_HTS_data
#' @param idx The index of the data that will be ploted
#' @param col_key Which color indicator of the data$anno file will be used
#' @return A figure of gates for indicated index
#' @examples
#' load("data/data.rda")
#' col_key="col_time"
#' plot_gates(data, idx=data$anno$plate=="PL1", col_key=col_key)
#' @export

plot_gates = function(data, idx, xlim, ylim, col_key="col", ...) {
  if (missing(idx)) {
    idx = rep(TRUE, nrow(data$anno))
  }
  data$anno = data$anno[idx,]
  data$alldat = data$alldat[idx]
  data$gdat = data$gdat[idx]
  data$gate = data$gate[idx]

  if (missing(xlim) | missing(ylim)) {
    bar = lapply(data$gate, function(g){
      list(rx = range(g[[1]]$x), ry = range(g[[1]]$y))
    })
    bar = do.call(rbind, bar)
    bar = data.frame((lapply(data.frame(bar, stringsAsFactors=FALSE), unlist)), stringsAsFactors=FALSE)
    if (missing(ylim)) {
      ylim=range(bar$ry)
    }
    if (missing(xlim)) {
      xlim=range(bar$rx)
    }
  }

  plot(0,0,col=0, xlim=xlim, ylim=ylim, xlab="FSC", ylab="SSC", ...)
  baz = sapply(1:length(data$gate), function(i){
    g = data$gate[[i]]
    col = data$anno[i, col_key]
    lines(g[[1]]$x, g[[1]]$y, lwd=3, col=adjustcolor(col, alpha.f=0.3))
  })
}


#---------------------------------------------
#' Plot FL1H, FSC and SSC for each well
#'
#' This function works on FL1H, FSC and SSC data acquired on 96-well plates
#' @param data The gated data generated by gate_facs_yeast_HTS_data
#' @param idx The index of the data that will be ploted
#' @param type The type of data to be used, has to be either "raw" or "gated"
#' @return A figure of 3 plots to control gating efficiency
#' @examples
#' load("data/data.rda")
#' col_key="col_time"
#' plot_qc(data, idx=data$anno$plate=="PL1", type="raw", col_key=col_key)
#' @export
#'
plot_qc = function(data, idx, type="raw", col_key="col", ...) {
if (missing(idx)) {
  idx = rep(TRUE, nrow(data$anno))
}
data$anno = data$anno[idx,]
data$alldat = data$alldat[idx]
data$gdat = data$gdat[idx]
data$gate = data$gate[idx]

if (type=="raw") {
  keys = c("fl1", "fsc", "ssc", "nb_cells")
  ylims=list("fl1"=c(100, 700), "fsc"=c(100,350), "ssc"=c(30,130), "nb_cells"=c(0,10000))
} else if (type=="gated") {
  keys = c("gfl1", "gfsc", "gssc", "nb_gcells")
  ylims=list("gfl1"=c(100, 700), "gfsc"=c(100,350), "gssc"=c(30,130), "nb_gcells"=c(0,5000))
}

layout(matrix(1:4,4,byrow=TRUE))
# stats
for(key in keys) {
  if (key %in% c("nb_cells", "nb_gcells") ) {
    ylim = ylims[[key]]
    plot(1:length(data$anno[[key]]), data$anno[[key]],  xaxt='n', xlab="", col=data$anno[[col_key]], ylab=key, cex=2, pch=16, ylim=ylims[[key]], ...)
    axis(side = 1, at = 1:length(data$anno[[key]]), labels = paste(data$anno$plate,data$anno$well,data$anno$time,data$anno$gal, data$anno$strain), tck = -0.01, las=2)
  } else {
    ylim = ylims[[key]]
    plot(1:length(data$anno[[paste("median", key, sep="_")]]), data$anno[[paste("median", key, sep="_")]],  xaxt='n', xlab="", col=data$anno[[col_key]], ylab=key, cex=2, pch=16, ylim=ylims[[key]], ...)
    axis(side = 1, at = 1:length(data$anno[[paste("median", key, sep="_")]]), labels = paste(data$anno$plate,data$anno$well,data$anno$time,data$anno$gal, data$anno$strain), tck = -0.01, las=2)
    # lines(xpos[idx2], m[idx2], col=col)
    # points(xpos[idx2], m[idx2], col=adjustcolor(col, alpha.f=0.5), pch=16)
    arrows(x0=1:length(data$anno[[paste("median", key, sep="_")]]), x1=1:length(data$anno[[paste("median", key, sep="_")]]), y0=data$anno[[paste("median", key, sep="_")]]+data$anno[[paste("sd", key, sep="_")]], y1=data$anno[[paste("median", key, sep="_")]]-data$anno[[paste("sd", key, sep="_")]], length=0, col=data$anno[[col_key]])
  }
}
}

#---------------------------------------------
#' Filters the data according to gating informations
#'
#' This function works on FL1H, FSC and SSC data acquired on 96-well plates
#' @param data The gated data generated by gate_facs_yeast_HTS_data
#' @param idx The index data to be removed
#' @return A filtered data list
#' @examples
#' load("data/data.rda")
#' idx_to_be_removed = (data$anno$nb_gcells < 500 | (data$anno$median_gssc < 33 & data$anno$plate=="PL3") )
#' data = filter_data(data, idx_to_be_removed)
#' @export

filter_data = function(data, idx_to_be_removed) {
  data$anno = data$anno[!idx_to_be_removed,]
  data$alldat = data$alldat[!idx_to_be_removed]
  data$gdat = data$gdat[!idx_to_be_removed]
  data$gate = data$gate[!idx_to_be_removed]
  return(data)
}
magrichard/facsticor documentation built on May 17, 2019, 8:16 a.m.