#!/bin/bash

fName="Loop_through_this_file_final.txt"

OFS=$IFS
IFS=$'\n'

module load plink
module load gtool
module load impute

for line in `sed '1d' "$fName"`
do
   chrom=`echo $line | cut -f1`
   band=`echo $line | cut -f2`
   start=`echo $line | cut -f3`
   end=`echo $line | cut -f4`
  
   plink --noweb --bfile FinalCommonEoE --recode --chr "$chrom" --from-mb "$start" --to-mb "$end" --out "${band}_genotyped"

   gtool -P --family --ped "${band}_genotyped.ped" --map "${band}_genotyped.map"

   impute2 -m "/database/impute2_reference/ALL_1000G_phase1integrated_SHAPEIT2_impute.polymorphic/genetic_map_chr${chrom}_combined_b37.txt" -h "/database/impute2_reference/ALL_1000G_phase1integrated_SHAPEIT2_impute.polymorphic/ALL.chr${chrom}.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.nomono.haplotypes.gz" -l "/database/impute2_reference/ALL_1000G_phase1integrated_SHAPEIT2_impute.polymorphic/ALL.chr${chrom}.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.nomono.legend.gz"  -g "${band}_genotyped.ped.gen"  -int "${start}e6" "${end}e6" buffer 20kb -iter 30 -burnin 10 -Ne 20000 -call_thresh 0.9 -o "${band}_imputed"

	gtool -G --g "${band}_imputed" --s "${band}_genotyped.ped.sample" --chr "{chrom}" --alleles --ped "${band}_imputed.ped" --map "${band}_imputed.map" --threshold 0.9 --sex sex --phenotype phenotype

	awk '{OFS="\t"; print 1, $2, $3, $4}' "${band}_imputed.map" > "${band}_imputed2.map"
	
	mv "${band}_imputed2.map" "${band}_imputed.map"
	
	plink --noweb --file "${band}_imputed" --make-bed --out "${band}_imputed_bin"
	
	awk '{OFS="\t"; print $1, $3, $4, $5, $6}' "${band}_imputed_bin.fam" | tr '_' "\t" > "${band}_imputed_bin2.fam"
	
	mv "${band}_imputed_bin2.fam" "${band}_imputed_bin.fam"
	
	plink --noweb --bfile "${band}_imputed_bin" --assoc --out "${band}_imputed_assoc"

done

IFS=$OFS
