R/getCalNetShifts.R

getCalNetShifts <- function(data, loc, time1, time2, step = 1, name){

        # generate list of rings and empty dataframe to store net shift data
        ringList <- unique(data$Ring)

        # locations for each time is determined using which, min, and abs func
        shiftPoints <- lapply(ringList, function(i){
                ringDat <- dplyr::filter(data, Ring == i)
                time1.loc <- which.min(abs(ringDat$Time - time1))
                time1.val <- ringDat$Shift[time1.loc]
                time2.loc <- which.min(abs(ringDat$Time - time2))
                time2.val <- ringDat$Shift[time2.loc]
                ring <- i
                group <- unique(ringDat$Group)
                target <- unique(ringDat$Target)
                experiment <- unique(ringDat$Experiment)
                channel <- unique(ringDat$Channel)
                conc <- unique(ringDat$Concentration)
                tmp <- data.frame(i, group, target, time1.val,
                           time2.val, experiment, channel, step, conc)
                tmp$target <- as.character(tmp$target)
                tmp
        })

        # renames shiftPoints columns
        shiftPoints <- dplyr::bind_rows(shiftPoints)
        names(shiftPoints) <- c("Ring", "Group", "Target", "Shift.1", "Shift.2",
                              "Experiment", "Channel", "Step", "Concentration")

        # calculate nat shift and create new column in dataframe
        shiftPoints <- dplyr::mutate(shiftPoints, NetShift = Shift.1 - Shift.2)

        cntl <- unique(data$Cntl)
        ch <- unique(data$Ch)

        # save net shift data
        readr::write_csv(shiftPoints, paste0(loc, "/", name, "_netShifts_", cntl,
                                    "cntl_", "ch", ch, "_step", step, ".csv"))

        return(shiftPoints)
}
BaileyLabUM/biosensor documentation built on May 28, 2019, 11:36 a.m.