#####################################
## step1: split reference vcf file ##
#####################################
foreach chr (`seq 1 22`)
  java -jar splitVCFref.jar -v chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.gz -wc 10Mb -wf 1Mb
  # this generates the following output 
  #   (1) chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.region*.gz files (which are the regional reference vcf files) and
  #   (2) chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.gz.splitlog.csv (which shows statistics regarding how split was done)
end


#######################
## step2: imputation ##
#######################
# note that one does NOT need to split target once reference is splitted because
#   markers in target but not in reference will be automatically ignored 
foreach chr (`seq 1 22`)
  @ numSegs = `wc -l < chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.gz.splitlog.csv` - 1
  foreach i (`seq 1 $numSegs`)
    @ corestart = `sed 1,1d chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.gz.splitlog.csv | head -$i | tail -1 | cut -f2 -d ',' `
    @ coreend   = `sed 1,1d chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.gz.splitlog.csv | head -$i | tail -1 | cut -f3 -d ',' `
    mach-admix --dosage -d my.chr$chr.dat -p my.chr$chr.ped -h chr$chr.phase1_release_v3.20101123.snps_indels_svs.genotypes.refpanel.ALL.vcf.region$i.gz --vcfRef --outputstart $corestart --outputend $coreend -o my.chr$chr.region$i.out > my.chr$chr.region$i.log
  end
end

