# Merging and Inflating two PIDs from the ZooScan equipment
# v0.beta
# Merges PIDs from two aliquots into one PID, simple and step-by-step
# Before merging , the files are "inflated", i.e., the number of objects (data rows) in each PID file is multiplied by the Subsampling factor.
# Ralf Schwamborn, 2017

library(mefa) # needs package "mefa" (multivariate data handling)
## mefa 3.2-7    2016-01-11
# Creates 4 Sudirectories, one Direcory for each step.


### ALL INPUTS AND USER ACTIONS: search "PLEASE" 

#. I Preparations

# Step I. 1. PLEASE Define your root working directory

# PLEASE INPUT HERE 
setwd("~/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files")

# setwd("C://ZooScan")

# PLEASE INPUT HERE 
mainDir <- "~/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files"

getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files"
# this creates a Subdirectory "1_Raw_INPUT PIDs",if there is not one already.


subDir_1 <- "1_Raw_INPUT_PIDs"

if (file.exists(subDir_1)){
  setwd(file.path(mainDir, subDir_1))
} else {
  dir.create(file.path(mainDir, subDir_1))

}


# Step I.2. PLEASE COPY all your PID files into the Subdirectory "1_Raw_INPUT PIDs" : raw PID files with headers

# Step I.3. This Creates the other neessary Subdirectories (and checks if they already exist)

subDir_2 <- "2_Inflated_PIDs"

if (file.exists(subDir_2)){
  setwd(file.path(mainDir, subDir_2))
} else {
  dir.create(file.path(mainDir, subDir_2))

}
## Warning in dir.create(file.path(mainDir, subDir_2)): 'C:\Users\R
## \Documents\Recife\Orientandos Recife\Gabi Figueiredo_Doutorado\PID_Files
## \2_Inflated_PIDs' já existe
subDir_3 <- "3_Infl_renamed_PIDs"

if (file.exists(subDir_3)){
  setwd(file.path(mainDir, subDir_3))
} else {
  dir.create(file.path(mainDir, subDir_3))
  
}
## Warning in dir.create(file.path(mainDir, subDir_3)): 'C:\Users\R
## \Documents\Recife\Orientandos Recife\Gabi Figueiredo_Doutorado\PID_Files
## \3_Infl_renamed_PIDs' já existe
subDir_4 <- "4_Infl_merged_PIDs"

if (file.exists(subDir_4)){
  setwd(file.path(mainDir, subDir_4))
} else {
  dir.create(file.path(mainDir, subDir_4))
  
}
## Warning in dir.create(file.path(mainDir, subDir_4)): 'C:\Users\R
## \Documents\Recife\Orientandos Recife\Gabi Figueiredo_Doutorado\PID_Files
## \4_Infl_merged_PIDs' já existe
# CLEANUP, remove files that contain the string "concentrations"

setwd(file.path(mainDir, subDir_1))

getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/1_Raw_INPUT_PIDs"
file.remove(list.files(pattern = "concentrations") )
## logical(0)
# Step I.4. This copies your PID files into  "2_Inflated_PIDs" and inflates them ( x SubPart)
# (duplicates all  object, according to the SubPart factor,e.g. if SuBpart = 4, now you will have 4 times more lines in the data table  )

