# 文件名
DIR=$1
SAMPLE=$2
SUFFIX=$3
BAMFILE="${DIR}/${SAMPLE}${SUFFIX}"
WGSEXTPATH="/home/linyize/WGSExtractv4"

mkdir tmp/${SAMPLE}

samtools index ${BAMFILE}
bcftools mpileup --ignore-RG -T ${WGSEXTPATH}/reference/microarray/All_SNPs_hg38_ref.tab.gz -f ${WGSEXTPATH}/reference/genomes/hg38.fa.gz -Ou ${BAMFILE} | bcftools call --ploidy-file ${WGSEXTPATH}/reference/microarray/ploidy.txt -V indels -m -P 0 --threads 16 -Oz -o tmp/${SAMPLE}/CombKit_called.vcf.gz
tabix -p vcf tmp/${SAMPLE}/CombKit_called.vcf.gz
bcftools annotate -Oz -a ${WGSEXTPATH}/reference/microarray/All_SNPs_hg38_ref.tab.gz -c CHROM,POS,ID tmp/${SAMPLE}/CombKit_called.vcf.gz > tmp/${SAMPLE}/CombKit_annotated.vcf.gz
tabix -p vcf tmp/${SAMPLE}/CombKit_annotated.vcf.gz
bcftools query -f '%ID	%CHROM	%POS[	%TGT]\n' tmp/${SAMPLE}/CombKit_annotated.vcf.gz -o tmp/${SAMPLE}/CombKit_result.tab
sed 's/chr//; s/	M	/	MT	/g; s/\///; s/\.\.$/--/; s/TA$/AT/; s/TC$/CT/; s/TG$/GT/; s/GA$/AG/;     s/GC$/CG/; s/CA$/AC/' tmp/${SAMPLE}/CombKit_result.tab | sort -t $'	' -k2,3 -V > tmp/${SAMPLE}/CombKit_result_sorted.tab
cat ${WGSEXTPATH}/reference/microarray/raw_file_templates/head/23andMe_V3.txt tmp/${SAMPLE}/CombKit_result_sorted.tab > tmp/${SAMPLE}/${SAMPLE}_CombinedKit.txt
python ${WGSEXTPATH}/program/hg38tohg19.py tmp/${SAMPLE}/${SAMPLE}_CombinedKit hg38

zip -mj ${DIR}/${SAMPLE}_CombinedKit.zip tmp/${SAMPLE}/${SAMPLE}_CombinedKit.txt

rm -rf tmp/${SAMPLE}


