''' This program is meant to take the Trace files outputted from ebFRET and determine the lifetime of each state in a given HMM before the state transitions to another state. At the moment this program generates files that can then be given to MEMLET to determine the rate constants for each state in a given HMM, though. ''' #Packages library(tidyverse) #Define global variables path_var <- r"(C:\Users\clark\Desktop\Documents for Thesis and Manuscript\Analysis_9.9.2022\Lifetime_of_Initial_Sampling_Event_CognateNoTig\ebFRET\Lifetime_of_Initial_Sampling_Event_CognateNoTig)" file_name <- "2stateHMM_Lifetime_of_Initial_Sampling_Event_CognateNoTig.dat" setwd(path_var) time_interval <- 0.04 #Load in the Full Report Trace outputs from ebFRET file_location <- paste(path_var, file_name, sep = "\\") TypeAll_traces_2states_ebFRET <- read.table(file_location, quote="\"", comment.char="") #Name the columns for each dataframe colnames(TypeAll_traces_2states_ebFRET) <- c("Trace", "Donor", "Acceptor", "FRET", "Viterbi_State", "Viterbi_Mean") #Use the rle() function to determine the number of occurances in a row for each state in the 2-state HMM Two_State_rle <- rle(TypeAll_traces_2states_ebFRET$Viterbi_State) Two_State_rle <- do.call(rbind.data.frame, Two_State_rle) Two_State_rle <- t(Two_State_rle) row.names(Two_State_rle) <- NULL colnames(Two_State_rle) <- c("Num_Occur", "State") Two_State_rle <- as_tibble(Two_State_rle) Two_State_rle <- Two_State_rle %>% mutate(Num_Occur = Num_Occur*time_interval) #Convert number of frames to time in seconds #Use dplyr to filter out the number of occurances in a row for each state in the 2-state HMM Length_State1_2HMM <- filter(Two_State_rle, State == 1) Length_State2_2HMM <- filter(Two_State_rle, State == 2) write.table(Length_State1_2HMM$Num_Occur, "NearCognate_25nMTC_PRE6_SecondaryEvents_State1_2HMM.txt", row.names = FALSE, col.names = FALSE) write.table(Length_State2_2HMM$Num_Occur, "NearCognate_25nMTC_PRE6_SecondaryEvents_State2_2HMM.txt", row.names = FALSE, col.names = FALSE)