#I.4.1 This lists all files in Input directory,copies them to subirectory "2_Inflated_PIDs"
setwd(file.path(mainDir, subDir_1))
getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/1_Raw_INPUT_PIDs"
list.files()
##  [1] "abr1_atol_st12_300_d1_d1_1_dat1.txt"      
##  [2] "abr1_atol_st12_300_d2_d2_1_dat1.txt"      
##  [3] "abr1_atol_st13_300_d1_d1_1_dat1.txt"      
##  [4] "abr1_atol_st13_300_d2_d2_1_dat1.txt"      
##  [5] "abr1_atol_st14_300_d1_d1_1_dat1.txt"      
##  [6] "abr1_atol_st14_300_d2_d2_1_dat1.txt"      
##  [7] "abr1_atol_st20_300_d1_d1_1_dat1.txt"      
##  [8] "abr1_atol_st20_300_d2_d2_1_dat1.txt"      
##  [9] "abr1_atol_st22_300_d1_d1_1_dat1.txt"      
## [10] "abr1_atol_st22_300_d2_d2_1_dat1.txt"      
## [11] "abr1_atol_st23_300_d1_d1_1_dat1.txt"      
## [12] "abr1_atol_st23_300_d2_d1_1_dat1.txt"      
## [13] "abr1_noronha_st01_300_d1_d1_1_dat1.txt"   
## [14] "abr1_noronha_st01_300_d2_d2_1_dat1.txt"   
## [15] "abr1_noronha_st02_300_d1_d1_1_dat1.txt"   
## [16] "abr1_noronha_st02_300_d2_d2_1_dat1.txt"   
## [17] "abr1_noronha_st03_300_d1_d1_1_dat1.txt"   
## [18] "abr1_noronha_st03_300_d2_d2_1_dat1.txt"   
## [19] "abr1_noronha_st04_300_d1_d1_1_dat1.txt"   
## [20] "abr1_noronha_st04_300_d2_d2_1_dat1.txt"   
## [21] "abr1_noronha_st05_300_d1_d1_1_dat1.txt"   
## [22] "abr1_noronha_st05_300_d2_d2_1_dat1.txt"   
## [23] "abr1_noronha_st06_300_d1_d1_1_dat1.txt"   
## [24] "abr1_noronha_st06_300_d2_d2_1_dat1.txt"   
## [25] "abr1_noronha_st07_300_d1_d1_1_dat1.txt"   
## [26] "abr1_noronha_st07_300_d2_d2_1_dat1.txt"   
## [27] "abr1_noronha_st08_300_d1_d1_1_dat1.txt"   
## [28] "abr1_noronha_st08_300_d2_d2_1_dat1.txt"   
## [29] "abr1_noronha_st09_300_d1_d1_1_dat1.txt"   
## [30] "abr1_noronha_st09_300_d2_d2_1_dat1.txt"   
## [31] "abr1_noronha_st11_300_d1_d1_1_dat1.txt"   
## [32] "abr1_noronha_st11_300_d2_d2_1_dat1.txt"   
## [33] "abr1_plataforma_st25_300_d1_d1_1_dat1.txt"
## [34] "abr1_plataforma_st25_300_d2_d2_1_dat1.txt"
## [35] "abr1_plataforma_st26_300_d1_d1_1_dat1.txt"
## [36] "abr1_plataforma_st26_300_d2_d2_1_dat1.txt"
## [37] "abr1_plataforma_st27_300_d1_d1_1_dat1.txt"
## [38] "abr1_plataforma_st27_300_d2_d2_1_dat1.txt"
## [39] "abr1_plataforma_st29_300_d1_d1_1_dat1.txt"
## [40] "abr1_plataforma_st29_300_d2_d2_1_dat1.txt"
## [41] "abr1_plataforma_st31_300_d1_d1_1_dat1.txt"
## [42] "abr1_plataforma_st31_300_d2_d2_1_dat1.txt"
## [43] "abr1_plataforma_st33_300_d1_d1_1_dat1.txt"
## [44] "abr1_plataforma_st33_300_d2_d2_1_dat1.txt"
## [45] "abr1_plataforma_st34_300_d1_d1_1_dat1.txt"
## [46] "abr1_plataforma_st34_300_d2_d2_1_dat1.txt"
## [47] "abr1_plataforma_st36_300_d1_d1_1_dat1.txt"
## [48] "abr1_plataforma_st36_300_d2_d2_1_dat1.txt"
## [49] "abr1_plataforma_st37_300_d1_d1_1_dat1.txt"
## [50] "abr1_plataforma_st37_300_d2_d2_1_dat1.txt"
## [51] "abr1_plataforma_st40_300_d1_d1_1_dat1.txt"
## [52] "abr1_plataforma_st40_300_d2_d2_1_dat1.txt"
## [53] "abr1_plataforma_st41_300_d1_d1_1_dat1.txt"
## [54] "abr1_plataforma_st41_300_d2_d2_1_dat1.txt"
## [55] "abr1_plataforma_st43_300_d1_d1_1_dat1.txt"
## [56] "abr1_plataforma_st43_300_d2_d2_1_dat1.txt"
## [57] "abr1_plataforma_st45_300_d1_d1_1_dat1.txt"
## [58] "abr1_plataforma_st45_300_d2_d2_1_dat1.txt"
## [59] "abr1_plataforma_st48_300_d1_d1_1_dat1.txt"
## [60] "abr1_plataforma_st48_300_d2_d2_1_dat1.txt"
## [61] "abr1_plataforma_st49_300_d1_d1_1_dat1.txt"
## [62] "abr1_plataforma_st49_300_d2_d2_1_dat1.txt"
## [63] "abr1_plataforma_st51_300_d1_d1_1_dat1.txt"
## [64] "abr1_plataforma_st51_300_d2_d2_1_dat1.txt"
## [65] "abr1_plataforma_st52_300_d1_d1_1_dat1.txt"
## [66] "abr1_plataforma_st52_300_d2_d2_1_dat1.txt"
## [67] "abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
## [68] "abr1_plataforma_st54_300_d2_d2_1_dat1.txt"
filestocopy <- list.files()
filestocopy
##  [1] "abr1_atol_st12_300_d1_d1_1_dat1.txt"      
##  [2] "abr1_atol_st12_300_d2_d2_1_dat1.txt"      
##  [3] "abr1_atol_st13_300_d1_d1_1_dat1.txt"      
##  [4] "abr1_atol_st13_300_d2_d2_1_dat1.txt"      
##  [5] "abr1_atol_st14_300_d1_d1_1_dat1.txt"      
##  [6] "abr1_atol_st14_300_d2_d2_1_dat1.txt"      
##  [7] "abr1_atol_st20_300_d1_d1_1_dat1.txt"      
##  [8] "abr1_atol_st20_300_d2_d2_1_dat1.txt"      
##  [9] "abr1_atol_st22_300_d1_d1_1_dat1.txt"      
## [10] "abr1_atol_st22_300_d2_d2_1_dat1.txt"      
## [11] "abr1_atol_st23_300_d1_d1_1_dat1.txt"      
## [12] "abr1_atol_st23_300_d2_d1_1_dat1.txt"      
## [13] "abr1_noronha_st01_300_d1_d1_1_dat1.txt"   
## [14] "abr1_noronha_st01_300_d2_d2_1_dat1.txt"   
## [15] "abr1_noronha_st02_300_d1_d1_1_dat1.txt"   
## [16] "abr1_noronha_st02_300_d2_d2_1_dat1.txt"   
## [17] "abr1_noronha_st03_300_d1_d1_1_dat1.txt"   
## [18] "abr1_noronha_st03_300_d2_d2_1_dat1.txt"   
## [19] "abr1_noronha_st04_300_d1_d1_1_dat1.txt"   
## [20] "abr1_noronha_st04_300_d2_d2_1_dat1.txt"   
## [21] "abr1_noronha_st05_300_d1_d1_1_dat1.txt"   
## [22] "abr1_noronha_st05_300_d2_d2_1_dat1.txt"   
## [23] "abr1_noronha_st06_300_d1_d1_1_dat1.txt"   
## [24] "abr1_noronha_st06_300_d2_d2_1_dat1.txt"   
## [25] "abr1_noronha_st07_300_d1_d1_1_dat1.txt"   
## [26] "abr1_noronha_st07_300_d2_d2_1_dat1.txt"   
## [27] "abr1_noronha_st08_300_d1_d1_1_dat1.txt"   
## [28] "abr1_noronha_st08_300_d2_d2_1_dat1.txt"   
## [29] "abr1_noronha_st09_300_d1_d1_1_dat1.txt"   
## [30] "abr1_noronha_st09_300_d2_d2_1_dat1.txt"   
## [31] "abr1_noronha_st11_300_d1_d1_1_dat1.txt"   
## [32] "abr1_noronha_st11_300_d2_d2_1_dat1.txt"   
## [33] "abr1_plataforma_st25_300_d1_d1_1_dat1.txt"
## [34] "abr1_plataforma_st25_300_d2_d2_1_dat1.txt"
## [35] "abr1_plataforma_st26_300_d1_d1_1_dat1.txt"
## [36] "abr1_plataforma_st26_300_d2_d2_1_dat1.txt"
## [37] "abr1_plataforma_st27_300_d1_d1_1_dat1.txt"
## [38] "abr1_plataforma_st27_300_d2_d2_1_dat1.txt"
## [39] "abr1_plataforma_st29_300_d1_d1_1_dat1.txt"
## [40] "abr1_plataforma_st29_300_d2_d2_1_dat1.txt"
## [41] "abr1_plataforma_st31_300_d1_d1_1_dat1.txt"
## [42] "abr1_plataforma_st31_300_d2_d2_1_dat1.txt"
## [43] "abr1_plataforma_st33_300_d1_d1_1_dat1.txt"
## [44] "abr1_plataforma_st33_300_d2_d2_1_dat1.txt"
## [45] "abr1_plataforma_st34_300_d1_d1_1_dat1.txt"
## [46] "abr1_plataforma_st34_300_d2_d2_1_dat1.txt"
## [47] "abr1_plataforma_st36_300_d1_d1_1_dat1.txt"
## [48] "abr1_plataforma_st36_300_d2_d2_1_dat1.txt"
## [49] "abr1_plataforma_st37_300_d1_d1_1_dat1.txt"
## [50] "abr1_plataforma_st37_300_d2_d2_1_dat1.txt"
## [51] "abr1_plataforma_st40_300_d1_d1_1_dat1.txt"
## [52] "abr1_plataforma_st40_300_d2_d2_1_dat1.txt"
## [53] "abr1_plataforma_st41_300_d1_d1_1_dat1.txt"
## [54] "abr1_plataforma_st41_300_d2_d2_1_dat1.txt"
## [55] "abr1_plataforma_st43_300_d1_d1_1_dat1.txt"
## [56] "abr1_plataforma_st43_300_d2_d2_1_dat1.txt"
## [57] "abr1_plataforma_st45_300_d1_d1_1_dat1.txt"
## [58] "abr1_plataforma_st45_300_d2_d2_1_dat1.txt"
## [59] "abr1_plataforma_st48_300_d1_d1_1_dat1.txt"
## [60] "abr1_plataforma_st48_300_d2_d2_1_dat1.txt"
## [61] "abr1_plataforma_st49_300_d1_d1_1_dat1.txt"
## [62] "abr1_plataforma_st49_300_d2_d2_1_dat1.txt"
## [63] "abr1_plataforma_st51_300_d1_d1_1_dat1.txt"
## [64] "abr1_plataforma_st51_300_d2_d2_1_dat1.txt"
## [65] "abr1_plataforma_st52_300_d1_d1_1_dat1.txt"
## [66] "abr1_plataforma_st52_300_d2_d2_1_dat1.txt"
## [67] "abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
## [68] "abr1_plataforma_st54_300_d2_d2_1_dat1.txt"
targetdir <-  file.path(mainDir, subDir_2)

file.copy(from=filestocopy, to=targetdir, 
          recursive = FALSE, 
          copy.mode = TRUE)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [67] FALSE FALSE
#I.4.2 inflates, renames all files in subirectory "2_Inflated_PIDs", sets all Subparts to 1
# new names : "Infl_..."
# inflate = duplicate data lines by the Subpart factor,  each file has more lines now

setwd(file.path(mainDir, subDir_2))

getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/2_Inflated_PIDs"
# Read the data and Sub Part of all Files


### INPUT I.4.3. PLEASE define the number of files to import (default: 3 Files, files 1 to 3)

N_FILES <- (length(filestocopy)) #NUMBER OF FILES TO BE ANALYZED

# N_FILES <- 3  # NUMBER OF FILES TO BE ANALYZED

 N_FILES
## [1] 68
# files.input <- list.files(pattern = "\\.txt$") #use this if your input file names are ".txt"
# files.input <- list.files(pattern = "\\.PID$") #use this if your input files nems are ".PID"

