R/read_patchliner_excel.R

Defines functions PLXLS_get PLXLS_get_channel PLXLS_set_file

#read Patchliner XLS into a data frame
PLXLS_get <- function(filename, dec = ".")
{
  df = read.csv2(filename,
                 sep = "\t",
                 dec = dec,
                 stringsAsFactors = F)
  
  # convert to seconds (no millis, 'ms' of lubdidate can do millis)
  # fun <- as.difftime
  # df$Time_secs <-
  #   fun(df$Time, format = "%H:%M:%S %t", units = "secs")
  # df$SweepTime_secs <-
  #  fun(df$SweepTime, format = "%M:%S", units = "secs")
  
  # extract tree path
  # df$Sweep %>% stringi::stri_split(fixed = "_") %>% do.call(rbind, .) %>% merge(df) -> df    
  # names(df)[1:3] <- c("exp", "ser", "swp")
  df
}

#extract the columns that belong to a given channel
PLXLS_get_channel <- function(df, channel) {
  if (!inherits(df, "data.frame"))
    stop("first argument must be a data frame")
  if (!inherits(channel, "numeric"))
    stop("channel argument must be numeric")
  
  # find columns belonging to channel n (they end with ".n.")
  pattern <- paste("\\.", as.character(channel), "\\.", sep = "")
  channel_columns <- grep(names(df), pattern = pattern)
  
  #keep some standard columns and the columns of selected channel
  if (length(channel_columns) > 0) {
    result_columns <- c(1:3, channel_columns)
    result_df <- df[, result_columns]
    
    #remove the channel suffix from columns names
    names(result_df) <-
      gsub(pattern = pattern, replacement = "", names(result_df))
    
    result_df
  }
}


#' Set path to Patchliner Excel file
#'
#' @param HEKAseries_ Series proto object for which to set the file path 
#' @param filepath    a valid file path to a Patchliner Excel results file
#'
#' @export
#'
#' @examples
#' tree <- get_treeinfo(examplefile("NaV"))
#' s <- getSeries(tree, 1, 1, 7)
#' s$PLXLS_set_file(examplefile("PLXLS"))
#' head(s$results()) #merges PLXLS file
#' plot(Online1~relTime, s$results(), type="o")
PLXLS_set_file <-function(HEKAseries_, filepath){
  HEKAseries_$sweeps$PLXLSfile <- filepath
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.