# A script to concatenate files exported as text from export events where all exported files have the same panel # Nothing will be done to change any data transformation / scaling from when the files were exported # A file number channel will be added to the concatenated file # Two CSV files will be written: the concatenated file, and a CSV file that maps the file number to the original filename # Instructions: # 1. If you have not run this script before, first run: install.packages('tidyverse') # 2. Export events from Cytobank as text files for the files you want to concatenate # 3. Download these exported events to your local computer # 4. Change the path in "folderToConcat" below to the path to the folder of downloaded exported events # 5. Change the concatOutputFileName to whatever you would like the concatenated file to be called. If no path is included, this and the key will be written to the current working directory. # 6. Change PATH in setwd('') to wherever you want to save the output file # 7. Run all of the code below. # A progress bar will be printed as each file is read in. This process may take a while. ##### CHANGE THINGS HERE AS APPROPRIATE folderToConcat = 'Concat file issues/experiment_150679_originalVisneFilesToConcat_spill_applied_events/' concatOutputFileName = 'experiment_150679_concatVisneFiles' setwd('PATH') ##### DO NOT CHANGE BELOW THIS LINE library(tidyverse) concatFiles <- function(folderToConcat, concatOutputFileName) { # List files filesToConcat <- list.files(folderToConcat, full.names = TRUE) # Check that files all have same panel panels <- t(sapply(filesToConcat, function(file) { suppressMessages(read_delim(file, col_names = FALSE, skip = 1, n_max = 1, delim = '\t')) })) if (any(apply(panels, 2, function(col) length(unique(col)))!=1)) stop('More than one panel in files to concat') # File name / file number key key <- data_frame(file = filesToConcat, fileNumber = 1:length(filesToConcat)) # Read in files data <- lapply(filesToConcat, function(file) { print(file) d <- suppressMessages(read_delim(file, delim = '\t', skip = 1)) d$fileNumber <- key$fileNumber[key$file == file] d }) # Concatenate files concat <- do.call(rbind, data) # Write out concatenated file and key write_csv(concat, paste0(concatOutputFileName, '.csv')) write_csv(key, paste0(concatOutputFileName, '_key.csv')) return('Concatenated file and key written to working directory unless one was specified in file path.') } concatFiles(folderToConcat, concatOutputFileName)