files.input <- filestocopy
files.input <- files.input[1:N_FILES] # SELECTS ONLY THE FIRST "N" FILES FOR ANLYSIS
files.input
##  [1] "abr1_atol_st12_300_d1_d1_1_dat1.txt"      
##  [2] "abr1_atol_st12_300_d2_d2_1_dat1.txt"      
##  [3] "abr1_atol_st13_300_d1_d1_1_dat1.txt"      
##  [4] "abr1_atol_st13_300_d2_d2_1_dat1.txt"      
##  [5] "abr1_atol_st14_300_d1_d1_1_dat1.txt"      
##  [6] "abr1_atol_st14_300_d2_d2_1_dat1.txt"      
##  [7] "abr1_atol_st20_300_d1_d1_1_dat1.txt"      
##  [8] "abr1_atol_st20_300_d2_d2_1_dat1.txt"      
##  [9] "abr1_atol_st22_300_d1_d1_1_dat1.txt"      
## [10] "abr1_atol_st22_300_d2_d2_1_dat1.txt"      
## [11] "abr1_atol_st23_300_d1_d1_1_dat1.txt"      
## [12] "abr1_atol_st23_300_d2_d1_1_dat1.txt"      
## [13] "abr1_noronha_st01_300_d1_d1_1_dat1.txt"   
## [14] "abr1_noronha_st01_300_d2_d2_1_dat1.txt"   
## [15] "abr1_noronha_st02_300_d1_d1_1_dat1.txt"   
## [16] "abr1_noronha_st02_300_d2_d2_1_dat1.txt"   
## [17] "abr1_noronha_st03_300_d1_d1_1_dat1.txt"   
## [18] "abr1_noronha_st03_300_d2_d2_1_dat1.txt"   
## [19] "abr1_noronha_st04_300_d1_d1_1_dat1.txt"   
## [20] "abr1_noronha_st04_300_d2_d2_1_dat1.txt"   
## [21] "abr1_noronha_st05_300_d1_d1_1_dat1.txt"   
## [22] "abr1_noronha_st05_300_d2_d2_1_dat1.txt"   
## [23] "abr1_noronha_st06_300_d1_d1_1_dat1.txt"   
## [24] "abr1_noronha_st06_300_d2_d2_1_dat1.txt"   
## [25] "abr1_noronha_st07_300_d1_d1_1_dat1.txt"   
## [26] "abr1_noronha_st07_300_d2_d2_1_dat1.txt"   
## [27] "abr1_noronha_st08_300_d1_d1_1_dat1.txt"   
## [28] "abr1_noronha_st08_300_d2_d2_1_dat1.txt"   
## [29] "abr1_noronha_st09_300_d1_d1_1_dat1.txt"   
## [30] "abr1_noronha_st09_300_d2_d2_1_dat1.txt"   
## [31] "abr1_noronha_st11_300_d1_d1_1_dat1.txt"   
## [32] "abr1_noronha_st11_300_d2_d2_1_dat1.txt"   
## [33] "abr1_plataforma_st25_300_d1_d1_1_dat1.txt"
## [34] "abr1_plataforma_st25_300_d2_d2_1_dat1.txt"
## [35] "abr1_plataforma_st26_300_d1_d1_1_dat1.txt"
## [36] "abr1_plataforma_st26_300_d2_d2_1_dat1.txt"
## [37] "abr1_plataforma_st27_300_d1_d1_1_dat1.txt"
## [38] "abr1_plataforma_st27_300_d2_d2_1_dat1.txt"
## [39] "abr1_plataforma_st29_300_d1_d1_1_dat1.txt"
## [40] "abr1_plataforma_st29_300_d2_d2_1_dat1.txt"
## [41] "abr1_plataforma_st31_300_d1_d1_1_dat1.txt"
## [42] "abr1_plataforma_st31_300_d2_d2_1_dat1.txt"
## [43] "abr1_plataforma_st33_300_d1_d1_1_dat1.txt"
## [44] "abr1_plataforma_st33_300_d2_d2_1_dat1.txt"
## [45] "abr1_plataforma_st34_300_d1_d1_1_dat1.txt"
## [46] "abr1_plataforma_st34_300_d2_d2_1_dat1.txt"
## [47] "abr1_plataforma_st36_300_d1_d1_1_dat1.txt"
## [48] "abr1_plataforma_st36_300_d2_d2_1_dat1.txt"
## [49] "abr1_plataforma_st37_300_d1_d1_1_dat1.txt"
## [50] "abr1_plataforma_st37_300_d2_d2_1_dat1.txt"
## [51] "abr1_plataforma_st40_300_d1_d1_1_dat1.txt"
## [52] "abr1_plataforma_st40_300_d2_d2_1_dat1.txt"
## [53] "abr1_plataforma_st41_300_d1_d1_1_dat1.txt"
## [54] "abr1_plataforma_st41_300_d2_d2_1_dat1.txt"
## [55] "abr1_plataforma_st43_300_d1_d1_1_dat1.txt"
## [56] "abr1_plataforma_st43_300_d2_d2_1_dat1.txt"
## [57] "abr1_plataforma_st45_300_d1_d1_1_dat1.txt"
## [58] "abr1_plataforma_st45_300_d2_d2_1_dat1.txt"
## [59] "abr1_plataforma_st48_300_d1_d1_1_dat1.txt"
## [60] "abr1_plataforma_st48_300_d2_d2_1_dat1.txt"
## [61] "abr1_plataforma_st49_300_d1_d1_1_dat1.txt"
## [62] "abr1_plataforma_st49_300_d2_d2_1_dat1.txt"
## [63] "abr1_plataforma_st51_300_d1_d1_1_dat1.txt"
## [64] "abr1_plataforma_st51_300_d2_d2_1_dat1.txt"
## [65] "abr1_plataforma_st52_300_d1_d1_1_dat1.txt"
## [66] "abr1_plataforma_st52_300_d2_d2_1_dat1.txt"
## [67] "abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
## [68] "abr1_plataforma_st54_300_d2_d2_1_dat1.txt"
# save &  load Workspace
# save.image("ZooScan_NBSS_1.RData")
# load("ZooScan_NBSS_1.RData")


# Step II. Creates lists of Names for BATCH Import (Data, Vol.filtr, Sub.Part_)

# original names of all ZooScan PID files
zznames = paste((basename(files.input[1:N_FILES]))) 

#  names of all ZooScan PID files with "zz_..."
zznames_Data = paste("zz_",substr(basename(files.input[1:N_FILES]), 0, nchar(basename(files.input[1:3])) ), sep="")

# Objects that will receive Sub.Part and Vol.filtr. values
zz_Sub.Part = paste("zzSub.Part_",substr(basename(files.input[1:N_FILES]), 10, nchar(basename(files.input[1:3])) - 4), sep="")
zz_Vol.f = paste("zzVol.f_",substr(basename(files.input[1:N_FILES]), 10, nchar(basename(files.input[1:3])) - 4), sep="")


# Step III. LOOP - BATCH IMPORT - Reads Data, Vol.filtr, Sub.Part (makes tables for Data, Vol.filtr, Sub.Part)

#  file.index <- 3

setwd(file.path(mainDir, subDir_1))
getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/1_Raw_INPUT_PIDs"
# for (file.index in 21:30)


for (file.index in 1:length(zznames_Data))
  
{
  #III.A. Read the file

  setwd(file.path(mainDir, subDir_1))

  full.textfileAll <- read.table(file = files.input[file.index], sep = ";", skip = 0, colClasses = "character", quote = "")
 
  #III.B. Import the Data Table
  
  #III.B.1 Searches, Finds and Gives the Line number of the Header, imports the Data Table
  
  grep("Data", full.textfileAll$V1)  # gives the line number of the line containing "SubPart"
  
  Data.line.many = grep("Data", full.textfileAll$V1) #  all line numbers containing "Data"
  Data.line <- Data.line.many[3]   #  Line number of the third time "Data" appears in the file, above the  Header of the Data Table.
  Data.line  #  Line number of the text "Data", above the  Header of the Data Table
  
  #III. B.2. Read the Data Table,save  it as "Data_(filename)"
  # assign(zznames_Data[2], read.table(file = files.input[2], sep = "\t", skip = (Data.line), header = TRUE)) # Imports the ZooScan Data Table
  temp.obj <-  assign(zznames_Data[file.index], read.table(file = files.input[file.index], sep = "\t", skip = (Data.line), header = TRUE)) # Imports the ZooScan Data Table
  
#View (temp.obj)

  #III.C.Searches, Finds and Gives the Line number of the Subsampling Factor
  
  
  #III. C1. Search and Find  the SubSampling Factor
  
  SP.line = grep("SubPart", full.textfileAll$V1) #  gives the line number of the line containing "SubPart"
  SP.line
  
  # III.C2. Read the SubSampling Factor
  
   #full.textfileSP <- read.table(file = files.input[2], sep = "=", skip = (SP.line - 1), nrows= 1, colClasses = "character")
  full.textfileSP <- read.table(file = files.input[file.index], sep = "=", skip = (SP.line - 1), nrows= 1, colClasses = "character")
  
  zz_Sub.Part[file.index] <- as.numeric(full.textfileSP[1,2]); 
  zz_Sub.Part[file.index]    # gives the SubSampling Factor
  #zz_Sub.Part[2] <- as.numeric(full.textfileSP[1,2]); 
  #zz_Sub.Part[2]    # gives the SubSampling Factor
  
  full.textfileSP <- zz_Sub.Part[file.index]
  # full.textfileSP <- zz_Sub.Part[2]
  
  
  # III.D. now inflate
  
  library(mefa)
  
  infl.temp<- temp.obj
  
  infl.temp2<-  mefa:::rep.data.frame(infl.temp, full.textfileSP)
  
  # View(infl.temp)

  
  length(infl.temp[,1]) # no de objetos pr?-infl.

    
  length(infl.temp2[,1]) # no de objetos p?s-infl.

  #View(infl.temp2)
  
    
  ###
  
  #  III.E. Rename the textfiles, merge  inflated data with old header,change SubPart to 1  
  
  
  # III.E.1 merge  inflated data with old header  
  
  #read the old header
   header.temp <- read.table(file = files.input[file.index], col.names = "V1", comment="", quote= NULL,sep = "\t", header = FALSE) # Imports the Header
   header.temp <- as.vector( header.temp)
   #View(header.temp)
   
   header.temp2 <- as.data.frame(header.temp[1:(Data.line+1),])
   # View(header.temp2)
   
   # merge header and data
   
   names(header.temp2) <- "V1"
   names(infl.temp2) <- "V1"
   
   newfile <- rbind.data.frame(header.temp2, (infl.temp2), make.row.names = FALSE, stringsAsFactors = FALSE)
   newfile$V1   <- as.character(newfile$V1) 
   
   
   # View(newfile) 
   
  # III.E.2 CHANGE SUBPART TO 1
   
   newfile[SP.line,1] <-  "SubPart= 1"
   
 
   setwd(file.path(mainDir, subDir_2))
   
   
     # III.E.3 save inflated file  with old name 
   
   write.table(newfile, file = paste("zz_Infl_" ,zznames[file.index] ),
               row.names = FALSE, quote=FALSE, col.names = FALSE)
  
   

}  # END OF first LOOP (inflate and rename)

