#The alleles of those crossbred animals simulated in step 1 were phased and assigned to their breed of origin (being either local or exotic). 
#using the AlphaPhase software package.
#Packages and Rscrpits used to phase the genotype data are presented below

library(data.table)
#Create working directory
setwd(adgg)

#Creating folders for phasing
sapply(c("localbred","exoticbred",paste0("crossbred", 1:5)), dir.create)

for (bred in dir(adgg, pattern="bred")) {
  
  # get into the sub directory
  setwd(bred)
  #create folders for replicates
  sapply(paste0("rep", 1:10), dir.create)
  setwd("../")
}

#Define alternative corelengths for phasing
cores = c(100, 120, 140, 160, 180, 200, 220, 240, 260, 280)

for (bred in dir(adgg, pattern="bred")) {
  replicates = dir(paste0(bred))
  for (rep in replicates) {

    setwd(paste0(bred, "/", rep))
    #Create corelengths in each replicate
    sapply(paste0("core", cores), dir.create)
    for (cor in cores) {
      core_folder = paste0("core", cor)
            
      setwd(paste0(bred, "/", rep, "/", core_folder))
      sapply(c("offset", "nooffset"), dir.create)
      
      kor = dir(paste0(bred,"/",rep, "/",core_folder))
      
      for(nof in kor) {
        # phase
        setwd(paste0(bred,"/",rep, "/", core_folder,"/offset"))
        system("cp /AlphaPhaseSpec.txt .")        
        pedigree_line = paste0("PedigreeFile, \rep,"/pedigree.", bred, "\"")
        genotype_line = paste0("GenotypeFile,  \rep,"/genotype.", bred, "\",  GenotypeFormat")
        
        core_line = paste0("GeneralCoreAndTailLength,"        ,cor)
        offset_line= paste0("GeneralCoreLength,",   cor/2, ",Offset")
        specFile = readLines("AlphaPhaseSpec.txt")
        specFile[1] = pedigree_line
        specFile[2] = genotype_line
        
        specFile[4] = core_line
        specFile[5] = offset_line
        write.table(specFile, "AlphaPhaseSpec.txt", row.names=F, col.names=F, quote=F)
        system("cp /AlphaPhase .")
        
        system("./AlphaPhase")
        unlink("Miscellaneous", recursive = TRUE)
        unlink("HaplotypeLibrary", recursive = TRUE)

        setwd(paste0(bred,"/",rep, "/", core_folder,"/nooffset"))
        system("cp /AlphaPhaseSpec.txt .")
        pedigree_line = paste0("PedigreeFile, \ rep,"/pedigree.", bred, "\"")
        genotype_line = paste0("GenotypeFile,  \ rep,"/genotype.", bred, "\",  GenotypeFormat")
        core_line = paste0("GeneralCoreAndTailLength,"        ,cor)
        
        offset_line= paste0("GeneralCoreLength,",   cor/2, ",NotOffset")
        specFile = readLines("AlphaPhaseSpec.txt")
        specFile[1] = pedigree_line
        specFile[2] = genotype_line
        specFile[4] = core_line
        specFile[5] = offset_line
        write.table(specFile, "AlphaPhaseSpec.txt", row.names=F, col.names=F, quote=F)
        system("cp /AlphaPhase .")
        
        system("./AlphaPhase")
        unlink("Miscellaneous", recursive = TRUE)
        unlink("HaplotypeLibrary", recursive = TRUE)
        
      }
    }
  }
  setwd("../")
}