R/parsing_from_excel.R

# Parse important information from excel files generated by the Ion Torrent platform

# Private helper function that does the extracting
merge_runs <- function(filename, master, attribute_name) {
  #Extracting primary keys and wanted attribute from data files
  mergand <- read.delim2(filename, stringsAsFactors=FALSE)[, c('Chrom', 'Position', attribute_name)]
  names(mergand) <- c('Chrom', 'Position', filename)
  
  #Merging by primary keys
  if (ncol(master) == 0)
    master <- mergand
  else 
    master <- merge(master, mergand, by = c('Chrom', 'Position'))
  
  return (master)
}

# A function that return a data-frame containing the requested attribute in all data files within containing folder
read_data <- function(containing_folder, attribute_name) {
  setwd(containing_folder)
  output <- data.frame(fix.empty.names = FALSE)
  
  for (file in list.files())
    output <- merge_runs(file, output, attribute_name)
  
  test_sample <- sapply(output[, 3:dim(output)[2]], as.numeric)
  
  output <- cbind(output[, 1:2], test_sample)

  return (output)
}

# A function that generate the csv file containing the data-frame from read_data method, for manual checking 
export_report <- function(containing_folder, attr) {
  data <- read_data(containing_folder, attr)
  stopifnot(nrow(data) != 0)
  write.csv2(data, paste(attr, "report.csv"), row.names = FALSE)
}
steven-le-thien/AMlimCNV documentation built on May 30, 2019, 4:42 p.m.