# OK
###


# MERGE pairs of inflated PIDs #


#  STEP IV. (USER ACTION): PLEASE check carefully (or manually rename) all pairs of inflated aliquots, place them in subdirectory " 3_Infl_renamed_PIDs "
# Example without renaming : "...300_d1..."  (Aliq. 1) and "...300_d2..." (Aliq. 1)  search these stings in old filenames, no renaming necessary 
# Example with  renaming: New file names:   "Ali1_Infl_..."  and "Ali2_Infl_..."
# file name "..." has to be identical for each pair of aliquots


#  STEP V:  now merge   "Ali1_Infl_"  and "Ali2_Infl_" Files, keep Header (SubPart = 1)
#   Creates one "Merged_Infl_..." file for each pair

# copy all inflated pairs from "2_Inflated_PIDs" into "3_Infl_renamed_PIDs", then check (or manually rename, if necessary)

subDir_2 <- "2_Inflated_PIDs"

subDir_3 <- "3_Infl_renamed_PIDs"

subDir_4 <- "4_Infl_merged_PIDs"

setwd(file.path(mainDir, subDir_2))
getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/2_Inflated_PIDs"
filestocopy <- list.files()
filestocopy
##   [1] "abr1_atol_st12_300_d1_d1_1_dat1.txt"               
##   [2] "abr1_atol_st12_300_d2_d2_1_dat1.txt"               
##   [3] "abr1_atol_st13_300_d1_d1_1_dat1.txt"               
##   [4] "abr1_atol_st13_300_d2_d2_1_dat1.txt"               
##   [5] "abr1_atol_st14_300_d1_d1_1_dat1.txt"               
##   [6] "abr1_atol_st14_300_d2_d2_1_dat1.txt"               
##   [7] "abr1_atol_st20_300_d1_d1_1_dat1.txt"               
##   [8] "abr1_atol_st20_300_d2_d2_1_dat1.txt"               
##   [9] "abr1_atol_st22_300_d1_d1_1_dat1.txt"               
##  [10] "abr1_atol_st22_300_d2_d2_1_dat1.txt"               
##  [11] "abr1_atol_st23_300_d1_d1_1_dat1.txt"               
##  [12] "abr1_atol_st23_300_d2_d1_1_dat1.txt"               
##  [13] "abr1_noronha_st01_300_d1_d1_1_dat1.txt"            
##  [14] "abr1_noronha_st01_300_d2_d2_1_dat1.txt"            
##  [15] "abr1_noronha_st02_300_d1_d1_1_dat1.txt"            
##  [16] "abr1_noronha_st02_300_d2_d2_1_dat1.txt"            
##  [17] "abr1_noronha_st03_300_d1_d1_1_dat1.txt"            
##  [18] "abr1_noronha_st03_300_d2_d2_1_dat1.txt"            
##  [19] "abr1_noronha_st04_300_d1_d1_1_dat1.txt"            
##  [20] "abr1_noronha_st04_300_d2_d2_1_dat1.txt"            
##  [21] "abr1_noronha_st05_300_d1_d1_1_dat1.txt"            
##  [22] "abr1_noronha_st05_300_d2_d2_1_dat1.txt"            
##  [23] "abr1_noronha_st06_300_d1_d1_1_dat1.txt"            
##  [24] "abr1_noronha_st06_300_d2_d2_1_dat1.txt"            
##  [25] "abr1_noronha_st07_300_d1_d1_1_dat1.txt"            
##  [26] "abr1_noronha_st07_300_d2_d2_1_dat1.txt"            
##  [27] "abr1_noronha_st08_300_d1_d1_1_dat1.txt"            
##  [28] "abr1_noronha_st08_300_d2_d2_1_dat1.txt"            
##  [29] "abr1_noronha_st09_300_d1_d1_1_dat1.txt"            
##  [30] "abr1_noronha_st09_300_d2_d2_1_dat1.txt"            
##  [31] "abr1_noronha_st11_300_d1_d1_1_dat1.txt"            
##  [32] "abr1_noronha_st11_300_d2_d2_1_dat1.txt"            
##  [33] "abr1_plataforma_st25_300_d1_d1_1_dat1.txt"         
##  [34] "abr1_plataforma_st25_300_d2_d2_1_dat1.txt"         
##  [35] "abr1_plataforma_st26_300_d1_d1_1_dat1.txt"         
##  [36] "abr1_plataforma_st26_300_d2_d2_1_dat1.txt"         
##  [37] "abr1_plataforma_st27_300_d1_d1_1_dat1.txt"         
##  [38] "abr1_plataforma_st27_300_d2_d2_1_dat1.txt"         
##  [39] "abr1_plataforma_st29_300_d1_d1_1_dat1.txt"         
##  [40] "abr1_plataforma_st29_300_d2_d2_1_dat1.txt"         
##  [41] "abr1_plataforma_st31_300_d1_d1_1_dat1.txt"         
##  [42] "abr1_plataforma_st31_300_d2_d2_1_dat1.txt"         
##  [43] "abr1_plataforma_st33_300_d1_d1_1_dat1.txt"         
##  [44] "abr1_plataforma_st33_300_d2_d2_1_dat1.txt"         
##  [45] "abr1_plataforma_st34_300_d1_d1_1_dat1.txt"         
##  [46] "abr1_plataforma_st34_300_d2_d2_1_dat1.txt"         
##  [47] "abr1_plataforma_st36_300_d1_d1_1_dat1.txt"         
##  [48] "abr1_plataforma_st36_300_d2_d2_1_dat1.txt"         
##  [49] "abr1_plataforma_st37_300_d1_d1_1_dat1.txt"         
##  [50] "abr1_plataforma_st37_300_d2_d2_1_dat1.txt"         
##  [51] "abr1_plataforma_st40_300_d1_d1_1_dat1.txt"         
##  [52] "abr1_plataforma_st40_300_d2_d2_1_dat1.txt"         
##  [53] "abr1_plataforma_st41_300_d1_d1_1_dat1.txt"         
##  [54] "abr1_plataforma_st41_300_d2_d2_1_dat1.txt"         
##  [55] "abr1_plataforma_st43_300_d1_d1_1_dat1.txt"         
##  [56] "abr1_plataforma_st43_300_d2_d2_1_dat1.txt"         
##  [57] "abr1_plataforma_st45_300_d1_d1_1_dat1.txt"         
##  [58] "abr1_plataforma_st45_300_d2_d2_1_dat1.txt"         
##  [59] "abr1_plataforma_st48_300_d1_d1_1_dat1.txt"         
##  [60] "abr1_plataforma_st48_300_d2_d2_1_dat1.txt"         
##  [61] "abr1_plataforma_st49_300_d1_d1_1_dat1.txt"         
##  [62] "abr1_plataforma_st49_300_d2_d2_1_dat1.txt"         
##  [63] "abr1_plataforma_st51_300_d1_d1_1_dat1.txt"         
##  [64] "abr1_plataforma_st51_300_d2_d2_1_dat1.txt"         
##  [65] "abr1_plataforma_st52_300_d1_d1_1_dat1.txt"         
##  [66] "abr1_plataforma_st52_300_d2_d2_1_dat1.txt"         
##  [67] "abr1_plataforma_st54_300_d1_d1_1_dat1.txt"         
##  [68] "abr1_plataforma_st54_300_d2_d2_1_dat1.txt"         
##  [69] "zz_Infl_ abr1_atol_st12_300_d1_d1_1_dat1.txt"      
##  [70] "zz_Infl_ abr1_atol_st12_300_d2_d2_1_dat1.txt"      
##  [71] "zz_Infl_ abr1_atol_st13_300_d1_d1_1_dat1.txt"      
##  [72] "zz_Infl_ abr1_atol_st13_300_d2_d2_1_dat1.txt"      
##  [73] "zz_Infl_ abr1_atol_st14_300_d1_d1_1_dat1.txt"      
##  [74] "zz_Infl_ abr1_atol_st14_300_d2_d2_1_dat1.txt"      
##  [75] "zz_Infl_ abr1_atol_st20_300_d1_d1_1_dat1.txt"      
##  [76] "zz_Infl_ abr1_atol_st20_300_d2_d2_1_dat1.txt"      
##  [77] "zz_Infl_ abr1_atol_st22_300_d1_d1_1_dat1.txt"      
##  [78] "zz_Infl_ abr1_atol_st22_300_d2_d2_1_dat1.txt"      
##  [79] "zz_Infl_ abr1_atol_st23_300_d1_d1_1_dat1.txt"      
##  [80] "zz_Infl_ abr1_atol_st23_300_d2_d1_1_dat1.txt"      
##  [81] "zz_Infl_ abr1_noronha_st01_300_d1_d1_1_dat1.txt"   
##  [82] "zz_Infl_ abr1_noronha_st01_300_d2_d2_1_dat1.txt"   
##  [83] "zz_Infl_ abr1_noronha_st02_300_d1_d1_1_dat1.txt"   
##  [84] "zz_Infl_ abr1_noronha_st02_300_d2_d2_1_dat1.txt"   
##  [85] "zz_Infl_ abr1_noronha_st03_300_d1_d1_1_dat1.txt"   
##  [86] "zz_Infl_ abr1_noronha_st03_300_d2_d2_1_dat1.txt"   
##  [87] "zz_Infl_ abr1_noronha_st04_300_d1_d1_1_dat1.txt"   
##  [88] "zz_Infl_ abr1_noronha_st04_300_d2_d2_1_dat1.txt"   
##  [89] "zz_Infl_ abr1_noronha_st05_300_d1_d1_1_dat1.txt"   
##  [90] "zz_Infl_ abr1_noronha_st05_300_d2_d2_1_dat1.txt"   
##  [91] "zz_Infl_ abr1_noronha_st06_300_d1_d1_1_dat1.txt"   
##  [92] "zz_Infl_ abr1_noronha_st06_300_d2_d2_1_dat1.txt"   
##  [93] "zz_Infl_ abr1_noronha_st07_300_d1_d1_1_dat1.txt"   
##  [94] "zz_Infl_ abr1_noronha_st07_300_d2_d2_1_dat1.txt"   
##  [95] "zz_Infl_ abr1_noronha_st08_300_d1_d1_1_dat1.txt"   
##  [96] "zz_Infl_ abr1_noronha_st08_300_d2_d2_1_dat1.txt"   
##  [97] "zz_Infl_ abr1_noronha_st09_300_d1_d1_1_dat1.txt"   
##  [98] "zz_Infl_ abr1_noronha_st09_300_d2_d2_1_dat1.txt"   
##  [99] "zz_Infl_ abr1_noronha_st11_300_d1_d1_1_dat1.txt"   
## [100] "zz_Infl_ abr1_noronha_st11_300_d2_d2_1_dat1.txt"   
## [101] "zz_Infl_ abr1_plataforma_st25_300_d1_d1_1_dat1.txt"
## [102] "zz_Infl_ abr1_plataforma_st25_300_d2_d2_1_dat1.txt"
## [103] "zz_Infl_ abr1_plataforma_st26_300_d1_d1_1_dat1.txt"
## [104] "zz_Infl_ abr1_plataforma_st26_300_d2_d2_1_dat1.txt"
## [105] "zz_Infl_ abr1_plataforma_st27_300_d1_d1_1_dat1.txt"
## [106] "zz_Infl_ abr1_plataforma_st27_300_d2_d2_1_dat1.txt"
## [107] "zz_Infl_ abr1_plataforma_st29_300_d1_d1_1_dat1.txt"
## [108] "zz_Infl_ abr1_plataforma_st29_300_d2_d2_1_dat1.txt"
## [109] "zz_Infl_ abr1_plataforma_st31_300_d1_d1_1_dat1.txt"
## [110] "zz_Infl_ abr1_plataforma_st31_300_d2_d2_1_dat1.txt"
## [111] "zz_Infl_ abr1_plataforma_st33_300_d1_d1_1_dat1.txt"
## [112] "zz_Infl_ abr1_plataforma_st33_300_d2_d2_1_dat1.txt"
## [113] "zz_Infl_ abr1_plataforma_st34_300_d1_d1_1_dat1.txt"
## [114] "zz_Infl_ abr1_plataforma_st34_300_d2_d2_1_dat1.txt"
## [115] "zz_Infl_ abr1_plataforma_st36_300_d1_d1_1_dat1.txt"
## [116] "zz_Infl_ abr1_plataforma_st36_300_d2_d2_1_dat1.txt"
## [117] "zz_Infl_ abr1_plataforma_st37_300_d1_d1_1_dat1.txt"
## [118] "zz_Infl_ abr1_plataforma_st37_300_d2_d2_1_dat1.txt"
## [119] "zz_Infl_ abr1_plataforma_st40_300_d1_d1_1_dat1.txt"
## [120] "zz_Infl_ abr1_plataforma_st40_300_d2_d2_1_dat1.txt"
## [121] "zz_Infl_ abr1_plataforma_st41_300_d1_d1_1_dat1.txt"
## [122] "zz_Infl_ abr1_plataforma_st41_300_d2_d2_1_dat1.txt"
## [123] "zz_Infl_ abr1_plataforma_st43_300_d1_d1_1_dat1.txt"
## [124] "zz_Infl_ abr1_plataforma_st43_300_d2_d2_1_dat1.txt"
## [125] "zz_Infl_ abr1_plataforma_st45_300_d1_d1_1_dat1.txt"
## [126] "zz_Infl_ abr1_plataforma_st45_300_d2_d2_1_dat1.txt"
## [127] "zz_Infl_ abr1_plataforma_st48_300_d1_d1_1_dat1.txt"
## [128] "zz_Infl_ abr1_plataforma_st48_300_d2_d2_1_dat1.txt"
## [129] "zz_Infl_ abr1_plataforma_st49_300_d1_d1_1_dat1.txt"
## [130] "zz_Infl_ abr1_plataforma_st49_300_d2_d2_1_dat1.txt"
## [131] "zz_Infl_ abr1_plataforma_st51_300_d1_d1_1_dat1.txt"
## [132] "zz_Infl_ abr1_plataforma_st51_300_d2_d2_1_dat1.txt"
## [133] "zz_Infl_ abr1_plataforma_st52_300_d1_d1_1_dat1.txt"
## [134] "zz_Infl_ abr1_plataforma_st52_300_d2_d2_1_dat1.txt"
## [135] "zz_Infl_ abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
## [136] "zz_Infl_ abr1_plataforma_st54_300_d2_d2_1_dat1.txt"
targetdir <-  file.path(mainDir, subDir_3)


