#!/bin/bash

pheno=$1
pop=$2

thr=12
mem=96000

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
pruneFile=${dir}/genotypes/pruning/ukb22418.hg38.${pop}.1000_100_09.prune.in.gz
pfile=${dir}/genotypes/ukb22418.hg38
condFile=${dir}/carrier/ukb23148.QCed.exome_meta.signal.${pheno}

cd /home/dnanexus/

cp /mnt/project/tool/plink2 . && chmod 755 plink2
docker pull skoyamamd/regenie:v3.1.3.gz

cp $covarFile .
cp $phenoFile .
cp $popFile .
cp $pruneFile .
cp $pfile.{pgen,pvar,psam} .
cp $condFile.{pgen,pvar,psam,list} .

./plink2 \
  --pfile $(basename $pfile) \
  --extract $(basename $pruneFile) \
  --keep $(basename $popFile) \
  --make-pgen \
  --autosome \
  --threads ${thr} \
  --maf 0.01 \
  --memory ${mem} \
  --out ukb22418.hg38.${pop}.pruned

## Run step1 - condition

docker run -v /home/dnanexus/:/home/dnanexus skoyamamd/regenie:v3.1.3.gz regenie \
  --step 1 \
  --pgen /home/dnanexus/ukb22418.hg38.${pop}.pruned \
  --covarFile /home/dnanexus/$(basename $covarFile) \
  --catCovarList SEX,ARRAY \
  --phenoFile /home/dnanexus/$(basename $phenoFile) \
  --bsize 1000 \
  --qt \
  --apply-rint \
  --threads ${thr} \
  --lowmem \
  --lowmem-prefix ukb22418.${pheno}.${pop}.condition \
  --condition-list /home/dnanexus/$(basename ${condFile}).list \
  --condition-file pgen,/home/dnanexus/$(basename ${condFile}) \
  --out /home/dnanexus/out/out/ukb22418.${pheno}.${pop}.condition

## Run step1 - noncondition

docker run -v /home/dnanexus/:/home/dnanexus skoyamamd/regenie:v3.1.3.gz regenie \
  --step 1 \
  --pgen /home/dnanexus/ukb22418.hg38.${pop}.pruned \
  --covarFile /home/dnanexus/$(basename $covarFile) \
  --catCovarList SEX,ARRAY \
  --phenoFile /home/dnanexus/$(basename $phenoFile) \
  --bsize 1000 \
  --qt \
  --apply-rint \
  --threads ${thr} \
  --lowmem \
  --lowmem-prefix ukb22418.${pheno}.${pop}.condition \
  --out /home/dnanexus/out/out/ukb22418.${pheno}.${pop}.noncondition

