Skip to content
Snippets Groups Projects
WellHandle.R 6.56 KiB
Newer Older
#' WellHandle
#' @description Object for handling database operations related to the storage and 
#' retrieval of Well objects.
#' @field dbConnection A dbConnection object
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
#' @export
WellHandle <- R6Class(
  classname = "WellHandle",
  lock_class = TRUE,
  public = list(
    dbConnection = NULL,
    #' @description Create an object for handling database operations related to 
    #' the storage and retrieval of Well objects.
    #' @param dbConnection A dbConnection object
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
    initialize = function(dbConnection = NA) {
      self$dbConnection <- dbConnection
    },
    #' @description Retrieve a Well object using its database ID
    #' @param plateID Database ID of the Plate object the well belongs to
    #' @param position Position of the well in the plate
    #' @return A Well object
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
    get_by_id = function(plateID, position) {
      query <- "SELECT * FROM Well WHERE Plate_ID = ? and position = ?"
      df <- self$dbConnection$get_data(query, list(plateID, position))
      if(length(df$Plate_ID)>0) {
        well <- Well$new(dbConnection = self$dbConnection,
                         ID = df$Plate_ID, position = df$Well_position, 
                         label = df$label, temperature = df$temperature)
        return(well)
      } else { return(NULL) }
    },
    #' @description Retrieve all wells belonging to the same plate
    #' @param plate A Plate object
    #' @return Vector of Well objects
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
    get_all_by_plate = function(plate) {
      query <- "SELECT * FROM Well WHERE Plate_ID = ?"
      data <- self$dbConnection$get_data(query, list(plate$ID))
      if(length(data$Plate_ID)>0) {
        wells <- list()
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
        df.list <- split(data, data$position)
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
        for(df in df.list) {
          w <- Well$new(dbConnection = self$dbConnection,
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
                        ID = df$Plate_ID, position = df$position,
                        label = df$label, temperature = df$temperature)
          wells <- c(wells, w)
        }
        return(wells)
      } else { return(NULL) }
    },
    #' @description Retrieve all wells where a compound is found
    #' @param compound A Compound object
    #' @return Vector of Well objects
    get_all_by_compound = function(compound) {
      query <- "SELECT W.* FROM Well AS W, Well_has_Compound AS WhC
                WHERE WhC.Compound_ID = ?
                AND WhC.Plate_ID = W.Plate_ID
                AND WhC.Well_position = W.position"
      data <- self$dbConnection$get_data(query, list(compound$ID))
      if(length(data$position)>0) {
        wells <- list()
        df.list <- split(data, paste0(data$Plate_ID, data$position))
        for(df in df.list) {
          w <- Well$new(dbConnection = self$dbConnection,
                        plate_ID = df$Plate_ID, position = df$position,
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
                        label = df$label, temperature = df$temperature)
          wells <- c(wells, w)
        }
        return(wells)
      } else { return(NULL) }
    },
    #' @description Store Well objects in the database
    #' @param well.list A vector of well objects
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
    store = function(well.list) {
      statement <- "INSERT IGNORE INTO Well (Plate_ID, position, label, temperature)
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
      for(well in well.list) {
        result <- self$dbConnection$execute(statement, list(well$plate_ID,
                                                            well$position,
                                                            well$label,
                                                            well$temperature))
        if(length(well$datafiles)>0) {
          dfh <- DatafileHandle$new(self$dbConnection)
          result <- dfh$store(well$datafiles)
          for(datafile in well$datafiles) {
            st <- "INSERT IGNORE INTO Well_has_Data (Plate_ID, Well_position, Datafile_ID)
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
            r <- self$dbConnection$execute(st, list(well$plate_ID,
                                                    well$position,
                                                    datafile$ID))
          }
        }
        if(length(well$qc)>0) {
          qch <- QCHandle$new(self$dbConnection)
          result <- qch$store(well$qc)
          for(qc in well$qc) {
            st <- "INSERT IGNORE INTO Well_has_QC (Plate_ID, Well_position, QC_ID)
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
            r <- self$dbConnection$execute(st, list(well$plate_ID,
                                                    well$position,
                                                    qc$ID))
          }
        }
        if(length(well$compounds)>0) {
          ch <- CompoundHandle$new(self$dbConnection)
          result <- ch$store(well$compounds)
          for(compound in well$compounds) {
            st <- "INSERT IGNORE INTO Well_has_Compound (Plate_ID, Well_position, 
                                                  Compound_ID, dose, unit)
Jean-Karim Heriche's avatar
Jean-Karim Heriche committed
            r <- self$dbConnection$execute(st, list(well$plate_ID,
                                                    well$position,
                                                    compound$ID,
                                                    compound$dose,
                                                    compound$unit))
          }
        }
        if(length(well$samples)>0) {
          sh <- SampleHandle$new(self$dbConnection)
          result <- sh$store(well$samples)
          for(sample in well$samples) {
            st <- "INSERT IGNORE INTO Well_has_Sample (Plate_ID, Well_position, Sample_ID, count)
                        VALUES (?, ?, ?, ?)"
            r <- self$dbConnection$execute(st, list(well$plate_ID,
                                                    well$position,
                                                    sample$ID,
                                                    sample$count))
          }
        }
        if(length(well$phenotypes)>0) {
          ph <- PhenotypeHandle$new(self$dbConnection)
          result <- ph$store(well$phenotypes)
          for(phenotype in well$phenotypes) {
            st <- "INSERT IGNORE INTO Well_has_Phenotype (Plate_ID, Well_position, Phenotype_ID, value, unit)
                        VALUES (?, ?, ?, ?, ?)"
            r <- self$dbConnection$execute(st, list(well$plate_ID,
                                                    well$position,
                                                    phenotype$ID,
                                                    phenotype$value,
                                                    phenotype$unit))
          }
        }
      }
    }
  )
)