file.copy(from=filestocopy, to=targetdir, 
          recursive = FALSE, 
          copy.mode = TRUE)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [111] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [122] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE
setwd(file.path(mainDir, subDir_3))
getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/3_Infl_renamed_PIDs"
list.files()
##   [1] "abr1_atol_st12_300_d1_d1_1_dat1.txt"               
##   [2] "abr1_atol_st12_300_d2_d2_1_dat1.txt"               
##   [3] "abr1_atol_st13_300_d1_d1_1_dat1.txt"               
##   [4] "abr1_atol_st13_300_d2_d2_1_dat1.txt"               
##   [5] "abr1_atol_st14_300_d1_d1_1_dat1.txt"               
##   [6] "abr1_atol_st14_300_d2_d2_1_dat1.txt"               
##   [7] "abr1_atol_st20_300_d1_d1_1_dat1.txt"               
##   [8] "abr1_atol_st20_300_d2_d2_1_dat1.txt"               
##   [9] "abr1_atol_st22_300_d1_d1_1_dat1.txt"               
##  [10] "abr1_atol_st22_300_d2_d2_1_dat1.txt"               
##  [11] "abr1_atol_st23_300_d1_d1_1_dat1.txt"               
##  [12] "abr1_atol_st23_300_d2_d1_1_dat1.txt"               
##  [13] "abr1_noronha_st01_300_d1_d1_1_dat1.txt"            
##  [14] "abr1_noronha_st01_300_d2_d2_1_dat1.txt"            
##  [15] "abr1_noronha_st02_300_d1_d1_1_dat1.txt"            
##  [16] "abr1_noronha_st02_300_d2_d2_1_dat1.txt"            
##  [17] "abr1_noronha_st03_300_d1_d1_1_dat1.txt"            
##  [18] "abr1_noronha_st03_300_d2_d2_1_dat1.txt"            
##  [19] "abr1_noronha_st04_300_d1_d1_1_dat1.txt"            
##  [20] "abr1_noronha_st04_300_d2_d2_1_dat1.txt"            
##  [21] "abr1_noronha_st05_300_d1_d1_1_dat1.txt"            
##  [22] "abr1_noronha_st05_300_d2_d2_1_dat1.txt"            
##  [23] "abr1_noronha_st06_300_d1_d1_1_dat1.txt"            
##  [24] "abr1_noronha_st06_300_d2_d2_1_dat1.txt"            
##  [25] "abr1_noronha_st07_300_d1_d1_1_dat1.txt"            
##  [26] "abr1_noronha_st07_300_d2_d2_1_dat1.txt"            
##  [27] "abr1_noronha_st08_300_d1_d1_1_dat1.txt"            
##  [28] "abr1_noronha_st08_300_d2_d2_1_dat1.txt"            
##  [29] "abr1_noronha_st09_300_d1_d1_1_dat1.txt"            
##  [30] "abr1_noronha_st09_300_d2_d2_1_dat1.txt"            
##  [31] "abr1_noronha_st11_300_d1_d1_1_dat1.txt"            
##  [32] "abr1_noronha_st11_300_d2_d2_1_dat1.txt"            
##  [33] "abr1_plataforma_st25_300_d1_d1_1_dat1.txt"         
##  [34] "abr1_plataforma_st25_300_d2_d2_1_dat1.txt"         
##  [35] "abr1_plataforma_st26_300_d1_d1_1_dat1.txt"         
##  [36] "abr1_plataforma_st26_300_d2_d2_1_dat1.txt"         
##  [37] "abr1_plataforma_st27_300_d1_d1_1_dat1.txt"         
##  [38] "abr1_plataforma_st27_300_d2_d2_1_dat1.txt"         
##  [39] "abr1_plataforma_st29_300_d1_d1_1_dat1.txt"         
##  [40] "abr1_plataforma_st29_300_d2_d2_1_dat1.txt"         
##  [41] "abr1_plataforma_st31_300_d1_d1_1_dat1.txt"         
##  [42] "abr1_plataforma_st31_300_d2_d2_1_dat1.txt"         
##  [43] "abr1_plataforma_st33_300_d1_d1_1_dat1.txt"         
##  [44] "abr1_plataforma_st33_300_d2_d2_1_dat1.txt"         
##  [45] "abr1_plataforma_st34_300_d1_d1_1_dat1.txt"         
##  [46] "abr1_plataforma_st34_300_d2_d2_1_dat1.txt"         
##  [47] "abr1_plataforma_st36_300_d1_d1_1_dat1.txt"         
##  [48] "abr1_plataforma_st36_300_d2_d2_1_dat1.txt"         
##  [49] "abr1_plataforma_st37_300_d1_d1_1_dat1.txt"         
##  [50] "abr1_plataforma_st37_300_d2_d2_1_dat1.txt"         
##  [51] "abr1_plataforma_st40_300_d1_d1_1_dat1.txt"         
##  [52] "abr1_plataforma_st40_300_d2_d2_1_dat1.txt"         
##  [53] "abr1_plataforma_st41_300_d1_d1_1_dat1.txt"         
##  [54] "abr1_plataforma_st41_300_d2_d2_1_dat1.txt"         
##  [55] "abr1_plataforma_st43_300_d1_d1_1_dat1.txt"         
##  [56] "abr1_plataforma_st43_300_d2_d2_1_dat1.txt"         
##  [57] "abr1_plataforma_st45_300_d1_d1_1_dat1.txt"         
##  [58] "abr1_plataforma_st45_300_d2_d2_1_dat1.txt"         
##  [59] "abr1_plataforma_st48_300_d1_d1_1_dat1.txt"         
##  [60] "abr1_plataforma_st48_300_d2_d2_1_dat1.txt"         
##  [61] "abr1_plataforma_st49_300_d1_d1_1_dat1.txt"         
##  [62] "abr1_plataforma_st49_300_d2_d2_1_dat1.txt"         
##  [63] "abr1_plataforma_st51_300_d1_d1_1_dat1.txt"         
##  [64] "abr1_plataforma_st51_300_d2_d2_1_dat1.txt"         
##  [65] "abr1_plataforma_st52_300_d1_d1_1_dat1.txt"         
##  [66] "abr1_plataforma_st52_300_d2_d2_1_dat1.txt"         
##  [67] "abr1_plataforma_st54_300_d1_d1_1_dat1.txt"         
##  [68] "abr1_plataforma_st54_300_d2_d2_1_dat1.txt"         
##  [69] "zz_Infl_ abr1_atol_st12_300_d1_d1_1_dat1.txt"      
##  [70] "zz_Infl_ abr1_atol_st12_300_d2_d2_1_dat1.txt"      
##  [71] "zz_Infl_ abr1_atol_st13_300_d1_d1_1_dat1.txt"      
##  [72] "zz_Infl_ abr1_atol_st13_300_d2_d2_1_dat1.txt"      
##  [73] "zz_Infl_ abr1_atol_st14_300_d1_d1_1_dat1.txt"      
##  [74] "zz_Infl_ abr1_atol_st14_300_d2_d2_1_dat1.txt"      
##  [75] "zz_Infl_ abr1_atol_st20_300_d1_d1_1_dat1.txt"      
##  [76] "zz_Infl_ abr1_atol_st20_300_d2_d2_1_dat1.txt"      
##  [77] "zz_Infl_ abr1_atol_st22_300_d1_d1_1_dat1.txt"      
##  [78] "zz_Infl_ abr1_atol_st22_300_d2_d2_1_dat1.txt"      
##  [79] "zz_Infl_ abr1_atol_st23_300_d1_d1_1_dat1.txt"      
##  [80] "zz_Infl_ abr1_atol_st23_300_d2_d1_1_dat1.txt"      
##  [81] "zz_Infl_ abr1_noronha_st01_300_d1_d1_1_dat1.txt"   
##  [82] "zz_Infl_ abr1_noronha_st01_300_d2_d2_1_dat1.txt"   
##  [83] "zz_Infl_ abr1_noronha_st02_300_d1_d1_1_dat1.txt"   
##  [84] "zz_Infl_ abr1_noronha_st02_300_d2_d2_1_dat1.txt"   
##  [85] "zz_Infl_ abr1_noronha_st03_300_d1_d1_1_dat1.txt"   
##  [86] "zz_Infl_ abr1_noronha_st03_300_d2_d2_1_dat1.txt"   
##  [87] "zz_Infl_ abr1_noronha_st04_300_d1_d1_1_dat1.txt"   
##  [88] "zz_Infl_ abr1_noronha_st04_300_d2_d2_1_dat1.txt"   
##  [89] "zz_Infl_ abr1_noronha_st05_300_d1_d1_1_dat1.txt"   
##  [90] "zz_Infl_ abr1_noronha_st05_300_d2_d2_1_dat1.txt"   
##  [91] "zz_Infl_ abr1_noronha_st06_300_d1_d1_1_dat1.txt"   
##  [92] "zz_Infl_ abr1_noronha_st06_300_d2_d2_1_dat1.txt"   
##  [93] "zz_Infl_ abr1_noronha_st07_300_d1_d1_1_dat1.txt"   
##  [94] "zz_Infl_ abr1_noronha_st07_300_d2_d2_1_dat1.txt"   
##  [95] "zz_Infl_ abr1_noronha_st08_300_d1_d1_1_dat1.txt"   
##  [96] "zz_Infl_ abr1_noronha_st08_300_d2_d2_1_dat1.txt"   
##  [97] "zz_Infl_ abr1_noronha_st09_300_d1_d1_1_dat1.txt"   
##  [98] "zz_Infl_ abr1_noronha_st09_300_d2_d2_1_dat1.txt"   
##  [99] "zz_Infl_ abr1_noronha_st11_300_d1_d1_1_dat1.txt"   
## [100] "zz_Infl_ abr1_noronha_st11_300_d2_d2_1_dat1.txt"   
## [101] "zz_Infl_ abr1_plataforma_st25_300_d1_d1_1_dat1.txt"
## [102] "zz_Infl_ abr1_plataforma_st25_300_d2_d2_1_dat1.txt"
## [103] "zz_Infl_ abr1_plataforma_st26_300_d1_d1_1_dat1.txt"
## [104] "zz_Infl_ abr1_plataforma_st26_300_d2_d2_1_dat1.txt"
## [105] "zz_Infl_ abr1_plataforma_st27_300_d1_d1_1_dat1.txt"
## [106] "zz_Infl_ abr1_plataforma_st27_300_d2_d2_1_dat1.txt"
## [107] "zz_Infl_ abr1_plataforma_st29_300_d1_d1_1_dat1.txt"
## [108] "zz_Infl_ abr1_plataforma_st29_300_d2_d2_1_dat1.txt"
## [109] "zz_Infl_ abr1_plataforma_st31_300_d1_d1_1_dat1.txt"
## [110] "zz_Infl_ abr1_plataforma_st31_300_d2_d2_1_dat1.txt"
## [111] "zz_Infl_ abr1_plataforma_st33_300_d1_d1_1_dat1.txt"
## [112] "zz_Infl_ abr1_plataforma_st33_300_d2_d2_1_dat1.txt"
## [113] "zz_Infl_ abr1_plataforma_st34_300_d1_d1_1_dat1.txt"
## [114] "zz_Infl_ abr1_plataforma_st34_300_d2_d2_1_dat1.txt"
## [115] "zz_Infl_ abr1_plataforma_st36_300_d1_d1_1_dat1.txt"
## [116] "zz_Infl_ abr1_plataforma_st36_300_d2_d2_1_dat1.txt"
## [117] "zz_Infl_ abr1_plataforma_st37_300_d1_d1_1_dat1.txt"
## [118] "zz_Infl_ abr1_plataforma_st37_300_d2_d2_1_dat1.txt"
## [119] "zz_Infl_ abr1_plataforma_st40_300_d1_d1_1_dat1.txt"
## [120] "zz_Infl_ abr1_plataforma_st40_300_d2_d2_1_dat1.txt"
## [121] "zz_Infl_ abr1_plataforma_st41_300_d1_d1_1_dat1.txt"
## [122] "zz_Infl_ abr1_plataforma_st41_300_d2_d2_1_dat1.txt"
## [123] "zz_Infl_ abr1_plataforma_st43_300_d1_d1_1_dat1.txt"
## [124] "zz_Infl_ abr1_plataforma_st43_300_d2_d2_1_dat1.txt"
## [125] "zz_Infl_ abr1_plataforma_st45_300_d1_d1_1_dat1.txt"
## [126] "zz_Infl_ abr1_plataforma_st45_300_d2_d2_1_dat1.txt"
## [127] "zz_Infl_ abr1_plataforma_st48_300_d1_d1_1_dat1.txt"
## [128] "zz_Infl_ abr1_plataforma_st48_300_d2_d2_1_dat1.txt"
## [129] "zz_Infl_ abr1_plataforma_st49_300_d1_d1_1_dat1.txt"
## [130] "zz_Infl_ abr1_plataforma_st49_300_d2_d2_1_dat1.txt"
## [131] "zz_Infl_ abr1_plataforma_st51_300_d1_d1_1_dat1.txt"
## [132] "zz_Infl_ abr1_plataforma_st51_300_d2_d2_1_dat1.txt"
## [133] "zz_Infl_ abr1_plataforma_st52_300_d1_d1_1_dat1.txt"
## [134] "zz_Infl_ abr1_plataforma_st52_300_d2_d2_1_dat1.txt"
## [135] "zz_Infl_ abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
## [136] "zz_Infl_ abr1_plataforma_st54_300_d2_d2_1_dat1.txt"
list.temp<-list.files()
nfiles <- length(list.temp)

