#!/bin/bash

pheno=$1
pop=$2
chr=$3
from=$4
to=$5

thr=8
mem=12000

dir=/mnt/project/project/exome_meta/

covarFile=${dir}/import/ukbb_qced.covar.txt.gz
phenoFile=${dir}/import/ukbb_qced.${pheno}.txt.gz
popFile=${dir}/import/population/ukbb_qced.${pop}.txt.gz

locoFileCond=${dir}/association/step1_condition/${pheno}.${pop}/ukb22418.${pheno}.${pop}.condition_*.loco
listFileCond=${dir}/association/step1_condition/${pheno}.${pop}/ukb22418.${pheno}.${pop}.condition_pred.list

locoFileNon=${dir}/association/step1_condition/${pheno}.${pop}/ukb22418.${pheno}.${pop}.noncondition_*.loco
listFileNon=${dir}/association/step1_condition/${pheno}.${pop}/ukb22418.${pheno}.${pop}.noncondition_pred.list

snpFile=${dir}/association/ALL.ADD.${pheno}.TOPMed.snp

bgenFile="/mnt/project/Bulk/Imputation/Imputation from genotype (TOPmed)/ukb21007_c${chr}_b0_v1.bgen"
bgiFile="/mnt/project/Bulk/Imputation/Imputation from genotype (TOPmed)/ukb21007_c${chr}_b0_v1.bgen.bgi"
sampleFile="/mnt/project/Bulk/Imputation/Imputation from genotype (TOPmed)/ukb21007_c${chr}_b0_v1.sample"

condFile=${dir}/carrier/ukb23148.QCed.exome_meta.signal.${pheno}

mkdir -p /home/dnanexus/out/out/tmp && cd /home/dnanexus/out/out/

cp /mnt/project/tool/plink2 . && chmod 755 plink2
cp /mnt/project/tool/bgenix . && chmod 755 bgenix

docker pull skoyamamd/regenie:v3.1.3.gz

cp ${covarFile} .
cp ${phenoFile} .
cp ${popFile} .
cp ${locoFileCond} .
cp ${listFileCond} .
cp ${locoFileNon} .
cp ${listFileNon} .
cp "${bgenFile}" .
cp "${bgiFile}" .
cp "${sampleFile}" .
cp ${snpFile} .
cp ${condFile}.{pgen,pvar,psam,list} .

./bgenix -g /home/dnanexus/out/out/$(basename "$bgenFile") -incl-range chr${chr}:${from}-${to} > tmp.bgen
cat /home/dnanexus/out/out/$(basename "$sampleFile") | cut -d ' ' -f 1-3 > tmp.sample

./plink2 \
  --bgen tmp.bgen ref-first \
  --sample tmp.sample \
  --keep $(basename $popFile) \
  --set-all-var-ids 'chr@:#:$r:$a' \
  --new-id-max-allele-len 10000 truncate \
  --make-pgen \
  --out tmp_all

./plink2 \
  --pfile tmp_all \
  --extract $(basename ${snpFile}) \
  --make-pgen \
  --out tmp

docker run -v /home/dnanexus/:/home/dnanexus skoyamamd/regenie:v3.1.3.gz regenie \
   --step 2 \
   --pgen /home/dnanexus/out/out/tmp \
   --phenoFile /home/dnanexus/out/out/$(basename $phenoFile) \
   --covarFile /home/dnanexus/out/out/$(basename $covarFile) \
   --catCovarList SEX,ARRAY \
   --bsize 1000 \
   --qt \
   --apply-rint \
   --minINFO 0.3 \
   --minMAC 5 \
   --threads ${thr} \
   --pred /home/dnanexus/out/out/$(basename $listFileCond) \
   --condition-list /home/dnanexus/out/out/$(basename ${condFile}).list \
   --condition-file pgen,/home/dnanexus/out/out/$(basename ${condFile}) \
   --out /home/dnanexus/out/out/tmp/ukb22418.${pheno}.${pop}.${chr}_${from}_${to}.condition

docker run -v /home/dnanexus/:/home/dnanexus skoyamamd/regenie:v3.1.3.gz regenie \
   --step 2 \
   --pgen /home/dnanexus/out/out/tmp \
   --phenoFile /home/dnanexus/out/out/$(basename $phenoFile) \
   --covarFile /home/dnanexus/out/out/$(basename $covarFile) \
   --catCovarList SEX,ARRAY \
   --bsize 1000 \
   --qt \
   --apply-rint \
   --minINFO 0.3 \
   --minMAC 5 \
   --threads ${thr} \
   --pred /home/dnanexus/out/out/$(basename $listFileNon) \
   --out /home/dnanexus/out/out/tmp/ukb22418.${pheno}.${pop}.${chr}_${from}_${to}.noncondition

rm *

ls tmp/*.condition_*.regenie | while read line; do
  cat $line | gzip -c > $(basename $line).gz
done

ls tmp/*.noncondition_*.regenie | while read line; do
  cat $line | gzip -c > $(basename $line).gz
done

rm -r tmp

