## Usage: Rscript plot_timestep.R input_file
## the input_file is a .ts file
## the output will be a correlation vs timestep plot

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
#print(args[1])
#length(args)
## test if there is at least one argument: if not, return an error
if (length(args)==0) {
  stop("Please provide the timestep file", call.=FALSE)
}
  
library(dplyr)
library(stringr)
#file = "/Users/bhandariyr/Documents/projects/afm/cafemol/dimer/onechain/run2_rescaled/output1/cbl_afm.ts"
file = args[1]
data.ts.lines = readLines(file)[grep("#all", readLines(file))]
data.ts =  data.ts.lines %>%
              strsplit("\\s+") %>%
              do.call(rbind, .) %>%
              data.frame()
# dim(data.ts)
# Open a pdf file
out.file = paste0(tools::file_path_sans_ext(basename(file)),"_ccplot.pdf")
pdf(out.file) 
plot(data.ts[,2], data.ts[,15], xlab = "time step", ylab = "correlation")

# Close the pdf file
dev.off() 