# STEP V.1: prepares file lists for aliquots 1 (filesAli1) and 2 (filesAli2)

# filesAli1 <- list.temp[1:(nfiles/2)]
# filesAli2 <- list.temp[((nfiles/2)+1):nfiles]

filesAli1 <- list.files(pattern="300_d1") #  search string
filesAli2 <- list.files(pattern="300_d2") #  search string


# Step V.II. FINAL LOOP - MERGE FILES

#  file.index <- 1


for (file.index in 1:length(filesAli1))
{
  #V.II.A. Read the fileS
  
  setwd(file.path(mainDir, subDir_3))
  
  
  full.textfileAl1 <- read.table(file = filesAli1[file.index], sep = ";", skip = 0, colClasses = "character", quote = "")
  full.textfileAl2 <- read.table(file = filesAli2[file.index], sep = ";", skip = 0, colClasses = "character", quote = "")
  
  #V.II.B. Import the Data Table
  
  #V.II.B.1 Searches, Finds and Gives the Line number of the Header, imports the Data Table
  
  grep("Data", full.textfileAl1$V1)  # gives the line number of the line containing "DATA"
  
  Data.line.many = grep("Data", full.textfileAl1$V1) #  all line numbers containing "Data"
  Data.line <- Data.line.many[3]   #  Line number of the third time "Data" appears in the file, above the  Header of the Data Table.
  Data.line  #  Line number of the text "Data", above the  Header of the Data Table
  
  #V.II. B.2. Read the Data Table,save  it as "Data_(filename)"
  # assign(zznames_Data[2], read.table(file = files.input[2], sep = "\t", skip = (Data.line), header = TRUE)) # Imports the ZooScan Data Table
  temp.obj1 <-   read.table(file = filesAli1[file.index], sep = "\t", skip = (Data.line), header = TRUE) # Imports the ZooScan Data Table
  temp.obj2 <-  read.table(file = filesAli2[file.index], sep = "\t", skip = (Data.line), header = TRUE) # Imports the ZooScan Data Table
  
  # V.II.B.3. Merge  obj1 and and obj2 
  
  names(temp.obj1) <- "V1"
  names(temp.obj2) <- "V1"
  
  temp.obj1_2merged <- rbind.data.frame(temp.obj1, temp.obj2, make.row.names = FALSE, stringsAsFactors = FALSE)
  temp.obj1_2merged$V1   <- as.character(temp.obj1_2merged$V1) 
  
  #  V.II.B.4. Merge  merged  data with  header  
  
  #read the old header
  header.temp <- read.table(file = filesAli1[file.index], col.names = "V1", comment="", quote= NULL,sep = "\t", header = FALSE) # Imports the Header
  header.temp <- as.vector( header.temp)
  #View(header.temp)
  
  header.temp2 <- as.data.frame(header.temp[1:(Data.line+1),])
  # View(header.temp2)
  
  # merge header and data
  
  names(header.temp2) <- "V1"
  names(temp.obj1_2merged) <- "V1"
  
  newfile <- rbind.data.frame(header.temp2, (temp.obj1_2merged), make.row.names = FALSE, stringsAsFactors = FALSE)
  newfile$V1   <- as.character(newfile$V1) 
  
  # CHANGE SUBPART TO 1
  
  newfile[SP.line,1] <-  "SubPart= 1"
  
  
  
  # V.II.C. save MERGED file  with NEW name in subDirerctory 4
  
  setwd(file.path(mainDir, subDir_4))
  getwd()
  
  write.table(newfile, file = paste("zz_Merged_", filesAli1[file.index] ),
              row.names = FALSE, quote=FALSE, col.names = FALSE)
  
  
} # End of Last Loop #
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
## dec, : EOF within quoted string
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec =
## dec, : EOF within quoted string
# PLease verify your final (inflated and merged) files in the subdiretory "4_Infl_merged_PIDs"

