Desired functions:

Reading in (and cleaning) data:

Fitting curves:

Merging metadata (Media, genotype, etc)

Plotting data

Other

library(stringr)
library(dplyr)
library(magrittr)
library(readr)
library(grofit)
library(tidyr)
time_to_dbl <- function(t) {
  t %>%
    stringr::str_replace_all("(\\d+) h( (\\d+) min)?", "\\1.0\\3") %>%
    stringr::str_split(string = ., pattern = "\\.") %>%
    sapply(FUN = function(x) {
      x %>% as.numeric() %>%
        magrittr::extract(2) %>%
        magrittr::divide_by(60) %>%
        magrittr::add(as.numeric(x[1]))
    })
}

time_to_dbl(c("43 h", "234 h 2 min", "0 h 1 min"))
file <- "~/1_Research/DATA/phenotyping/plate reader/2017-08-21-Stacker8/TRno1886.CSV"

get_barcode_BMG <- function(file, trim = FALSE) {
  if (trim == TRUE) {
    file %>% readLines() %>% 
      grep("ID1: ", ., value = T) %>% 
      str_extract("(?<=ID1: ).*(?= \\d{1,2}/\\d{1,2}/\\d{2,4})") %>%
      substr(regexpr("[^0]", .), nchar(.))
  } else if (trim == FALSE) {
    file %>% readLines() %>% 
      grep("ID1: ", ., value = T) %>% 
      str_extract("(?<=ID1: ).*(?= \\d{1,2}/\\d{1,2}/\\d{2,4})")
  }
}

get_barcode_BMG(file, trim = TRUE)
get_barcode_BMG(file, trim = F)
# Add option to extract barcode? Would store in "plate" column. 

load_BMG <- function(file, time.limits = c(0, Inf), get.barcode = FALSE) {
  x <- file %>%
    readLines() %>%
    grepl("Well Row", .) %>%
    which() %>%
    `-`(1) %>%
    read_csv(file, skip = .)
  x <- x %>% rename_at(vars(-c(1:3)),
                  funs(x %>% slice(1) %>% select(-c(1:3)))) %>%
    dplyr::slice(-1) %>%
    tidyr::gather(time, OD, -`Well Row`, -`Well Col`, -Content) %>%
    dplyr::mutate(.data = ., time = time_to_dbl(time), OD = as.numeric(OD)) %>%
    dplyr::filter(time > time.limits[1], time < time.limits[2])

  if (get.barcode == TRUE) {
    x <-  dplyr::mutate(.data = x, plate = get_barcode_BMG(file))
  } else {
    x <-  dplyr::mutate(.data = x, plate = basename(file) %>%
                        strsplit("\\.") %>% `[[`(1) %>% `[`(1))
  }
}

R73G <- load_BMG("~/1_Research/DATA/phenotyping/plate reader/2017-07-19-Stacker6/TRno1806.CSV", time.limits = c(5, 90))
R73G
#' @importFrom magrittr %>% %$% extract2
#' @importFrom dplyr mutate select rename
#' @importFrom tidyr spread
#' @importFrom tidyselect everything
#' @import grofit
#'
#' @export

grofitr <- function(plate, ...) {
  timepoints <- plate %>% select(time) %>% unique() %>% unlist()
  n <- length(timepoints)
  times <- timepoints %>% rep(96) %>% matrix(c(n, 96)) %>% t()

  plate <- plate %>%
    mutate(OD = as.numeric(OD)) %>%
    spread(time, OD) %>%
    select(-Content) %>%
    select(3,1,2, everything())

  grofit(times,
         plate,
         ec50 = F,
         control = grofit::grofit.control(neg.nan.act = F,
                                          interactive = F,
                                          suppress.messages = T,
                                          ...)) %>%
         extract2("gcFit") %>%
         extract2("gcTable") %>%
         rename(`Well Row` = Well.Row,
                `Well Col` = Well.Col)
}

R73G_results <- "~/1_Research/DATA/phenotyping/plate reader/2017-07-19-Stacker6/TRno1806.CSV" %>% 
  load_BMG(time.limits = c(5, 95))

timepoints <- R73G_results %>% select(time) %>% unique() %>% unlist()
n <- length(timepoints)
times <- timepoints %>% rep(96) %>% matrix(c(n, 96)) %>% t()

R73G_results <- R73G_results %>%
    mutate(OD = as.numeric(OD)) %>%
    spread(time, OD) %>%
    select(-Content) %>%
    select(3,1,2, everything())

grofit(time = times,
       data = R73G_results,
         ec50 = F,
         control = grofit::grofit.control(neg.nan.act = F,
                                          interactive = F,
                                          suppress.messages = T,
                                          )) %>%
         extract2("gcFit") %>%
         extract2("gcTable") %>%
         rename(`Well Row` = Well.Row,
                `Well Col` = Well.Col)

R73G_results %>% grofitr()
merge_meta <- function(data) {}
plot_plates <- function(data){
  ggplot(data, aes(x = time, y = OD)) +
    geom_point(size = 0.2) + 
    geom_line() +
    facet_grid(`Well Row` ~ `Well Col`, switch = "y") +
    scale_y_continuous(position = "right") +
    theme_bw() +
    theme(panel.grid = element_blank())
  }


dtdoering/grofitr documentation built on May 7, 2019, 9:48 p.m.