####################################################################################################################################################################### # This program is meant to take the Click1 and Click2 values determined by the TTimes.py program and cut out everything that is not within these values. # Note that this program assumes that you have the "SavedData_formatted_NoNeg.csv" file in a directory with only the files contained within the file. If you include # files in the directory that are not in the "SavedData_formatted_NoNeg.csv" file, then the program will give you an error when it encounters a file that is not in the # SavedData.csv file. ####################################################################################################################################################################### library(tidyverse) # create the output folder path_var <- r"(E:\12.8.2021\s0201\ck\Analysis\Pie_Charts\Sampling_and_Accommodation\Lifetime_of_Initial_Sampling_Events\SavedData)" dir.create(paste(toString(path_var), "T1_to_T2_Synchronized", sep = "///"), showWarnings = FALSE) output_dir <- paste(path_var, "T1_to_T2_Synchronized", sep = "\\") # get the names of the input files with their full path files <- list.files(path_var, "pairProfile", full.name = TRUE) savedData <- read.csv(list.files(path_var, "SavedData.csv", full.name = TRUE), header = FALSE, sep = ",") # loop through all the input files for (file in files) { # read the file, specify the correct separator data <- read.csv(file, header = FALSE, sep = ",") #Use the select function in Dplyr to select only the V1 and V2 columns varSelected_data <- select(data, V1, V2) #Use the slice function in Dplyr to cut coordinate row out of the dataframe cut_data <- slice(varSelected_data, 2:nrow(varSelected_data)) #Use the filter and grepl function in Dplyr to select the row that has the same filename as the current file formatted_Name <- strsplit(file, "/")[[1]][2] dummyTest2 <- dplyr::filter(savedData, grepl(formatted_Name, V13)) #Use the pull function in Dplyr to obtain the T1 and T2 values that correspond to the current file T1_dummyTest2 <- pull(dummyTest2, V3) T2_dummyTest2 <- pull(dummyTest2, V4) #Cut the current file according to the (T1 - certain number of frames) and (T2 + certain number of frames) values. This can be changed depending on how you want to display the data. synch_data <- slice(cut_data, (T1_dummyTest2):(T2_dummyTest2)) #Define the new file name outfile <- paste(strsplit(formatted_Name, ".csv")[[1]][1], "T1_to_T2_Synchronized", sep = "-") outfile <- paste(outfile, "csv", sep = ".") # write the file. write.table(synch_data, paste(output_dir, outfile, sep = "\\"), sep = ",", col.names = FALSE, row.names = FALSE, quote = FALSE) }