setwd(file.path(mainDir, subDir_4))
getwd()
## [1] "C:/Users/R/Documents/Recife/Orientandos Recife/Gabi Figueiredo_Doutorado/PID_Files/4_Infl_merged_PIDs"
list.files()
##  [1] "zz_Merged_ abr1_atol_st12_300_d1_d1_1_dat1.txt"                                   
##  [2] "zz_Merged_ abr1_atol_st13_300_d1_d1_1_dat1.txt"                                   
##  [3] "zz_Merged_ abr1_atol_st14_300_d1_d1_1_dat1.txt"                                   
##  [4] "zz_Merged_ abr1_atol_st20_300_d1_d1_1_dat1.txt"                                   
##  [5] "zz_Merged_ abr1_atol_st22_300_d1_d1_1_dat1.txt"                                   
##  [6] "zz_Merged_ abr1_atol_st23_300_d1_d1_1_dat1.txt"                                   
##  [7] "zz_Merged_ abr1_noronha_st01_300_d1_d1_1_dat1.txt"                                
##  [8] "zz_Merged_ abr1_noronha_st02_300_d1_d1_1_dat1.txt"                                
##  [9] "zz_Merged_ abr1_noronha_st03_300_d1_d1_1_dat1.txt"                                
## [10] "zz_Merged_ abr1_noronha_st04_300_d1_d1_1_dat1.txt"                                
## [11] "zz_Merged_ abr1_noronha_st05_300_d1_d1_1_dat1.txt"                                
## [12] "zz_Merged_ abr1_noronha_st06_300_d1_d1_1_dat1.txt"                                
## [13] "zz_Merged_ abr1_noronha_st07_300_d1_d1_1_dat1.txt"                                
## [14] "zz_Merged_ abr1_noronha_st08_300_d1_d1_1_dat1.txt"                                
## [15] "zz_Merged_ abr1_noronha_st09_300_d1_d1_1_dat1.txt"                                
## [16] "zz_Merged_ abr1_noronha_st11_300_d1_d1_1_dat1.txt"                                
## [17] "zz_Merged_ abr1_plataforma_st25_300_d1_d1_1_dat1.txt"                             
## [18] "zz_Merged_ abr1_plataforma_st26_300_d1_d1_1_dat1.txt"                             
## [19] "zz_Merged_ abr1_plataforma_st27_300_d1_d1_1_dat1.txt"                             
## [20] "zz_Merged_ abr1_plataforma_st29_300_d1_d1_1_dat1.txt"                             
## [21] "zz_Merged_ abr1_plataforma_st31_300_d1_d1_1_dat1.txt"                             
## [22] "zz_Merged_ abr1_plataforma_st33_300_d1_d1_1_dat1.txt"                             
## [23] "zz_Merged_ abr1_plataforma_st34_300_d1_d1_1_dat1.txt"                             
## [24] "zz_Merged_ abr1_plataforma_st36_300_d1_d1_1_dat1.txt"                             
## [25] "zz_Merged_ abr1_plataforma_st37_300_d1_d1_1_dat1.txt"                             
## [26] "zz_Merged_ abr1_plataforma_st40_300_d1_d1_1_dat1.txt"                             
## [27] "zz_Merged_ abr1_plataforma_st41_300_d1_d1_1_dat1.txt"                             
## [28] "zz_Merged_ abr1_plataforma_st43_300_d1_d1_1_dat1.txt"                             
## [29] "zz_Merged_ abr1_plataforma_st45_300_d1_d1_1_dat1.txt"                             
## [30] "zz_Merged_ abr1_plataforma_st48_300_d1_d1_1_dat1.txt"                             
## [31] "zz_Merged_ abr1_plataforma_st49_300_d1_d1_1_dat1.txt"                             
## [32] "zz_Merged_ abr1_plataforma_st51_300_d1_d1_1_dat1.txt"                             
## [33] "zz_Merged_ abr1_plataforma_st52_300_d1_d1_1_dat1.txt"                             
## [34] "zz_Merged_ abr1_plataforma_st54_300_d1_d1_1_dat1.txt"                             
## [35] "zz_Merged_ zz_Infl_ abr1_atol_st12_300_d1_d1_1_dat1.txt"                          
## [36] "zz_Merged_ zz_Infl_ abr1_atol_st13_300_d1_d1_1_dat1.txt"                          
## [37] "zz_Merged_ zz_Infl_ abr1_atol_st14_300_d1_d1_1_dat1.txt"                          
## [38] "zz_Merged_ zz_Infl_ abr1_atol_st20_300_d1_d1_1_dat1.txt"                          
## [39] "zz_Merged_ zz_Infl_ abr1_atol_st22_300_d1_d1_1_dat1.txt"                          
## [40] "zz_Merged_ zz_Infl_ abr1_atol_st23_300_d1_d1_1_dat1.txt"                          
## [41] "zz_Merged_ zz_Infl_ abr1_noronha_st01_300_d1_d1_1_dat1.txt"                       
## [42] "zz_Merged_ zz_Infl_ abr1_noronha_st01_300_d1_d1_1_dat1_corr_excl_OK.CSV"          
## [43] "zz_Merged_ zz_Infl_ abr1_noronha_st02_300_d1_d1_1_dat1.txt"                       
## [44] "zz_Merged_ zz_Infl_ abr1_noronha_st03_300_d1_d1_1_dat1.txt"                       
## [45] "zz_Merged_ zz_Infl_ abr1_noronha_st04_300_d1_d1_1_dat1.txt"                       
## [46] "zz_Merged_ zz_Infl_ abr1_noronha_st05_300_d1_d1_1_dat1.txt"                       
## [47] "zz_Merged_ zz_Infl_ abr1_noronha_st06_300_d1_d1_1_dat1.txt"                       
## [48] "zz_Merged_ zz_Infl_ abr1_noronha_st07_300_d1_d1_1_dat1.txt"                       
## [49] "zz_Merged_ zz_Infl_ abr1_noronha_st08_300_d1_d1_1_dat1.txt"                       
## [50] "zz_Merged_ zz_Infl_ abr1_noronha_st09_300_d1_d1_1_dat1.txt"                       
## [51] "zz_Merged_ zz_Infl_ abr1_noronha_st11_300_d1_d1_1_dat1.txt"                       
## [52] "zz_Merged_ zz_Infl_ abr1_plataforma_st25_300_d1_d1_1_dat1.txt"                    
## [53] "zz_Merged_ zz_Infl_ abr1_plataforma_st26_300_d1_d1_1_dat1.txt"                    
## [54] "zz_Merged_ zz_Infl_ abr1_plataforma_st27_300_d1_d1_1_dat1.txt"                    
## [55] "zz_Merged_ zz_Infl_ abr1_plataforma_st29_300_d1_d1_1_dat1.txt"                    
## [56] "zz_Merged_ zz_Infl_ abr1_plataforma_st31_300_d1_d1_1_dat1.txt"                    
## [57] "zz_Merged_ zz_Infl_ abr1_plataforma_st33_300_d1_d1_1_dat1.txt"                    
## [58] "zz_Merged_ zz_Infl_ abr1_plataforma_st34_300_d1_d1_1_dat1.txt"                    
## [59] "zz_Merged_ zz_Infl_ abr1_plataforma_st36_300_d1_d1_1_dat1.txt"                    
## [60] "zz_Merged_ zz_Infl_ abr1_plataforma_st37_300_d1_d1_1_dat1.txt"                    
## [61] "zz_Merged_ zz_Infl_ abr1_plataforma_st40_300_d1_d1_1_dat1.txt"                    
## [62] "zz_Merged_ zz_Infl_ abr1_plataforma_st41_300_d1_d1_1_dat1.txt"                    
## [63] "zz_Merged_ zz_Infl_ abr1_plataforma_st41_300_d1_d1_1_dat1__CORR_lines EXCL_OK.csv"
## [64] "zz_Merged_ zz_Infl_ abr1_plataforma_st43_300_d1_d1_1_dat1.txt"                    
## [65] "zz_Merged_ zz_Infl_ abr1_plataforma_st45_300_d1_d1_1_dat1.txt"                    
## [66] "zz_Merged_ zz_Infl_ abr1_plataforma_st45_300_d1_d1_1_dat1_CORR_lines_EXCL_OK.csv" 
## [67] "zz_Merged_ zz_Infl_ abr1_plataforma_st48_300_d1_d1_1_dat1.txt"                    
## [68] "zz_Merged_ zz_Infl_ abr1_plataforma_st49_300_d1_d1_1_dat1.txt"                    
## [69] "zz_Merged_ zz_Infl_ abr1_plataforma_st51_300_d1_d1_1_dat1.txt"                    
## [70] "zz_Merged_ zz_Infl_ abr1_plataforma_st52_300_d1_d1_1_dat1.txt"                    
## [71] "zz_Merged_ zz_Infl_ abr1_plataforma_st54_300_d1_d1_1_dat1.txt"
  ### End of Script ###