command<-paste0(commandArgs(trailingOnly=FALSE),collapse=",")# recover the full command
args<-commandArgs(trailingOnly=TRUE)# recover arguments written after the call of the R script
if(any(is.na(args))){
stop(paste0("\n\n================\n\nERROR IN final_insertion_files.R\nTHE args OBJECT HAS NA\n\n================\n\n"),call.=FALSE)
}
tempo.arg.names<-c(
"pos",
"ori_coord",
"ter_coord",
"file_name",
"cute",
"log"
)# objects names exactly in the same order as in the bash code and recovered in args. Here only one, because only the path of the config file to indicate after the final_insertion_files.R script execution
if(length(args)!=length(tempo.arg.names)){
stop(paste0("\n\n================\n\nERROR IN final_insertion_files.R\nTHE NUMBER OF ELEMENTS IN args (",length(args),") IS DIFFERENT FROM THE NUMBER OF ELEMENTS IN tempo.arg.names (",length(tempo.arg.names),")\nargs:",paste0(args,collapse=","),"\ntempo.arg.names:",paste0(tempo.arg.names,collapse=","),"\n\n================\n\n"),call.=FALSE)
}
for(i1in1:length(tempo.arg.names)){
assign(tempo.arg.names[i1],args[i1])
}
rm(tempo.arg.names,args,i1)
}elseif(sys.nframe()==0L){# detection of copy-paste/direct execution (for debugging). With script it is also 0, with source, it is 4
run.way<-"COPY-PASTE"
cat(paste0("\n\n",tempo.cat,run.way,"\n"))
}else{
run.way<-"SOURCE"# using source(), sys.nframe() is 4
cat(paste0("\n\n",tempo.cat,run.way,"\n"))
}
rm(tempo.cat)
################################ End Config import
################################ Test
# cat("\n\n!!!!!!!!!!!!!!!!!!! WARNING: test values are activated\n\n")
# stat <- "C:/Users/Gael/Documents/Git_projects/14985_loot/dataset/test.fastq_Nremove_trim_5pAttc_1-51.stat"
stop(paste0("\n\n================\n\nINTERNAL CODE ERROR 2 IN final_insertion_files.R\nINCONSISTENCIES BETWEEN THE ARGUMENTS USED AND THE PARAMETERS REQUIRED IN THE EXECUTABLE CODE FILE\nTHE ARGUMENTS NOT PRESENT IN THE EXECUTABLE FILE (final_insertion_files.R) ARE:\n",paste(created.object.control[!created.object.control%in%param.list],collapse=" "),"\nTHE PARAMETERS OF THE EXECUTABLE FILE (final_insertion_files.R) NOT PRESENT IN THE ARGUMENTS ARE:\n",paste(param.list[!param.list%in%created.object.control],collapse=" "),"\n\n================\n\n"),call.=FALSE)# message for developers
param.ini.settings[i]<-paste0("\n",param.list[i],paste0(rep(" ",space.add[i]),collapse=""),paste0(get(param.list[i]),collapse=","))# no env = sys.nframe(), inherit = FALSE in get() because look for function in the classical scope
}
################################ End Recording of the initial parameters
################################ Functions
# Functions are built such that they should have no direct use of Global objects (going through the R scope), and only use function arguments
# 1) Cute little function is sourced for the moment into the .GlobalEnv environment, but may be interesting to put it into a new environement just above .GlobalEnv environment. See https://stackoverflow.com/questions/9002544/how-to-add-functions-in-an-existing-environment
# 2) Argument names of each function must not be a name of Global objects (error message otherwise)
# 3) Argument name of each function ends with "_fun" in the first function, "_2fun" in the second, etc. This prevent conflicts with the argument partial names when using these functions, notably when they are imbricated
################ import functions from cute little functions toolbox
if(length(cute)!=1){
stop(paste0("\n\n============\n\nERROR IN final_insertion_files.R\ncute PARAMETER MUST BE LENGTH 1: ",paste(cute,collapse=" "),"\n\n============\n\n"),call.=FALSE)
stop(paste0("\n\n============\n\nERROR IN final_insertion_files.R\nHTTP INDICATED IN THE cute PARAMETER DOES NOT EXISTS: ",cute,"\n\n============\n\n"),call.=FALSE)
}else{
source(cute,local=.GlobalEnv)# source the fun_ functions used below
}
}elseif(!grepl(x=cute,pattern="^http")){
if(!file.exists(cute)){
stop(paste0("\n\n============\n\nERROR IN final_insertion_files.R\nFILE INDICATED IN THE cute PARAMETER DOES NOT EXISTS: ",cute,"\n\n============\n\n"),call.=FALSE)
}else{
source(cute,local=.GlobalEnv)# source the fun_ functions used below
}
}else{
tempo.cat<-paste0("\n\n================\n\nINTERNAL CODE ERROR 3 IN final_insertion_files.R: CODE HAS TO BE MODIFIED\n\n============\n\n")
stop(tempo.cat,call.=FALSE)
}
# required cute function checking
req.function<-c(
"fun_check",
"fun_pack",
"fun_df_remod",
"fun_gg_scatter",
"fun_gg_palette",
"fun_open",
"fun_gg_empty_graph",
"fun_report"
)
tempo<-NULL
for(i1inreq.function){
if(length(find(i1,mode="function"))==0L){
tempo<-c(tempo,i1)
}
}
if(!is.null(tempo)){
tempo.cat<-paste0("ERROR IN final_insertion_files.R\nREQUIRED cute FUNCTION",ifelse(length(tempo)>1,"S ARE"," IS")," MISSING IN THE R ENVIRONMENT:\n",paste0(tempo,collapse="()\n"))
stop(paste0("\n\n================\n\n",tempo.cat,"\n\n================\n\n"),call.=FALSE)# == in stop() to be able to add several messages between ==
# fun_pack(req.package = req.package.list, load = TRUE, lib.path = NULL) # packages are imported even if inside functions are written as package.name::function() in the present code
stop(paste0("\n\n================\n\n",paste(text.check[arg.check],collapse="\n"),"\n\n================\n\n"),call.=FALSE)# == in stop() to be able to add several messages between == #
if(any(tempo.log)==TRUE){# normally no NA with is.null()
tempo.cat<-paste0("ERROR IN final_insertion_files.R:\n",ifelse(sum(tempo.log,na.rm=TRUE)>1,"THESE ARGUMENTS\n","THIS ARGUMENT\n"),paste0(tempo.arg[tempo.log],collapse="\n"),"\nCANNOT BE NULL")
stop(paste0("\n\n================\n\n",tempo.cat,"\n\n================\n\n"),call.=FALSE)# == in stop() to be able to add several messages between ==
}
# end management of NULL arguments
# code that protects set.seed() in the global environment
# end code that protects set.seed() in the global environment
# warning initiation
ini.warning.length<-options()$warning.length
options(warning.length=8170)
warn<-NULL
# warn.count <- 0 # not required
# end warning initiation
# other checkings
ori_coord<-strsplit(ori_coord,split=" ")[[1]]
if(length(ori_coord)!=2&any(grepl(ori_coord,pattern="\\D"))){# normally no NA with is.null()
tempo.cat<-paste0("ERROR IN final_insertion_files.R:\nTHE ori_coord PARAMETER MUST BE TWO INTEGERS SEPARATED BY A SINGLE SPACE\nHERE IT IS: \n",paste0(ori_coord,collapse=" "))
stop(paste0("\n\n================\n\n",tempo.cat,"\n\n================\n\n"),call.=FALSE)# == in stop() to be able to add several messages between ==
}else{
ori_coord<-as.integer(ori_coord)
}
ter_coord<-strsplit(ter_coord,split=" ")[[1]]
if(length(ter_coord)!=2&any(grepl(ter_coord,pattern="\\D"))){# normally no NA with is.null()
tempo.cat<-paste0("ERROR IN final_insertion_files.R:\nTHE ter_coord PARAMETER MUST BE TWO INTEGERS SEPARATED BY A SINGLE SPACE\nHERE IT IS: \n",paste0(ter_coord,collapse=" "))
stop(paste0("\n\n================\n\n",tempo.cat,"\n\n================\n\n"),call.=FALSE)# == in stop() to be able to add several messages between ==
}else{
ter_coord<-as.integer(ter_coord)
}
# end other checkings
# reserved word checking
# end reserved word checking
# end second round of checking and data preparation
# package checking
# end package checking
################################ End pre-ignition checking
obs$fork[obs$pos<ori_center|obs$pos>ter_center]<-abs(obs$fork[obs$pos<ori_center|obs$pos>ter_center]-16)# left of the Ori and right of the dif (dif > ori) is switch 0 -> 16 and 16 -> 0 to have the leading and lagging
}else{
obs$fork[obs$pos<=ori_center&obs$pos>=ter_center]<-abs(obs$fork[obs$pos<=ori_center&obs$pos>=ter_center]-16)# right of the Ori and left of the dif (dif < ori) is switch 0 -> 16 and 16 -> 0 to have the leading and lagging
fun_report(data=paste0("\nNUMBER OF OBS POSITIONS:\n",format(nrow(obs),big.mark=",")),output=log,path="./",overwrite=FALSE)
# saving position file
options(scipen=1000)# to avoid writing of scientific numbers in tables, see https://stackoverflow.com/questions/3978266/number-format-writing-1e-5-instead-of-0-00001
options(scipen=1000)# to avoid writing of scientific numbers in tables, see https://stackoverflow.com/questions/3978266/number-format-writing-1e-5-instead-of-0-00001
obs$fork[obs$pos<ori_center|obs$pos>ter_center]<-abs(obs$fork[obs$pos<ori_center|obs$pos>ter_center]-16)# left of the Ori and right of the dif (dif > ori) is switch 0 -> 16 and 16 -> 0 to have the leading and lagging
}else{
obs$fork[obs$pos<=ori_center&obs$pos>=ter_center]<-abs(obs$fork[obs$pos<=ori_center&obs$pos>=ter_center]-16)# right of the Ori and left of the dif (dif < ori) is switch 0 -> 16 and 16 -> 0 to have the leading and lagging
fun_report(data=paste0("\nNUMBER OF OBS POSITIONS:\n",format(nrow(obs),big.mark=",")),output=log,path="./",overwrite=FALSE)
# saving position file
options(scipen=1000)# to avoid writing of scientific numbers in tables, see https://stackoverflow.com/questions/3978266/number-format-writing-1e-5-instead-of-0-00001
options(scipen=1000)# to avoid writing of scientific numbers in tables, see https://stackoverflow.com/questions/3978266/number-format-writing-1e-5-instead-of-0-00001
echo -e "\\n\\nNumber of different positions: \$(printf "%'d" \${pos_nb})\\n" >> report.rmd
echo -e "\\n\\nSee the CL Labbook section 24.7.3 to explain the limitation around 100 bp\\n" >> report.rmd
echo -e '
\\n\\n<br /><br />\\n\\n</center>\\n\\n
{width=600}
\\n\\n</center>\\n\\n<br /><br />\\n\\n
{width=600}
\\n\\n</center>\\n\\n<br /><br />\\n\\n
{width=600}
\\n\\n</center>\\n\\n<br /><br />\\n\\n
{width=600}