R/qPCR_plate_setup.R

Defines functions qPCR_plate_setup

#' @export

qPCR_plate_setup <- function(rxns,plate.dimensions,forbidden.wells=NULL,save.plates=TRUE,plate.filename="plates") {

  # Sort the reactions in plates

  # Steps:
  # Iterate over every gene target
  #   Check if it fits within the same plate
  #   If so, assign it within the same plate and subtract the number of used wells from the plate
  #   -> Have a counter for used wells within the current plate
  #   If not, start a new plate or not? Can the total remaining rxns fit in the remaining empty wells
  #   -> Have a counter for total unused wells in used plates
  #   If they can, DO NOT start a new plate. Instead, split them in the already used plates

  # Initial check of entered parameters

  if(!(typeof(plate.dimensions) %in% c("integer","numeric","double")) | length(plate.dimensions) != 2) {
    stop("Error! Plate dimensions should be an integer vector of length 2. Please enter it as 'c(row_number,column_number)'.")
  }

  plate.layout <- .plate_layout(plate.dimensions=plate.dimensions,forbidden.wells=forbidden.wells)

  rxns <- data.table::copy(rxns)

  rxns <- rxns |>
    .distr_rxns_in_plates(plate.layout) |>
    .distr_rxns_within_plates(plate.layout)

  # Make plate schemes
  plate.schemes <- lapply(X=split(rxns,by="plate"),
                          FUN=function(plate,plate.layout) {
                            return(
                              .plate_scheme(p=plate,plate.layout=plate.layout)
                              )
                            },
                          plate.layout=plate.layout)

  if(save.plates==TRUE) {
    pdf(file=paste0(plate.filename,".pdf"),
        height=plate.dimensions[1] * 5/4,
        width=plate.dimensions[2] * 8/7)
    print(plate.schemes)
    dev.off()
    message(paste0("Plate layouts saved in ",getwd(),"/",plate.filename,".pdf"))
  }

  return(list(rxns=rxns,plates=plate.schemes))

}
dimitriskokoretsis/qpcrR documentation built on May 29, 2022, 10:11 p.m.