Skip to content
Snippets Groups Projects
Well.R 4.09 KiB
Newer Older
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
#' @export
Well <- R6Class(
  classname = "Well",
  lock_class = TRUE,
  public = list(
    dbConnection = NULL,
    plate_ID = NULL,
    position = NULL,
    label = NULL,
    temperature = NULL,
    initialize = function(dbConnection = NA, plate_ID = NA, position = NA,
                          label = NA, temperature = NA) {
      if(!("DBConnection" %in% class(dbConnection))) {
        stop("DB connection object required to create Well object")
      }
      if(is.na(plate_ID) || is.na(position)) { stop("Plate ID and well position required to create Well object") }
      if(is.na(label)) { warning("Consider giving a label to new Well object") }
      self$dbConnection <- dbConnection
      if("Plate" %in% class(plate_ID)) {
        plate_ID <- plate_ID$ID
      }
      self$plate_ID <- plate_ID
      self$position <- position
      self$label <- label
      self$temperature <- temperature
    },
    print = function() {
      cat(paste0(paste0("Plate ID: ", self$plate_ID), "\n",
                 paste0("Well position: ", self$position), "\n",
                 paste0("Label: ", self$label), "\n",
                 paste0("Samples: ", length(self$samples)), "\n",
                 paste0("Compounds: ", length(self$compounds)), "\n",
                 paste0("Temperature: ", self$temperature), "\n",
                 paste0("Quality control: ", length(self$qc)), "\n",
                 paste0("Phenotypes: ", length(self$phenotypes)), "\n",
                 paste0("Data files: ", length(self$datafiles)), "\n"))
    },
    store = function() {
      wh <- WellHandle$new(self$dbConnection)
      status <- wh$store(list(self))
    }
  ),
  private = list(
    sample_list = NULL,
    compound_list = NULL,
    qc_list = NULL,
    datafile_list = NULL,
    phenotype_list = NULL
  ),
  active = list(
    samples = function(s) {
      if(missing(s)) {
        if(length(private$sample_list) == 0) {
          sh <- SampleHandle$new(self$dbConnection)
          private$sample_list <- sh$get_all_by_well(self)
        }
        return(private$sample_list)
      } else {
        for(item in s) {
          if(!("Sample" %in% class(item))) {
            stop("Well samples must be Sample objects.")
          }
        }
        private$sample_list <- s
      }
    },
    compounds = function(c) {
      if(missing(c)) {
        if(length(private$compound_list) == 0) {
          ch <- CompoundHandle$new(self$dbConnection)
          private$compound_list <- ch$get_all_by_well(self)
        }
        return(private$compound_list)
      } else {
        for(item in c) {
          if(!("Compound" %in% class(item))) {
            stop("Well compounds must be Compound objects.")
          }
        }
        private$compound_list <- c
      }
    },
    qc = function(qctrl) {
      qh <- QCHandle$new(self$dbConnection)
      if(missing(qctrl)) {
        if(length(private$qc_list) == 0) {
          private$qc_list <- qh$get_all_by_well(self)
        }
        return(private$qc_list)
      } else {
        for(item in qctrl) {
          if(!("QC" %in% class(item))) {
            stop("Well qc must be QC objects.")
          }
        }
        private$qc_list <- qctrl
      }
    },
    datafiles = function(d) {
      if(missing(d)) {
        if(length(private$datafile_list) == 0) {
          dh <- DatafileHandle$new(self$dbConnection)
          private$datafile_list <- dh$get_all_by_well(self)
        }
        return(private$datafile_list)
      } else {
        for(item in d) {
          if(!("Datafile" %in% class(item))) {
            stop("Well datafiles must be Datafile objects.")
          }
        }
        private$datafile_list <- d
      }
    },
    phenotypes = function(p) {
      if(missing(p)) {
        if(length(private$phenotype_list) == 0) {
          ph <- PhenotypeHandle$new(self$dbConnection)
          private$phenotype_list <- ph$get_all_by_well(self)
        }
        return(private$phenotype_list)
      } else {
        for(item in p) {
          if(!("Phenotype" %in% class(item))) {
            stop("Well phenotypes must be Phenotype objects.")
          }
        }
        private$phenotype_list <- p
      }
    }
  )
)